/*!
* UI development toolkit for HTML5 (OpenUI5)
* (c) Copyright 2009-2018 SAP SE or an SAP affiliate company.
* Licensed under the Apache License, Version 2.0 - see LICENSE.txt.
*/
sap.ui.define(['jquery.sap.global','./ComboBox','./library','sap/ui/core/History','sap/ui/core/SeparatorItem',"./DropdownBoxRenderer"],function(q,C,a,H,S,D){"use strict";var b=C.extend("sap.ui.commons.DropdownBox",{metadata:{library:"sap.ui.commons",properties:{searchHelpEnabled:{type:"boolean",group:"Behavior",defaultValue:false},searchHelpText:{type:"string",group:"Appearance",defaultValue:null},searchHelpAdditionalText:{type:"string",group:"Appearance",defaultValue:null},searchHelpIcon:{type:"sap.ui.core.URI",group:"Appearance",defaultValue:null},maxHistoryItems:{type:"int",group:"Behavior",defaultValue:0}},events:{searchHelp:{parameters:{value:{type:"string"}}}}}});b.prototype.init=function(){C.prototype.init.apply(this,arguments);this._oValueBeforePaste=null;this._oValueBeforeOpen=null;this.__aItems=null;this._iCursorPosBeforeBackspace=null;this._searchHelpItem=null;this._iItemsForHistory=10;this._oHistory=new H(this.getId());};b.prototype.exit=function(){var I=this.getId()+"-h-";if(this._searchHelpItem){this._searchHelpItem[0].destroy();this._searchHelpItem[1].destroy();this._searchHelpItem=null;}C.prototype.exit.apply(this,arguments);function r(c){var o=sap.ui.getCore().byId(c);if(o){o.destroy();}}for(var i=0;i=this.__aItems.length){q.sap.log.warning("Element.removeAggregation called with invalid index: Items, "+e);}else{I=this.__aItems[e];this.__aItems.splice(e,1);}}if(this.__aItems.length<=this._iItemsForHistory&&!this._searchHelpItem){this._getListBox().removeItem(o);}if(!this._bNoItemCheck){var r=q(this.getInputDomRef());var c=r.cursorPos();this._doTypeAhead(r.val().substr(0,c),"");}return I;}else{return C.prototype.removeItem.apply(this,arguments);}};b.prototype.removeAllItems=function(){if(this.oPopup&&this.oPopup.isOpen()){var i=this.__aItems;if(!i){return[];}C.prototype.removeAllItems.apply(this,arguments);this.__aItems=[];return i;}else{return C.prototype.removeAllItems.apply(this,arguments);}};b.prototype.indexOfItem=function(I){if(this.oPopup&&this.oPopup.isOpen()){if(this.__aItems){if(this.__aItems.length==undefined){return-2;}for(var i=0;ithis._iItemsForHistory||this._searchHelpItem){var I;var i=0;switch(e.getParameter("event")){case"destroyItems":for(i=0;i0&&!this.mobile){this._callDoSelectAfterFocusIn(0,l);}this._bFocusByOpen=undefined;}C.prototype.onfocusin.apply(this,arguments);};b.prototype._callDoSelectAfterFocusIn=function(s,e){if(!sap.ui.Device.browser.internet_explorer){this._doSelect(s,e);}else{if(!this._eDoSelectAfterFocusIn||this._eDoSelectAfterFocusIn!=="_doSelect"){this._eDoSelectAfterFocusIn="onfocusin";this._doSelect(s,e);}else{this._eDoSelectAfterFocusIn=undefined;}}};b.prototype.onselect=function(e){var t=new Date().getTime();if(this._bIgnoreSelect){this._bIgnoreSelect=false;this.iOldTimestamp=t;return;}if(this.iOldTimestamp&&t-this.iOldTimestamp<50){return;}this.iOldTimestamp=undefined;if(!this.getEnabled()||!this.getEditable()){return;}var r=q(this.getInputDomRef()),n=r.cursorPos(),v=r.val();if(v.length>0&&n>0){this._doTypeAhead(v.substr(0,n),"");if(!this.oPopup||!this.oPopup.isOpen()){this._cleanupClose(this._getListBox());}}e.preventDefault();};b.prototype._determinePosinset=function(i,n){var p=n+1;if(this.oPopup&&this.oPopup.isOpen()){this.dontSetPoisinset=undefined;var I=i[n];var h=i[0].getId().search(this.getId()+"-h-")!=-1;if(I.getId().search(this.getId()+"-h-")==-1){if(h){p=p-1;}if(this._searchHelpItem){p=p-2;}}}return p;};b.prototype._doSelect=function(s,e){this._bIgnoreSelect=true;var d=this.getInputDomRef();if(d){var r=q(d);r.selectText(s?s:0,e?e:r.val().length);}return this;};b.prototype._updateSelection=function(m){var r=q(this.getInputDomRef()),n=r.cursorPos()+(m||0),v=r.val();this._doTypeAhead(v.substr(0,n),"");if(!this.oPopup||!this.oPopup.isOpen()){this._cleanupClose(this._getListBox());}else{this._getListBox().rerender();}};b.prototype._doTypeAhead=function(v,n,N,I){if(this.__doTypeAhead===true){return;}this.__doTypeAhead=true;this._sWantedSelectedKey=undefined;this._sWantedSelectedItemId=undefined;this._sWantedValue=undefined;var l=this._getListBox(),m=this.getMaxPopupItems(),c=this.__aItems||l.getItems(),V=c.length,h=this.getMaxHistoryItems()>0&&c.length>this._iItemsForHistory,f=!N&&h,o=v+n,s=new RegExp("[.*+?|()\\[\\]{}\\\\]","g"),r=o.toLowerCase().replace(s,"\\$&"),d=RegExp("^"+r+".*$"),M=n&&n.length||0,R=q(this.getInputDomRef());this.__aItems=c;if(V<=0){this.__doTypeAhead=false;return false;}var e,F=this._getFilteredItems(c,d),g=F.length>0;if(!g){f=false;}if(f){e=F;}else{e=c.slice(0);}var j=[];if(h){j=this._addHistoryItems(e,f&&d);l.setItems(e,false,true);V=e.length;}l.setVisibleItems(m=0){k=c[I];}if(!f&&p>0&&g){j=this._getFilteredItems(j,d);k=j[0];}if(f){k=F[0];}else if(!k){if(F.length>0){k=F[0];}else{var O=R.val();var t=0;for(i=0;i0){x=x-1;}if(w){x=x-2;}if(P>j.length){if(j.length>0){P=P-1;}if(w){P=P-2;}}this._updatePosInSet(R,P,(k.getAdditionalText?k.getAdditionalText():""));R.attr("aria-setsize",x);R.val(T);this._sTypedChars=o;this._doSelect(v.length+M,T.length);l.setSelectedIndex(i);if(w&&i==2){l.scrollToIndex(0);}else{l.scrollToIndex(i);}this._iClosedUpDownIdx=i;if(!g){R=this.$();R.addClass("sapUiTfErr");q.sap.delayedCall(300,R,"removeClass",["sapUiTfErr"]);R.cursorPos(v.length);this._doSelect(v.length,T.length);}this.__doTypeAhead=false;return g;};b.prototype._prepareOpen=function(l,p){this._oValueBeforeOpen=q(this.getInputDomRef()).val();this._bOpening=true;if(!this.noTypeAheadByOpen){var i;if(this._iClosedUpDownIdx>=0){i=this._iClosedUpDownIdx;}else if(this.getSelectedItemId()){i=this.indexOfItem(sap.ui.getCore().byId(this.getSelectedItemId()));}this._doTypeAhead("",q(this.getInputDomRef()).val(),true,i);this._doSelect();}return this;};b.prototype._handleOpened=function(){C.prototype._handleOpened.apply(this,arguments);if(!sap.ui.Device.browser.internet_explorer){q(this.getInputDomRef()).focus();}else{this._bFocusByOpen=true;}};b.prototype._cleanupClose=function(l){if(this.__aItems){var s=l.getSelectedItem();l.setItems(this.__aItems,false,true);this._iClosedUpDownIdx=l.indexOfItem(s);l.setSelectedIndex(this._iClosedUpDownIdx);this.__aItems=undefined;}this._oValueBeforeOpen=null;this._bOpening=undefined;return this;};b.prototype._getFilteredItems=function(I,r){var t=I.slice(0),o;for(var i=t.length-1;i>=0;i--){o=t[i];if(!r.test(o.getText().toLowerCase())||!o.getEnabled()){t.splice(i,1);}}return t;};b.prototype._addHistoryItems=function(I,r){var s=this.getId()+"-h-",o,h=this._oHistory.get(),l=h.length,n=[];for(var i=0,j=0;j0){var c=s+"separator",d=this._getSeparator(c);n.push(d);}I.unshift.apply(I,n);return n;};b.prototype._getSeparator=function(s){if(!this.__oSeparator&&s){this.__oSeparator=sap.ui.getCore().byId(s)||new S(s);}return this.__oSeparator||null;};b.prototype.fireChange=function(A){this.fireEvent("change",A);if(A.newValue&&(this.getMaxHistoryItems()>0)){this._oHistory.add(A.newValue);}this._sWantedValue=undefined;return this;};b.prototype.setValue=function(v,n){v=(v===undefined||v===null||v==="")?"":v;var I=this.getItems(),t,V=false,f;for(var i=0,l=I.length;i0){C.prototype.setValue.call(this,f,n);}else{this._sWantedValue=v;}return this;};b.prototype.applyFocusInfo=function(f){C.prototype.applyFocusInfo.apply(this,arguments);if(!this._bOpening&&(!this.oPopup||!this.oPopup.isOpen())){this._cleanupClose(this._getListBox());}return this;};b.prototype._focusAfterListBoxClick=function(){if(!sap.ui.Device.browser.webkit){this.focus();}else{var l=this._getListBox();l.addDelegate({onclick:function(){l.removeDelegate(this);this.focus();}.bind(this)});}};b.prototype.onsapfocusleave=function(e){var l=this._getListBox();if(e.relatedControlId&&q.sap.containsOrEquals(l.getFocusDomRef(),sap.ui.getCore().byId(e.relatedControlId).getFocusDomRef())){this._focusAfterListBoxClick();}else{if(this.oPopup&&this.oPopup.isOpen()){this._close();}sap.ui.commons.TextField.prototype.onsapfocusleave.apply(this,arguments);}};b.prototype.getTooltip_AsString=function(){var t=C.prototype.getTooltip_AsString.apply(this,arguments);if(!this._searchHelpItem){return t;}else{var r=sap.ui.getCore().getLibraryResourceBundle("sap.ui.commons");var s=r.getText("DDBX_SHI_ARIA");s=s==="DDBX_SHI_ARIA"?"Open search help via {0}":s;var A=this._searchHelpItem[0]&&this._searchHelpItem[0].getAdditionalText()||r.getText("DDBX_SHIF4");A=A==="DDBX_SHIF4"?"F4":A;s=s.replace("{0}",A);return(t?t+" - ":"")+s;}};b.prototype._handleSelect=function(c){if(this._searchHelpItem&&c.getParameter("selectedItem")===this._searchHelpItem[0]){var e=new q.Event("sapshow");e.which=q.sap.KeyCodes.F4;this.onsapshow(e);}else{var i=c.getParameter("selectedItem");if(!i){i=sap.ui.getCore().byId(c.getParameter("selectedId"));}if(i.getId().search(this.getId()+"-h-")!=-1){var l=this._getListBox(),I=l.getItems();var L=this._oHistory.get().length;if(L>this.getMaxHistoryItems()){L=Math.max(this.getMaxHistoryItems(),0);}for(var d=L;d0){var V=false;var f;var i=0,l=0;var o;var e=false;var t="";if(this._sWantedValue){for(i=0,l=I.length;i