@aurigma/ui-framework
Version:
A platform which allows building print product personalization editors based on Aurigma's Customer's Canvas.
3 lines • 6.59 kB
JavaScript
(window.multistep_editor_jsonpFunction=window.multistep_editor_jsonpFunction||[]).push([[22],{869:function(e,t){e.exports='<style>\n :host {\n width: 100%;\n height: 100%;\n }\n</style>\n\n<iframe id="frameID" width="100%" height="100%" style="border: none"></iframe>\n'},940:function(e,t,i){i.r(t),i.d(t,"AuWidgetDataDrivenEditor",function(){return n}),i.d(t,"widget",function(){return n});var t=i(0),s=i(1),a=i(2),r=i(160),i=i(869);let n=class extends r.c{constructor(){super(),this.previewUrls=[],this._frameIsReady=!1,this._isReadyEmitted=!1,this._initIFrame=this._initIFrame.bind(this)}async exportWidgetData(e){return this.params?{design:{designId:this.stateId,isPrivateDesign:!0,designPath:this.params.config.design.designPath,dataSetId:this.params.config.design.dataSetId},access:{userId:this.userId}}:{}}async restoreWidgetFromData(t,e){this._isRestoring=!0,this._getEditorElement().then(async()=>{var e=null!=(e=this.params.config)?e:{},e=Object.assign(Object.assign({},e),{access:Object.assign(Object.assign({},e.access),t.access),design:Object.assign(Object.assign({},e.design),t.design)});this.stateId=t.design.designId,this.userId=t.access.userId,await this.setEditorConfig(e)}),this._isRestoring=!1,this.emitChange()}updateParams(e,t){}async updateParamsAsync(e,t){var i,s,a,r,n,o,d;await super.updateParamsAsync(e,t),e.config&&!this._frameIsReady&&await this._initIFrame(),this._element=this._getEditorEl(),this._element&&null!=e.config&&(this.params=e,this.userId=this.driver.user.id,{token:t,assetProcessorUrl:i,designAtomsApiUrl:s,assetStorageUrl:a,tenantId:d,backOfficeUrl:r}=this.driver.backOffice,n=this.language.slice(0,2),o=await this.getLocalization(n),d=Object.assign(Object.assign({},e.config),{access:Object.assign({userId:this.userId,tenantId:null==d?void 0:d.toString(),token:t},e.config.access),api:Object.assign({assetProcessor:i,assetStorage:a,designAtoms:s,backOffice:r},e.config.api),settings:Object.assign({lang:n},e.config.settings),translation:o}),this.setEditorConfig(d).then(()=>this._emitOnReady()),this._scheduleTokenUpdate(e.config),this._element.$isValid.subscribe(e=>{this.isValid=e,this.emitChange()}))}_emitOnReady(){var e;!1===this._isReadyEmitted&&(e=[].concat(this.params.onReady).filter(e=>e),s.a.functionWrapperConstructor(e)(),this._isReadyEmitted=!0)}_scheduleTokenUpdate(e){this._timerId&&(clearInterval(this._timerId),this._updateToken(e)),this._timerId=setInterval(async()=>this._updateToken(e),27e5)}async _updateToken(e){var t=null!=(t=null!=(t=e.api.backOffice)?t:null==(t=this.driver.backOffice)?void 0:t.backOfficeUrl)?t:"";t&&(t=await this._getToken({backOfficeUrl:t,tenantId:e.access.tenantId,userId:this.driver.user.id}),this._element.updateToken)&&this._element.updateToken(t)}async _getToken({backOfficeUrl:e,tenantId:t,userId:i}){e=s.a.ensureEndsWith(e,"/")+`/api/v1/tenants/${t}/integrations/gettoken`,t=window.location.origin;return(await(await fetch(e,{method:"post",headers:{"Content-type":"application/json"},body:JSON.stringify({userGuid:i,ecommerceDomain:t,origin:t})})).json()).result}async getLocalization(e){var t=null==(t=null==(t=this.driver)?void 0:t.localization)?void 0:t.find(e=>"data-driven-editor"===e.app);let i={};if(t)if(s.a.isNotNullOrUndefined(t.url)&&!s.a.isEmptyString(t.url)&&"string"==typeof t.url)try{i=await s.a.loadJson(t.url+"?ts="+Date.now())}catch(e){console.error(e)}else s.a.isNotNullOrUndefined(t.data)&&"object"==typeof t.data&&0!==Object.keys(t.data).length&&(i=-1!==Object.keys(t.data).indexOf(e)?s.a.DeepMerge(i,t.data):s.a.DeepMerge(i,{[e]:t.data}));return i}async setEditorConfig(e){await this._element.setEditorConfig(e)}setLang(e){this._element.setLang(e)}updateToken(e){}async createPdfPreview(e){e=e||this.params.config.renderer;try{return this.pdfUrl=await this._element.getPdfPreview(e),this.emitChange(),this.pdfUrl}catch(e){return this.showToast("Error: can't create pdf file."),""}}async createPreview(e){e=e||this.params.config.renderer;try{var{width:t,height:i}=this.params.config.preview;return this.previewUrls=await this._element.getPreview(this.stateId,t,i),this.emitChange(),this.previewUrls}catch(e){return this.showToast("Error: can't create preview images."),[]}}async createIdStr(){return this.idStr=await this._element.getIdString(),this.emitChange(),this.idStr}async saveProduct(e){this.stateId=await this._element.save(e);var{width:e,height:t}=this.params.config.preview;this.previewUrlLink=await this._element.getPreviewLink(this.stateId,e,t),this.emitChange()}validate(){return this._element.validate()}setUserInfo(e){this._element.setUserInfo(e)}getFormData(e){return this._element.getFormData(e)}_getEditorEl(){var e;return this._element||(e=this._iframeDoc.getElementById("dd-editor"),window.Aurigma=Object.assign(window.Aurigma,this._iframeWin.contentWindow.Aurigma),e)}async _initIFrame(){this._iFrameEl=this.querySelectorShadow("#frameID"),this._iframeWin=this._iFrameEl||this._iFrameEl.contentWindow,this._iframeDoc=this._iFrameEl.contentDocument||this._iframeWin.document,this._iframeDoc.write('<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap" rel="stylesheet" />'),this._iframeDoc.write('<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />'),this._iframeDoc.write("<style> .mat-button-toggle-button { height: 100%; } </style>"),this._iframeDoc.write(`<div style="width:100%; height:100%">
<au-data-driven-editor id="dd-editor"></au-data-driven-editor>
</div>`);var e=await this._getWorkflowElementsBaseUrl();this._iframeDoc.write(`<link href="${e}/data-driven-editor/styles.css" rel="stylesheet">`);let i=this._iframeDoc.createElement("script");return i.src=e+"/data-driven-editor/index.js",this._iframeDoc.body.appendChild(i),this._frameIsReady=!0,new Promise((e,t)=>{i.onload=()=>e(),i.onerror=()=>{t("Can't load widget script!"),this._iframeDoc.write("Can't load widget script!")}})}async _getWorkflowElementsBaseUrl(){var{backOfficeUrl:e,tenantId:t,token:i}=this.driver.backOffice;if(null!=e)return e=s.a.removeTrailingSlash(e),(await(await fetch(e+"/api/storefront/v1/tenant-info/applications?tenantId="+t,{headers:{Authorization:"Bearer "+i}})).json()).workflowElementsUrl;this._iframeDoc.write("This widget unsupported work without Back Office services!")}_getEditorElement(){return new Promise((t,e)=>{if(this._element)t(this._element);else{let e=()=>{setTimeout(async()=>{this._iframeDoc&&(this._element?t(this._element):e())},200)};e()}})}};n=Object(t.a)([Object(a.a)("au-widget-data-driven-editor",i),Object(t.b)("design:paramtypes",[])],n)}}]);