UNPKG

@aurigma/ui-framework

Version:

A platform which allows building print product personalization editors based on Aurigma's Customer's Canvas.

6 lines 7.4 kB
(window.multistep_editor_jsonpFunction=window.multistep_editor_jsonpFunction||[]).push([[14],{896:function(e,t,s){s.r(t),s.d(t,"AuWidgetAjax",function(){return i}),s.d(t,"widget",function(){return i});var t=s(0),r=s(2),o=(s(201),s(37)),n=s(1),s=s(160);let i=class extends s.a{constructor(){super(...arguments),this._isRestoring=!1,this._isLoaded=!1,this.enabled=!0,this.headers={},this.method="POST",this.responses=[],this.error=null,this.statusCode=null,this.responseHeaders=null,this.responseType="json",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(t){var e;n.a.isNotNullOrUndefined(this.requests)&&n.a.isNotEmptyArray(this.requests)?(e=this.requests.map(e=>{e={enabled:void 0===e.enabled||e.enabled,_onSuccess:e.onSuccess?n.a.functionWrapperConstructor(e.onSuccess):this._onSuccess,_onError:e.onSuccess?n.a.functionWrapperConstructor(e.onError):this._onError,headers:e.headers||this.headers,request:e.request||this.request,responseType:e.responseType||this.responseType,method:e.method||this.method,url:e.url||this.url};return this._createFetch(e)}),Promise.all(e).then(async e=>{this._fetchTimer=null,n.a.isNullableArray(e)||(this.responses=e,this.emitChange({responses:this.responses},!1)),t(e)})):(e={enabled:this.enabled,_onSuccess:this._onSuccess,_onError:this._onError,headers:this.headers,request:this.request,responseType:this.responseType,method:this.method,url:this.url},this._createFetch(e).then(async e=>{this._fetchTimer=null,e&&(this.response=e,this.emitChange({response:this.response},!1)),t(e)}))}async _createFetch(r){if(n.a.isNotUndefined(r.url)){if(!1===r.enabled)return Promise.resolve(null);this._requestPending=!0;let t={method:r.method,redirect:"follow"},s=(n.a.isObjectNotEmpty(r.request)&&(t.body=JSON.stringify(r.request)),-1!==["get","head"].indexOf(t.method.toLowerCase())&&delete t.body,(n.a.isObjectNotEmpty(r.headers)||t.body)&&(t.headers=new Headers(Object.assign(t.body?{"Content-type":"application/json"}:{},r.headers||{}))),new Request(r.url,t));this._currentAbortController=new AbortController;r._currentFetch=fetch(s,{signal:this._currentAbortController.signal}).then(async e=>(e.redirected&&(s=new Request(e.url,t),e=await fetch(s,{signal:this._currentAbortController.signal})),e)).then(e=>(this.statusCode=e.status,this.emitChange({statusCode:this.statusCode},!1),e)).then(async e=>{if(e.ok)return e;var t=await e.text();try{var s={body:JSON.parse(t),headers:e.headers,statusCode:e.status};r._onError(s)}catch(e){r._onError()}throw Error(`[${e.status},${e.statusText}] : `+t)}).then(async t=>{switch(this.statusCode=t.status,this.responseHeaders=t.headers,this.emitChange({responseHeaders:this.responseHeaders},!1),r.responseType){case"text":return t.text().then(e=>e.replace(/^"/g,"").replace(/\"$/g,""));case"status_code":return t.status;case"blob":return t.blob();case"json":try{return await t.clone().json()}catch(e){var s=Error(` [request url: ${t.url}, request status code: ${t.status}] `+`unexpected error: ${e} response body: ${await t.clone().text()} `);return Promise.reject(s)}case"blob_url":return t.blob().then(e=>URL.createObjectURL(e));default:return t.text()}}).then(e=>(r.error=null,r.response=e,this.response=e,r._onSuccess(),r.response)).catch(e=>{"AbortError"!==e.name&&(r.error=e,this.error=e,r._onError(),console.error(e))}).finally(()=>{this._requestPending=!1})}return r._currentFetch||Promise.resolve(null)}updateParams(e){var t;n.a.isEmptyString(e.url)||e.url.toLowerCase().startsWith("http")&&(t=o.Maybe.maybe(e),this.enabled=t.bind(e=>n.a.maybeGet(()=>e.enabled)).valueOr(this.enabled),this.headers=t.bind(e=>n.a.maybeGet(()=>e.headers)).valueOr(this.headers),this.request=t.bind(e=>n.a.maybeGet(()=>e.request)).valueOr(this.request),this.requests=t.bind(e=>n.a.maybeGet(()=>e.requests)).valueOr(this.requests),this.responseType=t.bind(e=>n.a.maybeGet(()=>e.responseType)).valueOr(this.responseType),this.method=t.bind(e=>n.a.maybeGet(()=>e.method)).valueOr(this.method),this.url=t.bind(e=>n.a.maybeGet(()=>e.url)).valueOr(this.url),this.responses=[],this._onSuccess=n.a.functionWrapperConstructor(e.onSuccess),this._onError=n.a.functionWrapperConstructor(e.onError))}async updateParamsAsync(e){return await super.updateParamsAsync(e),this._lockList=o.Maybe.maybe(e).bind(e=>n.a.maybeGet(()=>e.lock)).map(e=>n.a.ensureArray(e)).valueOr(this._lockList),this._isLoaded||(this.getInitialParamsResolved(),this._isLoaded=!0),this._doFetchDebounce()}async exportWidgetData(e){return{response:this.response,responses:this.responses,headers:this.headers,status:this.statusCode}}async restoreWidgetFromData(e,t){e&&(this.response=e.response,this.responses=e.responses,this.headers=e.headers,this.statusCode=e.status,await this.emitChange({response:this.response,responses:this.responses,headers:this.headers,statusCode:this.statusCode},!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}};Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Object)],i.prototype,"request",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Boolean)],i.prototype,"enabled",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Array)],i.prototype,"requests",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Object)],i.prototype,"headers",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",String)],i.prototype,"method",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",String)],i.prototype,"url",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Object)],i.prototype,"response",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Object)],i.prototype,"responses",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Object)],i.prototype,"error",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Number)],i.prototype,"statusCode",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Headers)],i.prototype,"responseHeaders",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",String)],i.prototype,"responseType",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Promise)],i.prototype,"requestPromise",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Boolean)],i.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)],i.prototype,"requestPendingObserver",null),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Function)],i.prototype,"_onSuccess",void 0),Object(t.a)([Object(r.c)(),Object(t.b)("design:type",Function)],i.prototype,"_onError",void 0),i=Object(t.a)([Object(r.a)("au-widget-ajax")],i)}}]);