Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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