tipuesearch.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. Tipue Search 7.1
  3. Copyright (c) 2019 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. var ront = '';
  290. f = 0;
  291. for (var i = 0; i < tipuesearch_related.Related.length; i++)
  292. {
  293. if (d == tipuesearch_related.Related[i].search)
  294. {
  295. if (!f)
  296. {
  297. out += '<div class="tipue_search_related">' + tipuesearch_string_10 + ': ';
  298. }
  299. if (show_replace)
  300. {
  301. d_o = d;
  302. }
  303. if (tipuesearch_related.Related[i].include)
  304. {
  305. var r_d = d_o + ' ' + tipuesearch_related.Related[i].related;
  306. }
  307. else
  308. {
  309. var r_d = tipuesearch_related.Related[i].related;
  310. }
  311. ront += '<a class="tipue_search_related_btn" id="' + r_d + '">' + tipuesearch_related.Related[i].related + '</a>, ';
  312. f++;
  313. }
  314. }
  315. if (f)
  316. {
  317. ront = ront.slice(0, -2);
  318. ront += '.</div>';
  319. out += ront;
  320. }
  321. }
  322. if (show_replace)
  323. {
  324. out += '<div id="tipue_search_replace">' + tipuesearch_string_2 + ' ' + d + '. ' + tipuesearch_string_3 + ' <a id="tipue_search_replaced">' + d_r + '</a></div>';
  325. }
  326. found.sort(function(a, b) { return b.score - a.score } );
  327. var l_o = 0;
  328. if (set.imageZoom)
  329. {
  330. 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>';
  331. }
  332. for (var i = 0; i < found.length; i++)
  333. {
  334. if (l_o >= start && l_o < set.show + start)
  335. {
  336. out += '<div class="tipue_search_result">';
  337. out += '<div class="tipue_search_content_title"><a href="' + found[i].url + '"' + tipue_search_w + '>' + found[i].title + '</a></div>';
  338. if (set.debug)
  339. {
  340. out += '<div class="tipue_search_content_debug">Score: ' + found[i].score + '</div>';
  341. }
  342. if (set.showURL)
  343. {
  344. var s_u = found[i].url.toLowerCase();
  345. if (s_u.indexOf('http://') == 0)
  346. {
  347. s_u = s_u.slice(7);
  348. }
  349. out += '<div class="tipue_search_content_url"><a href="' + found[i].url + '"' + tipue_search_w + '>' + s_u + '</a></div>';
  350. }
  351. if (found[i].img)
  352. {
  353. if (set.imageZoom)
  354. {
  355. 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>';
  356. }
  357. else
  358. {
  359. 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>';
  360. }
  361. }
  362. if (found[i].desc)
  363. {
  364. var t = found[i].desc;
  365. if (set.showContext)
  366. {
  367. d_w = d.split(' ');
  368. var s_1 = found[i].desc.toLowerCase().indexOf(d_w[0]);
  369. if (s_1 > set.contextStart)
  370. {
  371. var t_1 = t.substr(s_1 - set.contextBuffer);
  372. var s_2 = t_1.indexOf(' ');
  373. t_1 = t.substr(s_1 - set.contextBuffer + s_2);
  374. t_1 = $.trim(t_1);
  375. if (t_1.length > set.contextLength)
  376. {
  377. t = '... ' + t_1;
  378. }
  379. }
  380. }
  381. if (standard)
  382. {
  383. d_w = d.split(' ');
  384. for (var f = 0; f < d_w.length; f++)
  385. {
  386. if (set.highlightTerms)
  387. {
  388. var patr = new RegExp('(' + d_w[f] + ')', 'gi');
  389. t = t.replace(patr, "<h0011>$1<h0012>");
  390. }
  391. }
  392. }
  393. else if (set.highlightTerms)
  394. {
  395. var patr = new RegExp('(' + d + ')', 'gi');
  396. t = t.replace(patr, "<span class=\"tipue_search_content_bold\">$1</span>");
  397. }
  398. var t_d = '';
  399. var t_w = t.split(' ');
  400. if (t_w.length < set.descriptiveWords)
  401. {
  402. t_d = t;
  403. }
  404. else
  405. {
  406. for (var f = 0; f < set.descriptiveWords; f++)
  407. {
  408. t_d += t_w[f] + ' ';
  409. }
  410. }
  411. t_d = $.trim(t_d);
  412. if (t_d.charAt(t_d.length - 1) != '.')
  413. {
  414. t_d += ' ...';
  415. }
  416. t_d = t_d.replace(/h0011/g, 'span class=\"tipue_search_content_bold\"');
  417. t_d = t_d.replace(/h0012/g, '/span');
  418. out += '<div class="tipue_search_content_text">' + t_d + '</div>';
  419. }
  420. if (found[i].note)
  421. {
  422. out += '<div class="tipue_search_note">' + found[i].note + '</div>';
  423. }
  424. out += '</div>';
  425. }
  426. l_o++;
  427. }
  428. if (c > set.show)
  429. {
  430. var pages = Math.ceil(c / set.show);
  431. var page = (start / set.show);
  432. if (set.footerPages < 3)
  433. {
  434. set.footerPages = 3;
  435. }
  436. out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
  437. if (start > 0)
  438. {
  439. out += '<li role="navigation"><a class="tipue_search_foot_box" accesskey="b" id="' + (start - set.show) + '_' + replace + '">' + tipuesearch_string_6 + '</a></li>';
  440. }
  441. if (page <= 2)
  442. {
  443. var p_b = pages;
  444. if (pages > set.footerPages)
  445. {
  446. p_b = set.footerPages;
  447. }
  448. for (var f = 0; f < p_b; f++)
  449. {
  450. if (f == page)
  451. {
  452. out += '<li class="current" role="navigation">' + (f + 1) + '</li>';
  453. }
  454. else
  455. {
  456. out += '<li role="navigation"><a class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  457. }
  458. }
  459. }
  460. else
  461. {
  462. var p_b = page + set.footerPages - 1;
  463. if (p_b > pages)
  464. {
  465. p_b = pages;
  466. }
  467. for (var f = page - 1; f < p_b; f++)
  468. {
  469. if (f == page)
  470. {
  471. out += '<li class="current" role="navigation">' + (f + 1) + '</li>';
  472. }
  473. else
  474. {
  475. out += '<li role="navigation"><a class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  476. }
  477. }
  478. }
  479. if (page + 1 != pages)
  480. {
  481. out += '<li role="navigation"><a class="tipue_search_foot_box" accesskey="m" id="' + (start + set.show) + '_' + replace + '">' + tipuesearch_string_7 + '</a></li>';
  482. }
  483. out += '</ul></div>';
  484. }
  485. }
  486. else
  487. {
  488. out += '<div id="tipue_search_error">' + tipuesearch_string_8 + '</div>';
  489. }
  490. }
  491. else
  492. {
  493. if (show_stop)
  494. {
  495. out += '<div id="tipue_search_error">' + tipuesearch_string_8 + ' ' + tipuesearch_string_9 + '</div>';
  496. }
  497. else
  498. {
  499. if (set.minimumLength == 1)
  500. {
  501. out += '<div id="tipue_search_error">' + tipuesearch_string_11 + '</div>';
  502. }
  503. else
  504. {
  505. out += '<div id="tipue_search_error">' + tipuesearch_string_12 + ' ' + set.minimumLength + ' ' + tipuesearch_string_13 + '</div>';
  506. }
  507. }
  508. }
  509. $('#tipue_search_content').hide().html(out).slideDown(200);
  510. $('#tipue_search_replaced').click(function()
  511. {
  512. getTipueSearch(0, false);
  513. });
  514. $('.tipue_search_related_btn').click(function()
  515. {
  516. $('#tipue_search_input').val($(this).attr('id'));
  517. getTipueSearch(0, true);
  518. });
  519. $('.tipue_search_image_zoom').click(function()
  520. {
  521. $('#tipue_search_image_modal').fadeIn(300);
  522. $('#tipue_search_zoom_img').attr('src', this.src);
  523. var z_u = $(this).attr('data-url');
  524. $('#tipue_search_zoom_url').attr('href', z_u);
  525. 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>';
  526. $('#tipue_search_zoom_text').html(z_o);
  527. });
  528. $('.tipue_search_image_close').click(function()
  529. {
  530. $('#tipue_search_image_modal').fadeOut(300);
  531. });
  532. $('.tipue_search_foot_box').click(function()
  533. {
  534. var id_v = $(this).attr('id');
  535. var id_a = id_v.split('_');
  536. getTipueSearch(parseInt(id_a[0]), id_a[1]);
  537. });
  538. }
  539. });
  540. };
  541. })(jQuery);