UNPKG

27.1 kBJavaScriptView Raw
1
2/*
3Tipue Search 5.0
4Copyright (c) 2015 Tipue
5Tipue Search is released under the MIT License
6http://www.tipue.com/search
7
8
9Also altered by @AlexDM0 for live updating :)
10*/
11var tipuesearch_in = {
12 pages: []
13 };
14
15(function($) {
16 $.fn.tipuesearch = function(options,dynamic,query) {
17 var set = $.extend( {
18
19 'show' : 7,
20 'newWindow' : false,
21 'showURL' : true,
22 'showTitleCount' : true,
23 'minimumLength' : 3,
24 'descriptiveWords' : 25,
25 'highlightTerms' : true,
26 'highlightEveryTerm' : false,
27 'mode' : 'static',
28 'liveDescription' : '*',
29 'liveContent' : '*',
30 'contentLocation' : 'tipuesearch/tipuesearch_content.json',
31 'debug' : false
32
33 }, options);
34 if (dynamic === undefined) {
35 dynamic = false;
36 }
37
38 return this.each(function() {
39
40 $.ajaxSetup({
41 async: false
42 });
43 var tipuesearch_t_c = 0;
44 if (set.mode == 'live' && tipuesearch_in.pages.length == 0)
45 {
46 for (var i = 0; i < tipuesearch_pages.length; i++)
47 {
48 $.get(tipuesearch_pages[i])
49 .done(function(html)
50 {
51 var cont = $(set.liveContent, html).text();
52 cont = cont.replace(/\s+/g, ' ');
53 var desc = $(set.liveDescription, html).text();
54 desc = desc.replace(/\s+/g, ' ');
55
56 var t_1 = html.toLowerCase().indexOf('<title>');
57 var t_2 = html.toLowerCase().indexOf('</title>', t_1 + 7);
58 if (t_1 != -1 && t_2 != -1)
59 {
60 var tit = html.slice(t_1 + 7, t_2);
61 }
62 else
63 {
64 var tit = tipuesearch_string_1;
65 }
66
67 tipuesearch_in.pages.push(
68 {
69 "title": tit,
70 "text": desc,
71 "tags": cont,
72 "url": tipuesearch_pages[i]
73 });
74 });
75 }
76 }
77
78 if (set.mode == 'json')
79 {
80 $.getJSON(set.contentLocation)
81 .done(function(json)
82 {
83 tipuesearch_in = $.extend({}, json);
84 });
85 }
86
87 if (set.mode == 'static')
88 {
89 tipuesearch_in = $.extend({}, tipuesearch);
90 }
91
92 var tipue_search_w = '';
93 if (set.newWindow)
94 {
95 tipue_search_w = ' target="_blank"';
96 }
97
98 function getURLP(name)
99 {
100 return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20')) || null;
101 }
102 // edit for live update ~ Alex de Mulder
103 if (getURLP('q') && dynamic === false)
104 {
105 $('#tipue_search_input').val(getURLP('q'));
106 getTipueSearch(0, true);
107 }
108 else if (dynamic === true) {
109 getTipueSearch(0, true);
110 }
111
112
113 $(this).keyup(function(event)
114 {
115 if(event.keyCode == '13')
116 {
117 getTipueSearch(0, true);
118 }
119 });
120
121
122 function getTipueSearch(start, replace)
123 {
124 $('#tipue_search_content').hide();
125 $('#tipue_search_content').html('<div class="tipue_search_spinner"><div class="tipue_search_rect1"></div><div class="tipue_search_rect2"></div><div class="rect3"></div></div>');
126 $('#tipue_search_content').show();
127
128 var out = '';
129 var results = '';
130 var show_replace = false;
131 var show_stop = false;
132 var standard = true;
133 var c = 0;
134 found = [];
135
136 var d = $('#tipue_search_input').val().toLowerCase();
137 d = $.trim(d);
138
139 if ((d.match("^\"") && d.match("\"$")) || (d.match("^'") && d.match("'$")))
140 {
141 standard = false;
142 }
143
144 if (standard)
145 {
146 var d_w = d.split(' ');
147 d = '';
148 for (var i = 0; i < d_w.length; i++)
149 {
150 var a_w = true;
151 for (var f = 0; f < tipuesearch_stop_words.length; f++)
152 {
153 if (d_w[i] == tipuesearch_stop_words[f])
154 {
155 a_w = false;
156 show_stop = true;
157 }
158 }
159 if (a_w)
160 {
161 d = d + ' ' + d_w[i];
162 }
163 }
164 d = $.trim(d);
165 d_w = d.split(' ');
166 }
167 else
168 {
169 d = d.substring(1, d.length - 1);
170 }
171
172 if (d.length >= set.minimumLength)
173 {
174 if (standard)
175 {
176 if (replace)
177 {
178 var d_r = d;
179 for (var i = 0; i < d_w.length; i++)
180 {
181 for (var f = 0; f < tipuesearch_replace.words.length; f++)
182 {
183 if (d_w[i] == tipuesearch_replace.words[f].word)
184 {
185 d = d.replace(d_w[i], tipuesearch_replace.words[f].replace_with);
186 show_replace = true;
187 }
188 }
189 }
190 d_w = d.split(' ');
191 }
192
193 var d_t = d;
194 for (var i = 0; i < d_w.length; i++)
195 {
196 for (var f = 0; f < tipuesearch_stem.words.length; f++)
197 {
198 if (d_w[i] == tipuesearch_stem.words[f].word)
199 {
200 d_t = d_t + ' ' + tipuesearch_stem.words[f].stem;
201 }
202 }
203 }
204 d_w = d_t.split(' ');
205
206 for (var i = 0; i < tipuesearch_in.pages.length; i++)
207 {
208 var score = 0;
209 var s_t = tipuesearch_in.pages[i].text;
210 for (var f = 0; f < d_w.length; f++)
211 {
212 var pat = new RegExp(d_w[f], 'gi');
213 if (tipuesearch_in.pages[i].title.search(pat) != -1)
214 {
215 var m_c = tipuesearch_in.pages[i].title.match(pat).length;
216 score += (20 * m_c);
217 }
218 if (tipuesearch_in.pages[i].text.search(pat) != -1)
219 {
220 var m_c = tipuesearch_in.pages[i].text.match(pat).length;
221 score += (20 * m_c);
222 }
223
224 if (set.highlightTerms)
225 {
226 if (set.highlightEveryTerm)
227 {
228 var patr = new RegExp('(' + d_w[f] + ')', 'gi');
229 }
230 else
231 {
232 var patr = new RegExp('(' + d_w[f] + ')', 'i');
233 }
234 s_t = s_t.replace(patr, "<span class=\"h01\">$1</span>");
235 }
236
237 if (tipuesearch_in.pages[i].tags.search(pat) != -1)
238 {
239 var m_c = tipuesearch_in.pages[i].tags.match(pat).length;
240 score += (10 * m_c);
241 }
242
243 if (tipuesearch_in.pages[i].url.search(pat) != -1)
244 {
245 score += 20;
246 }
247
248 if (score != 0)
249 {
250 for (var e = 0; e < tipuesearch_weight.weight.length; e++)
251 {
252 if (tipuesearch_in.pages[i].url == tipuesearch_weight.weight[e].url)
253 {
254 score += tipuesearch_weight.weight[e].score;
255 }
256 }
257 }
258
259 if (d_w[f].match('^-'))
260 {
261 pat = new RegExp(d_w[f].substring(1), 'i');
262 if (tipuesearch_in.pages[i].title.search(pat) != -1 || tipuesearch_in.pages[i].text.search(pat) != -1 || tipuesearch_in.pages[i].tags.search(pat) != -1)
263 {
264 score = 0;
265 }
266 }
267 }
268
269 if (score != 0)
270 {
271 found.push(
272 {
273 "score": score,
274 "title": tipuesearch_in.pages[i].title,
275 "desc": s_t,
276 "url": tipuesearch_in.pages[i].url
277 });
278 c++;
279 }
280 }
281 }
282 else
283 {
284 for (var i = 0; i < tipuesearch_in.pages.length; i++)
285 {
286 var score = 0;
287 var s_t = tipuesearch_in.pages[i].text;
288 var pat = new RegExp(d, 'gi');
289 if (tipuesearch_in.pages[i].title.search(pat) != -1)
290 {
291 var m_c = tipuesearch_in.pages[i].title.match(pat).length;
292 score += (20 * m_c);
293 }
294 if (tipuesearch_in.pages[i].text.search(pat) != -1)
295 {
296 var m_c = tipuesearch_in.pages[i].text.match(pat).length;
297 score += (20 * m_c);
298 }
299
300 if (set.highlightTerms)
301 {
302 if (set.highlightEveryTerm)
303 {
304 var patr = new RegExp('(' + d + ')', 'gi');
305 }
306 else
307 {
308 var patr = new RegExp('(' + d + ')', 'i');
309 }
310 s_t = s_t.replace(patr, "<span class=\"h01\">$1</span>");
311 }
312
313 if (tipuesearch_in.pages[i].tags.search(pat) != -1)
314 {
315 var m_c = tipuesearch_in.pages[i].tags.match(pat).length;
316 score += (10 * m_c);
317 }
318
319 if (tipuesearch_in.pages[i].url.search(pat) != -1)
320 {
321 score += 20;
322 }
323
324 if (score != 0)
325 {
326 for (var e = 0; e < tipuesearch_weight.weight.length; e++)
327 {
328 if (tipuesearch_in.pages[i].url == tipuesearch_weight.weight[e].url)
329 {
330 score += tipuesearch_weight.weight[e].score;
331 }
332 }
333 }
334
335 if (score != 0)
336 {
337 found.push(
338 {
339 "score": score,
340 "title": tipuesearch_in.pages[i].title,
341 "desc": s_t,
342 "url": tipuesearch_in.pages[i].url
343 });
344 c++;
345 }
346 }
347 }
348
349 if (c != 0)
350 {
351 if (set.showTitleCount && tipuesearch_t_c == 0)
352 {
353 var title = document.title;
354 // fix for no stacking of the counters ~ Alex
355 title = title.replace(/(\(.+\) )/g,"");
356 document.title = '(' + c + ') ' + title;
357 tipuesearch_t_c++;
358 }
359
360 if (show_replace == 1)
361 {
362 out += '<div id="tipue_search_warning">' + tipuesearch_string_2 + ' ' + d + '. ' + tipuesearch_string_3 + ' <a id="tipue_search_replaced">' + d_r + '</a></div>';
363 }
364 if (c == 1)
365 {
366 out += '<div id="tipue_search_results_count">' + tipuesearch_string_4 + '</div>';
367 }
368 else
369 {
370 c_c = c.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
371 out += '<div id="tipue_search_results_count">' + c_c + ' ' + tipuesearch_string_5 + '</div>';
372 }
373
374 found.sort(function(a, b) { return b.score - a.score } );
375
376 var l_o = 0;
377 for (var i = 0; i < found.length; i++)
378 {
379 if (l_o >= start && l_o < set.show + start)
380 {
381 out += '<div class="tipue_search_content_title"><a href="' + found[i].url + '"' + tipue_search_w + '>' + found[i].title + '</a></div>';
382
383 if (set.debug)
384 {
385 out += '<div class="tipue_search_content_debug">Score: ' + found[i].score + '</div>';
386 }
387
388 if (set.showURL)
389 {
390 var s_u = found[i].url.toLowerCase();
391 if(s_u.indexOf('http://') == 0)
392 {
393 s_u = s_u.slice(7);
394 }
395 out += '<div class="tipue_search_content_url"><a href="' + found[i].url + '"' + tipue_search_w + '>' + s_u + '</a></div>';
396 }
397
398 if (found[i].desc)
399 {
400 var t = found[i].desc;
401 var t_d = '';
402 var t_w = t.split(' ');
403 if (t_w.length < set.descriptiveWords)
404 {
405 t_d = t;
406 }
407 else
408 {
409 for (var f = 0; f < set.descriptiveWords; f++)
410 {
411 t_d += t_w[f] + ' ';
412 }
413 }
414 t_d = $.trim(t_d);
415 if (t_d.charAt(t_d.length - 1) != '.')
416 {
417 t_d += ' ...';
418 }
419 out += '<div class="tipue_search_content_text">' + t_d + '</div>';
420 }
421 }
422 l_o++;
423 }
424
425 if (c > set.show)
426 {
427 var pages = Math.ceil(c / set.show);
428 var page = (start / set.show);
429 out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
430
431 if (start > 0)
432 {
433 out += '<li><a class="tipue_search_foot_box" id="' + (start - set.show) + '_' + replace + '">' + tipuesearch_string_6 + '</a></li>';
434 }
435
436 if (page <= 2)
437 {
438 var p_b = pages;
439 if (pages > 3)
440 {
441 p_b = 3;
442 }
443 for (var f = 0; f < p_b; f++)
444 {
445 if (f == page)
446 {
447 out += '<li class="current">' + (f + 1) + '</li>';
448 }
449 else
450 {
451 out += '<li><a class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
452 }
453 }
454 }
455 else
456 {
457 var p_b = page + 2;
458 if (p_b > pages)
459 {
460 p_b = pages;
461 }
462 for (var f = page - 1; f < p_b; f++)
463 {
464 if (f == page)
465 {
466 out += '<li class="current">' + (f + 1) + '</li>';
467 }
468 else
469 {
470 out += '<li><a class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
471 }
472 }
473 }
474
475 if (page + 1 != pages)
476 {
477 out += '<li><a class="tipue_search_foot_box" id="' + (start + set.show) + '_' + replace + '">' + tipuesearch_string_7 + '</a></li>';
478 }
479
480 out += '</ul></div>';
481 }
482 }
483 else
484 {
485 out += '<div id="tipue_search_warning">' + tipuesearch_string_8 + '</div>';
486 }
487 }
488 else
489 {
490 if (show_stop)
491 {
492 out += '<div id="tipue_search_warning">' + tipuesearch_string_8 + '. ' + tipuesearch_string_9 + '</div>';
493 }
494 else
495 {
496 out += '<div id="tipue_search_warning">' + tipuesearch_string_10 + '</div>';
497 if (set.minimumLength == 1)
498 {
499 out += '<div id="tipue_search_warning">' + tipuesearch_string_11 + '</div>';
500 }
501 else
502 {
503 out += '<div id="tipue_search_warning">' + tipuesearch_string_12 + ' ' + set.minimumLength + ' ' + tipuesearch_string_13 + '</div>';
504 }
505 }
506 }
507
508 $('#tipue_search_content').hide();
509 $('#tipue_search_content').html(out);
510 $('#tipue_search_content').slideDown(0);
511
512 $('#tipue_search_replaced').click(function()
513 {
514 getTipueSearch(0, false);
515 });
516
517 $('.tipue_search_foot_box').click(function()
518 {
519 var id_v = $(this).attr('id');
520 var id_a = id_v.split('_');
521
522 getTipueSearch(parseInt(id_a[0]), id_a[1]);
523 });
524 }
525
526 });
527 };
528
529})(jQuery);