features.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * jQuery.fn.sortElements
  3. * --------------
  4. * @param Function comparator:
  5. * Exactly the same behaviour as [1,2,3].sort(comparator)
  6. *
  7. * @param Function getSortable
  8. * A function that should return the element that is
  9. * to be sorted. The comparator will run on the
  10. * current collection, but you may want the actual
  11. * resulting sort to occur on a parent or another
  12. * associated element.
  13. *
  14. * E.g. $('td').sortElements(comparator, function(){
  15. * return this.parentNode;
  16. * })
  17. *
  18. * The <td>'s parent (<tr>) will be sorted instead
  19. * of the <td> itself.
  20. *
  21. * Credit: http://james.padolsey.com/javascript/sorting-elements-with-jquery/
  22. *
  23. */
  24. jQuery.fn.sortElements = (function(){
  25. var sort = [].sort;
  26. return function(comparator, getSortable) {
  27. getSortable = getSortable || function(){return this;};
  28. var placements = this.map(function(){
  29. var sortElement = getSortable.call(this),
  30. parentNode = sortElement.parentNode,
  31. // Since the element itself will change position, we have
  32. // to have some way of storing its original position in
  33. // the DOM. The easiest way is to have a 'flag' node:
  34. nextSibling = parentNode.insertBefore(
  35. document.createTextNode(''),
  36. sortElement.nextSibling
  37. );
  38. return function() {
  39. if (parentNode === this) {
  40. throw new Error(
  41. "You can't sort elements if any one is a descendant of another."
  42. );
  43. }
  44. // Insert before flag:
  45. parentNode.insertBefore(this, nextSibling);
  46. // Remove flag:
  47. parentNode.removeChild(nextSibling);
  48. };
  49. });
  50. return sort.call(this, comparator).each(function(i){
  51. placements[i].call(getSortable.call(this));
  52. });
  53. };
  54. })();
  55. (function ($) {
  56. Drupal.behaviors.features = {
  57. attach: function(context, settings) {
  58. // Features management form
  59. $('table.features:not(.processed)', context).each(function() {
  60. $(this).addClass('processed');
  61. // Check the overridden status of each feature
  62. Drupal.features.checkStatus();
  63. // Add some nicer row hilighting when checkboxes change values
  64. $('input', this).bind('change', function() {
  65. if (!$(this).attr('checked')) {
  66. $(this).parents('tr').removeClass('enabled').addClass('disabled');
  67. }
  68. else {
  69. $(this).parents('tr').addClass('enabled').removeClass('disabled');
  70. }
  71. });
  72. });
  73. // Export form component selector
  74. $('form.features-export-form select.features-select-components:not(.processed)', context).each(function() {
  75. $(this)
  76. .addClass('processed')
  77. .change(function() {
  78. var target = $(this).val();
  79. $('div.features-select').hide();
  80. $('div.features-select-' + target).show();
  81. return false;
  82. }).trigger('change');
  83. });
  84. // Export form machine-readable JS
  85. $('.feature-name:not(.processed)', context).each(function() {
  86. $('.feature-name')
  87. .addClass('processed')
  88. .after(' <small class="feature-module-name-suffix">&nbsp;</small>');
  89. if ($('.feature-module-name').val() === $('.feature-name').val().toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/_+/g, '_') || $('.feature-module-name').val() === '') {
  90. $('.feature-module-name').parents('.form-item').hide();
  91. $('.feature-name').bind('keyup change', function() {
  92. var machine = $(this).val().toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/_+/g, '_');
  93. if (machine !== '_' && machine !== '') {
  94. $('.feature-module-name').val(machine);
  95. $('.feature-module-name-suffix').empty().append(' Machine name: ' + machine + ' [').append($('<a href="#">'+ Drupal.t('Edit') +'</a>').click(function() {
  96. $('.feature-module-name').parents('.form-item').show();
  97. $('.feature-module-name-suffix').hide();
  98. $('.feature-name').unbind('keyup');
  99. return false;
  100. })).append(']');
  101. }
  102. else {
  103. $('.feature-module-name').val(machine);
  104. $('.feature-module-name-suffix').text('');
  105. }
  106. });
  107. $('.feature-name').keyup();
  108. }
  109. });
  110. //View info dialog
  111. var infoDialog = $('#features-info-file');
  112. if (infoDialog.length != 0) {
  113. infoDialog.dialog({
  114. autoOpen: false,
  115. modal: true,
  116. draggable: false,
  117. resizable: false,
  118. width: 600,
  119. height: 480
  120. });
  121. }
  122. if ((Drupal.settings.features != undefined) && (Drupal.settings.features.info != undefined)) {
  123. $('#features-info-file textarea').val(Drupal.settings.features.info);
  124. $('#features-info-file').dialog('open');
  125. //To be reset by the button click ajax
  126. Drupal.settings.features.info = undefined;
  127. }
  128. // mark any conflicts with a class
  129. if ((Drupal.settings.features != undefined) && (Drupal.settings.features.conflicts != undefined)) {
  130. for (var moduleName in Drupal.settings.features.conflicts) {
  131. moduleConflicts = Drupal.settings.features.conflicts[moduleName];
  132. $('#features-export-wrapper input[type=checkbox]', context).each(function() {
  133. if (!$(this).hasClass('features-checkall')) {
  134. var key = $(this).attr('name');
  135. var matches = key.match(/^([^\[]+)(\[.+\])?\[(.+)\]\[(.+)\]$/);
  136. var component = matches[1];
  137. var item = matches[4];
  138. if ((component in moduleConflicts) && (moduleConflicts[component].indexOf(item) != -1)) {
  139. $(this).parent().addClass('features-conflict');
  140. }
  141. }
  142. });
  143. }
  144. }
  145. function _checkAll(value) {
  146. if (value) {
  147. $('#features-export-wrapper .component-select input[type=checkbox]:visible', context).each(function() {
  148. var move_id = $(this).attr('id');
  149. $(this).click();
  150. $('#'+ move_id).attr('checked', 'checked');
  151. });
  152. }
  153. else {
  154. $('#features-export-wrapper .component-added input[type=checkbox]:visible', context).each(function() {
  155. var move_id = $(this).attr('id');
  156. $('#'+ move_id).removeAttr('checked');
  157. $(this).click();
  158. $('#'+ move_id).removeAttr('checked');
  159. });
  160. }
  161. }
  162. function moveCheckbox(item, section, value) {
  163. var curParent = item;
  164. if ($(item).hasClass('form-type-checkbox')) {
  165. item = $(item).children('input[type=checkbox]');
  166. }
  167. else {
  168. curParent = $(item).parents('.form-type-checkbox');
  169. }
  170. var newParent = $(curParent).parents('.features-export-parent').find('.form-checkboxes.component-'+section);
  171. $(curParent).detach();
  172. $(curParent).appendTo(newParent);
  173. var list = ['select', 'added', 'detected', 'included'];
  174. for (i in list) {
  175. $(curParent).removeClass('component-' + list[i]);
  176. $(item).removeClass('component-' + list[i]);
  177. }
  178. $(curParent).addClass('component-'+section);
  179. $(item).addClass('component-'+section);
  180. if (value) {
  181. $(item).attr('checked', 'checked');
  182. }
  183. else {
  184. $(item).removeAttr('checked')
  185. }
  186. $(newParent).parent().removeClass('features-export-empty');
  187. // re-sort new list of checkboxes based on labels
  188. $(newParent).find('label').sortElements(
  189. function(a, b){
  190. return $(a).text() > $(b).text() ? 1 : -1;
  191. },
  192. function(){
  193. return this.parentNode;
  194. }
  195. );
  196. }
  197. // provide timer for auto-refresh trigger
  198. var timeoutID = 0;
  199. var inTimeout = 0;
  200. function _triggerTimeout() {
  201. timeoutID = 0;
  202. _updateDetected();
  203. }
  204. function _resetTimeout() {
  205. inTimeout++;
  206. // if timeout is already active, reset it
  207. if (timeoutID != 0) {
  208. window.clearTimeout(timeoutID);
  209. if (inTimeout > 0) inTimeout--;
  210. }
  211. timeoutID = window.setTimeout(_triggerTimeout, 500);
  212. }
  213. function _updateDetected() {
  214. var autodetect = $('#features-autodetect input[type=checkbox]');
  215. if ((autodetect.length > 0) && (!autodetect.is(':checked'))) return;
  216. // query the server for a list of components/items in the feature and update
  217. // the auto-detected items
  218. var items = []; // will contain a list of selected items exported to feature
  219. var components = {}; // contains object of component names that have checked items
  220. $('#features-export-wrapper input[type=checkbox]:checked', context).each(function() {
  221. if (!$(this).hasClass('features-checkall')) {
  222. var key = $(this).attr('name');
  223. var matches = key.match(/^([^\[]+)(\[.+\])?\[(.+)\]\[(.+)\]$/);
  224. components[matches[1]] = matches[1];
  225. if (!$(this).hasClass('component-detected')) {
  226. items.push(key);
  227. }
  228. }
  229. });
  230. var featureName = $('#edit-module-name').val();
  231. if (featureName == '') {
  232. featureName = '*';
  233. }
  234. var url = Drupal.settings.basePath + 'features/ajaxcallback/' + featureName;
  235. var excluded = Drupal.settings.features.excluded;
  236. var postData = {'items': items, 'excluded': excluded};
  237. jQuery.post(url, postData, function(data) {
  238. if (inTimeout > 0) inTimeout--;
  239. // if we have triggered another timeout then don't update with old results
  240. if (inTimeout == 0) {
  241. // data is an object keyed by component listing the exports of the feature
  242. for (var component in data) {
  243. var itemList = data[component];
  244. $('#features-export-wrapper .component-' + component + ' input[type=checkbox]', context).each(function() {
  245. var key = $(this).attr('value');
  246. // first remove any auto-detected items that are no longer in component
  247. if ($(this).hasClass('component-detected')) {
  248. if (!(key in itemList)) {
  249. moveCheckbox(this, 'select', false)
  250. }
  251. }
  252. // next, add any new auto-detected items
  253. else if ($(this).hasClass('component-select')) {
  254. if (key in itemList) {
  255. moveCheckbox(this, 'detected', itemList[key]);
  256. $(this).parent().show(); // make sure it's not hidden from filter
  257. }
  258. }
  259. });
  260. }
  261. // loop over all selected components and check for any that have been completely removed
  262. for (var component in components) {
  263. if ((data == null) || !(component in data)) {
  264. $('#features-export-wrapper .component-' + component + ' input[type=checkbox].component-detected', context).each(function() {
  265. moveCheckbox(this, 'select', false);
  266. });
  267. }
  268. }
  269. }
  270. }, "json");
  271. }
  272. // Handle component selection UI
  273. $('#features-export-wrapper input[type=checkbox]', context).click(function() {
  274. _resetTimeout();
  275. if ($(this).hasClass('component-select')) {
  276. moveCheckbox(this, 'added', true);
  277. }
  278. else if ($(this).hasClass('component-included')) {
  279. moveCheckbox(this, 'added', false);
  280. }
  281. else if ($(this).hasClass('component-added')) {
  282. if ($(this).is(':checked')) {
  283. moveCheckbox(this, 'included', true);
  284. }
  285. else {
  286. moveCheckbox(this, 'select', false);
  287. }
  288. }
  289. });
  290. // Handle select/unselect all
  291. $('#features-filter .features-checkall', context).click(function() {
  292. if ($(this).attr('checked')) {
  293. _checkAll(true);
  294. $(this).next().html(Drupal.t('Deselect all'));
  295. }
  296. else {
  297. _checkAll(false);
  298. $(this).next().html(Drupal.t('Select all'));
  299. }
  300. _resetTimeout();
  301. });
  302. // Handle filtering
  303. // provide timer for auto-refresh trigger
  304. var filterTimeoutID = 0;
  305. var inFilterTimeout = 0;
  306. function _triggerFilterTimeout() {
  307. filterTimeoutID = 0;
  308. _updateFilter();
  309. }
  310. function _resetFilterTimeout() {
  311. inFilterTimeout++;
  312. // if timeout is already active, reset it
  313. if (filterTimeoutID != 0) {
  314. window.clearTimeout(filterTimeoutID);
  315. if (inFilterTimeout > 0) inFilterTimeout--;
  316. }
  317. filterTimeoutID = window.setTimeout(_triggerFilterTimeout, 200);
  318. }
  319. function _updateFilter() {
  320. var filter = $('#features-filter input').val();
  321. var regex = new RegExp(filter, 'i');
  322. // collapse fieldsets
  323. var newState = {};
  324. var currentState = {};
  325. $('#features-export-wrapper .component-select label', context).each(function() {
  326. // expand parent fieldset
  327. var section = '';
  328. $(this).parents('fieldset.features-export-component').each(function() {
  329. section = $(this).attr('id');
  330. currentState[section] = !($(this).hasClass('collapsed'));
  331. if (!(section in newState)) {
  332. newState[section] = false;
  333. }
  334. });
  335. if (filter == '') {
  336. if (currentState[section]) {
  337. Drupal.toggleFieldset($('#'+section));
  338. currentState[section] = false;
  339. }
  340. $(this).parent().show();
  341. }
  342. else if ($(this).text().match(regex)) {
  343. $(this).parent().show();
  344. newState[section] = true;
  345. }
  346. else {
  347. $(this).parent().hide();
  348. }
  349. });
  350. for (section in newState) {
  351. if (currentState[section] != newState[section]) {
  352. Drupal.toggleFieldset($('#'+section));
  353. }
  354. }
  355. }
  356. $('#features-filter input', context).bind("input", function() {
  357. _resetFilterTimeout();
  358. });
  359. $('#features-filter .features-filter-clear', context).click(function() {
  360. $('#features-filter input').val('');
  361. _updateFilter();
  362. });
  363. // show the filter bar
  364. $('#features-filter', context).removeClass('element-invisible');
  365. }
  366. }
  367. Drupal.features = {
  368. 'checkStatus': function() {
  369. $('table.features tbody tr').not('.processed').filter(':first').each(function() {
  370. var elem = $(this);
  371. $(elem).addClass('processed');
  372. var uri = $(this).find('a.admin-check').attr('href');
  373. if (uri) {
  374. $.get(uri, [], function(data) {
  375. $(elem).find('.admin-loading').hide();
  376. switch (data.storage) {
  377. case 3:
  378. $(elem).find('.admin-rebuilding').show();
  379. break;
  380. case 2:
  381. $(elem).find('.admin-needs-review').show();
  382. break;
  383. case 1:
  384. $(elem).find('.admin-overridden').show();
  385. break;
  386. default:
  387. $(elem).find('.admin-default').show();
  388. break;
  389. }
  390. Drupal.features.checkStatus();
  391. }, 'json');
  392. }
  393. else {
  394. Drupal.features.checkStatus();
  395. }
  396. });
  397. }
  398. };
  399. })(jQuery);