(function(){"use strict";u8.Crm.Widgets.ActivityFormatEdit=function(t,e){this.Widget(t,$.extend({},u8.Crm.Widgets.ActivityFormatEdit.defaults,e));this.commandBindings={Save:this._onCommandSave.bind(this),LoadFormat:this._onCommandLoadFormat.bind(this),Proceed:this._onCommandProceed.bind(this)};this.model=new u8.Crm.ActivityFormatEditModel(this.model);this._create()};u8.Crm.Widgets.ActivityFormatEdit.defaults={};$.extend(u8.Crm.Widgets.ActivityFormatEdit.prototype,{_create:function(){var t=Function.chain.onDisposedAbort;Function.chain(t,this._createModel,this._load,this._draw.curry(t)).call(this)},_createModel:function(t){var e=this;u8.services.formats.readXml({uid:this.activityUid,formatAlias:this.formatAlias},function(t,i){$assert(i.xml,"No format XML!");var a=i.xml;var o=$(a).xpath("/FormatDefinition/FormatHeader/Name").text();if(!o){o=e.defaultFormatName}e.model.updateData(i.xml,o);if(!e._isDisposed){e.raise("ModelUpdated")}}.chain(t))},_draw:function(t,e){Function.chain(t,this._createViewModel.async(),this._createView).call(this,e)},_load:function(t){u8.using({texts:[],templates:[{name:"u8-activity-format-edit",url:"~/crm/ActivityFormatEdit.utml.htm"}]},t)},_createViewModel:function(){this._viewModel={texts:{loadFormatText:$t("TXT_Campaign_Designer_143"),saveFormatText:this.saveFormatButtonText}}},_createView:function(t){var e=this;var i=this.$(this.element);if(this.formatEditForm){this.formatEditForm.dispose()}if(this.formatDefinitionForm){this.formatDefinitionForm.dispose()}i.addClass("u8-activity-format-edit").toggleClass("page",!!this.isDefault).empty().utml({name:"u8-activity-format-edit",data:this._viewModel,itemOptions:{parent:this}},function(){var a=i.find("#formatEditForm");e.formatEditForm=new u8.Base.Widgets.Form(a,{formName:e.formatEditFormName,parent:e,onPreCreate:function(t){var i=t.components.$context;$assert(i,"Missing xml data source.");$.extend(i.options,{xml:e.model.xml,xpathPrefix:"/FormatDefinition/Activity/"})}});var o=i.find("#formatDefinitionForm");e.formatDefinitionForm=new u8.Base.Widgets.Form(o,{formName:e.formatDefinitionFormName,parent:e,onPreCreate:function(t){var i=t.context;i.set("model",e.model)}});if(t){t()}})},_onCommandSave:function(){var t=this.model;var e=this;var i={model:t,callback:function(t){switch(t){case"Done":e.model.resetIsModifiedState();e._showInfoMessage(e.$(e.element),"TXT_notifications_1","success");break;case"FormatNameEmpty":e._showInfoMessage(e.$(e.element),"TXT_error_text_86","error");break}}};this.raise("Save",i)},_showInfoMessage:function(t,e,i){var a=t.find("#infoMessage");a.text($t(e));a.attr("data-status",i);a.fadeIn().delay(5e3).fadeOut()},_onCommandProceed:function(){this.raise("Proceed",{})},_onCommandLoadFormat:function(t,e,i){var a=this;var o={includePublicFormats:this.allowLoadingOfPublicFormats,includePrivateFormats:this.allowLoadingOfPrivateFormats,formatType:8};u8.services.selectors.selectFormat(o,function(t,e){if(!e||!e.formatName){return}a._readSelectedFormat(e)})},_readSelectedFormat:function(t){var e=this;u8.services.formats.readXml({formatName:t.formatName,formatType:t.format.formatType,formatScope:"Public"},function(i,a){if(a.error){return}e.model.updateData(a.xml,t.formatName);$assert(e.model.xml,"No format XML!");if(!e._isDisposed){e.raise("ModelUpdated");e._createView()}})},dispose:function(){if(this._isDisposed){return}this.Widget_dispose()}});u8.Crm.Widgets.ActivityFormatEdit.makeClass({name:"ActivityFormatEdit",deriveFrom:u8.Base.Widgets.Widget,events:["ModelUpdated","Save"]})})();