features.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. //View info dialog
  85. var infoDialog = $('#features-info-file');
  86. if (infoDialog.length != 0) {
  87. infoDialog.dialog({
  88. autoOpen: false,
  89. modal: true,
  90. draggable: false,
  91. resizable: false,
  92. width: 600,
  93. height: 480
  94. });
  95. }
  96. if ((Drupal.settings.features != undefined) && (Drupal.settings.features.info != undefined)) {
  97. $('#features-info-file textarea').val(Drupal.settings.features.info);
  98. $('#features-info-file').dialog('open');
  99. //To be reset by the button click ajax
  100. Drupal.settings.features.info = undefined;
  101. }
  102. // mark any conflicts with a class
  103. if ((Drupal.settings.features != undefined) && (Drupal.settings.features.conflicts != undefined)) {
  104. for (var moduleName in Drupal.settings.features.conflicts) {
  105. moduleConflicts = Drupal.settings.features.conflicts[moduleName];
  106. $('#features-export-wrapper input[type=checkbox]', context).each(function() {
  107. if (!$(this).hasClass('features-checkall')) {
  108. var key = $(this).attr('name');
  109. var matches = key.match(/^([^\[]+)(\[.+\])?\[(.+)\]\[(.+)\]$/);
  110. if (matches != null) {
  111. var component = matches[1];
  112. var item = matches[4];
  113. if ((component in moduleConflicts) && (moduleConflicts[component].indexOf(item) != -1)) {
  114. $(this).parent().addClass('features-conflict');
  115. }
  116. }
  117. }
  118. });
  119. }
  120. }
  121. function _checkAll(value) {
  122. if (value) {
  123. $('#features-export-wrapper .component-select input[type=checkbox]:visible', context).each(function() {
  124. var move_id = $(this).attr('id');
  125. $(this).click();
  126. $('#'+ move_id).attr('checked', 'checked');
  127. });
  128. }
  129. else {
  130. $('#features-export-wrapper .component-added input[type=checkbox]:visible', context).each(function() {
  131. var move_id = $(this).attr('id');
  132. $('#'+ move_id).removeAttr('checked');
  133. $(this).click();
  134. $('#'+ move_id).removeAttr('checked');
  135. });
  136. }
  137. }
  138. function updateComponentCountInfo(item, section) {
  139. switch (section) {
  140. case 'select':
  141. var parent = $(item).closest('.features-export-list').siblings('.features-export-component');
  142. $('.component-count', parent).text(function (index, text) {
  143. return +text + 1;
  144. }
  145. );
  146. break;
  147. case 'added':
  148. case 'detected':
  149. var parent = $(item).closest('.features-export-component');
  150. $('.component-count', parent).text(function (index, text) {
  151. return text - 1;
  152. });
  153. }
  154. }
  155. function moveCheckbox(item, section, value) {
  156. updateComponentCountInfo(item, section);
  157. var curParent = item;
  158. if ($(item).hasClass('form-type-checkbox')) {
  159. item = $(item).children('input[type=checkbox]');
  160. }
  161. else {
  162. curParent = $(item).parents('.form-type-checkbox');
  163. }
  164. var newParent = $(curParent).parents('.features-export-parent').find('.form-checkboxes.component-'+section);
  165. $(curParent).detach();
  166. $(curParent).appendTo(newParent);
  167. var list = ['select', 'added', 'detected', 'included'];
  168. for (i in list) {
  169. $(curParent).removeClass('component-' + list[i]);
  170. $(item).removeClass('component-' + list[i]);
  171. }
  172. $(curParent).addClass('component-'+section);
  173. $(item).addClass('component-'+section);
  174. if (value) {
  175. $(item).attr('checked', 'checked');
  176. }
  177. else {
  178. $(item).removeAttr('checked')
  179. }
  180. $(newParent).parent().removeClass('features-export-empty');
  181. // re-sort new list of checkboxes based on labels
  182. $(newParent).find('label').sortElements(
  183. function(a, b){
  184. return $(a).text() > $(b).text() ? 1 : -1;
  185. },
  186. function(){
  187. return this.parentNode;
  188. }
  189. );
  190. }
  191. // provide timer for auto-refresh trigger
  192. var timeoutID = 0;
  193. var inTimeout = 0;
  194. function _triggerTimeout() {
  195. timeoutID = 0;
  196. _updateDetected();
  197. }
  198. function _resetTimeout() {
  199. inTimeout++;
  200. // if timeout is already active, reset it
  201. if (timeoutID != 0) {
  202. window.clearTimeout(timeoutID);
  203. if (inTimeout > 0) inTimeout--;
  204. }
  205. timeoutID = window.setTimeout(_triggerTimeout, 500);
  206. }
  207. function _updateDetected() {
  208. var autodetect = $('#features-autodetect input[type=checkbox]');
  209. if ((autodetect.length > 0) && (!autodetect.is(':checked'))) return;
  210. // query the server for a list of components/items in the feature and update
  211. // the auto-detected items
  212. var items = []; // will contain a list of selected items exported to feature
  213. var components = {}; // contains object of component names that have checked items
  214. $('#features-export-wrapper input[type=checkbox]:checked', context).each(function() {
  215. if (!$(this).hasClass('features-checkall')) {
  216. var key = $(this).attr('name');
  217. var matches = key.match(/^([^\[]+)(\[.+\])?\[(.+)\]\[(.+)\]$/);
  218. components[matches[1]] = matches[1];
  219. if (!$(this).hasClass('component-detected')) {
  220. items.push(key);
  221. }
  222. }
  223. });
  224. var featureName = $('#edit-module-name').val();
  225. if (featureName == '') {
  226. featureName = '*';
  227. }
  228. var url = Drupal.settings.basePath + 'features/ajaxcallback/' + featureName;
  229. var excluded = Drupal.settings.features.excluded;
  230. var postData = {'items': items, 'excluded': excluded};
  231. jQuery.post(url, postData, function(data) {
  232. if (inTimeout > 0) inTimeout--;
  233. // if we have triggered another timeout then don't update with old results
  234. if (inTimeout == 0) {
  235. // data is an object keyed by component listing the exports of the feature
  236. for (var component in data) {
  237. var itemList = data[component];
  238. $('#features-export-wrapper .component-' + component + ' input[type=checkbox]', context).each(function() {
  239. var key = $(this).attr('value');
  240. // first remove any auto-detected items that are no longer in component
  241. if ($(this).hasClass('component-detected')) {
  242. if (!(key in itemList)) {
  243. moveCheckbox(this, 'select', false)
  244. }
  245. }
  246. // next, add any new auto-detected items
  247. else if ($(this).hasClass('component-select')) {
  248. if (key in itemList) {
  249. moveCheckbox(this, 'detected', itemList[key]);
  250. $(this).parent().show(); // make sure it's not hidden from filter
  251. }
  252. }
  253. });
  254. }
  255. // loop over all selected components and check for any that have been completely removed
  256. for (var component in components) {
  257. if ((data == null) || !(component in data)) {
  258. $('#features-export-wrapper .component-' + component + ' input[type=checkbox].component-detected', context).each(function() {
  259. moveCheckbox(this, 'select', false);
  260. });
  261. }
  262. }
  263. }
  264. }, "json");
  265. }
  266. // Handle component selection UI
  267. $('#features-export-wrapper input[type=checkbox]:not(.processed)', context).addClass('processed').click(function() {
  268. _resetTimeout();
  269. if ($(this).hasClass('component-select')) {
  270. moveCheckbox(this, 'added', true);
  271. }
  272. else if ($(this).hasClass('component-included')) {
  273. moveCheckbox(this, 'added', false);
  274. }
  275. else if ($(this).hasClass('component-added')) {
  276. if ($(this).is(':checked')) {
  277. moveCheckbox(this, 'included', true);
  278. }
  279. else {
  280. moveCheckbox(this, 'select', false);
  281. }
  282. }
  283. });
  284. // Handle select/unselect all
  285. $('#features-filter .features-checkall', context).click(function() {
  286. if ($(this).attr('checked')) {
  287. _checkAll(true);
  288. $(this).next().html(Drupal.t('Deselect all'));
  289. }
  290. else {
  291. _checkAll(false);
  292. $(this).next().html(Drupal.t('Select all'));
  293. }
  294. _resetTimeout();
  295. });
  296. // Handle filtering
  297. // provide timer for auto-refresh trigger
  298. var filterTimeoutID = 0;
  299. var inFilterTimeout = 0;
  300. function _triggerFilterTimeout() {
  301. filterTimeoutID = 0;
  302. _updateFilter();
  303. }
  304. function _resetFilterTimeout() {
  305. inFilterTimeout++;
  306. // if timeout is already active, reset it
  307. if (filterTimeoutID != 0) {
  308. window.clearTimeout(filterTimeoutID);
  309. if (inFilterTimeout > 0) inFilterTimeout--;
  310. }
  311. filterTimeoutID = window.setTimeout(_triggerFilterTimeout, 200);
  312. }
  313. function _updateFilter() {
  314. var filter = $('#features-filter input').val();
  315. var regex = new RegExp(filter, 'i');
  316. // collapse fieldsets
  317. var newState = {};
  318. var currentState = {};
  319. $('#features-export-wrapper fieldset.features-export-component', context).each(function() {
  320. // expand parent fieldset
  321. var section = $(this).attr('id');
  322. currentState[section] = !($(this).hasClass('collapsed'));
  323. if (!(section in newState)) {
  324. newState[section] = false;
  325. }
  326. $(this).find('div.component-select label').each(function() {
  327. if (filter == '') {
  328. if (currentState[section]) {
  329. Drupal.toggleFieldset($('#'+section));
  330. currentState[section] = false;
  331. }
  332. $(this).parent().show();
  333. }
  334. else if ($(this).text().match(regex)) {
  335. $(this).parent().show();
  336. newState[section] = true;
  337. }
  338. else {
  339. $(this).parent().hide();
  340. }
  341. });
  342. });
  343. for (section in newState) {
  344. if (currentState[section] != newState[section]) {
  345. Drupal.toggleFieldset($('#'+section));
  346. }
  347. }
  348. }
  349. $('#features-filter input', context).bind("input", function() {
  350. _resetFilterTimeout();
  351. });
  352. $('#features-filter .features-filter-clear', context).click(function() {
  353. $('#features-filter input').val('');
  354. _updateFilter();
  355. });
  356. // show the filter bar
  357. $('#features-filter', context).removeClass('element-invisible');
  358. }
  359. }
  360. Drupal.features = {
  361. 'checkStatus': function() {
  362. $('table.features tbody tr').not('.processed').filter(':first').each(function() {
  363. var elem = $(this);
  364. $(elem).addClass('processed');
  365. var uri = $(this).find('a.admin-check').attr('href');
  366. if (uri) {
  367. $.get(uri, [], function(data) {
  368. $(elem).find('.admin-loading').hide();
  369. switch (data.storage) {
  370. case 3:
  371. $(elem).find('.admin-rebuilding').show();
  372. break;
  373. case 2:
  374. $(elem).find('.admin-needs-review').show();
  375. break;
  376. case 1:
  377. $(elem).find('.admin-overridden').show();
  378. break;
  379. default:
  380. $(elem).find('.admin-default').show();
  381. break;
  382. }
  383. Drupal.features.checkStatus();
  384. }, 'json');
  385. }
  386. else {
  387. Drupal.features.checkStatus();
  388. }
  389. });
  390. }
  391. };
  392. })(jQuery);