tipuesearch.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. Tipue Search 3.0.1
  3. Copyright (c) 2013 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. $('#tipue_search_button').click(function()
  88. {
  89. getTipueSearch(0, true);
  90. });
  91. $(this).keyup(function(event)
  92. {
  93. if(event.keyCode == '13')
  94. {
  95. getTipueSearch(0, true);
  96. }
  97. });
  98. function getTipueSearch(start, replace)
  99. {
  100. $('#tipue_search_content').hide();
  101. var out = '';
  102. var results = '';
  103. var show_replace = false;
  104. var show_stop = false;
  105. var d = $('#tipue_search_input').val().toLowerCase();
  106. d = $.trim(d);
  107. var d_w = d.split(' ');
  108. d = '';
  109. for (var i = 0; i < d_w.length; i++)
  110. {
  111. var a_w = true;
  112. for (var f = 0; f < tipuesearch_stop_words.length; f++)
  113. {
  114. if (d_w[i] == tipuesearch_stop_words[f])
  115. {
  116. a_w = false;
  117. show_stop = true;
  118. }
  119. }
  120. if (a_w)
  121. {
  122. d = d + ' ' + d_w[i];
  123. }
  124. }
  125. d = $.trim(d);
  126. d_w = d.split(' ');
  127. if (d.length >= set.minimumLength)
  128. {
  129. if (replace)
  130. {
  131. var d_r = d;
  132. for (var i = 0; i < d_w.length; i++)
  133. {
  134. for (var f = 0; f < tipuesearch_replace.words.length; f++)
  135. {
  136. if (d_w[i] == tipuesearch_replace.words[f].word)
  137. {
  138. d = d.replace(d_w[i], tipuesearch_replace.words[f].replace_with);
  139. show_replace = true;
  140. }
  141. }
  142. }
  143. d_w = d.split(' ');
  144. }
  145. var d_t = d;
  146. for (var i = 0; i < d_w.length; i++)
  147. {
  148. for (var f = 0; f < tipuesearch_stem.words.length; f++)
  149. {
  150. if (d_w[i] == tipuesearch_stem.words[f].word)
  151. {
  152. d_t = d_t + ' ' + tipuesearch_stem.words[f].stem;
  153. }
  154. }
  155. }
  156. d_w = d_t.split(' ');
  157. var c = 0;
  158. found = new Array();
  159. for (var i = 0; i < tipuesearch_in.pages.length; i++)
  160. {
  161. var score = 1000000000;
  162. var s_t = tipuesearch_in.pages[i].text;
  163. for (var f = 0; f < d_w.length; f++)
  164. {
  165. var pat = new RegExp(d_w[f], 'i');
  166. if (tipuesearch_in.pages[i].title.search(pat) != -1)
  167. {
  168. score -= (200000 - i);
  169. }
  170. if (tipuesearch_in.pages[i].text.search(pat) != -1)
  171. {
  172. score -= (150000 - i);
  173. }
  174. if (set.highlightTerms)
  175. {
  176. if (set.highlightEveryTerm)
  177. {
  178. var patr = new RegExp('(' + d_w[f] + ')', 'gi');
  179. }
  180. else
  181. {
  182. var patr = new RegExp('(' + d_w[f] + ')', 'i');
  183. }
  184. s_t = s_t.replace(patr, "<b>$1</b>");
  185. }
  186. if (tipuesearch_in.pages[i].tags.search(pat) != -1)
  187. {
  188. score -= (100000 - i);
  189. }
  190. }
  191. if (score < 1000000000)
  192. {
  193. found[c++] = score + '^' + tipuesearch_in.pages[i].title + '^' + s_t + '^' + tipuesearch_in.pages[i].loc;
  194. }
  195. }
  196. if (c != 0)
  197. {
  198. if (show_replace == 1)
  199. {
  200. out += '<div id="tipue_search_warning_head">Showing results for ' + d + '</div>';
  201. out += '<div id="tipue_search_warning">Search for <a href="javascript:void(0)" id="tipue_search_replaced">' + d_r + '</a></div>';
  202. }
  203. if (c == 1)
  204. {
  205. out += '<div id="tipue_search_results_count">1 result</div>';
  206. }
  207. else
  208. {
  209. c_c = c.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  210. out += '<div id="tipue_search_results_count">' + c_c + ' results</div>';
  211. }
  212. found.sort();
  213. var l_o = 0;
  214. for (var i = 0; i < found.length; i++)
  215. {
  216. var fo = found[i].split('^');
  217. if (l_o >= start && l_o < set.show + start)
  218. {
  219. out += '<div class="tipue_search_content_title"><a href="' + fo[3] + '"' + tipue_search_w + '>' + fo[1] + '</a></div>';
  220. var t = fo[2];
  221. var t_d = '';
  222. var t_w = t.split(' ');
  223. if (t_w.length < set.descriptiveWords)
  224. {
  225. t_d = t;
  226. }
  227. else
  228. {
  229. for (var f = 0; f < set.descriptiveWords; f++)
  230. {
  231. t_d += t_w[f] + ' ';
  232. }
  233. }
  234. t_d = $.trim(t_d);
  235. if (t_d.charAt(t_d.length - 1) != '.')
  236. {
  237. t_d += ' ...';
  238. }
  239. out += '<div class="tipue_search_content_text">' + t_d + '</div>';
  240. if (set.showURL)
  241. {
  242. t_url = fo[3];
  243. if (t_url.length > 45)
  244. {
  245. t_url = fo[3].substr(0, 45) + ' ...';
  246. }
  247. out += '<div class="tipue_search_content_loc"><a href="' + fo[3] + '"' + tipue_search_w + '>' + t_url + '</a></div>';
  248. }
  249. }
  250. l_o++;
  251. }
  252. if (c > set.show)
  253. {
  254. var pages = Math.ceil(c / set.show);
  255. var page = (start / set.show);
  256. out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
  257. if (start > 0)
  258. {
  259. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start - set.show) + '_' + replace + '">Prev</a></li>';
  260. }
  261. if (page <= 2)
  262. {
  263. var p_b = pages;
  264. if (pages > 3)
  265. {
  266. p_b = 3;
  267. }
  268. for (var f = 0; f < p_b; f++)
  269. {
  270. if (f == page)
  271. {
  272. out += '<li class="current">' + (f + 1) + '</li>';
  273. }
  274. else
  275. {
  276. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  277. }
  278. }
  279. }
  280. else
  281. {
  282. var p_b = page + 3;
  283. if (p_b > pages)
  284. {
  285. p_b = pages;
  286. }
  287. for (var f = page; f < p_b; f++)
  288. {
  289. if (f == page)
  290. {
  291. out += '<li class="current">' + (f + 1) + '</li>';
  292. }
  293. else
  294. {
  295. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  296. }
  297. }
  298. }
  299. if (page + 1 != pages)
  300. {
  301. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start + set.show) + '_' + replace + '">Next</a></li>';
  302. }
  303. out += '</ul></div>';
  304. }
  305. }
  306. else
  307. {
  308. out += '<div id="tipue_search_warning_head">Nothing found</div>';
  309. }
  310. }
  311. else
  312. {
  313. if (show_stop)
  314. {
  315. out += '<div id="tipue_search_warning_head">Nothing found</div><div id="tipue_search_warning">Common words are largely ignored</div>';
  316. }
  317. else
  318. {
  319. out += '<div id="tipue_search_warning_head">Search too short</div>';
  320. if (set.minimumLength == 1)
  321. {
  322. out += '<div id="tipue_search_warning">Should be one character or more</div>';
  323. }
  324. else
  325. {
  326. out += '<div id="tipue_search_warning">Should be ' + set.minimumLength + ' characters or more</div>';
  327. }
  328. }
  329. }
  330. $('#tipue_search_content').html(out);
  331. $('#tipue_search_content').slideDown(200);
  332. $('#tipue_search_replaced').click(function()
  333. {
  334. getTipueSearch(0, false);
  335. });
  336. $('.tipue_search_foot_box').click(function()
  337. {
  338. var id_v = $(this).attr('id');
  339. var id_a = id_v.split('_');
  340. getTipueSearch(parseInt(id_a[0]), id_a[1]);
  341. });
  342. }
  343. });
  344. };
  345. })(jQuery);