diff --git a/hinter.js b/hinter.js index 836a9a6..c19c797 100644 --- a/hinter.js +++ b/hinter.js @@ -569,6 +569,21 @@ SimpleAutocomplete.GlobalMouseDown = function(ev) // *** UTILITY FUNCTIONS *** // Remove this section if you already have these functions defined somewhere else +// Cross-browser add/remove event listeners +var addListener = function() +{ + return window.addEventListener + ? function(el, type, fn) { el.addEventListener(type, fn, false); } + : function(el, type, fn) { el.attachEvent('on'+type, fn); }; +}(); + +var removeListener = function() +{ + return window.removeEventListener + ? function(el, type, fn) { el.removeEventListener(type, fn, false); } + : function(el, type, fn) { el.detachEvent('on'+type, fn); }; +}(); + // Cancel event bubbling and/or default action var stopEvent = function(ev, cancelBubble, preventDefault) { @@ -585,6 +600,15 @@ var stopEvent = function(ev, cancelBubble, preventDefault) return !preventDefault; }; +// Remove leading and trailing whitespace +if (!String.prototype.trim) +{ + String.prototype.trim = function() + { + return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); + }; +} + // Get element position, relative to the top-left corner of page var getOffset = function(elem) { diff --git a/hinter.min.js b/hinter.min.js index 5157190..6bc9dfc 100644 --- a/hinter.min.js +++ b/hinter.min.js @@ -1,4 +1,4 @@ // (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.persist;this.j=c.className||"hintLayer";if(this.h===undefined){this.h="#MORE"}if(this.g===undefined){this.g=300}this.k=0;this.l=null;this.m=[];this.n=[];this.o=0;this.p=-1;this.q=false;this.r()};SimpleAutocomplete.prototype.r=function(){var d=this.input;var a=SimpleAutocomplete.s;this.t=this.input.id+a.length;a.push(this);var c=this.u=document.createElement("div");c.className=this.j;if(!this.i){c.style.display="none";c.style.position="absolute";c.style.zIndex=1000;document.body.appendChild(c)}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.v("keydown",function(e){return b.w(e)});this.v("keyup",function(e){return b.x(e)});this.v("change",function(){return b.onChange()});this.v("focus",function(){return b.y()});this.v("blur",function(){return b.z()});addListener(c,"mousedown",function(e){return b.aa(e)});this.onChange(true)};SimpleAutocomplete.prototype.replaceItems=function(c,a){if(!a){this.u.scrollTop=0;this.p=0;this.n=[];if(!c||c.length==0){if(this.e){this.u.innerHTML='
'+this.e+"
"}else{this.ab()}return}while(this.p'+this.f+"":"";this.ac()}if(this.b){var e={};var b=this.input.value.split(this.b);for(var d=0;d=0){var d=this.aj();if(d){d.className=this.n[this.p][2]?"hintDisabledItem":"hintItem"}}this.p=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.aj=function(a){if(a==null){a=this.p}if(a<0){return null}return document.getElementById(this.t+"_item_"+this.p)};SimpleAutocomplete.prototype.selectItem=function(a){if(this.n[a][2]){return false}if(this.h&&this.n[a][1]==this.h){this.n.splice(a,1);elm.parentNode.removeChild(elm);this.k++;this.onChange(true);return}if(!this.b&&!this.c){this.input.value=this.n[a][1];this.hide()}else{document.getElementById(this.t+"_check_"+a).checked=this.n[a][3]=!this.n[a][3];if(this.c&&!this.c(this,a,this.n[a])){return}this.ak(a)}this.al=this.input.value;if(this.d){this.d(this,a,this.n[a])}};SimpleAutocomplete.prototype.ak=function(c){var b=this.input.value.split(this.b);for(var d=0;d=0;d--){if(b[d]==this.n[c][1]){b.splice(d,1)}}this.input.value=b.join(this.b+" ")}else{var e={};for(var d=0;da){this.u.style.right=(a-b.left-this.input.offsetWidth)+"px";this.u.style.left=""}return true}};SimpleAutocomplete.prototype.ab=function(){this.q=true;this.hide()};SimpleAutocomplete.prototype.ac=function(){this.q=false;if(this.am){this.show()}};SimpleAutocomplete.prototype.ae=function(a){return stopEvent(a||window.event,false,true)};SimpleAutocomplete.prototype.aa=function(a){a=a||window.event;if(this.am){this.o++}return stopEvent(a,true,false)};SimpleAutocomplete.prototype.af=function(a){return this.ai(a)};SimpleAutocomplete.prototype.ag=function(b,c){var a=parseInt(c.id.substr(this.t.length+6));this.selectItem(a);return true};SimpleAutocomplete.prototype.onChange=function(c){var b=this.input.value.trim();if(!c){this.k=0}if(b!=this.al||c){this.al=b;if(!this.g||c){this.a(this,b,this.k)}else{if(!this.l){var a=this;this.l=setTimeout(function(){a.a(a,a.al,a.k);a.l=null},this.g)}}}return true};SimpleAutocomplete.prototype.x=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.u.style.display==""){return stopEvent(a,true,true)}else{return true}}this.onChange();return true};SimpleAutocomplete.prototype.w=function(e){if(this.u.style.display=="none"){return true}e=e||window.event;if(e.keyCode==38){this.ah(-1)}else{if(e.keyCode==40){this.ah(1)}else{if(e.keyCode==10||e.keyCode==13){if(this.p>=0){this.selectItem(this.p)}return stopEvent(e,true,true)}else{if(e.keyCode==27){this.hide();return stopEvent(e,true,true)}else{return true}}}}if(this.p>=0){var f=this.aj();var b=this.u;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'+this.e+""}else{this.ab()}return}while(this.p'+this.f+"":"";this.ac()}if(this.b){var e={};var b=this.input.value.split(this.b);for(var d=0;d=0){var d=this.aj();if(d){d.className=this.n[this.p][2]?"hintDisabledItem":"hintItem"}}this.p=a;b.className="hintActiveItem";return false};SimpleAutocomplete.prototype.aj=function(a){if(a==null){a=this.p}if(a<0){return null}return document.getElementById(this.t+"_item_"+this.p)};SimpleAutocomplete.prototype.selectItem=function(a){if(this.n[a][2]){return false}if(this.h&&this.n[a][1]==this.h){this.n.splice(a,1);elm.parentNode.removeChild(elm);this.k++;this.onChange(true);return}if(!this.b&&!this.c){this.input.value=this.n[a][1];this.hide()}else{document.getElementById(this.t+"_check_"+a).checked=this.n[a][3]=!this.n[a][3];if(this.c&&!this.c(this,a,this.n[a])){return}this.ak(a)}this.al=this.input.value;if(this.d){this.d(this,a,this.n[a])}};SimpleAutocomplete.prototype.ak=function(c){var b=this.input.value.split(this.b);for(var d=0;d=0;d--){if(b[d]==this.n[c][1]){b.splice(d,1)}}this.input.value=b.join(this.b+" ")}else{var e={};for(var d=0;da){this.u.style.right=(a-b.left-this.input.offsetWidth)+"px";this.u.style.left=""}return true}};SimpleAutocomplete.prototype.ab=function(){this.q=true;this.hide()};SimpleAutocomplete.prototype.ac=function(){this.q=false;if(this.am){this.show()}};SimpleAutocomplete.prototype.ae=function(a){return stopEvent(a||window.event,false,true)};SimpleAutocomplete.prototype.aa=function(a){a=a||window.event;if(this.am){this.o++}return stopEvent(a,true,false)};SimpleAutocomplete.prototype.af=function(a){return this.ai(a)};SimpleAutocomplete.prototype.ag=function(b,c){var a=parseInt(c.id.substr(this.t.length+6));this.selectItem(a);return true};SimpleAutocomplete.prototype.onChange=function(c){var b=this.input.value.trim();if(!c){this.k=0}if(b!=this.al||c){this.al=b;if(!this.g||c){this.a(this,b,this.k)}else{if(!this.l){var a=this;this.l=setTimeout(function(){a.a(a,a.al,a.k);a.l=null},this.g)}}}return true};SimpleAutocomplete.prototype.x=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.u.style.display==""){return stopEvent(a,true,true)}else{return true}}this.onChange();return true};SimpleAutocomplete.prototype.w=function(e){if(this.u.style.display=="none"){return true}e=e||window.event;if(e.keyCode==38){this.ah(-1)}else{if(e.keyCode==40){this.ah(1)}else{if(e.keyCode==10||e.keyCode==13){if(this.p>=0){this.selectItem(this.p)}return stopEvent(e,true,true)}else{if(e.keyCode==27){this.hide();return stopEvent(e,true,true)}else{return true}}}}if(this.p>=0){var f=this.aj();var b=this.u;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