Files
SIG/Scripts/AjaxControlToolkit/Release/ListSearch.js
2024-11-05 10:47:20 -07:00

1 line
11 KiB
JavaScript

Type.registerNamespace("Sys.Extended.UI"),Sys.Extended.UI.ListSearchBehavior=function(e){Sys.Extended.UI.ListSearchBehavior.initializeBase(this,[e]),this._promptCssClass=null,this._promptText=Sys.Extended.UI.Resources&&Sys.Extended.UI.Resources.ListSearch_DefaultPrompt||"Type to search",this._offsetX=0,this._offsetY=0,this._promptPosition=Sys.Extended.UI.ListSearchPromptPosition.Top,this._raiseImmediateOnChange=!1,this._queryPattern=Sys.Extended.UI.ListSearchQueryPattern.StartsWith,this._isSorted=!1,this._popupBehavior=null,this._onShowJson=null,this._onHideJson=null,this._originalIndex=0,this._newIndex=-1,this._showingPromptText=!1,this._searchText="",this._ellipsis=String.fromCharCode(8230),this._binarySearch=!1,this._applicationLoadDelegate=null,this._focusIndex=0,this._queryTimeout=0,this._timer=null,this._matchFound=!1,this._focusHandler=null,this._blurHandler=null,this._keyDownHandler=null,this._keyUpHandler=null,this._keyPressHandler=null},Sys.Extended.UI.ListSearchBehavior.prototype={initialize:function(){Sys.Extended.UI.ListSearchBehavior.callBaseMethod(this,"initialize");var e=this.get_element();e&&"SELECT"===e.tagName&&(this._focusHandler=Function.createDelegate(this,this._onFocus),this._blurHandler=Function.createDelegate(this,this._onBlur),this._keyDownHandler=Function.createDelegate(this,this._onKeyDown),this._keyUpHandler=Function.createDelegate(this,this._onKeyUp),this._keyPressHandler=Function.createDelegate(this,this._onKeyPress),$addHandler(e,"focus",this._focusHandler),$addHandler(e,"blur",this._blurHandler),$addHandler(e,"keydown",this._keyDownHandler),$addHandler(e,"keyup",this._keyUpHandler),$addHandler(e,"keypress",this._keyPressHandler),this._applicationLoadDelegate=Function.createDelegate(this,this._onApplicationLoad),Sys.Application.add_load(this._applicationLoadDelegate))},dispose:function(){var e=this.get_element();$removeHandler(e,"keypress",this._keyPressHandler),$removeHandler(e,"keyup",this._keyUpHandler),$removeHandler(e,"keydown",this._keyDownHandler),$removeHandler(e,"blur",this._blurHandler),$removeHandler(e,"focus",this._focusHandler),this._onShowJson=null,this._onHideJson=null,this._disposePopupBehavior(),this._applicationLoadDelegate&&(Sys.Application.remove_load(this._applicationLoadDelegate),this._applicationLoadDelegate=null),this._timer&&this._stopTimer(),Sys.Extended.UI.ListSearchBehavior.callBaseMethod(this,"dispose")},_onApplicationLoad:function(e,t){var i=!1,s=Sys.Extended.UI.ListSearchBehavior.callBaseMethod(this,"get_ClientState");null!=s&&""!=s&&(i="Focused"===s,Sys.Extended.UI.ListSearchBehavior.callBaseMethod(this,"set_ClientState",null)),i&&this._handleFocus()},_checkIfSorted:function(e){if(this._isSorted)return!0;for(var t=null,i=e.length,s=0;s<i;s++){var o=e[s].text.toLowerCase();if(t&&this._compareStrings(o,t)<0)return!1;t=o}return!0},_onFocus:function(e){this._handleFocus()},_handleFocus:function(){var e=this.get_element();this._focusIndex=e.selectedIndex,this._promptDiv||(this._promptDiv=document.createElement("div"),this._promptDiv.id=e.id+"_promptDiv",this._promptDiv.innerHTML=this._promptText&&this._promptText.length>0?this._promptText:Sys.Extended.UI.Resources.ListSearch_DefaultPrompt,this._showingPromptText=!0,this._promptCssClass&&(this._promptDiv.className=this._promptCssClass),e.parentNode.insertBefore(this._promptDiv,e.nextSibling),this._promptDiv.style.overflow="hidden",this._promptDiv.style.height=this._promptDiv.offsetHeight+"px",this._promptDiv.style.width=e.offsetWidth+"px"),this._popupBehavior||(this._popupBehavior=$create(Sys.Extended.UI.PopupBehavior,{parentElement:e},{},{},this._promptDiv)),this._promptPosition&&this._promptPosition==Sys.Extended.UI.ListSearchPromptPosition.Bottom?this._popupBehavior.set_positioningMode(Sys.Extended.UI.PositioningMode.BottomLeft):this._popupBehavior.set_positioningMode(Sys.Extended.UI.PositioningMode.TopLeft),this._onShowJson&&this._popupBehavior.set_onShow(this._onShowJson),this._onHideJson&&this._popupBehavior.set_onHide(this._onHideJson),this._popupBehavior.show(),this._updatePromptDiv(this._promptText)},_onBlur:function(){this._disposePopupBehavior();var e=this._promptDiv,t=this.get_element();e&&(this._promptDiv=null,e.parentNode.removeChild(e)),this._raiseImmediateOnChange||this._focusIndex==t.selectedIndex||this._raiseOnChange(t)},_disposePopupBehavior:function(){this._popupBehavior&&(this._popupBehavior.dispose(),this._popupBehavior=null)},_onKeyDown:function(e){var t=this.get_element(),i=this._promptDiv;t&&i&&(this._originalIndex=t.selectedIndex,this._showingPromptText&&(i.innerHTML="",this._searchText="",this._showingPromptText=!1,this._binarySearch=this._checkIfSorted(t.options)),e.keyCode==Sys.UI.Key.backspace?(e.preventDefault(),e.stopPropagation(),this._removeCharacterFromPromptDiv(),this._searchForTypedText(t),this._searchText&&0!=this._searchText.length||this._stopTimer()):e.keyCode==Sys.UI.Key.esc?(e.preventDefault(),e.stopPropagation(),i.innerHTML="",this._searchText="",this._searchForTypedText(t),this._stopTimer()):e.keyCode!=Sys.UI.Key.enter||this._raiseImmediateOnChange||this._focusIndex==t.selectedIndex||(this._focusIndex=t.selectedIndex,this._raiseOnChange(t)))},_onKeyUp:function(e){var t=this.get_element(),i=this._promptDiv;if(t&&i){if(this._newIndex==-1||!t||!i||""==i.innerHTML)return void(this._newIndex=-1);t.selectedIndex=this._newIndex,this._newIndex=-1}},_onKeyPress:function(e){var t=this.get_element(),i=this._promptDiv;if(t&&i){if(!this._isNormalChar(e))return void(e.charCode==Sys.UI.Key.backspace&&(e.preventDefault(),e.stopPropagation(),this._searchText&&0==this._searchText.length&&this._stopTimer()));e.preventDefault(),e.stopPropagation(),this._addCharacterToPromptDiv(e.charCode),this._searchForTypedText(t),this._stopTimer(),this._searchText&&0!=this._searchText.length&&this._startTimer()}},_isNormalChar:function(e){return!(Sys.Browser.agent==Sys.Browser.Firefox&&Sys.Browser.version<66&&e.rawEvent.keyCode)&&((Sys.Browser.agent!=Sys.Browser.Opera||0!=e.rawEvent.which)&&(!e.charCode||!(e.charCode<Sys.UI.Key.space||e.charCode>6e3)))},_updatePromptDiv:function(e){var t=this._promptDiv;if(t&&this.get_element()){var i="undefined"==typeof e?this._searchText:e,s=t.firstChild;if(s?s.nodeValue=i:(s=document.createTextNode(i),t.appendChild(s)),!(t.scrollWidth<=t.offsetWidth&&t.scrollHeight<=t.offsetHeight))for(var o=i.length-1;o>0&&(t.scrollWidth>t.offsetWidth||t.scrollHeight>t.offsetHeight);o--)s.nodeValue=this._ellipsis+i.substring(i.length-o,i.length)}},_addCharacterToPromptDiv:function(e){this._searchText+=String.fromCharCode(e),this._updatePromptDiv()},_removeCharacterFromPromptDiv:function(){this._searchText&&""!=this._searchText&&(this._searchText=this._searchText.substring(0,this._searchText.length-1),this._updatePromptDiv())},_searchForTypedText:function(e){var t=this._searchText,i=e.options,s=t?t.toLowerCase():"";if(this._matchFound=!1,0==s.length)i.length>0&&(e.selectedIndex=0,this._newIndex=0);else{var o=-1;o=this._binarySearch&&this._queryPattern==Sys.Extended.UI.ListSearchQueryPattern.StartsWith?this._doBinarySearch(i,s,0,i.length-1):this._doLinearSearch(i,s,0,i.length-1),o==-1?this._newIndex=this._originalIndex:(e.selectedIndex=o,this._newIndex=o,this._matchFound=!0)}this._raiseImmediateOnChange&&this._originalIndex!=e.selectedIndex&&this._raiseOnChange(e)},_raiseOnChange:function(e){if(document.createEvent){var t=document.createEvent("HTMLEvents");t.initEvent("change",!0,!1),e.dispatchEvent(t)}else document.createEventObject&&e.fireEvent("onchange")},_compareStrings:function(e,t){return e==t?0:e<t?-1:1},_doBinarySearch:function(e,t,i,s){for(;i<=s;){var o=Math.floor((i+s)/2),r=e[o].text.toLowerCase().substring(0,t.length),n=this._compareStrings(t,r);if(n>0)i=o+1;else{if(!(n<0)){for(;o>0&&e[o-1].text.toLowerCase().startsWith(t);)o--;return o}s=o-1}}return-1},_doLinearSearch:function(e,t,i,s){if(this._queryPattern==Sys.Extended.UI.ListSearchQueryPattern.Contains){for(var o=i;o<=s;o++)if(e[o].text.toLowerCase().indexOf(t)>=0)return o}else if(this._queryPattern==Sys.Extended.UI.ListSearchQueryPattern.StartsWith)for(var o=i;o<=s;o++)if(e[o].text.toLowerCase().startsWith(t))return o;return-1},_onTimerTick:function(){this._stopTimer(),this._matchFound||(this._searchText="",this._updatePromptDiv())},_startTimer:function(){this._queryTimeout>0&&(this._timer=window.setTimeout(Function.createDelegate(this,this._onTimerTick),this._queryTimeout))},_stopTimer:function(){null!=this._timer&&window.clearTimeout(this._timer),this._timer=null},get_onShow:function(){return this._popupBehavior?this._popupBehavior.get_onShow():this._onShowJson},set_onShow:function(e){this._popupBehavior?this._popupBehavior.set_onShow(e):this._onShowJson=e,this.raisePropertyChanged("onShow")},get_onShowBehavior:function(){return this._popupBehavior?this._popupBehavior.get_onShowBehavior():null},onShow:function(){this._popupBehavior&&this._popupBehavior.onShow()},get_onHide:function(){return this._popupBehavior?this._popupBehavior.get_onHide():this._onHideJson},set_onHide:function(e){this._popupBehavior?this._popupBehavior.set_onHide(e):this._onHideJson=e,this.raisePropertyChanged("onHide")},get_onHideBehavior:function(){return this._popupBehavior?this._popupBehavior.get_onHideBehavior():null},onHide:function(){this._popupBehavior&&this._popupBehavior.onHide()},get_promptText:function(){return this._promptText},set_promptText:function(e){this._promptText!=e&&(this._promptText=e,this.raisePropertyChanged("promptText"))},get_promptCssClass:function(){return this._promptCssClass},set_promptCssClass:function(e){this._promptCssClass!=e&&(this._promptCssClass=e,this.raisePropertyChanged("promptCssClass"))},get_promptPosition:function(){return this._promptPosition},set_promptPosition:function(e){this._promptPosition!=e&&(this._promptPosition=e,this.raisePropertyChanged("promptPosition"))},get_raiseImmediateOnChange:function(){return this._raiseImmediateOnChange},set_raiseImmediateOnChange:function(e){this._raiseImmediateOnChange!=e&&(this._raiseImmediateOnChange=e,this.raisePropertyChanged("raiseImmediateOnChange"))},get_queryTimeout:function(){return this._queryTimeout},set_queryTimeout:function(e){this._queryTimeout!=e&&(this._queryTimeout=e,this.raisePropertyChanged("queryTimeout"))},get_isSorted:function(){return this._isSorted},set_isSorted:function(e){this._isSorted!=e&&(this._isSorted=e,this.raisePropertyChanged("isSorted"))},get_queryPattern:function(){return this._queryPattern},set_queryPattern:function(e){this._queryPattern!=e&&(this._queryPattern=e,this.raisePropertyChanged("queryPattern"))}},Sys.Extended.UI.ListSearchBehavior.registerClass("Sys.Extended.UI.ListSearchBehavior",Sys.Extended.UI.BehaviorBase),Sys.Extended.UI.ListSearchPromptPosition=function(){throw Error.invalidOperation()},Sys.Extended.UI.ListSearchPromptPosition.prototype={Top:0,Bottom:1},Sys.Extended.UI.ListSearchPromptPosition.registerEnum("Sys.Extended.UI.ListSearchPromptPosition"),Sys.Extended.UI.ListSearchQueryPattern=function(){throw Error.invalidOperation()},Sys.Extended.UI.ListSearchQueryPattern.prototype={StartsWith:0,Contains:1},Sys.Extended.UI.ListSearchQueryPattern.registerEnum("Sys.Extended.UI.ListSearchQueryPattern");