SimpleAutocomplete/hinter.min.js

4 lines
8.9 KiB
JavaScript
Raw 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
2015-09-06 19:23:26 +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.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.l=0;this.m=null;this.n=[];this.o=[];this.p=0;this.q=-1;this.r=false;this.s=null;this.t()};SimpleAutocomplete.prototype.t=function(){var d=this.input;var a=SimpleAutocomplete.u;this.v=this.input.id+a.length;a.push(this);var c=this.w=document.createElement("div");c.className=this.k;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.x("keydown",function(e){return b.y(e)});this.x("keyup",function(e){return b.z(e)});this.x("change",function(){return b.onChange()});this.x("focus",function(){return b.aa()});this.x("blur",function(){return b.ab()});addListener(c,"mousedown",function(e){return b.ac(e)});this.onChange(true)};SimpleAutocomplete.prototype.replaceItems=function(c,a){if(!a){this.w.scrollTop=0;this.q=0;this.o=[];if(!c||c.length==0){if(this.e){this.w.innerHTML='<div class="hintEmptyText">'+this.e+"</div>"}else{this.ad()}return}while(this.q<c.length&&c[this.q][2]){this.q++}this.w.innerHTML=this.f?'<div class="hintPrompt">'+this.f+"</div>":"";this.ae()}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.w.appendChild(this.af(this.o.length,c[d]));this.o.push(c[d])}};SimpleAutocomplete.prototype.x=function(b,a){this.n[b]=a;addListener(this.input,b,a)};SimpleAutocomplete.prototype.remove=function(){if(!this.w){return}this.w.parentNode.removeChild(this.w);for(var a in this.n){removeListener(this.input,a,this.n[a])}for(var a=0;a<SimpleAutocomplete.u.length;a++){if(SimpleAutocomplete.u[a]==this){SimpleAutocomplete.u.splice(a,1);break}}this.n={};this.input=null;this.w=null;this.o=null};SimpleAutocomplete.prototype.af=function(e,f){var g=document.createElement("div");g.id=this.v+"_item_"+e;g.className=f[2]?"hintDisabledItem":(this.q==e?"hintActiveItem":"hintItem");g.title=f[1];if(this.b||this.c){var h=document.createElement("input");h.type="checkbox";h.id=this.v+"_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.ag)}else{g.innerHTML=f[0]}var b=this;addListener(g,"mouseover",function(){return b.ah(this)});addListener(g,"click",function(c){return b.ai(c,this)});return g};SimpleAutocomplete.prototype.aj=function(b){var c=this.q+b;if(c<0){c=0}while(this.o[c]&&this.o[c][2]){c+=b}var a=document.getElementById(this.v+"_item_"+c);if(!a){return true}return this.ak(a)};SimpleAutocomplete.prototype.ak=function(b){var a=parseInt(b.id.substr(this.v.length+6));if(this.o[a][2]){return false}if(this.q>=0){var d=this.al();if(d){d.className=this.o[this.q][2]?"hintDisabledItem":"hintItem"}}this.q=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.al=function(a){if(a==null){a=this.q}if(a<0){return null}return document.getElementById(this.v+"_item_"+this.q)};SimpleAutocomplete.prototype.selectItem=function(a){if(this.o[a][2]){return false}if(this.h&&this.o[a][1]==this.h){this.o.splice(a,1);var b=document.getElementById(this.v+"_item_"+a);b.parentNode.removeChild(b);this.l++;this.onChange(true);return}if(!this.b&&!this.c){this.input.value=this.o[a][1];if(this.i){this.i.value=this.o[a][3]}this.hi