UNPKG

@aurigma/ui-framework

Version:

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

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