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