mollom.admin.blacklist.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. (function ($) {
  2. /**
  3. * Filters blacklist entries.
  4. */
  5. Drupal.behaviors.mollomBlacklistFilter = {
  6. attach: function (context) {
  7. var self = this;
  8. $('#mollom-blacklist', context).once('mollom-blacklist-filter', function () {
  9. // Prepare a list of all entries to optimize performance. Each key is a
  10. // blacklisted value and each value is an object containing the
  11. // corresponding table row, context, and match.
  12. self.entries = {};
  13. $(this).find('tr:has(.mollom-blacklist-value)').each(function () {
  14. var $row = $(this), entry = {
  15. value: $row.find('.mollom-blacklist-value').text(),
  16. context: $row.children('.mollom-blacklist-context').attr('class').match(/value-(\w+)/)[1],
  17. match: $row.children('.mollom-blacklist-match').attr('class').match(/value-(\w+)/)[1],
  18. row: this
  19. };
  20. self.entries[entry.context + '-' + entry.match + '-' + entry.value] = entry;
  21. });
  22. // Attach the instant text filtering behavior.
  23. var $filterText = $('#mollom-blacklist-filter-value', context);
  24. var $filterContext = $('#mollom-blacklist-filter-context', context);
  25. var $filterMatch = $('#mollom-blacklist-filter-match', context);
  26. self.lastSearch = {};
  27. var filterRows = function () {
  28. // Prepare static variables and conditions only once.
  29. var i, visible, changed;
  30. var search = {
  31. // Blacklist entries are stored in lowercase, so to get any filter
  32. // results, the entered text must be converted to lowercase, too.
  33. value: $filterText.val().toLowerCase(),
  34. context: $filterContext.val(),
  35. match: $filterMatch.val()
  36. };
  37. // Immediately cancel processing if search values did not change.
  38. changed = false;
  39. for (i in search) {
  40. if (search[i] != self.lastSearch[i]) {
  41. changed = true;
  42. break;
  43. }
  44. }
  45. if (!changed) {
  46. return;
  47. }
  48. self.lastSearch = search;
  49. // Blacklists can contain thousands of entries, so we use a simple
  50. // for...in loop instead of jQuery.each() to save many function calls.
  51. // Likewise, we directly apply the 'display' style, since
  52. // jQuery.fn.hide() and jQuery.fn.show() call into jQuery.fn.animate(),
  53. // which is useless for this purpose.
  54. for (i in self.entries) {
  55. visible = (search.context.length == 0 || self.entries[i].context == search.context);
  56. visible = visible && (search.match.length == 0 || self.entries[i].match == search.match);
  57. visible = visible && (search.value.length == 0 || self.entries[i].value.indexOf(search.value) != -1);
  58. if (visible) {
  59. self.entries[i].row.style.display = '';
  60. }
  61. else {
  62. self.entries[i].row.style.display = 'none';
  63. }
  64. }
  65. };
  66. $filterText.bind('keyup change', filterRows);
  67. $filterContext.change(filterRows);
  68. $filterMatch.change(filterRows);
  69. });
  70. }
  71. };
  72. })(jQuery);