merged cont.
[opensuse:yast-rest-service.git] / webyast / public / javascripts / jquery.qsearch.js
1 /*
2  Copyright (C) by Rik Lomas at Lomalogue
3  You may use jQuery quicksearch <by Rik Lomas at Lomalogue, see https://github.com/riklomas/quicksearch/blob/master/README.markdown> 
4  under the terms of either the MIT License or the GNU General Public License (GPL) Version 2
5 */
6
7 (function($, window, document, undefined) {
8         $.fn.quicksearch = function (target, opt) {
9                 
10                 var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({ 
11                         delay: 100,
12                         selector: null,
13                         stripeRows: null,
14                         loader: null,
15                         noResults: '',
16                         bind: 'keyup',
17                         onBefore: function () { 
18                                 return;
19                         },
20                         onAfter: function () { 
21                                 return;
22                         },
23                         show: function () {
24                                 this.style.display = "";
25                         },
26                         hide: function () {
27                                 this.style.display = "none";
28                         },
29                         prepareQuery: function (val) {
30                                 return val.toLowerCase().split(' ');
31                         },
32                         testQuery: function (query, txt, _row) {
33                                 for (var i = 0; i < query.length; i += 1) {
34                                         if (txt.indexOf(query[i]) === -1) {
35                                                 return false;
36                                         }
37                                 }
38                                 return true;
39                         }
40                 }, opt);
41                 
42                 this.go = function () {
43                         
44                         var i = 0, 
45                         noresults = true, 
46                         query = options.prepareQuery(val),
47                         val_empty = (val.replace(' ', '').length === 0);
48                         
49                         for (var i = 0, len = rowcache.length; i < len; i++) {
50                                 if (val_empty || options.testQuery(query, cache[i], rowcache[i])) {
51                                         options.show.apply(rowcache[i]);
52                                         noresults = false;
53                                 } else {
54                                         options.hide.apply(rowcache[i]);
55                                 }
56                         }
57                         
58                         if (noresults) {
59                                 this.results(false);
60                         } else {
61                                 this.results(true);
62                                 this.stripe();
63                         }
64                         
65                         this.loader(false);
66                         options.onAfter();
67                         
68                         return this;
69                 };
70                 
71                 this.stripe = function () {
72                         
73                         if (typeof options.stripeRows === "object" && options.stripeRows !== null)
74                         {
75                                 var joined = options.stripeRows.join(' ');
76                                 var stripeRows_length = options.stripeRows.length;
77                                 
78                                 jq_results.not(':hidden').each(function (i) {
79                                         $(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]);
80                                 });
81                         }
82                         
83                         return this;
84                 };
85                 
86                 this.strip_html = function (input) {
87                         var output = input.replace(new RegExp('<[^<]+\>', 'g'), "");
88                         output = $.trim(output.toLowerCase());
89                         return output;
90                 };
91                 
92                 this.results = function (bool) {
93                         if (typeof options.noResults === "string" && options.noResults !== "") {
94                                 if (bool) {
95                                         $(options.noResults).hide();
96                                 } else {
97                                         $(options.noResults).show();
98                                 }
99                         }
100                         return this;
101                 };
102                 
103                 this.loader = function (bool) {
104                         if (typeof options.loader === "string" && options.loader !== "") {
105                                  (bool) ? $(options.loader).show() : $(options.loader).hide();
106                         }
107                         return this;
108                 };
109                 
110                 this.cache = function () {
111                         
112                         jq_results = $(target);
113                         
114                         if (typeof options.noResults === "string" && options.noResults !== "") {
115                                 jq_results = jq_results.not(options.noResults);
116                         }
117                         
118                         var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults);
119                         cache = t.map(function () {
120                                 return e.strip_html(this.innerHTML);
121                         });
122                         
123                         rowcache = jq_results.map(function () {
124                                 return this;
125                         });
126                         
127                         return this.go();
128                 };
129                 
130                 this.trigger = function () {
131                         this.loader(true);
132                         options.onBefore();
133                         
134                         window.clearTimeout(timeout);
135                         timeout = window.setTimeout(function () {
136                                 e.go();
137                         }, options.delay);
138                         
139                         return this;
140                 };
141                 
142                 this.cache();
143                 this.results(true);
144                 this.stripe();
145                 this.loader(false);
146                 
147                 return this.each(function () {
148                         $(this).bind(options.bind, function () {
149                                 val = $(this).val();
150                                 e.trigger();
151                         });
152                 });
153                 
154         };
155
156 }(jQuery, this, document));