SimpleAutocomplete/hinter.min.js

4 lines
8.8 KiB
JavaScript
Raw Normal View History

2013-03-14 00:51:44 +04:00
// (c) Vitaliy Filippov 2011-2013
// @license MPL 2.0 http://www.mozilla.org/MPL/2.0/
// http://yourcmc.ru/wiki/SimpleAutocomplete
2015-03-25 15:47:45 +03:00
var SimpleAutocomplete=function(b,a,c){if(typeof(b)=="string"){b=document.getElementById(b)}if(!c){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()};SimpleAutocomplete.prototype.s=function(){var d=this.input;var a=SimpleAutocomplete.t;this.u=this.input.id+a.length;a.push(this);var c=this.v=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.w("keydown",function(e){return b.x(e)});this.w("keyup",function(e){return b.y(e)});this.w("change",function(){return b.onChange()});this.w("focus",function(){return b.z()});this.w("blur",function(){return b.aa()});addListener(c,"mousedown",function(e){return b.ab(e)});this.onChange(true)};SimpleAutocomplete.prototype.replaceItems=function(c,a){if(!a){this.v.scrollTop=0;this.q=0;this.o=[];if(!c||c.length==0){if(this.e){this.v.innerHTML='<div class="hintEmptyText">'+this.e+"</div>"}else{this.ac()}return}while(this.q<c.length&&c[this.q][2]){this.q++}this.v.innerHTML=this.f?'<div class="hintPrompt">'+this.f+"</div>":"";this.ad()}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.v.appendChild(this.ae(this.o.length,c[d]));this.o.push(c[d])}};SimpleAutocomplete.prototype.w=function(b,a){this.n[b]=a;addListener(this.input,b,a)};SimpleAutocomplete.prototype.remove=function(){if(!this.v){return}this.v.parentNode.removeChild(this.v);for(var a in this.n){removeListener(this.input,a,this.n[a])}for(var a=0;a<SimpleAutocomplete.t.length;a++){if(SimpleAutocomplete.t[a]==this){SimpleAutocomplete.t.splice(a,1);break}}this.n={};this.input=null;this.v=null;this.o=null};SimpleAutocomplete.prototype.ae=function(e,f){var g=document.createElement("div");g.id=this.u+"_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.u+"_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.af)}else{g.innerHTML=f[0]}var b=this;addListener(g,"mouseover",function(){return b.ag(this)});addListener(g,"click",function(c){return b.ah(c,this)});return g};SimpleAutocomplete.prototype.ai=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.u+"_item_"+c);if(!a){return true}return this.aj(a)};SimpleAutocomplete.prototype.aj=function(b){var a=parseInt(b.id.substr(this.u.length+6));if(this.o[a][2]){return false}if(this.q>=0){var d=this.ak();if(d){d.className=this.o[this.q][2]?"hintDisabledItem":"hintItem"}}this.q=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.ak=function(a){if(a==null){a=this.q}if(a<0){return null}return document.getElementById(this.u+"_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);elm.parentNode.removeChild(elm);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.hide()}else{document.getElementById(this.u+"_check_"+a).checked=this.o[a][3