|
| 1 | +$(function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + function titleCase(string) { |
| 5 | + return string.replace(/\w\S*/g, function(txt) {return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); |
| 6 | + } |
| 7 | + |
| 8 | + function parseResponse(xmlResponse) { |
| 9 | + // get the contents from search data |
| 10 | + var datas = $('entry', xmlResponse).map(function() { |
| 11 | + return { |
| 12 | + title: $('title', this).text(), |
| 13 | + content: $('content', this).text(), |
| 14 | + url: $('url', this).text() |
| 15 | + }; |
| 16 | + }).get(); |
| 17 | + |
| 18 | + var $input = document.getElementById('local-search-input'); |
| 19 | + var $resultContent = document.getElementById('local-search-result'); |
| 20 | + |
| 21 | + $('input#local-search-input').keyup(function(e) { |
| 22 | + if (e.keyCode == 27) { |
| 23 | + $(this).val(''); |
| 24 | + $resultContent.innerHTML = ''; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + $input.addEventListener('input', function() { |
| 29 | + var str = '<ul>'; |
| 30 | + var keywords = this.value.trim(); |
| 31 | + |
| 32 | + $resultContent.innerHTML = ''; |
| 33 | + if (keywords.length <= 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + keywords = keywords.toLowerCase().split(/[\s\-]+/); |
| 37 | + |
| 38 | + // perform local searching |
| 39 | + datas.forEach(function(data) { |
| 40 | + var isMatch = true; |
| 41 | + var contentIndex = []; |
| 42 | + var dataTitle = data.title.trim().toLowerCase(); |
| 43 | + var dataContent = data.content.trim().replace(/<[^>]+>/g,'').toLowerCase(); |
| 44 | + var dataUrl = data.url; |
| 45 | + var indexTitle = -1; |
| 46 | + var indexContent = -1; |
| 47 | + var firstOccur = -1; |
| 48 | + |
| 49 | + // only match artiles with not empty titles and contents |
| 50 | + if(dataTitle != '' && dataContent != '') { |
| 51 | + keywords.forEach(function(keyword, i) { |
| 52 | + indexTitle = dataTitle.indexOf(keyword); |
| 53 | + indexContent = dataContent.indexOf(keyword); |
| 54 | + |
| 55 | + if (indexTitle < 0 && indexContent < 0) { |
| 56 | + isMatch = false; |
| 57 | + } |
| 58 | + else { |
| 59 | + if (indexContent < 0) { |
| 60 | + indexContent = 0; |
| 61 | + } |
| 62 | + if (i == 0) { |
| 63 | + firstOccur = indexContent; |
| 64 | + } |
| 65 | + // contentIndex.push({indexContent:indexContent, keyword_len:keyword_len}); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + // show search results |
| 71 | + if (isMatch) { |
| 72 | + var content = data.content.trim().replace(/<[^>]+>/g,''); |
| 73 | + |
| 74 | + if (!content.length) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + str += '<li><a href="' + dataUrl + '" class="search-result-title">' + titleCase(dataTitle) + '</a>'; |
| 79 | + |
| 80 | + if (firstOccur >= 0) { |
| 81 | + // cut out 100 characters |
| 82 | + var start = firstOccur - 100; |
| 83 | + var end = firstOccur + 100; |
| 84 | + |
| 85 | + if (start < 0) { |
| 86 | + start = 0; |
| 87 | + } |
| 88 | + |
| 89 | + if (start == 0) { |
| 90 | + end = 200; |
| 91 | + } |
| 92 | + |
| 93 | + if (end > content.length) { |
| 94 | + end = content.length; |
| 95 | + } |
| 96 | + |
| 97 | + var matchContent = content.substring(start, end); |
| 98 | + |
| 99 | + // highlight all keywords |
| 100 | + keywords.forEach(function(keyword) { |
| 101 | + var regS = new RegExp(keyword, 'gi'); |
| 102 | + |
| 103 | + matchContent = matchContent.replace(regS, '<code>' + keyword + '</code>'); |
| 104 | + }); |
| 105 | + |
| 106 | + str += '<a class="search-result" href="' + dataUrl + '"><p>' + matchContent + '...</p></a>'; |
| 107 | + } |
| 108 | + str += '</li>'; |
| 109 | + } |
| 110 | + }); |
| 111 | + str += '</ul>'; |
| 112 | + $resultContent.innerHTML = str; |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + $.ajax({ |
| 117 | + url: '/search.xml', |
| 118 | + dataType: 'xml', |
| 119 | + success: parseResponse |
| 120 | + }); |
| 121 | +}); |
0 commit comments