SimpleAutocomplete/hinter.min.js

4 lines
8.2 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
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||c.multipleListener;this.c=c.multipleListener;this.d=c.onChangeListener;this.e=c.emptyText;this.f=c.prompt;this.g=c.delay;this.h=c.moreMarker;if(this.h===undefined){this.h="#MORE"}if(this.g===undefined){this.g=300}this.i=0;this.j=null;this.k=[];this.l=[];this.m=0;this.n=-1;this.o=false;this.p()};SimpleAutocomplete.prototype.p=function(){var f=this.input;f.autocomplete="off";var a=SimpleAutocomplete.q;this.r=this.input.id+a.length;a.push(this);var d=getOffset(f);var c=this.s=document.createElement("div");c.className="hintLayer";c.style.display="none";c.style.position="absolute";c.style.top=(d.top+f.offsetHeight)+"px";c.style.zIndex=1000;c.style.left=d.left+"px";document.body.appendChild(c);f.SimpleAutocomplete_input=this;c.SimpleAutocomplete_layer=this;var b=this;this.t("keydown",function(e){return b.u(e)});this.t("keyup",function(e){return b.v(e)});this.t("change",function(){return b.onChange()});this.t("focus",function(){return b.w()});this.t("blur",function(){return b.x()});addListener(window,"beforeunload",this.k.beforeunload=function(){f.autocomplete="on"});addListener(c,"mousedown",function(e){return b.y(e)});this.onChange()};SimpleAutocomplete.prototype.replaceItems=function(b,e){if(!e){this.s.scrollTop=0;this.n=0;this.l=[];if(!b||b.length==0){if(this.e){this.s.innerHTML='<div class="hintEmptyText">'+this.e+"</div>"}else{this.z()}return}this.s.innerHTML=this.f?'<div class="hintPrompt">'+this.f+"</div>":"";this.aa()}if(!this.c){for(var c in b){b[c][2]=0}}if(this.b&&!this.c){var d={};var a=this.input.value.split(this.b);for(var c=0;c<a.length;c++){d[a[c].trim()]=true}for(var c in b){b[c][3]=d[b[c][1]]}}for(var c in b){this.s.appendChild(this.ab(this.l.length,b[c]));this.l.push(b[c])}};SimpleAutocomplete.prototype.t=function(b,a){this.k[b]=a;addListener(this.input,b,a)};SimpleAutocomplete.prototype.remove=function(){if(!this.s){return}this.s.parentNode.removeChild(this.s);removeListener(window,"beforeunload",this.k.beforeunload);delete this.k.beforeunload;for(var a in this.k){removeListener(this.input,a,this.k[a])}for(var a=0;a<SimpleAutocomplete.q.length;a++){if(SimpleAutocomplete.q[a]==this){SimpleAutocomplete.q.splice(a,1);break}}this.k={};this.input=null;this.s=null;this.l=null};SimpleAutocomplete.prototype.ab=function(b,e){var f=document.createElement("div");f.id=this.r+"_item_"+b;f.className=e[2]?"hintDisabledItem":(this.n==b?"hintActiveItem":"hintItem");f.title=e[1];f.innerHTML=e[0];if(this.b){var g=document.createElement("input");g.type="checkbox";g.id=this.r+"_check_"+b;g.checked=e[3]&&true;g.disabled=e[2]&&true;g.value=e[1];if(f.childNodes.length){f.insertBefore(g,f.firstChild)}else{f.appendChild(g)}addListener(g,"click",this.ac)}var a=this;addListener(f,"mouseover",function(){return a.ad(this)});addListener(f,"mousedown",function(c){return a.ae(c,this)});return f};SimpleAutocomplete.prototype.af=function(b){var c=this.n+b;if(c<0){c=0}var a=document.getElementById(this.r+"_item_"+c);if(!a){return true}return this.ag(a)};SimpleAutocomplete.prototype.ag=function(b){var a=parseInt(b.id.substr(this.r.length+6));if(this.l[a][2]){return false}if(this.n>=0){var d=this.ah();if(d){d.className="hintItem"}}this.n=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.ah=function(a){if(a==null){a=this.n}if(a<0){return null}return document.getElementById(this.r+"_item_"+this.n)};SimpleAutocomplete.prototype.ai=function(a){if(!this.b){this.input.value=this.l[a][1];this.hide()}else{document.getElementById(this.r+"_check_"+a).checked=this.l[a][3]=!this.l[a][3];if(this.c){this.c(this,a,this.l[a]);return}this.aj(a)}this.ak=this.input.value;if(this.d){this.d(this,a)}};SimpleAutocomplete.prototype.aj=function(c){var b=this.input.value.split(this.b);for(var d=0;d<b.length;d++){b[d]=b[d].trim()}if(!this.l[c][3]){for(var d=b.length-1;d>=0;d--){if(b[d]==this.l[c][1]){b.splice(d,1)}}this.input.value=b.join(this.b+" ")}else{var e={};for(var d=0;d<this.l.length;d++