(function(){"use strict";u8.Crm.Widgets.RepSetSelector=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.RepSetSelector.defaults,t));$assert(this.repSets,"Property 'repSets' must not be null or empty.");this._combo=null;this._isCreated=false;if(this.repSet&&!this.repSet.name){var s=this.repSet.clone();s.name=null;this.repSets.defaultRepSet=s}this.repSets.bind("ItemsChange",this._onRepSetsItemsChange.bind(this),this.id);this._create()};u8.Crm.Widgets.RepSetSelector.defaults={label:null,image:"u8-img-rep-s"};u8.Crm.Widgets.RepSetSelector.makeClass({name:"RepSetSelector",deriveFrom:u8.Base.Widgets.Widget,events:["ValueChange","Create"]});$.extend(u8.Crm.Widgets.RepSetSelector.prototype,{getValue:function(){return this.repSet},setValue:function(e){if(this.repSet&&this.repSet.equals(e)&&this.repSet.name==e.name){return}if(this._combo){this._combo.withoutEvents(this._combo.setValue,e&&e.name?e.name:"-1")}this.repSet=e;if(this._isCreated){this.raise("ValueChange",{})}},dispose:function(){if(this._isDisposed){return}if(this.repSets&&!this.repSets._isDisposed){this.repSets.unbind("*",this.id)}this.repSets=null;this.Widget_dispose()},setDisabled:function(e){this.disabled=e;if(this._combo){this._combo.attr({disabled:e})}var t=this.$(".image",this.element);if(e){t.hide()}else{t.show()}return this},_create:function(){this._using(this._createView.bind(this))},_createView:function(){var e='<table cellspacing="0" cellpadding="0"><tr>'+(this.label?'<td><div class="label u8-label"></div></td>':"")+'<td style="width:100%;"><div class="selector"></div></td>'+(this.image?'<td><div class="image"><span class="u8-clickable"></span></div></td>':"")+"</tr></table>";this.$(this.element).addClass("u8-repsetselector").html(e);if(this.label){this.$(".label",this.element).text(this.label)}this._combo=new u8.Base.Widgets.ComboBox(this.$(".selector",this.element),{parent:this,disabled:true,value:"",items:[{key:"",text:u8.session.commonTexts.loading}],onValueChange:this._onComboBoxValueChange.bind(this)});if(this.image){var t=typeof this.image=="string"?this.image:"u8-img-rep-s";this.$(".image > span",this.element).sprite(t).attr("title",$t("TXT_net_calendar_filters_23")||"").on("click",this._onPopupClick.bind(this))}if(!this.repSets.items){this.repSets.load(this._onRepSetsLoad.bind(this))}else{this._onRepSetsLoad()}},_using:function(e){u8.using({texts:["RepUtils_text","net_calendar_filters"]},e)},_onRepSetsLoad:function(){if(this._isDisposed){return}this._combo.attr({disabled:false});this._updateCombo();u8.tools.created(this)},_onComboBoxValueChange:function(){if(!this._combo){return}var e=this._combo.getValue();var t=this.repSets.findByName(e);if(e=="-1"&&t===null){t=this.repSets.defaultRepSet.clone()}this.setValue(t)},_updateCombo:function(){var e=this._combo;if(!e){return}var t=this.repSet&&this.repSet.name?this.repSet.name:"-1";u8.Crm.Widgets.RepSetSelector.itemsToCombo(this.repSets,e,t);var s=this.repSets.findByName(e.getValue());if(s){this.setValue(s)}},_onRepSetsItemsChange:function(){this._updateCombo()},_onPopupClick:function(e){if(this.disabled){return}var t=new u8.Crm.Widgets.RepSetPopup(document.body,{parent:this,alignTo:e.target,repSets:this.repSets,repSet:this.getValue(),onClose:this._onPopupClose.bind(this),editor:this.editor});t.open();this._popup=t},_onPopupClose:function(e,t){if(!t.ok){return}var s=e.getValue();this.setValue(s)}});u8.Crm.Widgets.RepSetSelector.itemsToCombo=function(e,t,s){if(!u8.services.texts._groups.RepUtils_text){$error("Before calling RepSetSelector.itemsToCombo you have to make sure that textgroup 'RepUtils_text' is loaded.")}var i=[];var o=$t("TXT_RepUtils_text_48");if(o==" "){o=""}i.push({key:"-1",text:o});var a=e.items||[];if(a.length>0){i.push({key:"_sep",html:"<div class='u8-combobox-separator'></div>",disabled:true})}for(var r=0;r<a.length;r++){var n=a[r];var l=n.name||"";i.push({key:l,text:l||""})}t.withoutEvents(t.attr,{items:i});t.setValue(s)}})();