features.js 15 KB

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