1
|
var KeyNavList=Class.create({initialize:function(E,C){var B=this.onClick.bindAsEventListener(this),F,D=0,A=this.onHover.bindAsEventListener(this);this.element=$(E);this.options=C||{};this.index=-1;this.entryCount=this.element.firstDescendant().childElements().size();for(;D<this.entryCount;D++){F=this.getEntry(D);F.writeAttribute("autocompleteIndex",D);F.observe("click",B);F.observe("mouseover",A)}this.options.onShow=this.options.onShow||function(G){new Effect.Appear(G,{duration:0.15})};this.options.onHide=this.options.onHide||function(G){new Effect.Fade(G,{duration:0.15})};this.element.observe("blur",this.onBlur.bind(this));document.observe("keypress",this.onKeyPress.bindAsEventListener(this))},show:function(){this.active=true;if(!this.element.visible()){this.options.onShow(this.element)}if(!this.iefix&&(navigator.appVersion.indexOf("MSIE")>0)&&(this.element.getStyle("position")=="absolute")){this.element.insert({after:'<iframe id="'+this.element.id+'_iefix" style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="javascript:false;" frameborder="0" scrolling="no"></iframe>'});this.iefix=$(this.element.id+"_iefix")}if(this.iefix){setTimeout(this.fixIEOverlapping.bind(this),50)}},fixIEOverlapping:function(){this.iefix.clonePosition(this.element).setStyle({zIndex:1}).show();this.element.setStyle({zIndex:2})},hide:function(){this.active=false;this.stopIndicator();if(this.element.visible()){this.options.onHide(this.element)}if(this.iefix){this.iefix.hide()}},startIndicator:function(){if(this.options.indicator){$(this.options.indicator).show()}},stopIndicator:function(){if(this.options.indicator){$(this.options.indicator).hide()}},onKeyPress:function(A){if(!this.active){return}switch(A.keyCode){case Event.KEY_TAB:case Event.KEY_RETURN:this.selectEntry();A.stop();case Event.KEY_ESC:this.hide();this.active=false;A.stop();return;case Event.KEY_LEFT:case Event.KEY_RIGHT:return;case Event.KEY_UP:this.markPrevious();this.render();A.stop();return;case Event.KEY_DOWN:this.markNext();this.render();A.stop();return}},onHover:function(C){var B=C.findElement("LI"),A=parseInt(B.readAttribute("autocompleteIndex"));if(this.index!=A){this.index=A;this.render()}C.stop()},onClick:function(B){var A=B.findElement("LI");this.index=parseInt(A.readAttribute("autocompleteIndex"));this.selectEntry();this.hide();B.stop()},onBlur:function(){setTimeout(this.hide.bind(this),250);this.active=false},render:function(){if(this.entryCount>0){for(var A=0;A<this.entryCount;A++){[this.getEntry(A)].invoke(this.index==A?"addClassName":"removeClassName","selected")}this.show();this.active=true}else{this.hide();this.active=false}},markPrevious:function(){if(this.index>0){this.index--}else{this.index=this.entryCount-1}},markNext:function(){if(this.index<this.entryCount-1){this.index++}else{this.index=0}},getEntry:function(A){return this.element.down().down(A)},getCurrentEntry:function(){return this.getEntry(this.index)},selectEntry:function(){this.active=false;if(typeof this.options.onChoose=="function"){this.options.onChoose(this.getCurrentEntry())}}});
|