(function(){u8.Crm.Widgets.CatalogValueImageEditor=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.CatalogValueImageEditor.defaults,t));this._window=$w(e);if(!this.options){this.options={}}this._isCreated=false;this._create()};u8.Crm.Widgets.CatalogValueImageEditor.defaults={_imageData:{}};u8.Crm.Widgets.CatalogValueImageEditor.makeClass({name:"CatalogValueImageEditor",deriveFrom:u8.Base.Widgets.Widget,events:["SelectImage","UploadImage","RemoveImage"]});$.extend(u8.Crm.Widgets.CatalogValueImageEditor.prototype,{_create:function(){Function.chain({onError:"abort"},this._using,this._createModel,this._createContext,this._initTemplate,function(e){self.raise("Create");e()}).call(this)},_using:function(e){e()},_createModel:function(e){var t=this;u8.request("CatalogChannel.GetCatalogLinkedImage",{options:{catalogNumber:t.catalogNumber,catalogCode:t.catalogCode,catalogLanguageNumber:t.catalogLanguageNumber}},function(a,o){if(o&&o.response&&o.response.imageData&&typeof o.response.imageData!="undefined"){t.model=o.response.imageData}return e&&e()}.bind(this))},_createContext:function(e){var t=this._getImageBase64(this.model),a=this._getImageName(this.model);this._context=u8.create("Model",{properties:{src:t,name:a,disableSelectImage:false,disableUploadImage:false,disableRemoveImage:!a&&!t}});e()},_initTemplate:function(e){this.$(this.element).utml({url:"~/crm/CatalogValueImageEditor.utml.htm",data:this.model,itemOptions:{parent:this,context:this._context}})},_getImageBase64:function(e){if(e){return"data:image/"+e.format+";base64,"+e.base64}return""},_getImageName:function(e){if(e){return e.name}return""},"Command.SelectImage":function(){this._selectImage()},"Command.UploadImage":function(){this._uploadImage()},"Command.RemoveImage":function(){this._removeImage()},_removeImage:function(){this._updateContext(null);this._setImageUid(null)},_selectImage:function(){this._showDocumentsPopup()},_uploadImage:function(){var e=this;var t={parent:e};u8.services.factory.load("DocumentUploadPopup",function(){var a=new u8.Crm.Widgets.DocumentUploadPopup($w(e.element),t);a.open()})},_showDocumentsPopup:function(){var e=this;u8.using({types:"SearchPopup"},function(){var t={infoAreaId:"D1",onClose:e._onSearchPopupClose.bind(e),parent:e};e._popup=u8.widget("SearchPopup",$w("main"),t);e._popup.open()})},_onSearchPopupClose:function(e,t){if(t.cancel){return}this._getDocumentImageData(t.selection&&t.selection.uid)},_getDocumentImageData:function(e){var t=this;u8.request("DocumentsChannel.GetDocumentImageData",{documentUid:e},function(a,o){if(o&&o.response){t._updateContext(o.response.imageData);t._setImageUid(e)}}.bind(this))},_updateContext:function(e){var t=this._getImageName(e);var a=this._getImageBase64(e);this._context.set("name",t);this._context.set("src",a);this._context.set("disableSelectImage",false);this._context.set("disableUploadImage",false);this._context.set("disableRemoveImage",!t&&!a)},_setImageUid:function(e){this.imageUid=e},getImageUid:function(){return this.imageUid}})})(u8._);