(function(){"use strict";u8.Crm.Widgets.CampaignGraph=function(e,i){this.Widget(e,$.extend({},u8.Crm.Widgets.CampaignGraph.defaults,i));this._isCreated=false;this._create()};u8.Crm.Widgets.CampaignGraph.defaults={zoomToFit:true,graph:{layoutOptions:{algorithm:"EfficientSugiyama",edgeRouting:"Orthogonal",positionMode:-1,vertexSpacing:10,layerSpacing:35}},theme:{css:{CM:"u8-campaigngraph-campaign",AK:"u8-campaigngraph-activity",AN:"u8-campaigngraph-response"}}};u8.Crm.Widgets.CampaignGraph.makeClass({name:"CampaignGraph",deriveFrom:u8.Base.Widgets.Widget,mixins:[u8.Base.ActionSource],events:["Create","ValueChange","Select"]});$.extend(u8.Crm.Widgets.CampaignGraph.prototype,{getValue:function(){return this.value},setValue:function(e){$assert(this._validateValue(e));if(u8.equalsRecordUid(this.value,e)){return this}this.value=e;this.update();this.raise("ValueChange");return this},setSize:function(e){if(e){var i=e.height,t=e.width;if(t||i){this.$(this.element).css({width:t,height:i})}}return this},update:function(e){this._clearView();var i=this._busy();Function.chain(this._createModel,this._updateView).call(this,function(){i.hide();if(e&&typeof e=="function"){e()}})},dispose:function(){if(this._isDisposed){return}this.$(this.element).removeClass("u8-campaigngraph");this.Widget_dispose()},zoom:function(e){if(!this._graph){return}this._graph.zoom(e)},_create:function(){var e=this._busy();Function.chain(this._createModel,this._createView).call(this,function(){e.hide();u8.tools.created(this)})},_createModel:function(e){var i=this;this._model={};if(this.value&&this._validateValue(this.value)){u8.request("CampaignManagementChannel.BuildCampaignGraph",{uid:this.value,options:{linkName:this.linkName}},t)}else if(this.__sdkModel){t(this,{response:this.__sdkModel})}else{t(this,{response:{vertices:[],edges:[],data:{}}})}function t(t,a){if(i._isDisposed){return}i._model.graphModel=a.response;var s=null,n=a.response.data;for(var r in n){if(u8.equalsRecordUid(n[r].uid,i.value)){s=r;break}}i._model.selection=s;e()}},_createView:function(e){var i=this.$("<div/>").addClass("u8-campaigngraph-container");this.$(this.element).empty().addClass("u8-campaigngraph").append(i);if(this.size){this.setSize(this.size)}var t=$.extend({},this.graph,{parent:this,model:this._model.graphModel,selection:this._model.selection,zoomToFit:this.zoomToFit,vertexRenderer:this._vertexRenderer.bind(this),onVertexDblClick:this._onVertexDblClick.bind(this)});this._graph=i.widget("Graph",t)[0];if(e){e()}},_updateView:function(e){if(this._graph){this._graph.setModel(this._model.graphModel);this._graph.setSelection(this._model.selection)}e()},_clearView:function(){if(this._graph){this._graph.setModel(null)}},_busy:function(){return this.busy({alignTo:this.element,modal:true,delay:0})},_vertexRenderer:function(e,i){var t=i.tableCaption,a=u8.session.images["INFOAREA_"+i.uid.infoAreaId],s=this.$(e).empty().addClass(this.theme.css[i.uid.infoAreaId]||"");this.$("<div/>").addClass("u8-campaigngraph-label").append(this.$("<span/>").sprite(a,"xs")).append(this.$("<span/>").text(t.text||"")).attr("title",t.text).appendTo(s)},_validateValue:function(e){return u8.isValidRecordUid(e)},_onVertexDblClick:function(e,i){var t=i.id,a=e.model.data[t],s=a&&a.uid;if(!s){return}var n=this.defaultActions&&(this.defaultActions[s.infoAreaId]||this.defaultActions["*"]);if(n==="nothing"){return}var r={source:this,target:this,uid:s,infoAreaId:s.infoAreaId,elWindow:$w(this.element)};if(n==="select"){this.raise("Select",r);return}this.executeAction({action:n||"DefaultAction",arguments:r})}})})();