tipuesearch.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. Tipue Search 4.0
  3. Copyright (c) 2014 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. 'show' : 7,
  11. 'newWindow' : false,
  12. 'showURL' : true,
  13. 'minimumLength' : 3,
  14. 'descriptiveWords' : 25,
  15. 'highlightTerms' : true,
  16. 'highlightEveryTerm' : false,
  17. 'mode' : 'static',
  18. 'liveDescription' : '*',
  19. 'liveContent' : '*',
  20. 'contentLocation' : 'tipuesearch/tipuesearch_content.json'
  21. }, options);
  22. return this.each(function() {
  23. var tipuesearch_in = {
  24. pages: []
  25. };
  26. $.ajaxSetup({
  27. async: false
  28. });
  29. if (set.mode == 'live')
  30. {
  31. for (var i = 0; i < tipuesearch_pages.length; i++)
  32. {
  33. $.get(tipuesearch_pages[i], '',
  34. function (html)
  35. {
  36. var cont = $(set.liveContent, html).text();
  37. cont = cont.replace(/\s+/g, ' ');
  38. var desc = $(set.liveDescription, html).text();
  39. desc = desc.replace(/\s+/g, ' ');
  40. var t_1 = html.toLowerCase().indexOf('<title>');
  41. var t_2 = html.toLowerCase().indexOf('</title>', t_1 + 7);
  42. if (t_1 != -1 && t_2 != -1)
  43. {
  44. var tit = html.slice(t_1 + 7, t_2);
  45. }
  46. else
  47. {
  48. var tit = 'No title';
  49. }
  50. tipuesearch_in.pages.push({
  51. "title": tit,
  52. "text": desc,
  53. "tags": cont,
  54. "loc": tipuesearch_pages[i]
  55. });
  56. }
  57. );
  58. }
  59. }
  60. if (set.mode == 'json')
  61. {
  62. $.getJSON(set.contentLocation,
  63. function(json)
  64. {
  65. tipuesearch_in = $.extend({}, json);
  66. }
  67. );
  68. }
  69. if (set.mode == 'static')
  70. {
  71. tipuesearch_in = $.extend({}, tipuesearch);
  72. }
  73. var tipue_search_w = '';
  74. if (set.newWindow)
  75. {
  76. tipue_search_w = ' target="_blank"';
  77. }
  78. function getURLP(name)
  79. {
  80. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20')) || null;
  81. }
  82. if (getURLP('q'))
  83. {
  84. $('#tipue_search_input').val(getURLP('q'));
  85. getTipueSearch(0, true);
  86. }
  87. $(this).keyup(function(event)
  88. {
  89. if(event.keyCode == '13')
  90. {
  91. getTipueSearch(0, true);
  92. }
  93. });
  94. function getTipueSearch(start, replace)
  95. {
  96. $('#tipue_search_content').hide();
  97. var out = '';
  98. var results = '';
  99. var show_replace = false;
  100. var show_stop = false;
  101. var standard = true;
  102. var c = 0;
  103. found = new Array();
  104. var d = $('#tipue_search_input').val().toLowerCase();
  105. d = $.trim(d);
  106. if ((d.match("^\"") && d.match("\"$")) || (d.match("^'") && d.match("'$")))
  107. {
  108. standard = false;
  109. }
  110. if (standard)
  111. {
  112. var d_w = d.split(' ');
  113. d = '';
  114. for (var i = 0; i < d_w.length; i++)
  115. {
  116. var a_w = true;
  117. for (var f = 0; f < tipuesearch_stop_words.length; f++)
  118. {
  119. if (d_w[i] == tipuesearch_stop_words[f])
  120. {
  121. a_w = false;
  122. show_stop = true;
  123. }
  124. }
  125. if (a_w)
  126. {
  127. d = d + ' ' + d_w[i];
  128. }
  129. }
  130. d = $.trim(d);
  131. d_w = d.split(' ');
  132. }
  133. else
  134. {
  135. d = d.substring(1, d.length - 1);
  136. }
  137. if (d.length >= set.minimumLength)
  138. {
  139. if (standard)
  140. {
  141. if (replace)
  142. {
  143. var d_r = d;
  144. for (var i = 0; i < d_w.length; i++)
  145. {
  146. for (var f = 0; f < tipuesearch_replace.words.length; f++)
  147. {
  148. if (d_w[i] == tipuesearch_replace.words[f].word)
  149. {
  150. d = d.replace(d_w[i], tipuesearch_replace.words[f].replace_with);
  151. show_replace = true;
  152. }
  153. }
  154. }
  155. d_w = d.split(' ');
  156. }
  157. var d_t = d;
  158. for (var i = 0; i < d_w.length; i++)
  159. {
  160. for (var f = 0; f < tipuesearch_stem.words.length; f++)
  161. {
  162. if (d_w[i] == tipuesearch_stem.words[f].word)
  163. {
  164. d_t = d_t + ' ' + tipuesearch_stem.words[f].stem;
  165. }
  166. }
  167. }
  168. d_w = d_t.split(' ');
  169. for (var i = 0; i < tipuesearch_in.pages.length; i++)
  170. {
  171. var score = 1000000000;
  172. var s_t = tipuesearch_in.pages[i].text;
  173. for (var f = 0; f < d_w.length; f++)
  174. {
  175. var pat = new RegExp(d_w[f], 'i');
  176. if (tipuesearch_in.pages[i].title.search(pat) != -1)
  177. {
  178. score -= (200000 - i);
  179. }
  180. if (tipuesearch_in.pages[i].text.search(pat) != -1)
  181. {
  182. score -= (150000 - i);
  183. }
  184. if (set.highlightTerms)
  185. {
  186. if (set.highlightEveryTerm)
  187. {
  188. var patr = new RegExp('(' + d_w[f] + ')', 'gi');
  189. }
  190. else
  191. {
  192. var patr = new RegExp('(' + d_w[f] + ')', 'i');
  193. }
  194. s_t = s_t.replace(patr, "<span class=\"h01\">$1</span>");
  195. }
  196. if (tipuesearch_in.pages[i].tags.search(pat) != -1)
  197. {
  198. score -= (100000 - i);
  199. }
  200. if (d_w[f].match("^-"))
  201. {
  202. pat = new RegExp(d_w[f].substring(1), 'i');
  203. 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)
  204. {
  205. score = 1000000000;
  206. }
  207. }
  208. }
  209. if (score < 1000000000)
  210. {
  211. found[c++] = score + '^' + tipuesearch_in.pages[i].title + '^' + s_t + '^' + tipuesearch_in.pages[i].loc;
  212. }
  213. }
  214. }
  215. else
  216. {
  217. for (var i = 0; i < tipuesearch_in.pages.length; i++)
  218. {
  219. var score = 1000000000;
  220. var s_t = tipuesearch_in.pages[i].text;
  221. var pat = new RegExp(d, 'i');
  222. if (tipuesearch_in.pages[i].title.search(pat) != -1)
  223. {
  224. score -= (200000 - i);
  225. }
  226. if (tipuesearch_in.pages[i].text.search(pat) != -1)
  227. {
  228. score -= (150000 - i);
  229. }
  230. if (set.highlightTerms)
  231. {
  232. if (set.highlightEveryTerm)
  233. {
  234. var patr = new RegExp('(' + d + ')', 'gi');
  235. }
  236. else
  237. {
  238. var patr = new RegExp('(' + d + ')', 'i');
  239. }
  240. s_t = s_t.replace(patr, "<span class=\"h01\">$1</span>");
  241. }
  242. if (tipuesearch_in.pages[i].tags.search(pat) != -1)
  243. {
  244. score -= (100000 - i);
  245. }
  246. if (score < 1000000000)
  247. {
  248. found[c++] = score + '^' + tipuesearch_in.pages[i].title + '^' + s_t + '^' + tipuesearch_in.pages[i].loc;
  249. }
  250. }
  251. }
  252. if (c != 0)
  253. {
  254. if (show_replace == 1)
  255. {
  256. out += '<div id="tipue_search_warning_head">Showing results for ' + d + '</div>';
  257. out += '<div id="tipue_search_warning">Search instead for <a href="javascript:void(0)" id="tipue_search_replaced">' + d_r + '</a></div>';
  258. }
  259. if (c == 1)
  260. {
  261. out += '<div id="tipue_search_results_count">1 result</div>';
  262. }
  263. else
  264. {
  265. c_c = c.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  266. out += '<div id="tipue_search_results_count">' + c_c + ' results</div>';
  267. }
  268. found.sort();
  269. var l_o = 0;
  270. for (var i = 0; i < found.length; i++)
  271. {
  272. var fo = found[i].split('^');
  273. if (l_o >= start && l_o < set.show + start)
  274. {
  275. out += '<div class="tipue_search_content_title"><a href="' + fo[3] + '"' + tipue_search_w + '>' + fo[1] + '</a></div>';
  276. if (set.showURL)
  277. {
  278. out += '<div class="tipue_search_content_url"><a href="' + fo[3] + '"' + tipue_search_w + '>' + fo[3] + '</a></div>';
  279. }
  280. var t = fo[2];
  281. var t_d = '';
  282. var t_w = t.split(' ');
  283. if (t_w.length < set.descriptiveWords)
  284. {
  285. t_d = t;
  286. }
  287. else
  288. {
  289. for (var f = 0; f < set.descriptiveWords; f++)
  290. {
  291. t_d += t_w[f] + ' ';
  292. }
  293. }
  294. t_d = $.trim(t_d);
  295. if (t_d.charAt(t_d.length - 1) != '.')
  296. {
  297. t_d += ' ...';
  298. }
  299. out += '<div class="tipue_search_content_text">' + t_d + '</div>';
  300. }
  301. l_o++;
  302. }
  303. if (c > set.show)
  304. {
  305. var pages = Math.ceil(c / set.show);
  306. var page = (start / set.show);
  307. out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
  308. if (start > 0)
  309. {
  310. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start - set.show) + '_' + replace + '">Prev</a></li>';
  311. }
  312. if (page <= 2)
  313. {
  314. var p_b = pages;
  315. if (pages > 3)
  316. {
  317. p_b = 3;
  318. }
  319. for (var f = 0; f < p_b; f++)
  320. {
  321. if (f == page)
  322. {
  323. out += '<li class="current">' + (f + 1) + '</li>';
  324. }
  325. else
  326. {
  327. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  328. }
  329. }
  330. }
  331. else
  332. {
  333. var p_b = page + 2;
  334. if (p_b > pages)
  335. {
  336. p_b = pages;
  337. }
  338. for (var f = page - 1; f < p_b; f++)
  339. {
  340. if (f == page)
  341. {
  342. out += '<li class="current">' + (f + 1) + '</li>';
  343. }
  344. else
  345. {
  346. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  347. }
  348. }
  349. }
  350. if (page + 1 != pages)
  351. {
  352. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start + set.show) + '_' + replace + '">Next</a></li>';
  353. }
  354. out += '</ul></div>';
  355. }
  356. }
  357. else
  358. {
  359. out += '<div id="tipue_search_warning_head">Nothing found</div>';
  360. }
  361. }
  362. else
  363. {
  364. if (show_stop)
  365. {
  366. out += '<div id="tipue_search_warning_head">Nothing found</div><div id="tipue_search_warning">Common words are largely ignored</div>';
  367. }
  368. else
  369. {
  370. out += '<div id="tipue_search_warning_head">Search too short</div>';
  371. if (set.minimumLength == 1)
  372. {
  373. out += '<div id="tipue_search_warning">Should be one character or more</div>';
  374. }
  375. else
  376. {
  377. out += '<div id="tipue_search_warning">Should be ' + set.minimumLength + ' characters or more</div>';
  378. }
  379. }
  380. }
  381. $('#tipue_search_content').html(out);
  382. $('#tipue_search_content').slideDown(200);
  383. $('#tipue_search_replaced').click(function()
  384. {
  385. getTipueSearch(0, false);
  386. });
  387. $('.tipue_search_foot_box').click(function()
  388. {
  389. var id_v = $(this).attr('id');
  390. var id_a = id_v.split('_');
  391. getTipueSearch(parseInt(id_a[0]), id_a[1]);
  392. });
  393. }
  394. });
  395. };
  396. })(jQuery);