@aurigma/ui-framework
Version:
A platform which allows building print product personalization editors based on Aurigma's Customer's Canvas.
1 lines • 6.27 kB
JavaScript
(window.multistep_editor_jsonpFunction=window.multistep_editor_jsonpFunction||[]).push([[15],{911:function(e,t,s){"use strict";s.r(t),s.d(t,"AuWidgetAssetStorageAjax",(function(){return o})),s.d(t,"widget",(function(){return o}));var i=s(0),r=s(160),a=(s(201),s(2)),n=s(1);let o=class extends r.a{constructor(){super(...arguments),this._isRestoring=!1,this._isLoaded=!1,this.response=[],this.requestPromise=Promise.resolve(),this._requestPending=!1,this._lockList=[],this._fetchTimer=null,this._fetchDelay=50,this._currentAbortController=null}requestPendingObserver(e){this.showPreloader(e)}async _doFetch(e){this._requestPending=!0;const t=this.folder||"";if(this.apiResponse=await this.getAssetStorageFolder(),this.apiResponse.entities&&this.apiResponse.entities.length>0&&this.entriesFilter)try{this.apiResponse.entities=await this.filterAsync(this.apiResponse.entities,this.entriesFilter)}catch(i){console.error(i)}if(this.apiResponse.folders&&this.apiResponse.folders.length>0&&this.folderFilter)try{this.apiResponse.folders=await this.filterAsync(this.apiResponse.folders,this.folderFilter)}catch(i){console.error(i)}this.response=this.apiResponse.entities.map((e=>{const s=t&&""!==t&&"/"!==t?t+"/"+e.name:e.name;return{id:e.id,name:e.name,path:s.replace(/([^:]\/)\/+/g,"$1"),previewUrl:"",customFields:e.customFields||{},metadata:e.metadata||{}}}));const s=this.previewOptions;if(s){const e=async(e,t)=>{if(e&&e.previews&&Object.keys(e.previews).length>0){let i=Object.keys(e.previews).find((t=>e.previews[t].namespace===s.namespace&&e.previews[t].name===s.name));if(i)this.response[t].previewUrl=e.previews[i].url;else if(s.height&&0!==s.height&&s.width&&0!==s.width){const i=await this.addPreview(s,e.id);this.response[t].previewUrl=""===i?e.previews[Object.keys(e.previews)[0]].url:i}else this.response[t].previewUrl=e.previews[Object.keys(e.previews)[0]].url}else{const i=await this.addPreview(s,e.id);this.response[t].previewUrl=""===i?e.previews[Object.keys(e.previews)[0]].url:i}},t=this.apiResponse.entities.map(e);await Promise.all(t)}this.emitChange({apiResponse:this.apiResponse,response:this.response},!1),this._fetchTimer=null,e(this.response),this._requestPending=!1}updateParams(e){var t,s,i;this.assetType=null!==(t=e.assetType)&&void 0!==t?t:null,this.folder=null!==(s=e.folder)&&void 0!==s?s:null,this.previewOptions=Object.assign({name:"uif",namespace:"storefront",width:200,height:200},null!==(i=e.previewOptions)&&void 0!==i?i:{}),this.folderFilter=e.folderFilter?n.a.functionWrapperConstructor(e.folderFilter):null,this.entriesFilter=e.entriesFilter?n.a.functionWrapperConstructor(e.entriesFilter):null}async updateParamsAsync(e){return await super.updateParamsAsync(e),this._lockList=e.lock?e.lock instanceof Array?e.lock:[e.lock]:[],this._isLoaded||(this.getInitialParamsResolved(),this._isLoaded=!0),this._doFetchDebounce()}async exportWidgetData(e){return{apiResponse:this.apiResponse,response:this.response}}async restoreWidgetFromData(e,t){e&&(this.apiResponse=e.apiResponse,this.response=e.response,this.emitChange({apiResponse:this.apiResponse,response:this.response},!1))}showPreloader(e){(this._lockList||[]).forEach((t=>{let s="main"===t?this.auWizard:this.auWizard.scope.$[t]||this.jsonParamsFetcher.scope[t]||this.jsonParamsFetcher.scope.$[t];s&&s.showPreloader(e)}))}_doFetchDebounce(){let e=null;this.requestPromise=new Promise((t=>{e=t}));const t=new Promise((e=>{null!==this._fetchTimer&&(window.clearTimeout(this._fetchTimer),this._currentAbortController&&this._currentAbortController.abort()),this._fetchTimer=window.setTimeout((async()=>{await this._doFetch(e)}),this._fetchDelay)}));return t.then((()=>{setTimeout((()=>{setTimeout((()=>{setTimeout((()=>{setTimeout((()=>{setTimeout((()=>{e()}),100)}),100)}),100)}),100)}),100)})),t}async addPreview(e,t){const s=this.assetType.replace("private","private-"),i=n.a.ensureEndsWith(this.auWizard.driver.backOffice.assetProcessorUrl,"/"),r=this.auWizard.driver.backOffice.tenantId,a=`${i}api/processor/v1/${s}/${t}/preview/${e.namespace}/${e.name}/${e.width}x${e.height}?tenantId=${r}`;return await this.generatePreview(a)}async generatePreview(e){const t=await fetch(e,{method:"GET",headers:{authorization:"Bearer "+this.auWizard.driver.backOffice.token}});if(200===t.status){const e=await t.blob();return URL.createObjectURL(e)}return""}async getAssetStorageFolder(){const e=this.assetType.replace("private","private-"),t=this.folder,s=n.a.ensureEndsWith(this.auWizard.driver.backOffice.assetStorageUrl,"/"),i=this.auWizard.driver.backOffice.tenantId;let r=s+`api/storage/v1/${e}/folders?`;null!=t&&(r+="fullPath="+t),e.includes("private")&&(r+="&userId="+this.auWizard.driver.backOffice.userId),r+="&tenantId="+i;const a=await fetch(r,{method:"GET",headers:{authorization:"Bearer "+this.auWizard.driver.backOffice.token,accept:"text/plain"}});return a.status>=200&&a.status<=300?await a.json():(console.error(`Failed to execute ajax: status ${a.status}: ${a.statusText}`),{folders:[],entities:[]})}mapAsync(e,t){return Promise.all(e.map(t))}async filterAsync(e,t){const s=await this.mapAsync(e,t);return e.filter(((e,t)=>s[t]))}};Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Object)],o.prototype,"apiResponse",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Array)],o.prototype,"response",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",String)],o.prototype,"assetType",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",String)],o.prototype,"folder",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Function)],o.prototype,"folderFilter",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Function)],o.prototype,"entriesFilter",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Object)],o.prototype,"previewOptions",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Promise)],o.prototype,"requestPromise",void 0),Object(i.a)([Object(a.c)(),Object(i.b)("design:type",Boolean)],o.prototype,"_requestPending",void 0),Object(i.a)([Object(a.b)("_requestPending"),Object(i.b)("design:type",Function),Object(i.b)("design:paramtypes",[Object]),Object(i.b)("design:returntype",void 0)],o.prototype,"requestPendingObserver",null),o=Object(i.a)([Object(a.a)("au-widget-asset-storage-ajax")],o)}}]);