ismism/ismism.ts/ui/bind/template.ts

235 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-03-06 20:19:40 +08:00
import type { Rec } from "../../src/eid/typ.ts"
2023-03-05 11:28:24 +08:00
import type { Pos } from "../../src/pra/pos.ts"
2023-03-06 15:01:03 +08:00
import type { Agd } from "./article.ts"
2023-03-04 15:52:16 +08:00
2023-03-05 17:16:09 +08:00
export const utc_refresh = 750
2023-03-04 15:52:16 +08:00
export let utc_etag = Date.now()
export const main = document.getElementById("main")! as HTMLDivElement
export const pas_a = document.getElementById("pas_a")! as HTMLAnchorElement
2023-03-05 17:16:09 +08:00
export const adm1 = document.getElementById("adm1")! as HTMLMenuElement
export const adm2 = document.getElementById("adm2")! as HTMLMenuElement
2023-03-04 15:52:16 +08:00
export async function que<T>(
q: string
) {
const r = await fetch(`/q/${q}`)
const etag = r.headers.get("etag")?.substring(3)
if (etag) utc_etag = parseInt(etag)
return r.json() as T
}
2023-03-14 13:47:51 +08:00
export type PosB = Record<string, string | number | boolean | Agd["img"] | Agd["goal"] | Rec["_id"]>
2023-03-04 15:52:16 +08:00
export async function pos<T>(
2023-03-14 13:47:51 +08:00
p: Pos,
b: PosB,
2023-03-04 15:52:16 +08:00
) {
2023-03-14 13:47:51 +08:00
const res = await fetch(`/p/${p}`, {
2023-03-04 15:52:16 +08:00
method: "POST",
body: JSON.stringify(b)
})
return res.json() as T
}
const t: typeof document.createElement = (s: string) => document.createElement(s)
const svg = <S extends keyof SVGElementTagNameMap>(s: S) => document.createElementNS("http://www.w3.org/2000/svg", s)
const section = {
idnam: {
idnam: t("a"), id: t("code"), nam: t("span"),
},
meta: {
adm: t("span"), utc: t("span"),
rej: t("span"), ref: t("span"),
rej2: t("em"), ref2: t("em"),
},
2023-03-05 21:40:00 +08:00
seladm: {
adm: t("section"), adm1: t("select"), adm2: t("select"),
},
2023-03-06 12:37:46 +08:00
re: {
urej: t("p"), uref: t("p"),
},
2023-03-05 21:40:00 +08:00
rel: {
2023-03-06 12:37:46 +08:00
sec: t("p"), uid: t("p"), res: t("p"),
},
cover: {
cover: t("section"),
imgn: t("div"), prev: t("button"), next: t("button"),
imgnam: t("div"), img: t("img"),
},
acct: {
fundbar: svg("rect"), expensebar: svg("rect"),
fund: svg("text"), fundpct: svg("text"), budget: svg("text"),
expense: svg("text"), expensepct: svg("text"),
account: t("a"),
},
2023-03-06 18:30:25 +08:00
rec: {
2023-03-19 13:16:14 +08:00
nrecday: t("section"), recwork: t("p"), recfund: t("p"),
2023-03-06 18:30:25 +08:00
},
2023-03-12 13:15:48 +08:00
wsllit: {
wsllit: t("section"), prewsla: t("button"), prelita: t("button"), prewsl: t("button"), prelit: t("button"),
},
2023-03-06 12:37:46 +08:00
putrel: {
2023-03-11 12:52:54 +08:00
putrel: t("section"), putsec: t("button"), putuid: t("button"), putresn: t("button"), putres: t("button"),
2023-03-05 21:40:00 +08:00
},
2023-03-06 12:37:46 +08:00
putpro: {
putpro: t("section"), putrej: t("button"), putref: t("button"),
2023-03-04 15:52:16 +08:00
},
}
export type Section = typeof section
const template = {
pas: {
tid: "pas" as const,
nbr: t("input"), send: t("button"),
...section.seladm,
pre: t("section"), actid: t("input"), act: t("button"),
pas: t("section"), code: t("input"), issue: t("button"),
hint: t("section"),
},
usr: {
tid: "usr" as const,
...section.idnam,
...section.meta,
2023-03-06 12:37:46 +08:00
rolref: t("p"),
...section.re,
2023-03-07 13:06:53 +08:00
intro: t("p"),
...section.rec,
2023-03-04 15:52:16 +08:00
pos: t("section"), put: t("button"), pas: t("button"),
2023-03-16 18:32:47 +08:00
pre: t("section"), preaud: t("button"), preaut: t("button"),
preusr: t("button"), presoc: t("button"), preagd: t("button"), prefund: t("button"),
2023-03-12 13:15:48 +08:00
...section.wsllit,
2023-03-06 12:37:46 +08:00
...section.putpro,
2023-03-05 18:21:08 +08:00
},
soc: {
tid: "soc" as const,
...section.idnam,
...section.meta,
2023-03-06 12:37:46 +08:00
...section.rel,
2023-03-06 18:30:25 +08:00
intro: t("p"),
...section.rec,
2023-03-05 18:21:08 +08:00
pos: t("section"), put: t("button"),
2023-03-06 12:37:46 +08:00
...section.putrel,
...section.putpro,
},
agd: {
tid: "agd" as const,
...section.idnam,
...section.meta,
...section.cover,
...section.acct,
2023-03-06 18:30:25 +08:00
goal: t("p"), intro: t("p"),
2023-03-05 21:40:00 +08:00
...section.rel,
2023-03-06 18:30:25 +08:00
...section.rec,
2023-03-26 11:55:09 +08:00
pos: t("section"), put: t("button"),
putimg: t("button"), putgoal: t("button"),
prelive: t("button"), prevideo: t("button"), prework: t("button"),
2023-03-06 12:37:46 +08:00
...section.putrel,
...section.putpro,
},
goal: {
tid: "goal" as const,
circle: svg("circle"), pct: svg("text"), nam: t("span"),
2023-03-04 15:52:16 +08:00
},
2023-03-19 13:16:14 +08:00
nrecday: {
tid: "nrecday" as const,
nrecday: svg("svg"),
2023-03-18 13:32:38 +08:00
},
2023-03-26 12:45:22 +08:00
live: {
tid: "live" as const,
live: t("p"), livep: t("p"),
},
2023-03-06 18:30:25 +08:00
rec: {
2023-03-07 16:59:42 +08:00
tid: "rec" as const, rec: t("article"),
2023-03-07 13:06:53 +08:00
unam: t("a"), anam: t("a"),
meta: t("section"),
msg: t("section"),
re: t("section"), rej: t("span"), ref: t("span"),
...section.putpro, put: t("button"),
2023-03-06 18:30:25 +08:00
},
2023-03-16 18:32:47 +08:00
aut: {
tid: "aut" as const,
sup: t("p"), aud: t("p"), aut: t("p"), wsl: t("p"), lit: t("p"),
},
2023-03-11 12:25:52 +08:00
md: {
tid: "md" as const,
...section.idnam,
utc: t("span"), utcp: t("span"), unam: t("span"),
md: t("section"),
2023-03-21 12:19:44 +08:00
put: t("button"), putpin: t("button"),
2023-03-11 12:25:52 +08:00
},
2023-03-05 13:35:22 +08:00
pre: {
tid: "pre" as const,
...section.idnam,
nbr: t("input"), pnam: t("input"),
...section.seladm,
pre: t("button"), cancel: t("button"),
},
2023-03-05 12:48:06 +08:00
put: {
tid: "put" as const,
...section.idnam,
2023-03-26 11:55:09 +08:00
p1: t("input"), p2: t("input"), p3: t("input"), p4: t("input"), pa: t("textarea"),
2023-03-14 13:47:51 +08:00
putn: t("button"), put: t("button"), cancel: t("button"),
},
putid: {
tid: "putid" as const,
...section.idnam,
2023-03-06 11:29:37 +08:00
meta: t("section"),
2023-03-05 12:48:06 +08:00
pnam: t("input"),
...section.seladm,
intro: t("textarea"),
uidlim: t("input"), reslim: t("input"),
account: t("input"), budget: t("input"), fund: t("input"), expense: t("input"),
2023-03-08 10:49:53 +08:00
putn: t("button"), put: t("button"), cancel: t("button"),
2023-03-05 12:48:06 +08:00
},
2023-03-04 15:52:16 +08:00
idn: {
tid: "idn" as const,
id: t("code"), meta: t("section"),
}
}
type Template = typeof template
export type Bind<
T extends keyof Template
> = {
bind: DocumentFragment
} & Template[T]
export function bind<
T extends keyof Template
>(
tid: T
): Bind<T> {
const temp = document.getElementById(tid) as HTMLTemplateElement
const t = temp.content.cloneNode(true) as DocumentFragment
const b = Object.fromEntries([
["bind", t], ...Object.keys(template[tid]).map(c => [
c, t.querySelector(`.${c}`) as HTMLElement
])
]) as Bind<T>
b.tid = tid
return b
}