1 2(function () { 3 var SOURCES = window.TEXT_VARIABLES.sources; 4 window.Lazyload.js(SOURCES.jquery, function() { 5 // search panel 6 var search = (window.search || (window.search = {})); 7 var useDefaultSearchBox = window.useDefaultSearchBox === undefined ? 8 true : window.useDefaultSearchBox ; 9 10 var $searchModal = $('.js-page-search-modal'); 11 var $searchToggle = $('.js-search-toggle'); 12 var searchModal = $searchModal.modal({ onChange: handleModalChange, hideWhenWindowScroll: true }); 13 var modalVisible = false; 14 search.searchModal = searchModal; 15 16 var $searchBox = null; 17 var $searchInput = null; 18 var $searchClear = null; 19 20 function getModalVisible() { 21 return modalVisible; 22 } 23 search.getModalVisible = getModalVisible; 24 25 function handleModalChange(visible) { 26 modalVisible = visible; 27 if (visible) { 28 search.onShow && search.onShow(); 29 useDefaultSearchBox && $searchInput[0] && $searchInput[0].focus(); 30 } else { 31 search.onShow && search.onHide(); 32 useDefaultSearchBox && $searchInput[0] && $searchInput[0].blur(); 33 setTimeout(function() { 34 useDefaultSearchBox && ($searchInput.val(''), $searchBox.removeClass('not-empty')); 35 search.clear && search.clear(); 36 window.pageAsideAffix && window.pageAsideAffix.refresh(); 37 }, 400); 38 } 39 } 40 41 $searchToggle.on('click', function() { 42 modalVisible ? searchModal.hide() : searchModal.show(); 43 }); 44 // Char Code: 83 S, 191 / 45 $(window).on('keyup', function(e) { 46 if (!modalVisible && !window.isFormElement(e.target || e.srcElement) && (e.which === 83 || e.which === 191)) { 47 modalVisible || searchModal.show(); 48 } 49 }); 50 51 if (useDefaultSearchBox) { 52 $searchBox = $('.js-search-box'); 53 $searchInput = $searchBox.children('input'); 54 $searchClear = $searchBox.children('.js-icon-clear'); 55 search.getSearchInput = function() { 56 return $searchInput.get(0); 57 }; 58 search.getVal = function() { 59 return $searchInput.val(); 60 }; 61 search.setVal = function(val) { 62 $searchInput.val(val); 63 }; 64 65 $searchInput.on('focus', function() { 66 $(this).addClass('focus'); 67 }); 68 $searchInput.on('blur', function() { 69 $(this).removeClass('focus'); 70 }); 71 $searchInput.on('input', window.throttle(function() { 72 var val = $(this).val(); 73 if (val === '' || typeof val !== 'string') { 74 search.clear && search.clear(); 75 } else { 76 $searchBox.addClass('not-empty'); 77 search.onInputNotEmpty && search.onInputNotEmpty(val); 78 } 79 }, 400)); 80 $searchClear.on('click', function() { 81 $searchInput.val(''); $searchBox.removeClass('not-empty'); 82 search.clear && search.clear(); 83 }); 84 } 85 }); 86})(); 87