1; (function ($, window) { 2 var defaults = { 3 maxShowNum: 10, 4 expires: 60, 5 input: '.history-input', 6 cookieName: 'searchHistory', 7 selected: function () { }, 8 beforeSend: function () { return true; }, 9 sendWhenSelect: true, 10 actionByCall:false 11 }; 12 $.fn.searchHistory = function (customOptions) { 13 var form = this; 14 if (typeof customOptions == 'string') { 15 switch (customOptions) { 16 case 'close': 17 _close(form[0]); 18 break; 19 case 'open': 20 _open(form[0]); 21 break; 22 case 'clear': 23 _clear(form[0]); 24 break; 25 } 26 return form; 27 } 28 29 var options = $.extend({}, defaults, customOptions); 30 if (form.length > 1) 31 return null; 32 if (form.length==0) 33 return null; 34 35 form.$input = form.find(options.input); 36 if (form.$input.length > 1) 37 return null; 38 var $history = $('<div class="search-history search-history-hide"></div>'); 39 var $list = $('<ul class="history-list"></ul>'); 40 var $item = $('<li class="history-item"></li>'); 41 var $clear = $('<li class="history-clear"><a href="#" class="history-clear-op">clear</a></li>'); 42 var strCookie = $.cookie(options.cookieName); 43 form.cookies = [] 44 if (strCookie && strCookie != 'null') 45 $.each(strCookie.split('&'), function (index, value) { 46 form.cookies.push(unescape(value)); 47 }); 48 for (var i = 0; i < form.cookies.length; i++) 49 $list.append($item.clone().html(decodeURI(form.cookies[i]))); 50 $list.append($clear); 51 $history.append($list); 52 53 //dom_form 54 form[0].$history = $history; 55 form[0].$list = $list; 56 form[0].$clear = $clear; 57 form[0].options = options; 58 //form.$input form.cookies 59 60 $history.insertAfter(form.$input); 61 62 options.actionByCall || form.$input.click(function (e) { 63 e.preventDefault(); 64 e.stopPropagation(); 65 if ($list.find('.history-item').length > 0) 66 _open(form[0]); 67 }); 68 69 $(options.input).click(function (e) { 70 var source = e.target; 71 if (form.has(source).length == 0) { 72 _close(form[0]); 73 } 74 }); 75 76 $(options.input).blur(function () { 77 setTimeout(() => { 78 _close(form[0]); 79 }, 300); 80 81 }); 82 83 $(options.input).keyup(function (e) { 84 var data = e.data; 85 var code = e.keyCode ? e.keyCode : e.which; 86 if (!(code === 40 || code === 38)) { 87 var tmpVal = $(options.input).val(); 88 if (tmpVal.length > 0) 89 _close(form[0]); 90 else 91 _open(form[0]); 92 } else { 93 e.preventDefault(); 94 e.stopPropagation(); 95 } 96 }); 97 98 $history.data('fromitem', false); 99 form.submit(function () { 100 var newCookie = $.trim(form.$input.val()); 101 var index = form.cookies.indexOf(newCookie); 102 var len = form.cookies.length; 103 if (index == -1) 104 len = form.cookies.unshift(newCookie); 105 else { 106 var temp = form.cookies[index]; 107 for (var i = index; i > 0; i--) 108 form.cookies[i] = form.cookies[i - 1]; 109 form.cookies[0] = temp; 110 } 111 var tempCookie = (len > options.maxShowNum)?form.cookies.slice(0, options.maxShowNum):form.cookies; 112 $.each(tempCookie, function (index, value) { 113 tempCookie[index] = escape(value); 114 }); 115 $.cookie(options.cookieName, tempCookie.join('&'), { expires: options.expires, path: '/' }); 116 117 typeof options.selected == 'function' && options.selected($.trim(form.$input.val())); 118 119 if ((!options.sendWhenSelect) && $history.data('fromitem')) { 120 _close(form[0]); 121 $history.data('fromitem', false); 122 return false; 123 } 124 125 if (typeof options.beforeSend == 'function') 126 return options.beforeSend($.trim(form.$input.val())); 127 128 return true; 129 }); 130 131 $clear.find('.history-clear-op').click(function (e) { 132 e.preventDefault(); 133 e.stopPropagation(); 134 _clear(form[0]); 135 }); 136 137 $list.find('.history-item').click(function (e) { 138 e.preventDefault(); 139 e.stopPropagation(); 140 form.$input.val($.trim($(this).html())); 141 typeof options.selected == 'function' && options.selected($.trim(form.$input.val())); 142 _close(form[0]); 143 form.$input.focus(); 144 if(options.sendWhenSelect) 145 console.log('form submited'); 146 form.submit(); 147 }); 148/* 149 $history.data('index', -1);
150 form.$input.keyup({ 151 '$input': form.$input, 152 '$history': $history, 153 '$list': $list.find('.history-item'), 154 'index': $history.data('index') 155 }, function (e) { 156 var data = e.data; 157 var code = e.keyCode ? e.keyCode : e.which; 158 159 if ((code === 40 || code === 38) && data.$history.hasClass('search-history-show')) { 160 var len = data.$list.length, 161 next, 162 prev; 163 164 if (data.index === -1) 165 data.$history.data('original', $.trim(data.$input.val())); 166 167 if (len) { 168 if (len > 1) { 169 if (data.index === len - 1) { 170 next = -1; 171 prev = data.index - 1; 172 } else if (data.index === 0) { 173 next = data.index + 1; 174 prev = -1; 175 } else if (data.index === -1) { 176 next = 0; 177 prev = len - 1; 178 } else { 179 next = data.index + 1; 180 prev = data.index - 1; 181 } 182 } else if (data.index === -1) { 183 next = 0; 184 prev = 0; 185 } else { 186 prev = -1; 187 next = -1; 188 } 189 data.index = (code === 40) ? next : prev; 190 191 data.$list.removeClass("history-item-selected"); 192 193 if (data.index !== -1) { 194data.$input.val(data.$list.eq(data.index).addClass("history-item-selected").html()); 195 data.$history.data('fromitem', true); 196 } else { 197 data.$input.val(data.$history.data('original')); 198 } 199 data.$history.data('index', data.index); 200 } 201 } 202 }); 203*/ 204 function _open(dom_form) { 205 if (dom_form.$list.find('.history-item').length > 0) 206 dom_form.$history.addClass('search-history-show').removeClass('search-history-hide'); 207 } 208 209 function _close(dom_form) { 210 dom_form.$history.addClass('search-history-hide').removeClass('search-history-show'); 211 } 212 213 function _clear(dom_form) { 214 $.cookie(dom_form.options.cookieName, null); 215 form.cookies = []; 216 dom_form.$list.find('.history-item').remove(); 217 _close(form[0]); 218 } 219 return form; 220 } 221})(jQuery, window);
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.