tipuesearch.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. Tipue Search 2.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' || set.mode == 'static-images')
  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, "<em>$1</em>");
  185. }
  186. if (tipuesearch_in.pages[i].tags.search(pat) != -1)
  187. {
  188. score -= (100000 - i);
  189. }
  190. }
  191. if (score < 1000000000)
  192. {
  193. if (set.mode == 'static-images')
  194. {
  195. found[c++] = score + '^' + tipuesearch_in.pages[i].title + '^' + s_t + '^' + tipuesearch_in.pages[i].loc + '^' + tipuesearch_in.pages[i].image;
  196. }
  197. else
  198. {
  199. found[c++] = score + '^' + tipuesearch_in.pages[i].title + '^' + s_t + '^' + tipuesearch_in.pages[i].loc;
  200. }
  201. }
  202. }
  203. if (c != 0)
  204. {
  205. if (show_replace == 1)
  206. {
  207. out += '<div id="tipue_search_warning_head">Showing results for ' + d + '</div>';
  208. out += '<div id="tipue_search_warning">Show results for <a href="javascript:void(0)" id="tipue_search_replaced">' + d_r + '</a></div>';
  209. }
  210. if (c == 1)
  211. {
  212. out += '<div id="tipue_search_results_count">1 result</div>';
  213. }
  214. else
  215. {
  216. c_c = c.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  217. out += '<div id="tipue_search_results_count">' + c_c + ' results</div>';
  218. }
  219. found.sort();
  220. var l_o = 0;
  221. for (var i = 0; i < found.length; i++)
  222. {
  223. var fo = found[i].split('^');
  224. if (l_o >= start && l_o < set.show + start)
  225. {
  226. out += '<div class="tipue_search_content_title"><a href="' + fo[3] + '"' + tipue_search_w + '>' + fo[1] + '</a></div>';
  227. if (set.mode == 'static-images')
  228. {
  229. if (fo[4])
  230. {
  231. out += '<div class="tipue_search_content_image_box"><img class="tipue_search_content_image" id="' + fo[1] + '^' + fo[3] + '" ';
  232. out += 'src=' + fo[4] + '></div><div style="clear: both;"></div>';
  233. }
  234. }
  235. var t = fo[2];
  236. var t_d = '';
  237. var t_w = t.split(' ');
  238. if (t_w.length < set.descriptiveWords)
  239. {
  240. t_d = t;
  241. }
  242. else
  243. {
  244. for (var f = 0; f < set.descriptiveWords; f++)
  245. {
  246. t_d += t_w[f] + ' ';
  247. }
  248. }
  249. t_d = $.trim(t_d);
  250. if (t_d.charAt(t_d.length - 1) != '.')
  251. {
  252. t_d += ' ...';
  253. }
  254. out += '<div class="tipue_search_content_text">' + t_d + '</div>';
  255. if (set.showURL)
  256. {
  257. out += '<div class="tipue_search_content_loc"><a href="' + fo[3] + '"' + tipue_search_w + '>' + fo[3] + '</a></div>';
  258. }
  259. }
  260. l_o++;
  261. }
  262. if (c > set.show)
  263. {
  264. var pages = Math.ceil(c / set.show);
  265. var page = (start / set.show);
  266. out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
  267. if (start > 0)
  268. {
  269. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start - set.show) + '_' + replace + '">&#171; Previous</a></li>';
  270. }
  271. if (page <= 4)
  272. {
  273. var p_b = pages;
  274. if (pages > 5)
  275. {
  276. p_b = 5;
  277. }
  278. for (var f = 0; f < p_b; f++)
  279. {
  280. if (f == page)
  281. {
  282. out += '<li class="current">' + (f + 1) + '</li>';
  283. }
  284. else
  285. {
  286. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  287. }
  288. }
  289. }
  290. else
  291. {
  292. var p_b = pages + 4;
  293. if (p_b > pages)
  294. {
  295. p_b = pages;
  296. }
  297. for (var f = page; f < p_b; f++)
  298. {
  299. if (f == page)
  300. {
  301. out += '<li class="current">' + (f + 1) + '</li>';
  302. }
  303. else
  304. {
  305. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  306. }
  307. }
  308. }
  309. if (page + 1 != pages)
  310. {
  311. out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start + set.show) + '_' + replace + '">Next &#187;</a></li>';
  312. }
  313. out += '</ul></div>';
  314. }
  315. }
  316. else
  317. {
  318. out += '<div id="tipue_search_warning_head">Nothing found</div>';
  319. }
  320. }
  321. else
  322. {
  323. if (show_stop)
  324. {
  325. out += '<div id="tipue_search_warning_head">Nothing found</div><div id="tipue_search_warning">Common words are largely ignored</div>';
  326. }
  327. else
  328. {
  329. out += '<div id="tipue_search_warning_head">Search too short</div>';
  330. if (set.minimumLength == 1)
  331. {
  332. out += '<div id="tipue_search_warning">Should be one character or more</div>';
  333. }
  334. else
  335. {
  336. out += '<div id="tipue_search_warning">Should be ' + set.minimumLength + ' characters or more</div>';
  337. }
  338. }
  339. }
  340. $('#tipue_search_content').html(out);
  341. $('#tipue_search_content').slideDown(200);
  342. $('#tipue_search_replaced').click(function()
  343. {
  344. getTipueSearch(0, false);
  345. });
  346. $('.tipue_search_content_image').click(function()
  347. {
  348. var src_i = $(this).attr('src');
  349. var id_v = $(this).attr('id');
  350. var id_a = id_v.split('^');
  351. var l_c_i = '<img src="' + src_i + '"><div id="tipue_lightbox_content_title">' + id_a[0] + '</div>';
  352. l_c_i += '<a href="' + id_a[1] + '"' + tipue_search_w + '><div id="tipue_lightbox_content_link"></div></a>';
  353. l_c_i += '<a href="' + src_i + '"' + tipue_search_w + '><div id="tipue_lightbox_content_expand"></div></a><div style="clear: both;"></div>';
  354. if ($('#tipue_lightbox').length > 0)
  355. {
  356. $('#tipue_lightbox_content').html(l_c_i);
  357. $('#tipue_lightbox').fadeIn();
  358. }
  359. else
  360. {
  361. var tipue_lightbox = '<div id="tipue_lightbox">' + '<div id="tipue_lightbox_content">' + l_c_i + '</div></div>';
  362. $('body').append(tipue_lightbox);
  363. $('#tipue_lightbox').fadeIn();
  364. }
  365. });
  366. $('#tipue_lightbox').live('click', function()
  367. {
  368. $('#tipue_lightbox').hide();
  369. });
  370. $('.tipue_search_foot_box').click(function()
  371. {
  372. var id_v = $(this).attr('id');
  373. var id_a = id_v.split('_');
  374. getTipueSearch(parseInt(id_a[0]), id_a[1]);
  375. });
  376. }
  377. });
  378. };
  379. })(jQuery);