base/dist/base.min.js

2 lines
47 KiB
JavaScript
Raw Normal View History

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@apigo.cc/state")):"function"==typeof define&&define.amd?define(["exports","@apigo.cc/state"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ApigoBase=t.ApigoBase||{},t.ApigoState)}(this,function(t,e){"use strict";const i={get:({url:t,...e})=>i.request({url:t,method:"GET",...e}),post:({url:t,data:e,...s})=>i.request({url:t,method:"POST",data:e,...s}),put:({url:t,data:e,...s})=>i.request({url:t,method:"PUT",data:e,...s}),delete:({url:t,...e})=>i.request({url:t,method:"DELETE",...e}),head:({url:t,...e})=>i.request({url:t,method:"HEAD",...e}),request:async({url:t,method:e="POST",data:s,headers:a={},responseType:l,timeout:n=1e4})=>{var o;const r={method:e=e.toUpperCase(),signal:null==(o=AbortSignal.timeout)?void 0:o.call(AbortSignal,n)};if(void 0!==s&&"GET"!==e&&"HEAD"!==e){if(s instanceof HTMLFormElement&&(s=new FormData(s)),s&&"object"==typeof s&&!(s instanceof FormData)&&!(s instanceof ArrayBuffer||ArrayBuffer.isView(s))&&Object.values(s).some(t=>t instanceof File||t instanceof Blob||t instanceof FileList||Array.isArray(t)&&t.some(t=>t instanceof File||t instanceof Blob))){const t=new FormData;for(const[e,i]of Object.entries(s))i instanceof FileList||Array.isArray(i)?Array.from(i).forEach(i=>t.append(e,i)):null!=i&&t.append(e,i);s=t}s instanceof FormData?delete a["Content-Type"]:"string"==typeof s||s instanceof ArrayBuffer||ArrayBuffer.isView(s)||(s=JSON.stringify(s),a["Content-Type"]||(a["Content-Type"]="application/json")),r.body=s}for(const t of i.keepHeaders)i._keepHeadersCache[t]&&!a[t]&&(a[t]=i._keepHeadersCache[t]);Object.keys(a).length&&(r.headers=a);const d={error:null,ok:null,status:0,headers:{},responseType:"",result:null};try{const e=await fetch(t,r);Object.assign(d,{ok:e.ok,status:e.status,headers:Object.fromEntries(e.headers.entries())});for(const t of i.keepHeaders){const s=e.headers.get(t);if(s){i._keepHeadersCache[t]=s;try{localStorage.setItem("_http_keep_"+t,s)}catch(t){}}}if(!l){const t=e.headers.get("Content-Type")||"";l=t.includes("application/json")?"json":/image|video|audio|pdf|zip|octet-stream/.test(t)?"binary":"text",d.responseType=l}!1===d.ok&&(d.error=(d.statusText||"HTTP "+d.status+" error")+" for "+t),d.result="json"===l?await e.json():"binary"===l?await e.arrayBuffer():await e.text()}catch(t){Object.assign(d,{error:t.message||String(t),ok:!1})}return d},keepHeaders:["Session-Id","Device-Id"],_keepHeadersCache:{}};for(const t of i.keepHeaders)try{const e=localStorage.getItem("_http_keep_"+t);null!==e&&(i._keepHeadersCache[t]=e)}catch(t){}globalThis.HTTP=i;const s={};globalThis.Component.register("Toast",t=>{t.toast=new bootstrap.Toast(t,{autohide:t.state.delay>0}),globalThis.Util.copyFunction(t,t.toast,"show","hide"),t.addEventListener("show.bs.toast",()=>{if(t.state.delay>0){let e;const i=()=>{t.state.left=t.state.delay/1e3,e=setInterval(()=>{(!t.isConnected||--t.state.left<=0)&&clearInterval(e)},1e3)};i(),t.addEventListener("mouseenter",()=>{clearInterval(e),t.state.left=void 0}),t.addEventListener("mouseleave",i)}})},globalThis.Util.makeDom('\n<div class="toast align-items-center border-0 m-1">\n <div $class="toast-body rounded p-3 text-bg-${this.state?.type}">\n <div class="d-flex align-items-center">\n <div class="flex-grow-1">\n <span style="white-space:pre-wrap" class="fs-6" $text="this.state?.message"></span>\n <template $if="this.state?.left !== undefined">\n <span class="small text-dim ms-2" $text="${this.state?.left}s"></span>\n </template>\n </div>\n <button type="button" class="btn btn-link ms-3 bi bi-x-lg link-reset" style="color:inherit" data-bs-dismiss="toast"></button>\n </div>\n <div class="d-flex justify-content-end gap-3">\n <template $each="this.state?.buttons || [\'{#Close#}\']">\n <button type="button" $class="btn btn-sm btn-${this.state?.type} mt-2" data-bs-dismiss="toast" $onclick="this.result=index+1" $text="item"></button>\n </template>\n </div>\n </div>\n</div>\n'),globalThis.Util.makeDom('<di