(function(){"use strict";u8.Crm.Widgets.FilterSetSelector=function(t,e){this.Widget(t,$.extend({},u8.Crm.Widgets.FilterSetSelector.defaults,e));$assert(this.filterSets,"Property 'filterSets' must not be null or empty.");this._combo=null;this._isCreated=false;if(this.filterSet&&!this.filterSet.id){var i=this.filterSet.clone();i.id=null;this._defaultFilterSet=i}else{this._defaultFilterSet=this.filterSets.defaultFilterSet.clone()}this.filterSets.bind("Save",this._onFilterSetSave.bind(this),this.id).bind("Delete",this._onFilterSetDelete.bind(this),this.id);this._create()};u8.Crm.Widgets.FilterSetSelector.defaults={label:null,image:"u8-img-filter-s"};u8.Crm.Widgets.FilterSetSelector.makeClass({name:"FilterSetSelector",deriveFrom:u8.Base.Widgets.Widget,events:["ValueChange","Create"]});$.extend(u8.Crm.Widgets.FilterSetSelector.prototype,{dispose:function(){if(this._isDisposed){return}this._combo=null;if(this.filterSets&&!this.filterSets._isDisposed){this.filterSets.unbind("*",this.id)}this.filterSets=null;this.Widget_dispose()},getValue:function(){return this.filterSet},setValue:function(t){if(this.filterSet==t){return}if(this._combo){this._combo.withoutEvents(this._combo.setValue,t&&t.id?t.id:"-1")}this.filterSet=t;if(!t.id){this._defaultFilterSet=t}var e=this;function i(){if(e._isCreated&&!e._isDisposed){e.raise("ValueChange",{filterSet:e.filterSet})}}if(this.filterSet){this.filterSet.loadFilters(i)}else{i()}},_create:function(){this._using(this._createView.bind(this))},_createView:function(){if(this._isDisposed){return}var t='<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-filtersetselector").html(t);if(this.label){this.$(".label",this.element).text(this.label)}if(this.image){this.$(".image > span",this.element).sprite(this.image).attr("title",$t("TXT_net_filterset_texts_10")||"").on("click",this._onFilterPopupClick.bind(this))}this._combo=new u8.Base.Widgets.ComboBox(this.$(".selector",this.element),{onValueChange:this._onComboBoxValueChange.bind(this),parent:this,disabled:true,value:"",items:[{key:"",text:u8.session.commonTexts.loading}]});if(!this.filterSets.items){this.filterSets.load(this._onFilterSetsLoad.bind(this))}else{this._onFilterSetsLoad()}},_using:function(t){u8.using({texts:["net_filterset_texts"]},t)},_onFilterPopupClick:function(t){this._popup=new u8.Crm.Widgets.FilterSetPopup(document.body,{parent:this,alignTo:t.target,filterSets:this.filterSets,filterSet:this.getValue(),onClose:this._onPopupClose.bind(this)});this._popup.open()},_onPopupClose:function(t,e){if(!e.ok){return}var i=t.getValue();this.setValue(i)},_onComboBoxValueChange:function(){if(!this._combo){return}var t=this._combo.getValue(),e=this.filterSets.findById(t);if(t=="-1"&&e===null){e=this._defaultFilterSet}this.setValue(e)},_updateCombo:function(){var t=this._combo;if(!t){return}var e=this.filterSet&&this.filterSet.id?this.filterSet.id:"-1";u8.Crm.Widgets.FilterSetSelector.itemsToCombo(this.filterSets,t,e);var i=this.filterSets.findById(t.getValue());if(i){this.filterSet=i}},_onFilterSetsLoad:function(){if(this._isDisposed){return}this._combo.attr({disabled:false});this._updateCombo();u8.tools.created(this)},_onFilterSetSave:function(){this._updateCombo()},_onFilterSetDelete:function(){this._updateCombo()}});u8.Crm.Widgets.FilterSetSelector.itemsToCombo=function(t,e,i){if(!u8.services.texts.isTextGroupLoaded("net_filterset_texts")){$error("Before calling FilterSetSelector.itemsToCombo you have to make sure that text group 'net_filterset_texts' is loaded.")}var s=[],l=0,r=t.items||[],o=false,n=$t("TXT_net_filterset_texts_4");if(n==" "){n=""}s.push({key:"-1",text:n});if(r.length>0){s.push({key:"_Sep"+l++,html:"<div class='u8-combobox-separator'></div>",disabled:true})}for(var a=0;a<r.length;a++){var h=r[a];if(!h.isReadOnly){continue}var u=h.id,d=h.name||h.id||"";s.push({key:u,text:d});o=true}var f=false;for(a=0;a<r.length;a++){h=r[a];if(h.isReadOnly){continue}if(!f){f=true;if(o){s.push({key:"_Sep"+l++,html:"<div class='u8-combobox-separator'></div>",disabled:true})}}u=h.id;d=h.name||"";s.push({key:u,text:d})}e.withoutEvents(e.attr,{items:s});e.setValue(i)}})();