tipuesearch.js 32 KB

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