1/** 2 * Mygica Download Search Widget JavaScript 3 * ä¸è½½æç´¢æ¡å°é¨ä»¶èæ¬ - å®ç°AJAXæç´¢åè½ 4 */ 5 6(function($) { 7 'use strict'; 8 9 class GentiatechDownloadSearch { 10 constructor(element) { 11 this.wrapper = $(element); 12 this.input = this.wrapper.find('.mygica-download-search-input'); 13 this.button = this.wrapper.find('.mygica-download-search-button'); 14 this.clearBtn = this.wrapper.find('.mygica-download-search-clear'); 15 this.status = this.wrapper.find('.mygica-download-search-status'); 16 this.loadingText = this.wrapper.find('.mygica-download-search-loading'); 17 18 // è·å设置 19 this.settings = this.getSettings(); 20 21 // æç´¢ç¸å ³åé 22 this.searchTimeout = null; 23 this.isSearching = false; 24 25 // åå§å 26 this.init(); 27 } 28 29 getSettings() { 30 const input = this.input; 31 return { 32 target: input.data('target') || '.mygica-download-center', 33 liveSearch: input.data('live-search') === 'yes', 34 searchDelay: parseInt(input.data('search-delay')) || 300, 35 minLength: parseInt(input.data('min-length')) || 2, 36 searchFields: (input.data('search-fields') || 'title,content,category').split(','), 37 noResultsMessage: input.data('no-results') || 'No downloads found matching your search.', 38 loadingText: input.data('loading-text') || 'Searching...', 39 showCount: input.data('show-count') === 'yes' 40 }; 41 } 42 43 init() { 44 this.bindEvents(); 45 this.loadSearchFromUrl(); 46 this.updateClearButton(); 47 } 48 49 loadSearchFromUrl() { 50 // ä»URLåæ°ä¸è¯»åæç´¢å ³é®è¯ 51 const urlParams = new URLSearchParams(window.location.search); 52 const searchQuery = urlParams.get('download_search'); 53 54 if (searchQuery) { 55 this.input.val(searchQuery); 56 this.updateClearButton(); 57 58 // æ¾ç¤ºæç´¢ç¶æ 59 if (this.settings.showCount) { 60 this.showStatus(`Searching for "${searchQuery}"`, 'success'); 61 } 62 } 63 } 64 65 bindEvents() { 66 // è¾å ¥äºä»¶ 67 this.input.on('input', (e) => { 68 this.handleInput(e); 69 }); 70 71 // é®çäºä»¶ 72 this.input.on('keydown', (e) => { 73 if (e.key === 'Enter') { 74 e.preventDefault(); 75 this.performSearch(); 76 } else if (e.key === 'Escape') { 77 this.clearSearch(); 78 } 79 }); 80 81 // æç´¢æé®ç¹å» 82 this.button.on('click', (e) => { 83 e.preventDefault(); 84 this.performSearch(); 85 }); 86 87 // æ¸ é¤æé®ç¹å» 88 this.clearBtn.on('click', (e) => { 89 e.preventDefault(); 90 this.clearSearch(); 91 }); 92 93 // 表åæäº¤ 94 this.wrapper.find('.mygica-download-search-form').on('submit', (e) => { 95 e.preventDefault(); 96 this.performSearch(); 97 }); 98 } 99 100 handleInput(e) { 101 const query = e.target.value.trim(); 102 103 // æ´æ°æ¸ 餿鮿¾ç¤ºç¶æ 104 this.updateClearButton(); 105 106 // 对äºGETæ¹å¼æç´¢ï¼ä¸å¯ç¨å®æ¶æç´¢ 107 // ç¨æ·éè¦æEnteræç¹å»æç´¢æé®æ¥æ§è¡æç´¢ 108 } 109 110 updateClearButton() { 111 const hasValue = this.input.val().trim().length > 0; 112 this.clearBtn.toggle(hasValue); 113 } 114 115 performSearch(query = null) { 116 if (query === null) { 117 query = this.input.val().trim(); 118 } 119 120 // 妿æ¥è¯¢é¿åº¦ä¸è¶³ï¼ä¸æ§è¡æç´¢ 121 if (query.length < this.settings.minLength) { 122 if (query.length === 0) { 123 this.resetSearch(); 124 } 125 return; 126 } 127 128 // éè¿GETæ¹å¼æç´¢ - ä¿®æ¹URLåæ°å¹¶å·æ°é¡µé¢ 129 this.searchByUrl(query); 130 } 131 132 searchByUrl(query) { 133 // è·åå½åURL 134 const currentUrl = new URL(window.location.href); 135 136 // 设置æç´¢åæ° 137 if (query && query.trim() !== '') { 138 currentUrl.searchParams.set('download_search', query.trim()); 139 } else { 140 currentUrl.searchParams.delete('download_search'); 141 } 142 143 // è·³è½¬å°æ°URL 144 window.location.href = currentUrl.toString(); 145 } 146 147 getTargetElement() { 148 let target; 149 150 if (this.settings.target) { 151 target = $(this.settings.target); 152 } else { 153 target = $('.mygica-download-center').first(); 154 } 155 156 return target; 157 } 158 159 resetSearch() { 160 // éè¿URLæ¹å¼éç½®æç´¢ - ç§»é¤æç´¢åæ° 161 this.searchByUrl(''); 162 } 163 164 clearSearch() { 165 // æ¸ é¤è¾å ¥æ¡ 166 this.input.val('').focus(); 167 168 // æ´æ°æ¸ é¤æé® 169 this.updateClearButton(); 170 171 // éç½®æç´¢ç»æ 172 this.resetSearch(); 173 174 // æ¸ é¤å®æ¶å¨ 175 if (this.searchTimeout) { 176 clearTimeout(this.searchTimeout); 177 this.searchTimeout = null; 178 } 179 180 // 触åèªå®ä¹äºä»¶ 181 this.wrapper.trigger('mygica:search:clear'); 182 } 183
184 setLoadingState(loading) { 185 // GETæ¹å¼æç´¢ä¸éè¦å è½½ç¶æï¼å 为ä¼è·³è½¬é¡µé¢ 186 this.isSearching = loading; 187 } 188 189 showStatus(message, type = 'info') { 190 this.status.removeClass('success error loading').addClass(type); 191 this.loadingText.text(message); 192 this.status.show(); 193 194 // èªå¨éèéå è½½ç¶æçæ¶æ¯ 195 if (type !== 'loading') { 196 setTimeout(() => { 197 this.hideStatus(); 198 }, 3000); 199 } 200 } 201 202 hideStatus() { 203 this.status.hide(); 204 } 205 206 // å ¬å ±APIæ¹æ³ 207 search(query) { 208 this.input.val(query); 209 this.updateClearButton(); 210 this.performSearch(query); 211 } 212 213 clear() { 214 this.clearSearch(); 215 } 216 217 destroy() { 218 // æ¸ çäºä»¶å宿¶å¨ 219 if (this.searchTimeout) { 220 clearTimeout(this.searchTimeout); 221 } 222 223 this.wrapper.off(); 224 this.wrapper.removeData('mygica-search'); 225 } 226 } 227 228 // jQueryæä»¶ 229 $.fn.gentiatechDownloadSearch = function(options) { 230 return this.each(function() { 231 const $this = $(this); 232 let instance = $this.data('mygica-search'); 233 234 if (!instance) { 235 instance = new GentiatechDownloadSearch(this); 236 $this.data('mygica-search', instance); 237 } 238 239 // å¦æä¼ å ¥äºæ¹æ³åï¼è°ç¨å¯¹åºæ¹æ³ 240 if (typeof options === 'string' && instance[options]) { 241 return instance[options].apply(instance, Array.prototype.slice.call(arguments, 1)); 242 } 243 }); 244 }; 245 246 // èªå¨åå§å 247 $(document).ready(function() { 248 $('.mygica-download-search-wrapper').gentiatechDownloadSearch(); 249 }); 250 251 // Elementorå端åå§å 252 $(window).on('elementor/frontend/init', function() { 253 elementorFrontend.hooks.addAction('frontend/element_ready/mygica_download_search.default', function($scope) { 254 $scope.find('.mygica-download-search-wrapper').gentiatechDownloadSearch(); 255 }); 256 }); 257 258 // 导åºå°å ¨å±ä½ç¨åï¼ç¨äºè°è¯ï¼ 259 window.GentiatechDownloadSearch = GentiatechDownloadSearch; 260 261})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.