1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| <script> // function searchToggle() { // const width = $(document.body).width() // if(width > 479) { // return; // } // const search = $('.search'); // const searchForm = $('.form-search')
// if(!search.hasClass("mobile-search")) { // search.addClass("mobile-search"); // } else { // search.removeClass("mobile-search"); // } // }
function searchToggle() { const width = $(document.body).width() if(width > 479) { return; } const search = $('.search'); const searchForm = $('.form-search'); const menuToggle = $('.menu-toggle'); const title = $('.navbar-header-title ');
if(!search.hasClass("mobile-search")) { search.addClass("mobile-search"); menuToggle.addClass("open-search") title.addClass("mobile-title-hidden") } else { search.removeClass("mobile-search"); menuToggle.removeClass("open-search") // title.css({visibility: 'visible'}) title.removeClass("mobile-title-hidden") } }
function search(searchInputEl, formEl, flag) { const path = "<%= config.root %>" + "<%= config.search.path %>"; // 可以在public 下查看这个search.json $(formEl).submit(function(e){ e.preventDefault(); let target = null if(searchInputEl == null) { const screenWidth = $(document.body).width(); target = screenWidth > 479 ? $('#pc-search-input') : $('#mobile-search-input'); console.log(target); } else { target = $(searchInputEl) }
if(!flag && target.val() === '') { return ; }
$("#u-search").fadeIn(500, function() { $("body > .wrapper").addClass("modal-active");
$.ajax({ url: path, dataType: "json", beforeSend: function (xhr) { $input = target.val(); $(".form-input").val($input); const loadingBar = $('.search-loading-bar') loadingBar.css({ width:'100%', display: 'block' }); }, success: function( datas ) { console.log(datas); const $resultPanel = $(".modal-body")[0]; let str = `<ul class="modal-results">`; var keywords = $(".form-input").val().trim().toLowerCase().split(/[\s\-]+/); $resultPanel.innerHTML = ""; let hasResult = false let text = `<div class="no-result">找不到与关键词相关的内容....</div>`;
if ($(".form-input").val().trim().length <= 0) { // 没有结果 $resultPanel.innerHTML = text; return; } datas.forEach(function (data, index) { var isMatch = true; if (!data.title || data.title.trim() === '') { data.title = "Untitled"; } var data_title = data.title.trim().toLowerCase(); var data_content = data.content && data.content.trim().replace(/<[^>]+>/g, "").toLowerCase() || ''; var data_url = data.url; var index_title = -1; var index_content = -1; var first_occur = -1; // only match artiles with not empty contents if (data_content !== '') { keywords.forEach(function (keyword, i) { index_title = data_title.indexOf(keyword); index_content = data_content.indexOf(keyword);
if (index_title < 0 && index_content < 0) { isMatch = false; } else { hasResult = true if (index_content < 0) { index_content = 0; } if (i == 0) { first_occur = index_content; } } }); } else { isMatch = false; } // show search results if (isMatch) { str += `<li class='result-item'><a href='${data_url}' class='result-item-detail'> <span class="title">${data_title}</span>`; var content = data.content.trim().replace(/<[^>]+>/g, ""); if (first_occur >= 0) { // cut out 200 characters var start = first_occur - 40; var end = first_occur + 160;
if (start < 0) { start = 0; }
if (start == 0) { end = 200; }
if (end > content.length) { end = content.length; }
var match_content = content.substring(start, end);
// highlight all keywords keywords.forEach(function (keyword) { var regS = new RegExp(keyword, "gi"); match_content = match_content.replace(regS, `<em class="search-keyword">${keyword}</em>`); });
str += `<span class="content"> ${match_content} ...</span></a>`; } str += "</li>"; } }); str += "</ul>"; if(hasResult) { $resultPanel.innerHTML = str; } else { $resultPanel.innerHTML = text; }
}, complete: function() { setTimeout(() => { const loadingBar = $('.search-loading-bar') loadingBar.css({ width:'0%', display: 'none' }); }, 300) } }); })
}); }
$(document).ready(function() { $('.modal-close').click(function () { $("#u-search").fadeOut(); $("body > .wrapper").removeClass("modal-active") })
$('.modal-overlay').click(function() { $("#u-search").fadeOut(); $("body > .wrapper").removeClass("modal-active") }) search(null, ".form-search", false) search("#u-search-modal-form .form-input", ".u-search-modal-form", true) }) </script>
|