base/dist/base.min.js

2 lines
42 KiB
JavaScript
Raw Permalink Normal View History

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@apigo.cc/state")):"function"==typeof define&&define.amd?define(["exports","@apigo.cc/state"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ApigoBase=e.ApigoBase||{},e.ApigoState)}(this,function(e,t){"use strict";const i={get:({url:e,...t})=>i.request({url:e,method:"GET",...t}),post:({url:e,data:t,...a})=>i.request({url:e,method:"POST",data:t,...a}),put:({url:e,data:t,...a})=>i.request({url:e,method:"PUT",data:t,...a}),delete:({url:e,...t})=>i.request({url:e,method:"DELETE",...t}),head:({url:e,...t})=>i.request({url:e,method:"HEAD",...t}),request:async({url:e,method:t="POST",data:a,headers:s={},responseType:n,timeout:l=1e4})=>{var o;const r={method:t=t.toUpperCase(),signal:null==(o=AbortSignal.timeout)?void 0:o.call(AbortSignal,l)};if(void 0!==a&&"GET"!==t&&"HEAD"!==t){if(a instanceof HTMLFormElement&&(a=new FormData(a)),a&&"object"==typeof a&&!(a instanceof FormData)&&!(a instanceof ArrayBuffer||ArrayBuffer.isView(a))&&Object.values(a).some(e=>e instanceof File||e instanceof Blob||e instanceof FileList||Array.isArray(e)&&e.some(e=>e instanceof File||e instanceof Blob))){const e=new FormData;for(const[t,i]of Object.entries(a))i instanceof FileList||Array.isArray(i)?Array.from(i).forEach(i=>e.append(t,i)):null!=i&&e.append(t,i);a=e}a instanceof FormData?delete s["Content-Type"]:"string"==typeof a||a instanceof ArrayBuffer||ArrayBuffer.isView(a)||(a=JSON.stringify(a),s["Content-Type"]||(s["Content-Type"]="application/json")),r.body=a}for(const e of i.keepHeaders)i._keepHeadersCache[e]&&!s[e]&&(s[e]=i._keepHeadersCache[e]);Object.keys(s).length&&(r.headers=s);const d={error:null,ok:null,status:0,headers:{},responseType:"",result:null};try{const t=await fetch(e,r);Object.assign(d,{ok:t.ok,status:t.status,headers:Object.fromEntries(t.headers.entries())});for(const e of i.keepHeaders){const a=t.headers.get(e);if(a){i._keepHeadersCache[e]=a;try{localStorage.setItem("_http_keep_"+e,a)}catch(e){}}}if(!n){const e=t.headers.get("Content-Type")||"";n=e.includes("application/json")?"json":/image|video|audio|pdf|zip|octet-stream/.test(e)?"binary":"text",d.responseType=n}!1===d.ok&&(d.error=(d.statusText||"HTTP "+d.status+" error")+" for "+e),d.result="json"===n?await t.json():"binary"===n?await t.arrayBuffer():await t.text()}catch(e){Object.assign(d,{error:e.message||String(e),ok:!1})}return d},keepHeaders:["Session-Id","Device-Id"],_keepHeadersCache:{}};for(const e of i.keepHeaders)try{const t=localStorage.getItem("_http_keep_"+e);null!==t&&(i._keepHeadersCache[e]=t)}catch(e){}globalThis.HTTP=i;const a={};globalThis.Component.register("Toast",e=>{e.toast=new bootstrap.Toast(e,{autohide:e.state.delay>0}),globalThis.Util.copyFunction(e,e.toast,"show","hide"),e.addEventListener("show.bs.toast",()=>{if(e.state.delay>0){let t;const i=()=>{e.state.left=e.state.delay/1e3,t=setInterval(()=>{(!e.isConnected||--e.state.left<=0)&&clearInterval(t)},1e3)};i(),e.addEventListener("mouseenter",()=>{clearInterval(t),e.state.left=void 0}),e.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