SimpleAutocomplete/hinter.min.js

4 lines
8.9 KiB
JavaScript

// (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;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.hide()}else{document.getElementById(this.v+"_check_"+a).checked=this.o[a][3]=!this.o[a][3];if(this.c&&!this.c(this,a,this.o[a])){return}this.am(a)}this.s=this.input.value;if(this.d){this.d(this,a,this.o[a])}};SimpleAutocomplete.prototype.am=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.o[c][3]){for(var d=b.length-1;d>=0;d--){if(b[d]==this.o[c][1]){b.splice(d,1)}}this.input.value=b.join(this.b+" ")}else{var e={};for(var d=0;d<this.o.length;d++){if(this.o[d][3]){e[this.o[d][1]]=true}}var a=[];for(var d=0;d<b.length;d++){if(e[b[d]]){delete e[b[d]];a.push(b[d])}}for(var d=0;d<this.o.length;d++){if(this.o[d][3]&&e[this.o[d][1]]){a.push(this.o[d][1])}}this.input.value=a.join(this.b+" ")}};SimpleAutocomplete.prototype.hide=function(){if(!this.j){if(!this.p){this.w.style.display="none";return true}else{this.p=0}}};SimpleAutocomplete.prototype.show=function(){if(!this.r&&!this.j&&this.w.style.display=="none"){var b=getOffset(this.input);this.w.style.top=(b.top+this.input.offsetHeight)+"px";this.w.style.left=b.left+"px";this.w.style.display="";var a=document.clientWidth||document.documentElement.clientWidth||document.body.clientWidth;if(b.left+this.w.offsetWidth>a){this.w.style.right=(a-b.left-this.input.offsetWidth)+"px";this.w.style.left=""}return true}};SimpleAutocomplete.prototype.ad=function(){this.r=true;this.hide()};SimpleAutocomplete.prototype.ae=function(){this.r=false;if(this.an){this.show()}};SimpleAutocomplete.prototype.ag=function(a){return stopEvent(a||window.event,false,true)};SimpleAutocomplete.prototype.ac=function(a){a=a||window.event;if(this.an){this.p++}return stopEvent(a,true,false)};SimpleAutocomplete.prototype.ah=function(a){return this.ak(a)};SimpleAutocomplete.prototype.ai=function(b,c){var a=parseInt(c.id.substr(this.v.length+6));this.selectItem(a);return true};SimpleAutocomplete.prototype.onChange=function(c){var b=this.input.value.trim();if(!c){this.l=0}if(b!=this.s||c){if(this.s!==null&&this.i){this.i.value=""}this.s=b;if(!this.g||c){this.a(this,b,this.l)}else{if(!this.m){var a=this;this.m=setTimeout(function(){a.a(a,a.s,a.l);a.m=null},this.g)}}}return true};SimpleAutocomplete.prototype.z=function(a){a=a||window.event;if(a.keyCode==38||a.keyCode==40){this.show()}if(a.keyCode==38||a.keyCode==40||a.keyCode==10||a.keyCode==13){if(this.w.style.display==""){return stopEvent(a,true,true)}else{return true}}this.onChange();return true};SimpleAutocomplete.prototype.y=function(e){if(this.w.style.display=="none"){return true}e=e||window.event;if(e.keyCode==38){this.aj(-1)}else{if(e.keyCode==40){this.aj(1)}else{if(e.keyCode==10||e.keyCode==13){if(this.q>=0){this.selectItem(this.q)}return stopEvent(e,true,true)}else{if(e.keyCode==27){this.hide();return stopEvent(e,true,true)}else{return true}}}}if(this.q>=0){var f=this.al();var b=this.w;var a=getOffset(f).top+b.scrollTop-b.style.top.substr(0,b.style.top.length-2);var d=f.scrollHeight;if(a+d-b.offsetHeight>b.scrollTop){b.scrollTop=a+d-b.offsetHeight}else{if(a<b.scrollTop){b.scrollTop=a}}}return stopEvent(e,true,true)};SimpleAutocomplete.prototype.aa=function(){this.show();this.an=true;return true};SimpleAutocomplete.prototype.ab=function(){this.hide();this.an=false;return true};SimpleAutocomplete.u=[];SimpleAutocomplete.ao=function(b){var d=b.target||b.srcElement;var c;while(d){c=d.SimpleAutocomplete_input;if(c){break}else{if(d.SimpleAutocomplete_layer){return true}}d=d.parentNode}for(var a in SimpleAutocomplete.u){if(SimpleAutocomplete.u[a]!=c){SimpleAutocomplete.u[a].hide()}}return true};var addListener=function(){return window.addEventListener?function(c,b,a){c.addEventListener(b,a,false)}:function(c,b,a){c.attachEvent("on"+b,a)}}();var removeListener=function(){return window.removeEventListener?function(c,b,a){c.removeEventListener(b,a,false)}:function(c,b,a){c.detachEvent("on"+b,a)}}();var stopEvent=function(c,b,a){if(b){if(c.stopPropagation){c.stopPropagation()}else{c.cancelBubble=true}}if(a&&c.preventDefault){c.preventDefault()}c.returnValue=!a;return !a};if(!String.prototype.trim){String.prototype.trim=function(){return this.ap(/^\s\s*/,"").replace(/\s\s*$/,"")}}var getOffset=function(a){if(a.getBoundingClientRect){return getOffsetRect(a)}else{return getOffsetSum(a)}};var getOffsetRect=function(d){var g=d.getBoundingClientRect();var h=document.body;var b=document.documentElement;var a=window.pageYOffset||b.scrollTop||h.scrollTop;var e=window.pageXOffset||b.scrollLeft||h.scrollLeft;var f=b.clientTop||h.clientTop||0;var i=b.clientLeft||h.clientLeft||0;var j=g.top+a-f;var c=g.left+e-i;return{top:Math.round(j),left:Math.round(c)}};var getOffsetSum=function(a){var c=0,b=0;while(a){c=c+parseInt(a.offsetTop);b=b+parseInt(a.offsetLeft);a=a.offsetParent}return{top:c,left:b}};addListener(window,"load",function(){addListener(document,"mousedown",SimpleAutocomplete.ao)});