(function(){"use strict";u8.Crm.Widgets.SearchPopup=function(e,i){this.Widget(e,$.extend({},u8.Crm.Widgets.SearchPopup.defaults,i));this.commandBindings={Select:this._onCommandSelect.bind(this)}};u8.Crm.Widgets.SearchPopup.defaults={selectionMode:"single"};u8.Crm.Widgets.SearchPopup.makeClass({name:"SearchPopup",deriveFrom:u8.Base.Widgets.Widget,events:["Select","Close"]});$.extend(u8.Crm.Widgets.SearchPopup.prototype,{open:function(){var e=this;var i=function(i){if(!e.infoareas){return e[i]}var t=e.infoareas.find(function(i){return i.infoAreaId===e.infoAreaId});if(!t){return e[i]}if(!t[i]){return e[i]}return t[i]||e[i]};var t={name:this.searchAndListName||this.infoAreaId,infoAreaId:this.infoAreaId,link:this.link,relationName:this.relationName,flavor:"Select",form:$.extend({useAutoSearch:this.useAutoSearch,filter:i("filter"),fixedFilterName:i("fixedFilterName"),showFilters:this.showFilters,sourceUid:this.sourceUid,sourceCopyFieldGroupName:this.sourceCopyFieldGroupName,searchContext:this.searchContext},this.form)};$.extend(t,this.searchOptions);if(this.filterFields){var s=[];for(var o in this.filterFields){var n=this.filterFields[o];if(typeof n.value=="undefined"){continue}var r=n.infoAreaId||this.infoAreaId;if(o.indexOf(".")>=0){var l=o.split(".");o=l[1];r=l[0]||r}s.push({fieldId:o,infoAreaId:r,value:n.value,isFixed:n.isFixed,important:!!n.important,compareOperator:n.compareOperator})}t.form.defaultFieldValues=s}if(this.selectionMode=="multi"){t.viewOptions={list:{grid:{selectionMode:"multi"}}}}var a=u8.services.navigation.getPageUrl("LinkInfoArea");this._popup=new u8.Base.Widgets.Popup(a,{size:{width:.9,height:.9},parent:this,commandBindings:this.commandBindings,pageOptions:t,onClose:this._onPopupClose.bind(this)});this._popup.open()},close:function(){if(!this._popup){return}this._popup.close()},select:function(e,i){if(u8.tools.isArray(e)){var t=e;for(var s=0;s<t.length;s++){this._addSelection({uid:t[s]})}}else{this._addSelection({uid:e})}this._completeSelection();if(i){i(this,{selection:this.selection})}},_addSelection:function(e){if(this.selectionMode=="single"){this.selection=e}else{if(!this.selection){this.selection=[]}this.selection.push(e)}},_completeSelection:function(){if(this.selectionMode=="singleStayOpen"){this.raise("Select",{selection:this.selection});return}this.close()},_onPopupClose:function(e,i){this._close(i)},_close:function(e){if(!this._popup){return}if(!e){e={}}if(!e.cancel){e.selection=this.selection}if(this.selectionMode!="singleStayOpen"){this.raise("Select",e)}this._popup.dispose();this._popup=null;this.raise("Close",e)},_onCommandSelect:function(e,i,t){this.select(this.selectionMode==="single"?t.uid||t.uids:t.uids||t.uid)}})})();