diff options
author | Christian Oliff <christianoliff@yahoo.com> | 2019-08-12 04:31:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-12 04:31:28 +0300 |
commit | c8b5c2e1765720b11b6e910b098abd624588deeb (patch) | |
tree | 8b6a3daacab8fae36c95999fae7a99e60747a4e5 /static | |
parent | 3c4599428eac764252e6fec9ae48f8a8635a5883 (diff) |
change to minified autocomplete for improved performance
from: https://github.com/Pixabay/JavaScript-autoComplete/blob/master/auto-complete.min.js
Diffstat (limited to 'static')
-rw-r--r-- | static/js/auto-complete.js | 226 |
1 files changed, 3 insertions, 223 deletions
diff --git a/static/js/auto-complete.js b/static/js/auto-complete.js index 7fbde99..0b46054 100644 --- a/static/js/auto-complete.js +++ b/static/js/auto-complete.js @@ -1,223 +1,3 @@ -/* - JavaScript autoComplete v1.0.4 - Copyright (c) 2014 Simon Steinberger / Pixabay - GitHub: https://github.com/Pixabay/JavaScript-autoComplete - License: http://www.opensource.org/licenses/mit-license.php -*/ - -var autoComplete = (function(){ - // "use strict"; - function autoComplete(options){ - if (!document.querySelector) return; - - // helpers - function hasClass(el, className){ return el.classList ? el.classList.contains(className) : new RegExp('\\b'+ className+'\\b').test(el.className); } - - function addEvent(el, type, handler){ - if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler); - } - function removeEvent(el, type, handler){ - // if (el.removeEventListener) not working in IE11 - if (el.detachEvent) el.detachEvent('on'+type, handler); else el.removeEventListener(type, handler); - } - function live(elClass, event, cb, context){ - addEvent(context || document, event, function(e){ - var found, el = e.target || e.srcElement; - while (el && !(found = hasClass(el, elClass))) el = el.parentElement; - if (found) cb.call(el, e); - }); - } - - var o = { - selector: 0, - source: 0, - minChars: 3, - delay: 150, - offsetLeft: 0, - offsetTop: 1, - cache: 1, - menuClass: '', - renderItem: function (item, search){ - // escape special characters - search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); - var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi"); - return '<div class="autocomplete-suggestion" data-val="' + item + '">' + item.replace(re, "<b>$1</b>") + '</div>'; - }, - onSelect: function(e, term, item){} - }; - for (var k in options) { if (options.hasOwnProperty(k)) o[k] = options[k]; } - - // init - var elems = typeof o.selector == 'object' ? [o.selector] : document.querySelectorAll(o.selector); - for (var i=0; i<elems.length; i++) { - var that = elems[i]; - - // create suggestions container "sc" - that.sc = document.createElement('div'); - that.sc.className = 'autocomplete-suggestions '+o.menuClass; - - that.autocompleteAttr = that.getAttribute('autocomplete'); - that.setAttribute('autocomplete', 'off'); - that.cache = {}; - that.last_val = ''; - - that.updateSC = function(resize, next){ - var rect = that.getBoundingClientRect(); - that.sc.style.left = Math.round(rect.left + (window.pageXOffset || document.documentElement.scrollLeft) + o.offsetLeft) + 'px'; - that.sc.style.top = Math.round(rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + o.offsetTop) + 'px'; - that.sc.style.width = Math.round(rect.right - rect.left) + 'px'; // outerWidth - if (!resize) { - that.sc.style.display = 'block'; - if (!that.sc.maxHeight) { that.sc.maxHeight = parseInt((window.getComputedStyle ? getComputedStyle(that.sc, null) : that.sc.currentStyle).maxHeight); } - if (!that.sc.suggestionHeight) that.sc.suggestionHeight = that.sc.querySelector('.autocomplete-suggestion').offsetHeight; - if (that.sc.suggestionHeight) - if (!next) that.sc.scrollTop = 0; - else { - var scrTop = that.sc.scrollTop, selTop = next.getBoundingClientRect().top - that.sc.getBoundingClientRect().top; - if (selTop + that.sc.suggestionHeight - that.sc.maxHeight > 0) - that.sc.scrollTop = selTop + that.sc.suggestionHeight + scrTop - that.sc.maxHeight; - else if (selTop < 0) - that.sc.scrollTop = selTop + scrTop; - } - } - } - addEvent(window, 'resize', that.updateSC); - document.body.appendChild(that.sc); - - live('autocomplete-suggestion', 'mouseleave', function(e){ - var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); - if (sel) setTimeout(function(){ sel.className = sel.className.replace('selected', ''); }, 20); - }, that.sc); - - live('autocomplete-suggestion', 'mouseover', function(e){ - var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); - if (sel) sel.className = sel.className.replace('selected', ''); - this.className += ' selected'; - }, that.sc); - - live('autocomplete-suggestion', 'mousedown', function(e){ - if (hasClass(this, 'autocomplete-suggestion')) { // else outside click - var v = this.getAttribute('data-val'); - that.value = v; - o.onSelect(e, v, this); - that.sc.style.display = 'none'; - } - }, that.sc); - - that.blurHandler = function(){ - try { var over_sb = document.querySelector('.autocomplete-suggestions:hover'); } catch(e){ var over_sb = 0; } - if (!over_sb) { - that.last_val = that.value; - that.sc.style.display = 'none'; - setTimeout(function(){ that.sc.style.display = 'none'; }, 350); // hide suggestions on fast input - } else if (that !== document.activeElement) setTimeout(function(){ that.focus(); }, 20); - }; - addEvent(that, 'blur', that.blurHandler); - - var suggest = function(data){ - var val = that.value; - that.cache[val] = data; - if (data.length && val.length >= o.minChars) { - var s = ''; - for (var i=0;i<data.length;i++) s += o.renderItem(data[i], val); - that.sc.innerHTML = s; - that.updateSC(0); - } - else - that.sc.style.display = 'none'; - } - - that.keydownHandler = function(e){ - var key = window.event ? e.keyCode : e.which; - // down (40), up (38) - if ((key == 40 || key == 38) && that.sc.innerHTML) { - var next, sel = that.sc.querySelector('.autocomplete-suggestion.selected'); - if (!sel) { - next = (key == 40) ? that.sc.querySelector('.autocomplete-suggestion') : that.sc.childNodes[that.sc.childNodes.length - 1]; // first : last - next.className += ' selected'; - console.log(next); - that.value = next.getAttribute('data-val'); - } else { - next = (key == 40) ? sel.nextSibling : sel.previousSibling; - if (next) { - sel.className = sel.className.replace('selected', ''); - next.className += ' selected'; - that.value = next.getAttribute('data-val'); - } - else { sel.className = sel.className.replace('selected', ''); that.value = that.last_val; next = 0; } - } - that.updateSC(0, next); - return false; - } - // esc - else if (key == 27) { that.value = that.last_val; that.sc.style.display = 'none'; } - // enter - else if (key == 13 || key == 9) { - var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); - if (sel && that.sc.style.display != 'none') { o.onSelect(e, sel.getAttribute('data-val'), sel); setTimeout(function(){ that.sc.style.display = 'none'; }, 20); } - } - }; - addEvent(that, 'keydown', that.keydownHandler); - - that.keyupHandler = function(e){ - var key = window.event ? e.keyCode : e.which; - if (!key || (key < 35 || key > 40) && key != 13 && key != 27) { - var val = that.value; - if (val.length >= o.minChars) { - if (val != that.last_val) { - that.last_val = val; - clearTimeout(that.timer); - if (o.cache) { - if (val in that.cache) { suggest(that.cache[val]); return; } - // no requests if previous suggestions were empty - for (var i=1; i<val.length-o.minChars; i++) { - var part = val.slice(0, val.length-i); - if (part in that.cache && !that.cache[part].length) { suggest([]); return; } - } - } - that.timer = setTimeout(function(){ o.source(val, suggest) }, o.delay); - } - } else { - that.last_val = val; - that.sc.style.display = 'none'; - } - } - }; - addEvent(that, 'keyup', that.keyupHandler); - - that.focusHandler = function(e){ - that.last_val = '\n'; - that.keyupHandler(e) - }; - if (!o.minChars) addEvent(that, 'focus', that.focusHandler); - } - - // public destroy method - this.destroy = function(){ - for (var i=0; i<elems.length; i++) { - var that = elems[i]; - removeEvent(window, 'resize', that.updateSC); - removeEvent(that, 'blur', that.blurHandler); - removeEvent(that, 'focus', that.focusHandler); - removeEvent(that, 'keydown', that.keydownHandler); - removeEvent(that, 'keyup', that.keyupHandler); - if (that.autocompleteAttr) - that.setAttribute('autocomplete', that.autocompleteAttr); - else - that.removeAttribute('autocomplete'); - document.body.removeChild(that.sc); - that = null; - } - }; - } - return autoComplete; -})(); - -(function(){ - if (typeof define === 'function' && define.amd) - define('autoComplete', function () { return autoComplete; }); - else if (typeof module !== 'undefined' && module.exports) - module.exports = autoComplete; - else - window.autoComplete = autoComplete; -})(); +// JavaScript autoComplete v1.0.4 +// https://github.com/Pixabay/JavaScript-autoComplete +var autoComplete=function(){function e(e){function t(e,t){return e.classList?e.classList.contains(t):new RegExp("\\b"+t+"\\b").test(e.className)}function o(e,t,o){e.attachEvent?e.attachEvent("on"+t,o):e.addEventListener(t,o)}function s(e,t,o){e.detachEvent?e.detachEvent("on"+t,o):e.removeEventListener(t,o)}function n(e,s,n,l){o(l||document,s,function(o){for(var s,l=o.target||o.srcElement;l&&!(s=t(l,e));)l=l.parentElement;s&&n.call(l,o)})}if(document.querySelector){var l={selector:0,source:0,minChars:3,delay:150,offsetLeft:0,offsetTop:1,cache:1,menuClass:"",renderItem:function(e,t){t=t.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&");var o=new RegExp("("+t.split(" ").join("|")+")","gi");return'<div class="autocomplete-suggestion" data-val="'+e+'">'+e.replace(o,"<b>$1</b>")+"</div>"},onSelect:function(){}};for(var c in e)e.hasOwnProperty(c)&&(l[c]=e[c]);for(var a="object"==typeof l.selector?[l.selector]:document.querySelectorAll(l.selector),u=0;u<a.length;u++){var i=a[u];i.sc=document.createElement("div"),i.sc.className="autocomplete-suggestions "+l.menuClass,i.autocompleteAttr=i.getAttribute("autocomplete"),i.setAttribute("autocomplete","off"),i.cache={},i.last_val="",i.updateSC=function(e,t){var o=i.getBoundingClientRect();if(i.sc.style.left=Math.round(o.left+(window.pageXOffset||document.documentElement.scrollLeft)+l.offsetLeft)+"px",i.sc.style.top=Math.round(o.bottom+(window.pageYOffset||document.documentElement.scrollTop)+l.offsetTop)+"px",i.sc.style.width=Math.round(o.right-o.left)+"px",!e&&(i.sc.style.display="block",i.sc.maxHeight||(i.sc.maxHeight=parseInt((window.getComputedStyle?getComputedStyle(i.sc,null):i.sc.currentStyle).maxHeight)),i.sc.suggestionHeight||(i.sc.suggestionHeight=i.sc.querySelector(".autocomplete-suggestion").offsetHeight),i.sc.suggestionHeight))if(t){var s=i.sc.scrollTop,n=t.getBoundingClientRect().top-i.sc.getBoundingClientRect().top;n+i.sc.suggestionHeight-i.sc.maxHeight>0?i.sc.scrollTop=n+i.sc.suggestionHeight+s-i.sc.maxHeight:0>n&&(i.sc.scrollTop=n+s)}else i.sc.scrollTop=0},o(window,"resize",i.updateSC),document.body.appendChild(i.sc),n("autocomplete-suggestion","mouseleave",function(){var e=i.sc.querySelector(".autocomplete-suggestion.selected");e&&setTimeout(function(){e.className=e.className.replace("selected","")},20)},i.sc),n("autocomplete-suggestion","mouseover",function(){var e=i.sc.querySelector(".autocomplete-suggestion.selected");e&&(e.className=e.className.replace("selected","")),this.className+=" selected"},i.sc),n("autocomplete-suggestion","mousedown",function(e){if(t(this,"autocomplete-suggestion")){var o=this.getAttribute("data-val");i.value=o,l.onSelect(e,o,this),i.sc.style.display="none"}},i.sc),i.blurHandler=function(){try{var e=document.querySelector(".autocomplete-suggestions:hover")}catch(t){var e=0}e?i!==document.activeElement&&setTimeout(function(){i.focus()},20):(i.last_val=i.value,i.sc.style.display="none",setTimeout(function(){i.sc.style.display="none"},350))},o(i,"blur",i.blurHandler);var r=function(e){var t=i.value;if(i.cache[t]=e,e.length&&t.length>=l.minChars){for(var o="",s=0;s<e.length;s++)o+=l.renderItem(e[s],t);i.sc.innerHTML=o,i.updateSC(0)}else i.sc.style.display="none"};i.keydownHandler=function(e){var t=window.event?e.keyCode:e.which;if((40==t||38==t)&&i.sc.innerHTML){var o,s=i.sc.querySelector(".autocomplete-suggestion.selected");return s?(o=40==t?s.nextSibling:s.previousSibling,o?(s.className=s.className.replace("selected",""),o.className+=" selected",i.value=o.getAttribute("data-val")):(s.className=s.className.replace("selected",""),i.value=i.last_val,o=0)):(o=40==t?i.sc.querySelector(".autocomplete-suggestion"):i.sc.childNodes[i.sc.childNodes.length-1],o.className+=" selected",i.value=o.getAttribute("data-val")),i.updateSC(0,o),!1}if(27==t)i.value=i.last_val,i.sc.style.display="none";else if(13==t||9==t){var s=i.sc.querySelector(".autocomplete-suggestion.selected");s&&"none"!=i.sc.style.display&&(l.onSelect(e,s.getAttribute("data-val"),s),setTimeout(function(){i.sc.style.display="none"},20))}},o(i,"keydown",i.keydownHandler),i.keyupHandler=function(e){var t=window.event?e.keyCode:e.which;if(!t||(35>t||t>40)&&13!=t&&27!=t){var o=i.value;if(o.length>=l.minChars){if(o!=i.last_val){if(i.last_val=o,clearTimeout(i.timer),l.cache){if(o in i.cache)return void r(i.cache[o]);for(var s=1;s<o.length-l.minChars;s++){var n=o.slice(0,o.length-s);if(n in i.cache&&!i.cache[n].length)return void r([])}}i.timer=setTimeout(function(){l.source(o,r)},l.delay)}}else i.last_val=o,i.sc.style.display="none"}},o(i,"keyup",i.keyupHandler),i.focusHandler=function(e){i.last_val="\n",i.keyupHandler(e)},l.minChars||o(i,"focus",i.focusHandler)}this.destroy=function(){for(var e=0;e<a.length;e++){var t=a[e];s(window,"resize",t.updateSC),s(t,"blur",t.blurHandler),s(t,"focus",t.focusHandler),s(t,"keydown",t.keydownHandler),s(t,"keyup",t.keyupHandler),t.autocompleteAttr?t.setAttribute("autocomplete",t.autocompleteAttr):t.removeAttribute("autocomplete"),document.body.removeChild(t.sc),t=null}}}}return e}();!function(){"function"==typeof define&&define.amd?define("autoComplete",function(){return autoComplete}):"undefined"!=typeof module&&module.exports?module.exports=autoComplete:window.autoComplete=autoComplete}(); |