vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
26 KiB

  1. /*
  2. Tipue Search 5.0
  3. Copyright (c) 2015 Tipue
  4. Tipue Search is released under the MIT License
  5. http://www.tipue.com/search
  6. */
  7. (function($) {
  8. $.fn.tipuesearch = function(options,dynamic) {
  9. var set = $.extend( {
  10. 'show' : 7,
  11. 'newWindow' : false,
  12. 'showURL' : true,
  13. 'showTitleCount' : true,
  14. 'minimumLength' : 3,
  15. 'descriptiveWords' : 25,
  16. 'highlightTerms' : true,
  17. 'highlightEveryTerm' : false,
  18. 'mode' : 'static',
  19. 'liveDescription' : '*',
  20. 'liveContent' : '*',
  21. 'contentLocation' : 'tipuesearch/tipuesearch_content.json',
  22. 'debug' : false
  23. }, options);
  24. if (dynamic === undefined) {
  25. dynamic = false;
  26. }
  27. return this.each(function() {
  28. var tipuesearch_in = {
  29. pages: []
  30. };
  31. $.ajaxSetup({
  32. async: false
  33. });
  34. var tipuesearch_t_c = 0;
  35. if (set.mode == 'live')
  36. {
  37. for (var i = 0; i < tipuesearch_pages.length; i++)
  38. {
  39. $.get(tipuesearch_pages[i])
  40. .done(function(html)
  41. {
  42. var cont = $(set.liveContent, html).text();
  43. cont = cont.replace(/\s+/g, ' ');
  44. var desc = $(set.liveDescription, html).text();
  45. desc = desc.replace(/\s+/g, ' ');
  46. var t_1 = html.toLowerCase().indexOf('<title>');
  47. var t_2 = html.toLowerCase().indexOf('</title>', t_1 + 7);
  48. if (t_1 != -1 && t_2 != -1)
  49. {
  50. var tit = html.slice(t_1 + 7, t_2);
  51. }
  52. else
  53. {
  54. var tit = tipuesearch_string_1;
  55. }
  56. tipuesearch_in.pages.push(
  57. {
  58. "title": tit,
  59. "text": desc,
  60. "tags": cont,
  61. "url": tipuesearch_pages[i]
  62. });
  63. });
  64. }
  65. }
  66. if (set.mode == 'json')
  67. {
  68. $.getJSON(set.contentLocation)
  69. .done(function(json)
  70. {
  71. tipuesearch_in = $.extend({}, json);
  72. });
  73. }
  74. if (set.mode == 'static')
  75. {
  76. tipuesearch_in = $.extend({}, tipuesearch);
  77. }
  78. var tipue_search_w = '';
  79. if (set.newWindow)
  80. {
  81. tipue_search_w = ' target="_blank"';
  82. }
  83. function getURLP(name)
  84. {
  85. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20')) || null;
  86. }
  87. // edit for live update ~ Alex de Mulder
  88. if (getURLP('q') && dynamic === false)
  89. {
  90. $('#tipue_search_input').val(getURLP('q'));
  91. getTipueSearch(0, true);
  92. }
  93. else if (dynamic === true) {
  94. getTipueSearch(0, true);
  95. }
  96. $(this).keyup(function(event)
  97. {
  98. if(event.keyCode == '13')
  99. {
  100. getTipueSearch(0, true);
  101. }
  102. });
  103. function getTipueSearch(start, replace)
  104. {
  105. $('#tipue_search_content').hide();
  106. $('#tipue_search_content').html('<div class="tipue_search_spinner"><div class="tipue_search_rect1"></div><div class="tipue_search_rect2"></div><div class="rect3"></div></div>');
  107. $('#tipue_search_content').show();
  108. var out = '';
  109. var results = '';
  110. var show_replace = false;
  111. var show_stop = false;
  112. var standard = true;
  113. var c = 0;
  114. found = [];
  115. var d = $('#tipue_search_input').val().toLowerCase();
  116. d = $.trim(d);
  117. if ((d.match("^\"") && d.match("\"$")) || (d.match("^'") && d.match("'$")))
  118. {
  119. standard = false;
  120. }
  121. if (standard)
  122. {
  123. var d_w = d.split(' ');
  124. d = '';
  125. for (var i = 0; i < d_w.length; i++)
  126. {
  127. var a_w = true;
  128. for (var f = 0; f < tipuesearch_stop_words.length; f++)
  129. {
  130. if (d_w[i] == tipuesearch_stop_words[f])
  131. {
  132. a_w = false;
  133. show_stop = true;
  134. }
  135. }
  136. if (a_w)
  137. {
  138. d = d + ' ' + d_w[i];
  139. }
  140. }
  141. d = $.trim(d);
  142. d_w = d.split(' ');
  143. }
  144. else
  145. {
  146. d = d.substring(1, d.length - 1);
  147. }
  148. if (d.length >= set.minimumLength)
  149. {
  150. if (standard)
  151. {
  152. if (replace)
  153. {
  154. var d_r = d;
  155. for (var i = 0; i < d_w.length; i++)
  156. {
  157. for (var f = 0; f < tipuesearch_replace.words.length; f++)
  158. {
  159. if (d_w[i] == tipuesearch_replace.words[f].word)
  160. {
  161. d = d.replace(d_w[i], tipuesearch_replace.words[f].replace_with);
  162. show_replace = true;
  163. }
  164. }
  165. }
  166. d_w = d.split(' ');
  167. }
  168. var d_t = d;
  169. for (var i = 0; i < d_w.length; i++)
  170. {
  171. for (var f = 0; f < tipuesearch_stem.words.length; f++)
  172. {
  173. if (d_w[i] == tipuesearch_stem.words[f].word)
  174. {
  175. d_t = d_t + ' ' + tipuesearch_stem.words[f].stem;
  176. }
  177. }
  178. }
  179. d_w = d_t.split(' ');
  180. for (var i = 0; i < tipuesearch_in.pages.length; i++)
  181. {
  182. var score = 0;
  183. var s_t = tipuesearch_in.pages[i].text;
  184. for (var f = 0; f < d_w.length; f++)
  185. {
  186. var pat = new RegExp(d_w[f], 'gi');
  187. if (tipuesearch_in.pages[i].title.search(pat) != -1)
  188. {
  189. var m_c = tipuesearch_in.pages[i].title.match(pat).length;
  190. score += (20 * m_c);
  191. }
  192. if (tipuesearch_in.pages[i].text.search(pat) != -1)
  193. {
  194. var m_c = tipuesearch_in.pages[i].text.match(pat).length;
  195. score += (20 * m_c);
  196. }
  197. if (set.highlightTerms)
  198. {
  199. if (set.highlightEveryTerm)
  200. {
  201. var patr = new RegExp('(' + d_w[f] + ')', 'gi');
  202. }
  203. else
  204. {
  205. var patr = new RegExp('(' + d_w[f] + ')', 'i');
  206. }
  207. s_t = s_t.replace(patr, "<span class=\"h01\">$1</span>");
  208. }
  209. if (tipuesearch_in.pages[i].tags.search(pat) != -1)
  210. {
  211. var m_c = tipuesearch_in.pages[i].tags.match(pat).length;
  212. score += (10 * m_c);
  213. }
  214. if (tipuesearch_in.pages[i].url.search(pat) != -1)
  215. {
  216. score += 20;
  217. }
  218. if (score != 0)
  219. {
  220. for (var e = 0; e < tipuesearch_weight.weight.length; e++)
  221. {
  222. if (tipuesearch_in.pages[i].url == tipuesearch_weight.weight[e].url)
  223. {
  224. score += tipuesearch_weight.weight[e].score;
  225. }
  226. }
  227. }
  228. if (d_w[f].match('^-'))
  229. {
  230. pat = new RegExp(d_w[f].substring(1), 'i');
  231. 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)
  232. {
  233. score = 0;
  234. }
  235. }
  236. }
  237. if (score != 0)
  238. {
  239. found.push(
  240. {
  241. "score": score,
  242. "title": tipuesearch_in.pages[i].title,
  243. "desc": s_t,
  244. "url": tipuesearch_in.pages[i].url
  245. });
  246. c++;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. for (var i = 0; i < tipuesearch_in.pages.length; i++)
  253. {
  254. var score = 0;
  255. var s_t = tipuesearch_in.pages[i].text;
  256. var pat = new RegExp(d, 'gi');
  257. if (tipuesearch_in.pages[i].title.search(pat) != -1)
  258. {
  259. var m_c = tipuesearch_in.pages[i].title.match(pat).length;
  260. score += (20 * m_c);
  261. }
  262. if (tipuesearch_in.pages[i].text.search(pat) != -1)
  263. {
  264. var m_c = tipuesearch_in.pages[i].text.match(pat).length;
  265. score += (20 * m_c);
  266. }
  267. if (set.highlightTerms)
  268. {
  269. if (set.highlightEveryTerm)
  270. {
  271. var patr = new RegExp('(' + d + ')', 'gi');
  272. }
  273. else
  274. {
  275. var patr = new RegExp('(' + d + ')', 'i');
  276. }
  277. s_t = s_t.replace(patr, "<span class=\"h01\">$1</span>");
  278. }
  279. if (tipuesearch_in.pages[i].tags.search(pat) != -1)
  280. {
  281. var m_c = tipuesearch_in.pages[i].tags.match(pat).length;
  282. score += (10 * m_c);
  283. }
  284. if (tipuesearch_in.pages[i].url.search(pat) != -1)
  285. {
  286. score += 20;
  287. }
  288. if (score != 0)
  289. {
  290. for (var e = 0; e < tipuesearch_weight.weight.length; e++)
  291. {
  292. if (tipuesearch_in.pages[i].url == tipuesearch_weight.weight[e].url)
  293. {
  294. score += tipuesearch_weight.weight[e].score;
  295. }
  296. }
  297. }
  298. if (score != 0)
  299. {
  300. found.push(
  301. {
  302. "score": score,
  303. "title": tipuesearch_in.pages[i].title,
  304. "desc": s_t,
  305. "url": tipuesearch_in.pages[i].url
  306. });
  307. c++;
  308. }
  309. }
  310. }
  311. if (c != 0)
  312. {
  313. if (set.showTitleCount && tipuesearch_t_c == 0)
  314. {
  315. var title = document.title;
  316. // fix for no stacking of the counters ~ Alex
  317. title = title.replace(/(\(.+\) )/g,"");
  318. document.title = '(' + c + ') ' + title;
  319. tipuesearch_t_c++;
  320. }
  321. if (show_replace == 1)
  322. {
  323. out += '<div id="tipue_search_warning">' + tipuesearch_string_2 + ' ' + d + '. ' + tipuesearch_string_3 + ' <a id="tipue_search_replaced">' + d_r + '</a></div>';
  324. }
  325. if (c == 1)
  326. {
  327. out += '<div id="tipue_search_results_count">' + tipuesearch_string_4 + '</div>';
  328. }
  329. else
  330. {
  331. c_c = c.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  332. out += '<div id="tipue_search_results_count">' + c_c + ' ' + tipuesearch_string_5 + '</div>';
  333. }
  334. found.sort(function(a, b) { return b.score - a.score } );
  335. var l_o = 0;
  336. for (var i = 0; i < found.length; i++)
  337. {
  338. if (l_o >= start && l_o < set.show + start)
  339. {
  340. out += '<div class="tipue_search_content_title"><a href="' + found[i].url + '"' + tipue_search_w + '>' + found[i].title + '</a></div>';
  341. if (set.debug)
  342. {
  343. out += '<div class="tipue_search_content_debug">Score: ' + found[i].score + '</div>';
  344. }
  345. if (set.showURL)
  346. {
  347. var s_u = found[i].url.toLowerCase();
  348. if(s_u.indexOf('http://') == 0)
  349. {
  350. s_u = s_u.slice(7);
  351. }
  352. out += '<div class="tipue_search_content_url"><a href="' + found[i].url + '"' + tipue_search_w + '>' + s_u + '</a></div>';
  353. }
  354. if (found[i].desc)
  355. {
  356. var t = found[i].desc;
  357. var t_d = '';
  358. var t_w = t.split(' ');
  359. if (t_w.length < set.descriptiveWords)
  360. {
  361. t_d = t;
  362. }
  363. else
  364. {
  365. for (var f = 0; f < set.descriptiveWords; f++)
  366. {
  367. t_d += t_w[f] + ' ';
  368. }
  369. }
  370. t_d = $.trim(t_d);
  371. if (t_d.charAt(t_d.length - 1) != '.')
  372. {
  373. t_d += ' ...';
  374. }
  375. out += '<div class="tipue_search_content_text">' + t_d + '</div>';
  376. }
  377. }
  378. l_o++;
  379. }
  380. if (c > set.show)
  381. {
  382. var pages = Math.ceil(c / set.show);
  383. var page = (start / set.show);
  384. out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
  385. if (start > 0)
  386. {
  387. out += '<li><a class="tipue_search_foot_box" id="' + (start - set.show) + '_' + replace + '">' + tipuesearch_string_6 + '</a></li>';
  388. }
  389. if (page <= 2)
  390. {
  391. var p_b = pages;
  392. if (pages > 3)
  393. {
  394. p_b = 3;
  395. }
  396. for (var f = 0; f < p_b; f++)
  397. {
  398. if (f == page)
  399. {
  400. out += '<li class="current">' + (f + 1) + '</li>';
  401. }
  402. else
  403. {
  404. out += '<li><a class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  405. }
  406. }
  407. }
  408. else
  409. {
  410. var p_b = page + 2;
  411. if (p_b > pages)
  412. {
  413. p_b = pages;
  414. }
  415. for (var f = page - 1; f < p_b; f++)
  416. {
  417. if (f == page)
  418. {
  419. out += '<li class="current">' + (f + 1) + '</li>';
  420. }
  421. else
  422. {
  423. out += '<li><a class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
  424. }
  425. }
  426. }
  427. if (page + 1 != pages)
  428. {
  429. out += '<li><a class="tipue_search_foot_box" id="' + (start + set.show) + '_' + replace + '">' + tipuesearch_string_7 + '</a></li>';
  430. }
  431. out += '</ul></div>';
  432. }
  433. }
  434. else
  435. {
  436. out += '<div id="tipue_search_warning">' + tipuesearch_string_8 + '</div>';
  437. }
  438. }
  439. else
  440. {
  441. if (show_stop)
  442. {
  443. out += '<div id="tipue_search_warning">' + tipuesearch_string_8 + '. ' + tipuesearch_string_9 + '</div>';
  444. }
  445. else
  446. {
  447. out += '<div id="tipue_search_warning">' + tipuesearch_string_10 + '</div>';
  448. if (set.minimumLength == 1)
  449. {
  450. out += '<div id="tipue_search_warning">' + tipuesearch_string_11 + '</div>';
  451. }
  452. else
  453. {
  454. out += '<div id="tipue_search_warning">' + tipuesearch_string_12 + ' ' + set.minimumLength + ' ' + tipuesearch_string_13 + '</div>';
  455. }
  456. }
  457. }
  458. $('#tipue_search_content').hide();
  459. $('#tipue_search_content').html(out);
  460. $('#tipue_search_content').slideDown(200);
  461. $('#tipue_search_replaced').click(function()
  462. {
  463. getTipueSearch(0, false);
  464. });
  465. $('.tipue_search_foot_box').click(function()
  466. {
  467. var id_v = $(this).attr('id');
  468. var id_a = id_v.split('_');
  469. getTipueSearch(parseInt(id_a[0]), id_a[1]);
  470. });
  471. }
  472. });
  473. };
  474. })(jQuery);