(function(){u8.Crm.Widgets.InfoAreaListInput=function(e,t){t=$.extend({},u8.Crm.Widgets.InfoAreaListInput.defaults,{searchProvider:this._createSearchProvider()},t);t.onItemCreate=this._onItemCreate.bind(this);t.onAction=this._onListInputAction.bind(this);this.infoAreasActions=[{title:$t("TXT_searchfilter_text_22"),image:"u8-img-search-s"}];t.actions=this.infoAreasActions;this.ListInput(e,t);var i=this.value;if(typeof i!="undefined"){delete this.value;this.withoutEvents(this.setValue,i)}if(typeof this.autoCompleteMinChars=="undefined"){this.autoCompleteMinChars=u8.session.webConfigValues.getInt("Rep.AutocompleteCharacters")}};u8.Crm.Widgets.InfoAreaListInput.makeClass({name:"InfoAreaListInput",deriveFrom:u8.Base.Widgets.ListInput,override:["setValue","getValue"]});u8.Crm.Widgets.InfoAreaListInput.defaults={disabled:false,readonly:false};$.extend(u8.Crm.Widgets.InfoAreaListInput.prototype,{setValue:function(e){if(!this._isCreated){this.value=e;return}if(!e){e=[]}if(!u8.tools.isArray(e)){e=[e]}var t=this.getValue().sorted();var i=e.sorted();if(t.compare(i)){return}var s=this._prepareItems(e);if(!s){return}var r=t.concat(s);this._items.attach(r)},getValue:function(){if(this._items){return this._items.toArray()}else{return[]}},_getInfoArea:function(e){return u8.services.schema.get(e,{default:true})},_onItemCreate:function(e,t){if(!t.item){t.cancel=true}if(!t.item.searchKey){t.item.searchKey=t.item.text.split(" (")[0]}},_onListInputAction:function(e,t){var i=this;var s=u8.services.schema.getInfoAreas();var r=[];var n=this.getValue();var a=[];if(n&&n.length>0){n.forEach(function(e){var t=e.key;if(t.indexOf("_")!=-1){a.push(t.split("_")[0])}else{a.push(t)}})}s.forEach(function(e){var t=a.indexOf(e.infoAreaId)>=0;if(!t){r.push(e.infoAreaId)}});var o=u8.widget("InfoAreaSelectorPopup",this._$content,{parent:this,onClose:this._onSelectorPopupClose.bind(this),selectionMode:"multi",infoAreaIds:r});o.open()},_onSelectorPopupClose:function(e,t){if(t.ok){var i=[];if(Array.isArray(t.infoAreaIds)){i=t.infoAreaIds}else{i.push(t.infoAreaIds)}for(var s=0;s<i.length;s++){var r=i[s];var n=u8.services.schema.getInfoArea(r);if(n==null){return}var a=n.label;this.setValue({text:a+" ("+r+")",key:r,searchKey:a})}}},_createSearchProvider:function(){return{search:this._searchToken.bind(this),abort:u8._.nop}},_searchToken:function(e,t){if(e.length<this.autoCompleteMinChars){t({});return{}}var i=this._infoAreaToItem.bind(this);var s={pattern:e,types:{All:false},filterName:this.filterName,maxRows:u8.session.webConfigValues.getInt("Rep.MaxAutocompleteResults",50)};var r=u8.services.schema.find(s,function(e,s){t({items:s.matches.map(i),moreRowsExist:false})});return{}},_infoAreaToItem:function(e){var t={};t[this.keyProperty]=e.id;t[this.textProperty]=e.text+" ("+e.id+")"||"";return t}})})();