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