quick_search.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. (function($, window, document, undefined) {
  2. $.fn.quicksearch = function (target, opt) {
  3. var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({
  4. delay: 10,
  5. selector: null,
  6. stripeRows: null,
  7. loader: null,
  8. noResults: '',
  9. bind: 'keyup',
  10. onBefore: function () {
  11. return;
  12. },
  13. onAfter: function () {
  14. return;
  15. },
  16. show: function () {
  17. this.style.display = "";
  18. },
  19. hide: function () {
  20. this.style.display = "none";
  21. },
  22. prepareQuery: function (val) {
  23. return val.toLowerCase().split(' ');
  24. },
  25. testQuery: function (query, txt, _row) {
  26. for (var i = 0; i < query.length; i += 1) {
  27. if (txt.indexOf(query[i]) === -1) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. }, opt);
  34. var oldVal='';
  35. this.go = function () {
  36. var i = 0,
  37. noresults = true,
  38. query = options.prepareQuery(val),
  39. val_empty = (val.replace(' ', '').length === 0);
  40. if (val != oldVal){
  41. oldVal=val;
  42. for (var i = 0, len = rowcache.length; i < len; i++) {
  43. if (val_empty || options.testQuery(query, cache[i], rowcache[i])) {
  44. options.show.apply(rowcache[i]);
  45. noresults = false;
  46. } else {
  47. options.hide.apply(rowcache[i]);
  48. }
  49. }
  50. if (noresults) {
  51. this.results(false);
  52. } else {
  53. this.results(true);
  54. this.stripe();
  55. }
  56. this.loader(false);
  57. options.onAfter();
  58. }
  59. return this;
  60. };
  61. this.stripe = function () {
  62. if (typeof options.stripeRows === "object" && options.stripeRows !== null)
  63. {
  64. var joined = options.stripeRows.join(' ');
  65. var stripeRows_length = options.stripeRows.length;
  66. jq_results.not(':hidden').each(function (i) {
  67. $(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]);
  68. });
  69. }
  70. return this;
  71. };
  72. this.strip_html = function (input) {
  73. var output = input.replace(new RegExp('<[^<]+\>', 'g'), "");
  74. output = $.trim(output.toLowerCase());
  75. return output;
  76. };
  77. this.results = function (bool) {
  78. if (typeof options.noResults === "string" && options.noResults !== "") {
  79. if (bool) {
  80. $(options.noResults).hide();
  81. } else {
  82. $(options.noResults).show();
  83. }
  84. }
  85. return this;
  86. };
  87. this.loader = function (bool) {
  88. if (typeof options.loader === "string" && options.loader !== "") {
  89. (bool) ? $(options.loader).show() : $(options.loader).hide();
  90. }
  91. return this;
  92. };
  93. this.cache = function () {
  94. jq_results = $(target);
  95. if (typeof options.noResults === "string" && options.noResults !== "") {
  96. jq_results = jq_results.not(options.noResults);
  97. }
  98. var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults);
  99. cache = t.map(function () {
  100. return e.strip_html(this.innerHTML);
  101. });
  102. rowcache = jq_results.map(function () {
  103. return this;
  104. });
  105. return this.go();
  106. };
  107. this.trigger = function () {
  108. this.loader(true);
  109. if (val != oldVal){
  110. options.onBefore();
  111. }
  112. window.clearTimeout(timeout);
  113. timeout = window.setTimeout(function () {
  114. e.go();
  115. }, options.delay);
  116. return this;
  117. };
  118. this.cache();
  119. this.results(true);
  120. this.stripe();
  121. this.loader(false);
  122. return this.each(function () {
  123. $(this).bind(options.bind, function () {
  124. val = $(this).val();
  125. e.trigger();
  126. });
  127. });
  128. };
  129. }(jQuery, this, document));