SimpleAutocomplete/hinter.min.js

4 lines
9.0 KiB
JavaScript
Raw Permalink Normal View History

2015-09-06 19:23:26 +03:00
// (c) Vitaliy Filippov 2011-2015
2013-03-14 00:51:44 +04:00
// @license MPL 2.0 http://www.mozilla.org/MPL/2.0/
// http://yourcmc.ru/wiki/SimpleAutocomplete
2018-06-01 16:34:34 +03:00
var SimpleAutocomplete=function(b,a,c){if(typeof(b)=="string"){b=document.getElementById(b)}if(!c){c={}}this.options=c;this.input=b;this.a=a;this.b=c.multipleDelimiter;this.c=c.multipleListener;this.d=c.onChangeListener;this.e=c.emptyText;this.f=c.prompt;this.g=c.delay;this.h=c.moreMarker;this.i=c.idField;this.j=c.persist;this.k=c.useTab&&true;this.l=c.className||"hintLayer";if(this.i&&typeof(this.i)=="string"){this.i=document.getElementById(this.i)}if(this.h===undefined){this.h="#MORE"}if(this.g===undefined){this.g=300}this.m=0;this.n=null;this.o=[];this.p=[];this.q=0;this.r=-1;this.s=false;this.t=null;this.u()};SimpleAutocomplete.prototype.u=function(){var d=this.input;var a=SimpleAutocomplete.v;this.w=this.input.id+a.length;a.push(this);var c=this.x=document.createElement("div");c.className=this.l;if(!this.j){c.style.display="none";c.style.position="absolute";c.style.zIndex=1000;document.body.insertBefore(c,document.body.childNodes[0])}else{d.nextSibling?d.parentNode.insertBefore(c,d.nextSibling):d.parentNode.appendChild(c)}d.SimpleAutocomplete_input=this;c.SimpleAutocomplete_layer=this;if(typeof d.autocomplete!=="undefined"){d.autocomplete="off";addListener(window,"beforeunload",function(){d.autocomplete="on"})}var b=this;this.y("keydown",function(e){return b.z(e)});this.y("keyup",function(e){return b.aa(e)});this.y("change",function(){return b.onChange()});this.y("focus",function(){return b.ab()});this.y("blur",function(){return b.ac()});addListener(c,"mousedown",function(e){return b.ad(e)});this.onChange(true)};SimpleAutocomplete.prototype.replaceItems=function(c,a){if(!a){this.x.scrollTop=0;this.r=0;this.p=[];if(!c||c.length==0){if(this.e){this.x.innerHTML='<div class="hintEmptyText">'+this.e+"</div>"}else{this.ae()}return}while(this.r<c.length&&c[this.r][2]){this.r++}this.x.innerHTML=this.f?'<div class="hintPrompt">'+this.f+"</div>":"";this.af()}if(this.b){var e={};var b=this.input.value.split(this.b);for(var d=0;d<b.length;d++){e[b[d].trim()]=true}for(var d in c){c[d][3]=e[c[d][1]]}}for(var d in c){this.x.appendChild(this.ag(this.p.length,c[d]));this.p.push(c[d])}};SimpleAutocomplete.prototype.y=function(b,a){this.o[b]=a;addListener(this.input,b,a)};SimpleAutocomplete.prototype.remove=function(){if(!this.x){return}this.x.parentNode.removeChild(this.x);for(var a in this.o){removeListener(this.input,a,this.o[a])}for(var a=0;a<SimpleAutocomplete.v.length;a++){if(SimpleAutocomplete.v[a]==this){SimpleAutocomplete.v.splice(a,1);break}}this.o={};this.input=null;this.x=null;this.p=null};SimpleAutocomplete.prototype.ag=function(e,f){var g=document.createElement("div");g.id=this.w+"_item_"+e;g.className=f[2]?"hintDisabledItem":(this.r==e?"hintActiveItem":"hintItem");g.title=f[1];if(this.b||this.c){var h=document.createElement("input");h.type="checkbox";h.id=this.w+"_check_"+e;h.checked=f[3]&&true;h.disabled=f[2]&&true;h.value=f[1];g.appendChild(h);var a=document.createElement("label");a.htmlFor=h.id;a.innerHTML=f[0];g.appendChild(a);addListener(a,"click",this.ah)}else{g.innerHTML=f[0]}var b=this;addListener(g,"mouseover",function(){return b.ai(this)});addListener(g,"click",function(c){return b.aj(c,this)});return g};SimpleAutocomplete.prototype.ak=function(b){var c=this.r+b;if(c<0){c=0}while(this.p[c]&&this.p[c][2]){c+=b}var a=document.getElementById(this.w+"_item_"+c);if(!a){return true}return this.al(a)};SimpleAutocomplete.prototype.al=function(b){var a=parseInt(b.id.substr(this.w.length+6));if(this.p[a][2]){return false}if(this.r>=0){var d=this.am();if(d){d.className=this.p[this.r][2]?"hintDisabledItem":"hintItem"}}this.r=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.am=function(a){if(a==null){a=this.r}if(a<0){return null}return document.getElementById(this.w+"_item_"+this.r)};SimpleAutocomplete.prototype.selectItem=function(a){if(this.p[a][2]){return false}if(this.h&&this.p[a][1]==this.h){this.p.splice(a,1);var b=document.getElementById(this.w+"_item_"+a);b.parentNode.removeChild(b);this.m++;this.onChange(true);return}if(!this.b&&!this.c){this.input.value=this.p[a][1];if(this.i){this.i.val