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