(function(){"use strict";u8.Crm.Widgets.LinkSelector=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.LinkSelector.defaults,t));this._currentIndex=-1;this._combo=null;this._isCreated=false;this._createModel()};u8.Crm.Widgets.LinkSelector.defaults={hideIfEmpty:true};u8.Crm.Widgets.LinkSelector.makeClass({name:"LinkSelector",deriveFrom:u8.Base.Widgets.Widget,events:["ValueChange","Load","Create"]});$.extend(u8.Crm.Widgets.LinkSelector.prototype,{dispose:function(){if(this._isDisposed){return}if(this._model){this._model.dispose()}this.Widget_dispose()},getValue:function(){return this._model.getValue()},setValue:function(e){this._model.setValue(e);return this},isHidden:function(){return this._isHidden},_createModel:function(){this._model=u8.services.links.createItemsModel({items:this.items,sourceUid:this.sourceUid,link:this.link,onLoad:this._onModelLoad.bind(this),onValueChange:this._onModelValueChange.bind(this),onItemsChange:this._onModelItemsChange.bind(this),noItemsIfOnlyEntireDatabaseItem:this.hideIfEmpty})},_onModelLoad:function(){this._createView();this.raise("Load");u8.ready(this._model,this._onCreated.bind(this))},_createView:function(){if(this._combo){this._combo.dispose()}this._combo=this.$("<div />").appendTo(this.element).widget("ComboBox",{items:this._model.items.slice(0),textProperty:"label",onValueChange:this._onLinkChanged.bind(this),parent:this})[0];if(this._model.getCurrentIndex()>=0){this._combo.setValue(this._model.items[this._model.getCurrentIndex()].key)}},_onModelValueChange:function(){if(this._combo){this._combo.setSelectedIndex(this._model.getCurrentIndex())}this.raise("ValueChange",{link:this._model.getValue()||undefined})},_onModelItemsChange:function(){if(this._combo){this._combo.items=this._model.items.slice(0);this._combo.update();this._combo.setSelectedIndex(this._model.getCurrentIndex())}},_onLinkChanged:function(e,t){this._model.setCurrentIndex(t.index)},_onCreated:function(){if(this.hideIfEmpty&&(!this._model.items||!this._model.items.length)){this._isHidden=true;this.$(this.element).hide()}else{this._isHidden=false}this.items=this._model.items;u8.tools.created(this)}})})();