Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
(function(D,U){U.ui=U.ui||{};
U.ui.Autocomplete=function(e,c,d){this.namespace="."+U.Event.createNamespace(this.name,e);
this.options={};
Z.constructor.call(this,e,e+J.SELECT,c,d);
this.attachToDom();
this.options=D.extend(this.options,Y,d);
this.value="";
this.index=null;
this.isFirstAjax=true;
this.lastMouseX=null;
this.lastMouseY=null;
Q.call(this);
P.call(this);
M.call(this,"")
};
U.ui.AutocompleteBase.extend(U.ui.Autocomplete);
var Z=U.ui.Autocomplete.$super;
var Y={itemClass:"rf-au-itm",selectedItemClass:"rf-au-itm-sel",subItemClass:"rf-au-opt",selectedSubItemClass:"rf-au-opt-sel",autofill:true,minChars:1,selectFirst:true,ajaxMode:true,lazyClientMode:false,isCachedAjax:true,tokens:"",attachToBody:true,filterFunction:undefined};
var J={SELECT:"List",ITEMS:"Items",VALUE:"Value"};
var A=/^[\n\s]*(.*)[\n\s]*$/;
var O=function(c){var d=[];
c.each(function(){d.push(D(this).text().replace(A,"$1"))
});
return d
};
var Q=function(){this.useTokens=(typeof this.options.tokens=="string"&&this.options.tokens.length>0);
if(this.useTokens){var c=this.options.tokens.split("").join("\\");
this.REGEXP_TOKEN_RIGHT=new RegExp("["+c+"]","i");
this.getLastTokenIndex=function(d){return RichFaces.ui.Autocomplete.__getLastTokenIndex(c,d)
}
}};
var P=function(){var c={};
c["click"+this.namespace]=c["mouseover"+this.namespace]=K;
if(!D.browser.msie&&!D.browser.opera){c["mouseenter"+this.namespace]=N;
c["mouseleave"+this.namespace]=b
}U.Event.bind(U.getDomElement(this.id+J.ITEMS).parentNode,c,this)
};
var b=function(c){U.Event.unbind(U.getDomElement(this.id+J.ITEMS).parentNode,"mousemove"+this.namespace);
this.lastMouseX=null;
this.lastMouseY=null
};
var R=function(c){this.lastMouseX=c.pageX;
this.lastMouseY=c.pageY
};
var N=function(c){this.lastMouseX=c.pageX;
this.lastMouseY=c.pageY;
U.Event.bind(U.getDomElement(this.id+J.ITEMS).parentNode,"mousemove"+this.namespace,R,this)
};
var K=function(e){var d=D(e.target).closest("."+this.options.itemClass,e.currentTarget).get(0);
if(d){if(e.type=="mouseover"){if(this.lastMouseX==null||this.lastMouseX!=e.pageX||this.lastMouseY!=e.pageY){var c=this.items.index(d);
F.call(this,e,c)
}}else{this.__onEnter(e);
U.Selection.setCaretTo(U.getDomElement(this.fieldId));
this.__hide(e)
}}};
var M=function(e,c){var f=D(U.getDomElement(this.id+J.ITEMS));
this.items=f.find("."+this.options.itemClass);
var d=f.data();
f.removeData();
if(this.items.length>0){this.cache=new U.utils.Cache((this.options.ajaxMode?e:""),this.items,c||d.componentData||O,!this.options.ajaxMode)
}};
var E=function(){var d=0;
this.items.slice(0,this.index).each(function(){d+=this.offsetHeight
});
var c=D(U.getDomElement(this.id+J.ITEMS)).parent();
if(d<c.scrollTop()){c.scrollTop(d)
}else{d+=this.items.eq(this.index).outerHeight();
if(d-c.scrollTop()>c.innerHeight()){c.scrollTop(d-c.innerHeight())
}}};
var S=function(c,e){if(this.options.autofill&&e.toLowerCase().indexOf(c)==0){var f=U.getDomElement(this.fieldId);
var g=U.Selection.getStart(f);
this.__setInputValue(c+e.substring(c.length));
var d=g+e.length-c.length;
U.Selection.set(f,g,d)
}};
var I=function(f,i){U.getDomElement(this.id+J.VALUE).value=this.value;
var h=this;
var c=f;
var e=function(j){M.call(h,h.value,j.componentData&&j.componentData[h.id]);
if(h.options.lazyClientMode&&h.value.length!=0){H.call(h,h.value)
}if(h.items.length!=0){if(i){(h.focused||h.isMouseDown)&&i.call(h,c)
}else{h.isVisible&&h.options.selectFirst&&F.call(h,c,0)
}}else{h.__hide(c)
}};
var d=function(j){h.__hide(c);
a.call(h)
};
this.isFirstAjax=false;
var g={};
g[this.id+".ajax"]="1";
U.ajax(this.id,f,{parameters:g,error:d,complete:e})
};
var W=function(){if(this.index!=null){var c=this.items.eq(this.index);
if(c.removeClass(this.options.selectedItemClass).hasClass(this.options.subItemClass)){c.removeClass(this.options.selectedSubItemClass)
}this.index=null
}};
var F=function(f,c,e){if(this.items.length==0||(!e&&c==this.index)){return 
}if(c==null||c==undefined){W.call(this);
return 
}if(e){if(this.index==null){c=0
}else{c=this.index+c
}}if(c<0){c=0
}else{if(c>=this.items.length){c=this.items.length-1
}}if(c==this.index){return 
}W.call(this);
this.index=c;
var d=this.items.eq(this.index);
if(d.addClass(this.options.selectedItemClass).hasClass(this.options.subItemClass)){d.addClass(this.options.selectedSubItemClass)
}E.call(this);
if(f&&f.keyCode!=U.KEYS.BACKSPACE&&f.keyCode!=U.KEYS.DEL&&f.keyCode!=U.KEYS.LEFT&&f.keyCode!=U.KEYS.RIGHT){S.call(this,this.value,T.call(this))
}};
var H=function(d){var c=this.cache.getItems(d,this.options.filterFunction);
this.items=D(c);
D(U.getDomElement(this.id+J.ITEMS)).empty().append(this.items)
};
var a=function(){D(U.getDomElement(this.id+J.ITEMS)).removeData().empty();
this.items=[]
};
var C=function(d,f,g){F.call(this,d);
var e=(typeof f=="undefined")?this.__getSubValue():f;
var c=this.value;
this.value=e;
if((this.options.isCachedAjax||!this.options.ajaxMode)&&this.cache&&this.cache.isCached(e)){if(c!=e){H.call(this,e)
}if(this.items.length!=0){g&&g.call(this,d)
}else{this.__hide(d)
}if(d.keyCode==U.KEYS.RETURN||d.type=="click"){this.__setInputValue(e)
}else{if(this.options.selectFirst){F.call(this,d,0)
}}}else{if(d.keyCode==U.KEYS.RETURN||d.type=="click"){this.__setInputValue(e)
}if(e.length>=this.options.minChars){if((this.options.ajaxMode||this.options.lazyClientMode)&&c!=e){I.call(this,d,g)
}}else{if(this.options.ajaxMode){a.call(this);
this.__hide(d)
}}}};
var T=function(){if(this.index!=null){var c=this.items.eq(this.index);
return this.cache.getItemValue(c)
}return undefined
};
var X=function(){if(this.useTokens){var h=U.getDomElement(this.fieldId);
var g=h.value;
var d=U.Selection.getStart(h);
var e=g.substring(0,d);
var f=g.substring(d);
var c=e.substring(this.getLastTokenIndex(e));
r=f.search(this.REGEXP_TOKEN_RIGHT);
if(r==-1){r=f.length
}c+=f.substring(0,r);
return c
}else{return this.getValue()
}};
var G=function(c){var d=U.Selection.getStart(c);
if(d<=0){d=this.getLastTokenIndex(c.value)
}return d
};
var L=function(l){var k=U.getDomElement(this.fieldId);
var e=k.value;
var c=this.__getCursorPosition(k);
var g=e.substring(0,c);
var i=e.substring(c);
var j=this.getLastTokenIndex(g);
var h=j!=-1?j:g.length;
j=i.search(this.REGEXP_TOKEN_RIGHT);
var d=j!=-1?j:i.length;
var f=e.substring(0,h)+l;
c=f.length;
k.value=f+i.substring(d);
k.focus();
U.Selection.setCaretTo(k,c);
return k.value
};
var B=function(){if(this.items.length==0){return -1
}var f=D(U.getDomElement(this.id+J.ITEMS)).parent();
var d=f.scrollTop()+f.innerHeight()+this.items[0].offsetTop;
var e;
var c=(this.index!=null&&this.items[this.index].offsetTop<=d)?this.index:0;
for(c;
c<this.items.length;
c++){e=this.items[c];
if(e.offsetTop+e.offsetHeight>d){c--;
break
}}if(c!=this.items.length-1&&c==this.index){d+=this.items[c].offsetTop-f.scrollTop();
for(++c;
c<this.items.length;
c++){e=this.items[c];
if(e.offsetTop+e.offsetHeight>d){break
}}}return c
};
var V=function(){if(this.items.length==0){return -1
}var f=D(U.getDomElement(this.id+J.ITEMS)).parent();
var d=f.scrollTop()+this.items[0].offsetTop;
var e;
var c=(this.index!=null&&this.items[this.index].offsetTop>=d)?this.index-1:this.items.length-1;
for(c;
c>=0;
c--){e=this.items[c];
if(e.offsetTop<d){c++;
break
}}if(c!=0&&c==this.index){d=this.items[c].offsetTop-f.innerHeight();
if(d<this.items[0].offsetTop){d=this.items[0].offsetTop
}for(--c;
c>=0;
c--){e=this.items[c];
if(e.offsetTop<d){c++;
break
}}}return c
};
D.extend(U.ui.Autocomplete.prototype,(function(){return{name:"Autocomplete",__updateState:function(c){var d=this.__getSubValue();
if(this.items.length==0&&this.isFirstAjax){if((this.options.ajaxMode&&d.length>=this.options.minChars)||this.options.lazyClientMode){this.value=d;
I.call(this,c,this.__show);
return true
}}return false
},__getSubValue:X,__getCursorPosition:G,__updateInputValue:function(c){if(this.useTokens){return L.call(this,c)
}else{return Z.__updateInputValue.call(this,c)
}},__setInputValue:function(c){this.currentValue=this.__updateInputValue(c)
},__onChangeValue:C,__onKeyUp:function(c){F.call(this,c,-1,true)
},__onKeyDown:function(c){F.call(this,c,1,true)
},__onPageUp:function(c){F.call(this,c,V.call(this))
},__onPageDown:function(c){F.call(this,c,B.call(this))
},__onKeyHome:function(c){F.call(this,c,0)
},__onKeyEnd:function(c){F.call(this,c,this.items.length-1)
},__onBeforeShow:function(c){},__onEnter:function(c){var d=T.call(this);
this.__onChangeValue(c,d);
this.invokeEvent("selectitem",U.getDomElement(this.fieldId),c,d)
},__onShow:function(c){if(this.options.selectFirst){F.call(this,c,0)
}},__onHide:function(c){F.call(this,c)
},destroy:function(){this.items=null;
this.cache=null;
var c=U.getDomElement(this.id+J.ITEMS);
D(c).removeData();
U.Event.unbind(c.parentNode,this.namespace);
this.__conceal();
Z.destroy.call(this)
}}
})());
D.extend(U.ui.Autocomplete,{setData:function(d,c){D(U.getDomElement(d)).data({componentData:c})
},__getLastTokenIndex:function(i,h){var d=new RegExp("["+i+"][^"+i+"]*$","i");
var e=new RegExp("[^"+i+" ]","i");
var h=h||"";
var g=h.search(d);
if(g<0){return 0
}var c=h.substring(g);
var f=c.search(e);
if(f<=0){f=c.length
}return g+f
}})
})(jQuery,RichFaces);
New to GrepCode? Check out our FAQ X