module_filter_tab.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. (function ($) {
  2. Drupal.ModuleFilter = Drupal.ModuleFilter || {};
  3. Drupal.ModuleFilter.textFilter = '';
  4. Drupal.ModuleFilter.timeout;
  5. Drupal.ModuleFilter.tabs = {};
  6. Drupal.ModuleFilter.enabling = {};
  7. Drupal.ModuleFilter.disabling = {};
  8. Drupal.behaviors.moduleFilter = {
  9. attach: function() {
  10. // Set the focus on the module filter textfield.
  11. $('input[name="module_filter[name]"]').focus();
  12. $('#module-filter-squeeze').css('min-height', $('#module-filter-tabs').height());
  13. $('#module-filter-left a.project-tab').each(function(i) {
  14. Drupal.ModuleFilter.tabs[$(this).attr('id')] = new Drupal.ModuleFilter.Tab(this);
  15. });
  16. // Move anchors to top of tabs.
  17. $('a.anchor', $('#module-filter-left')).remove().prependTo('#module-filter-tabs');
  18. $('input[name="module_filter[name]"]').keyup(function(e) {
  19. switch (e.which) {
  20. case 13:
  21. if (Drupal.ModuleFilter.timeout) {
  22. clearTimeout(Drupal.ModuleFilter.timeout);
  23. }
  24. Drupal.ModuleFilter.filter(Drupal.ModuleFilter.textFilter);
  25. break;
  26. default:
  27. if (Drupal.ModuleFilter.textFilter != $(this).val()) {
  28. Drupal.ModuleFilter.textFilter = this.value;
  29. if (Drupal.ModuleFilter.timeout) {
  30. clearTimeout(Drupal.ModuleFilter.timeout);
  31. }
  32. Drupal.ModuleFilter.timeout = setTimeout('Drupal.ModuleFilter.filter("' + Drupal.ModuleFilter.textFilter + '")', 500);
  33. }
  34. break;
  35. }
  36. });
  37. $('input[name="module_filter[name]"]').keypress(function(e) {
  38. if (e.which == 13) e.preventDefault();
  39. });
  40. Drupal.ModuleFilter.showEnabled = $('#edit-module-filter-show-enabled').is(':checked');
  41. $('#edit-module-filter-show-enabled').change(function() {
  42. Drupal.ModuleFilter.showEnabled = $(this).is(':checked');
  43. Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
  44. });
  45. Drupal.ModuleFilter.showDisabled = $('#edit-module-filter-show-disabled').is(':checked');
  46. $('#edit-module-filter-show-disabled').change(function() {
  47. Drupal.ModuleFilter.showDisabled = $(this).is(':checked');
  48. Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
  49. });
  50. Drupal.ModuleFilter.showRequired = $('#edit-module-filter-show-required').is(':checked');
  51. $('#edit-module-filter-show-required').change(function() {
  52. Drupal.ModuleFilter.showRequired = $(this).is(':checked');
  53. Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
  54. });
  55. Drupal.ModuleFilter.showUnavailable = $('#edit-module-filter-show-unavailable').is(':checked');
  56. $('#edit-module-filter-show-unavailable').change(function() {
  57. Drupal.ModuleFilter.showUnavailable = $(this).is(':checked');
  58. Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
  59. });
  60. if (Drupal.settings.moduleFilter.visualAid == 1) {
  61. $('table.package tbody td.checkbox input').change(function() {
  62. if ($(this).is(':checked')) {
  63. Drupal.ModuleFilter.updateVisualAid('enable', $(this).parents('tr'));
  64. }
  65. else {
  66. Drupal.ModuleFilter.updateVisualAid('disable', $(this).parents('tr'));
  67. }
  68. });
  69. }
  70. // Check for anchor.
  71. var url = document.location.toString();
  72. if (url.match('#')) {
  73. // Make tab active based on anchor.
  74. var anchor = '#' + url.split('#')[1];
  75. $('a[href="' + anchor + '"]').click();
  76. }
  77. // Else if no active tab is defined, set it to the all tab.
  78. else if (Drupal.ModuleFilter.activeTab == undefined) {
  79. Drupal.ModuleFilter.activeTab = Drupal.ModuleFilter.tabs['all-tab'];
  80. }
  81. }
  82. }
  83. Drupal.ModuleFilter.visible = function(checkbox) {
  84. if (checkbox.length > 0) {
  85. if (Drupal.ModuleFilter.showEnabled) {
  86. if ($(checkbox).is(':checked') && !$(checkbox).is(':disabled')) {
  87. return true;
  88. }
  89. }
  90. if (Drupal.ModuleFilter.showDisabled) {
  91. if (!$(checkbox).is(':checked') && !$(checkbox).is(':disabled')) {
  92. return true;
  93. }
  94. }
  95. if (Drupal.ModuleFilter.showRequired) {
  96. if ($(checkbox).is(':checked') && $(checkbox).is(':disabled')) {
  97. return true;
  98. }
  99. }
  100. }
  101. if (Drupal.ModuleFilter.showUnavailable) {
  102. if (checkbox.length == 0 || (!$(checkbox).is(':checked') && $(checkbox).is(':disabled'))) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. Drupal.ModuleFilter.filter = function(string) {
  109. var stringLowerCase = string.toLowerCase();
  110. var flip = 'odd';
  111. if (Drupal.ModuleFilter.activeTab.id == 'all-tab') {
  112. var selector = 'table.package tbody tr td label > strong';
  113. }
  114. else {
  115. var selector = 'table.package tbody tr.' + Drupal.ModuleFilter.activeTab.id + '-content td label strong';
  116. }
  117. $(selector).each(function(i) {
  118. var $row = $(this).parents('tr');
  119. var module = $(this).text();
  120. var moduleLowerCase = module.toLowerCase();
  121. if (moduleLowerCase.match(stringLowerCase)) {
  122. if (Drupal.ModuleFilter.visible($('td.checkbox :checkbox', $row))) {
  123. $row.removeClass('odd even');
  124. $row.addClass(flip);
  125. $row.show();
  126. flip = (flip == 'odd') ? 'even' : 'odd';
  127. }
  128. else {
  129. $row.hide();
  130. }
  131. }
  132. else {
  133. $row.hide();
  134. }
  135. });
  136. }
  137. Drupal.ModuleFilter.Tab = function(element) {
  138. this.id = $(element).attr('id');
  139. this.element = element;
  140. $(this.element).click(function() {
  141. Drupal.ModuleFilter.tabs[$(this).attr('id')].setActive();
  142. });
  143. }
  144. Drupal.ModuleFilter.Tab.prototype.setActive = function() {
  145. if (Drupal.ModuleFilter.activeTab) {
  146. $(Drupal.ModuleFilter.activeTab.element).parent().removeClass('active');
  147. }
  148. // Assume the default active tab is #all-tab. Remove its active class.
  149. else {
  150. $('#all-tab').parent().removeClass('active');
  151. }
  152. Drupal.ModuleFilter.activeTab = this;
  153. $(Drupal.ModuleFilter.activeTab.element).parent().addClass('active');
  154. Drupal.ModuleFilter.activeTab.displayRows();
  155. // Clear filter textfield and refocus on it.
  156. $('input[name="module_filter[name]"]').val('');
  157. $('input[name="module_filter[name]"]').focus();
  158. }
  159. Drupal.ModuleFilter.Tab.prototype.displayRows = function() {
  160. var flip = 'odd';
  161. var selector = (Drupal.ModuleFilter.activeTab.id == 'all-tab') ? 'table.package tbody tr' : 'table.package tbody tr.' + this.id + '-content';
  162. $('table.package tbody tr').hide();
  163. $('table.package tbody tr').removeClass('odd even');
  164. $(selector).each(function(i) {
  165. if (Drupal.ModuleFilter.visible($('td.checkbox input', $(this)))) {
  166. $(this).addClass(flip);
  167. flip = (flip == 'odd') ? 'even' : 'odd';
  168. $(this).show();
  169. }
  170. });
  171. }
  172. Drupal.ModuleFilter.Tab.prototype.updateEnabling = function(amount) {
  173. this.enabling = this.enabling || 0;
  174. this.enabling += amount;
  175. if (this.enabling == 0) {
  176. delete(this.enabling);
  177. }
  178. }
  179. Drupal.ModuleFilter.Tab.prototype.updateDisabling = function(amount) {
  180. this.disabling = this.disabling || 0;
  181. this.disabling += amount;
  182. if (this.disabling == 0) {
  183. delete(this.disabling);
  184. }
  185. }
  186. Drupal.ModuleFilter.Tab.prototype.updateVisualAid = function() {
  187. var visualAid = '';
  188. if (this.enabling != undefined) {
  189. visualAid += '<span class="enabling">' + Drupal.t('+@count', { '@count': this.enabling }) + '</span>';
  190. }
  191. if (this.disabling != undefined) {
  192. visualAid += '<span class="disabling">' + Drupal.t('-@count', { '@count': this.disabling }) + '</span>';
  193. }
  194. if (!$('span.visual-aid', $(this.element)).size() && visualAid != '') {
  195. $(this.element).prepend('<span class="visual-aid"></span>');
  196. }
  197. $('span.visual-aid', $(this.element)).empty().append(visualAid);
  198. }
  199. Drupal.ModuleFilter.updateVisualAid = function(type, row) {
  200. // Find row class.
  201. var classes = row.attr('class').split(' ');
  202. for (var i in classes) {
  203. // Remove '-content' so we can use as id.
  204. var id = classes[i].substring(0, classes[i].length - 8);
  205. if (Drupal.ModuleFilter.tabs[id] != undefined) {
  206. break;
  207. }
  208. }
  209. if (Drupal.ModuleFilter.activeTab.id == 'all-tab') {
  210. var allTab = Drupal.ModuleFilter.activeTab;
  211. var projectTab = Drupal.ModuleFilter.tabs[id];
  212. }
  213. else {
  214. var allTab = Drupal.ModuleFilter.tabs['all-tab'];
  215. var projectTab = Drupal.ModuleFilter.activeTab;
  216. }
  217. var name = $('td label strong', row).text();
  218. switch (type) {
  219. case 'enable':
  220. if (Drupal.ModuleFilter.disabling[id + name] != undefined) {
  221. delete(Drupal.ModuleFilter.disabling[id + name]);
  222. allTab.updateDisabling(-1);
  223. projectTab.updateDisabling(-1);
  224. row.removeClass('disabling');
  225. }
  226. else {
  227. Drupal.ModuleFilter.enabling[id + name] = name;
  228. allTab.updateEnabling(1);
  229. projectTab.updateEnabling(1);
  230. row.addClass('enabling');
  231. }
  232. break;
  233. case 'disable':
  234. if (Drupal.ModuleFilter.enabling[id + name] != undefined) {
  235. delete(Drupal.ModuleFilter.enabling[id + name]);
  236. allTab.updateEnabling(-1);
  237. projectTab.updateEnabling(-1);
  238. row.removeClass('enabling');
  239. }
  240. else {
  241. Drupal.ModuleFilter.disabling[id + name] = name;
  242. allTab.updateDisabling(1);
  243. projectTab.updateDisabling(1);
  244. row.addClass('disabling');
  245. }
  246. break;
  247. }
  248. allTab.updateVisualAid();
  249. projectTab.updateVisualAid();
  250. }
  251. })(jQuery);