(function(){"use strict";u8.Crm.Widgets.CatalogInput=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.CatalogInput.defaults,t));this.commandBindings={"CatalogInput/addValue":this._onAddCatalogValue.bind(this)};this._isCreated=false;this._createModel(t);if(!this._model.isCreated()){this._loading()}};u8.Crm.Widgets.CatalogInput.defaults={infoAreaId:null,fieldId:null,imageTemplate:null,handleUpDownKeysWhenClosed:true,allowNew:true};u8.Crm.Widgets.CatalogInput.makeClass({name:"CatalogInput",deriveFrom:u8.Base.Widgets.Widget,events:["ValueChange","Create","Action"]});$.extend(u8.Crm.Widgets.CatalogInput.prototype,{dispose:function(){if(this._isDisposed){return}this._model.dispose();this.Widget_dispose()},update:function(e){if(!e){return this._update()}switch(e.reason){case"attr":if(e.properties.disabled){if(this._comboBox){this._comboBox.attr({disabled:this.disabled})}}if(e.properties.tabIndex){if(this._comboBox){this._comboBox.attr({tabIndex:this.tabIndex})}}break;case"items":this._model.update();break;default:break}},isFocusable:function(){return this._comboBox&&this._comboBox.isFocusable()},getValue:function(){if(this._model){return this._model.getValue()}return this.value},setValue:function(e){if(this._model){this._model.setValue(e)}else{this.value=e}return this},getParentValue:function(){if(this._model){return this._model.getParentValue()}return this.parentValue},setParentValue:function(e){if(this._model){this._model.setParentValue(e)}this.parentValue=e},_createModel:function(){if(!("lockedCatalogMode"in this)){if(this.page&&this.page.using&&this.page.using.types&&this.page.using.types.indexOf("QueryEditor")>=0){this.inheritProperty("lockedCatalogMode",u8.session.webConfigValues.get("Query_Analysis.ShowLockedCatalogValues")||u8.Crm.Catalogs.LockedCatalogMode.disable)}else{this.inheritProperty("lockedCatalogMode",u8.Crm.Catalogs.LockedCatalogMode.hide)}}var e={id:this.id,page:this.page,allowNull:this.allowNull,allowNew:this.allowNew,actions:this.actions||[],infoAreaId:this.infoAreaId,fieldId:this.fieldId,lockedCatalogMode:this.lockedCatalogMode,imageTemplate:this.imageTemplate,onCreate:this._onModelCreate.bind(this),onUpdate:this._onModelUpdate.bind(this),link:this.page.url.getLinkUid()};if(typeof this.value!="undefined"){e.value=this.value;delete this.value}if(typeof this.parentValue!="undefined"){e.parentValue=this.parentValue;delete this.parentValue}this._model=u8.services.catalogs.createItemsModel(e)},_update:function(){this.empty();this._comboBox=null;this._model.update(this._createView.bind(this))},_loading:function(){var e=this.$("<div class='u8-label' />").text(u8.session.commonTexts.loading||"Loading ...");this.$(this.element).empty().append(e)},_createView:function(){this.empty();var e=this.$("<div></div>").appendTo(this.element);var t={keyProperty:"code",textProperty:"text",style:this.style,autoGrow:this.autoGrow,parent:this,disabled:this.disabled,actions:this._model.actions,items:this._model.items,value:this._model.getValue(),handleUpDownKeysWhenClosed:this.handleUpDownKeysWhenClosed,tabIndex:this.tabIndex,onAction:this._onAction.bind(this),onValueChange:this._onComboBoxValueChange.bind(this)};this._comboBox=new u8.Base.Widgets.ComboBox(e,t)},_onModelUpdate:function(e,t){if(t.items){if(this._comboBox){this._comboBox.attr({items:this._model.items})}}if(t.value){var i=this._model.getValue();if(this._comboBox){this._comboBox.setValue(i)}this.raise("ValueChange",{value:i})}if(t.actions){if(this._comboBox){this._comboBox.update({reason:"actions"})}}},_onModelCreate:function(e){this._model=e;this._createView();u8.tools.created(this)},_onComboBoxValueChange:function(e,t){var i=null;if(this._model.catalog.fixed&&!this._model.catalog.values.find(u8._.eq("code",t.key))){i=0}this._model.setValue(t.index<0?i:t.key)},_onAction:function(e,t){if(!t.action||!t.action.command||!this.commandBindings[t.action.command]){this.raise("Action",t)}},_onAddCatalogValue:function(e,t,i){i.elEvent.stopPropagation();var o=u8.services.schema.getField(this.infoAreaId,this.fieldId);var a={infoArea:this.infoAreaId,catalogNo:o.catalogNo,executeSearch:true,createNewPopup:{width:.9,height:.9}};u8.services.navigation.navigateToPage("CatalogMaintenance",a)},"Popup.onAutoClose":function(e,t){if(this.isParentOf(t.elEvent&&t.elEvent.target)){t.cancel=true}}})})();