1 | jQuery.fn.quicksearch = function (options) { |
---|
2 | |
---|
3 | this.timeout = null; |
---|
4 | |
---|
5 | this.settings = $.extend({ |
---|
6 | |
---|
7 | position: 'prepend', |
---|
8 | attached: 'body', |
---|
9 | formId: 'quicksearch', |
---|
10 | labelText: 'Quick Search', |
---|
11 | labelClass: 'qs_label', |
---|
12 | inputText: null, |
---|
13 | inputClass: 'qs_input', |
---|
14 | loaderId: 'loader', |
---|
15 | loaderClass: 'loader', |
---|
16 | loaderImg: null, |
---|
17 | loaderText: 'Loading...', |
---|
18 | stripeRowClass: null, |
---|
19 | hideElement: null, |
---|
20 | delay: 500, |
---|
21 | focusOnLoad: false, |
---|
22 | randomElement: 'qs'+Math.floor(Math.random()*1000000) |
---|
23 | }, options || {}); |
---|
24 | |
---|
25 | var el = this; |
---|
26 | |
---|
27 | var form1 = new jQuery._form(this.settings); |
---|
28 | var key1 = new jQuery._key(this.settings); |
---|
29 | var loader = new jQuery._loader(this.settings); |
---|
30 | var stripes = new jQuery._stripe(this.settings.stripeRowClass); |
---|
31 | |
---|
32 | form1.initialize(); |
---|
33 | loader.setTo('hide'); |
---|
34 | |
---|
35 | if(this.settings.stripeRowClass != null) { |
---|
36 | $(el).each(function () { |
---|
37 | if(el.settings.hideElement == "grandparent") { |
---|
38 | stripes.go(this.parentNode.parentNode); |
---|
39 | } else if (el.settings.hideElement == "parent") { |
---|
40 | stripes.go(this.parentNode); |
---|
41 | } else { |
---|
42 | stripes.go(this); |
---|
43 | } |
---|
44 | }) |
---|
45 | stripes.reset(); |
---|
46 | } |
---|
47 | |
---|
48 | $('form.quicksearch').submit( function () { return false; }); |
---|
49 | |
---|
50 | $('input[@rel="'+this.settings.randomElement+'"]').keydown(function(e) { |
---|
51 | switch(e.keyCode) { |
---|
52 | case 9: |
---|
53 | case 13: |
---|
54 | case 38: |
---|
55 | case 40: |
---|
56 | e.preventDefault(); |
---|
57 | break; |
---|
58 | default: |
---|
59 | clearTimeout(this.timeout); |
---|
60 | |
---|
61 | this.timeout = setTimeout(function () { |
---|
62 | loader.setTo('show'); |
---|
63 | setTimeout( function () { |
---|
64 | |
---|
65 | key1.setKey(); |
---|
66 | |
---|
67 | $(el).each( function () { |
---|
68 | |
---|
69 | if(el.settings.hideElement == "grandparent") { |
---|
70 | var hide = $(this).parent().parent(); |
---|
71 | } else if (el.settings.hideElement == "parent") { |
---|
72 | var hide = $(this).parent(); |
---|
73 | } else { |
---|
74 | var hide = $(this) |
---|
75 | } |
---|
76 | |
---|
77 | if(key1.test(this)) { |
---|
78 | |
---|
79 | $(hide).show(); |
---|
80 | } else { |
---|
81 | $(hide).hide(); |
---|
82 | } |
---|
83 | if(el.settings.stripeRowClass != null) { |
---|
84 | if(el.settings.hideElement == "grandparent") { |
---|
85 | stripes.go(this.parentNode.parentNode); |
---|
86 | } else if (el.settings.hideElement == "parent") { |
---|
87 | stripes.go(this.parentNode); |
---|
88 | } else { |
---|
89 | stripes.go(this); |
---|
90 | } |
---|
91 | } |
---|
92 | }); |
---|
93 | stripes.reset(); |
---|
94 | }, el.settings.delay); |
---|
95 | setTimeout( function () { |
---|
96 | loader.setTo('hide'); |
---|
97 | }, el.settings.delay); |
---|
98 | }, el.settings.delay); |
---|
99 | break; |
---|
100 | } |
---|
101 | }); |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | jQuery._key = function (set) { |
---|
110 | |
---|
111 | this.key = ""; |
---|
112 | this.settings = set; |
---|
113 | this.getKey = function () { |
---|
114 | return this.key; |
---|
115 | }; |
---|
116 | this.setKey = function () { |
---|
117 | var input = $('input[@rel="'+this.settings.randomElement+'"]').val(); |
---|
118 | var string = input.replace(/\s{2,}/g, " ").toLowerCase(); |
---|
119 | var arr = string.split(" "); |
---|
120 | for(var i in arr) { |
---|
121 | var regexp = new RegExp(/([^A-Za-z0-9])/gi); |
---|
122 | if(arr[i] == "") { |
---|
123 | arr.splice(i,1); |
---|
124 | } |
---|
125 | } |
---|
126 | this.key = arr; |
---|
127 | }; |
---|
128 | this.test = function (el) { |
---|
129 | if( this.getKey() == '' ) { |
---|
130 | return true; |
---|
131 | } else { |
---|
132 | var searchStrings = this.getKey(); |
---|
133 | var text = $._stripHtml( $(el).html() ); |
---|
134 | for (var i = 0; i < searchStrings.length; i++) { |
---|
135 | var test = text.indexOf(searchStrings[i]); |
---|
136 | if (test == -1) { |
---|
137 | return false; |
---|
138 | } |
---|
139 | } |
---|
140 | return true; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | jQuery._form = function (set) { |
---|
146 | |
---|
147 | this.settings = set; |
---|
148 | |
---|
149 | this.initialize = function () { |
---|
150 | this.placeForm(); |
---|
151 | if(this.settings.focusOnLoad) { |
---|
152 | this.focusOnLoad(); |
---|
153 | } |
---|
154 | if(this.settings.inputText != "" && this.settings.inputText != null) { |
---|
155 | this.toggleText(); |
---|
156 | } |
---|
157 | }; |
---|
158 | this.placeForm = function () { |
---|
159 | var formPosition = this.settings.position; |
---|
160 | var formAttached = this.settings.attached; |
---|
161 | |
---|
162 | if(formPosition == 'before') { |
---|
163 | $(formAttached).before( jQuery._makeForm(this.settings) ); |
---|
164 | } else if(formPosition == 'prepend') { |
---|
165 | $(formAttached).prepend( jQuery._makeForm(this.settings) ); |
---|
166 | } else if(formPosition == 'append') { |
---|
167 | $(formAttached).append( jQuery._makeForm(this.settings) ); |
---|
168 | } else { |
---|
169 | $(formAttached).after( jQuery._makeForm(this.settings) ); |
---|
170 | } |
---|
171 | }; |
---|
172 | this.focusOnLoad = function () { |
---|
173 | $('input[@rel="'+this.settings.randomElement+'"]').get(0).focus(); |
---|
174 | }; |
---|
175 | this.toggleText = function () { |
---|
176 | var el = this; |
---|
177 | $('input[@rel="'+this.settings.randomElement+'"]').focus(function () { |
---|
178 | if($(this).val() == el.settings.inputText) { |
---|
179 | $(this).val(''); |
---|
180 | } |
---|
181 | }), |
---|
182 | $('input[@rel="'+this.settings.randomElement+'"]').blur(function () { |
---|
183 | if($(this).val() == "") { |
---|
184 | $(this).val(el.settings.inputText); |
---|
185 | } |
---|
186 | }) |
---|
187 | }; |
---|
188 | }; |
---|
189 | |
---|
190 | jQuery._loader = function (set) { |
---|
191 | this.settings = set; |
---|
192 | this.setTo = function (to) { |
---|
193 | if(this.settings.loaderId) { |
---|
194 | if(to == 'hide') { |
---|
195 | $('input[@rel="'+this.settings.randomElement+'"]').parent().find('.loader').hide(); |
---|
196 | } else { |
---|
197 | $('input[@rel="'+this.settings.randomElement+'"]').parent().find('.loader').show(); |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | jQuery._makeForm = function (set) { |
---|
204 | this.settings = set; |
---|
205 | this.mform = function () { |
---|
206 | return '<form action="#" ' + |
---|
207 | 'id="'+ this.settings.formId + '" ' + |
---|
208 | 'class="quicksearch">' + |
---|
209 | this.mlabel() + |
---|
210 | this.minput() + |
---|
211 | this.mloader() + |
---|
212 | '</form>'; |
---|
213 | }; |
---|
214 | this.mlabel = function () { |
---|
215 | if(!this.isEmpty(this.settings.labelText)) { |
---|
216 | return '<label for="' + this.settings.inputId + '" '+ |
---|
217 | 'class="' + this.settings.labelClass + '">' |
---|
218 | + this.settings.labelText |
---|
219 | + '</label> '; |
---|
220 | } |
---|
221 | return ''; |
---|
222 | }; |
---|
223 | this.minput = function () { |
---|
224 | var val = (!this.isEmpty(this.settings.inputText)) ? this.settings.inputText : "" |
---|
225 | return '<input type="text" ' + |
---|
226 | 'value="' + val + '" ' + |
---|
227 | 'rel="' + this.settings.randomElement + '" ' + |
---|
228 | 'class="' + this.settings.inputClass + '" ' + |
---|
229 | '/> '; |
---|
230 | }; |
---|
231 | this.mloader = function () { |
---|
232 | if(!this.isEmpty(this.settings.loaderImg)) { |
---|
233 | return '<img src="' + this.settings.loaderImg + '" alt="Loading" id="' + this.settings.loaderId + '" class="' + this.settings.loaderClass + '" />'; |
---|
234 | } else { |
---|
235 | return '<span id="' + this.settings.loaderId + '" class="' + this.settings.loaderClass + '">' + this.settings.loaderText + '</span>'; |
---|
236 | } |
---|
237 | }; |
---|
238 | this.isEmpty = function (input) { |
---|
239 | return (input == null || input == undefined || input == '' || input == 0) ? true: false; |
---|
240 | }; |
---|
241 | return this.mform(); |
---|
242 | } |
---|
243 | |
---|
244 | jQuery._stripHtml = function (input) { |
---|
245 | var regexp = new RegExp( /\<[^\<]+\>/g ); |
---|
246 | var output = input.replace(regexp, ""); |
---|
247 | output = output.toLowerCase(); |
---|
248 | return output; |
---|
249 | } |
---|
250 | |
---|
251 | jQuery._stripe = function (set) { |
---|
252 | this.i = 0; |
---|
253 | this.set = set; |
---|
254 | this.go = function (el) { |
---|
255 | this.removeClasses(el); |
---|
256 | if(el.getAttribute('style') != "display: none;") { |
---|
257 | $(el).addClass(this.set[this.i%this.set.length]); |
---|
258 | this.i += 1; |
---|
259 | } |
---|
260 | }; |
---|
261 | this.removeClasses = function (el) { |
---|
262 | for(var j = 0; j < this.set.length; j++) { |
---|
263 | if(this.i%this.set.length != j) { |
---|
264 | $(el).removeClass(this.set[j]); |
---|
265 | } |
---|
266 | } |
---|
267 | }; |
---|
268 | this.reset = function () { |
---|
269 | this.i = 0; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | |
---|