(function(){"use strict";u8.Crm.Widgets.ListingInput=function(t,e){this.Widget(t,$.extend({},u8.Crm.Widgets.ListingInput.defaults,e));this._isCreated=false;if(u8.isValidRecordUid(this.link)){this._create()}};u8.Crm.Widgets.ListingInput.makeClass({name:"ListingInput",deriveFrom:u8.Base.Widgets.Widget,events:["Create","ValueChange"]});u8.Crm.Widgets.ListingInput.defaults={};$.extend(u8.Crm.Widgets.ListingInput.prototype,{setValue:function(t){if(t==this.value){return}this.value=t;if(this._isCreated){this._model.setValue(t);this._updateTextInput();this.raise("ValueChange",{value:t})}},getValue:function(){return $.extend(this.value,{__type:"update.Crm.Bc.SerialEntry.ListingDescriptor,update.Crm.Bc"})},setLink:function(t){this.link=t;if(!this._isCreated){this._create()}},getLink:function(){return this.link},_create:function(){var t=this;Function.chain(this._createModel,this._createView,u8.tools.createdWhenReady(this,{})).call(this,function(){t.setValue(t._model.getValue())})},_createModel:function(t){var e=this;u8.using({texts:"SerialEntry_text"},function(){e._model=new u8.Crm.Widgets.ListingTreeModel({link:e.link,onSelectionChange:e._onModelSelectionChange.bind(e),onCreate:t})})},_createView:function(t){this._createTextInput();this._createDropDown();t()},_createTextInput:function(){var t={parent:this,value:this._getFormattedTextValue()};var e=this._model;if(e.getTree()===null){t=$.extend(t,{ghostText:$t("TXT_SerialEntry_text_16"),disabled:true})}else if(!e.canSelect()){t=$.extend(t,{disabled:true})}else{t=$.extend(t,{ghostText:$t("TXT_SerialEntry_text_17"),readonly:true,autoDisableActions:false,actions:[{name:"showListingTree",image:"u8-img-combobox-down-s",title:$t("TXT_SerialEntry_text_18")}],onAction:this._onTextInputAction.bind(this)})}this._input=u8.widget("TextInput",this.element,t)},_createDropDown:function(){var t=this._input.getInputElement();if(!this.designer){t.on("click",this._onInputClick.bind(this))}var e=this.$(this.element);var i=this;this._dropDown=u8.widget("DropDown",e,{parent:this,inputElement:t,createContent:function(){var t=this.$("<div/>").addClass("u8-padding").appendTo(this.getContent());t.widget("ListingTree",{parent:i,value:i.value,model:i._model})},onShow:this._onDropDownShowHide.bind(this),onHide:this._onDropDownShowHide.bind(this)})},_updateTextInput:function(){if(!this._input){return}this._input.setValue(this._getFormattedTextValue())},_getFormattedTextValue:function(){var t=this._model.getPath();if(t.length>1){t.slice(1)}return t.join(" > ")},_toggleDropDown:function(){if(this.designer){return}this._dropDown.setVisible(!this._dropDown.isVisible())},_onDropDownShowHide:function(t,e){var i=t.isVisible();this._input.actions[0].image=i?"u8-img-combobox-up-s":"u8-img-combobox-down-s";this._input.update({reason:"action",index:0})},_onInputClick:function(t){if(this.designer||!this._dropDown){return}t.preventDefault();t.stopPropagation();this._toggleDropDown()},_onTextInputAction:function(t,e){this._toggleDropDown()},_onModelSelectionChange:function(t){this.setValue(t.getValue());if(this._dropDown){this._dropDown.setVisible(false)}},_onPopupPreClose:function(t,e){e.cancel=true;t.hide()},"Popup.onAutoClose":function(t,e){if(this.isParentOf(e.elEvent&&e.elEvent.target)){e.cancel=true}}})})();