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.

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