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:i,headers:a={},responseType:s,timeout:n=1e4})=>{var l;const o={method:t=t.toUpperCase(),signal:null==(l=AbortSignal.timeout)?void 0:l.call(AbortSignal,n)};if(void 0!==i&&"GET"!==t&&"HEAD"!==t){if(i instanceof HTMLFormElement&&(i=new FormData(i)),i&&"object"==typeof i&&!(i instanceof FormData)&&!(i instanceof ArrayBuffer||ArrayBuffer.isView(i))&&Object.values(i).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,a]of Object.entries(i))a instanceof FileList||Array.isArray(a)?Array.from(a).forEach(i=>e.append(t,i)):null!=a&&e.append(t,a);i=e}i instanceof FormData?delete a["Content-Type"]:"string"==typeof i||i instanceof ArrayBuffer||ArrayBuffer.isView(i)||(i=JSON.stringify(i),a["Content-Type"]||(a["Content-Type"]="application/json")),o.body=i}Object.keys(a).length&&(o.headers=a);const r={error:null,ok:null,status:0,headers:{},responseType:"",result:null};try{const t=await fetch(e,o);if(Object.assign(r,{ok:t.ok,status:t.status,headers:Object.fromEntries(t.headers.entries())}),!s){const e=t.headers.get("Content-Type")||"";s=e.includes("application/json")?"json":/image|video|audio|pdf|zip|octet-stream/.test(e)?"binary":"text",r.responseType=s}!1===r.ok&&(r.error=(r.statusText||"HTTP "+r.status+" error")+" for "+e),r.result="json"===s?await t.json():"binary"===s?await t.arrayBuffer():await t.text()}catch(e){Object.assign(r,{error:e.message||String(e),ok:!1})}return r}};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('<div toast-container="default" class="position-fixed bottom-0 end-0 overflow-auto" style="z-index:3000;max-height:80%"></div>')),a.toast=function(e,t={}){const i=t.delay??5e3,a=document.createElement("Toast");a.state={delay:i,left:i?i/1e3:void 0,type:t.type||"primary",message:e,buttons:t.buttons||[]},globalThis.$(`[toast-container="${t.container||"default"}"]`).appendChild(a),Promise.resolve().then(()=>a.show())},a.to