(async () => { // 場所ごとの空きを平面図で見せる。SVG は絵だけで、押せるのは上に重ねた HTML の要素。 const el = (tag, className, ...children) => { const node = document.createElement(tag); if (className) node.className = className; node.append(...children); return node; }; // 押せる要素には、名前(data-on-tap)と読み上げの名前(aria-label・120 字まで)が要る。 const tap = (node, name, label, data) => { node.setAttribute("data-on-tap", name); node.setAttribute("aria-label", label.slice(0, 120)); for (const [key, value] of Object.entries(data || {})) node.dataset[key] = value; return node; }; const dayOf = (instant) => Number(instant.slice(5, 7)) + "/" + Number(instant.slice(8, 10)); // 申込の状態。参加している → 満席 → 受付終了 → 残りの席 の順に見る。 const stateOf = (event, text) => event.joined ? text.joined : event.registration === "FULL" ? text.full : event.registration === "CLOSED" ? text.closed : event.seats ? text.left(event.seats.left) : ""; const join = (...parts) => parts.filter((part) => part !== "").join(" "); const svg = (tag, attributes, ...children) => { const node = document.createElementNS("http://www.w3.org/2000/svg", tag); for (const [name, value] of Object.entries(attributes)) node.setAttribute(name, value); node.append(...children); return node; }; const TEXT = { ja: { open: "あいています", mine: "あなたの予約", exclusive: "貸切", until: (time) => time + "まで", vacant: (count) => count + "か所あいています", none: "いまはいっぱいです", noZones: "場所がまだありません", previousDay: "前の日", nextDay: "次の日", previousHour: "1時間前", nextHour: "1時間後", play: "一日を流す", stop: "止める", joined: "参加します", full: "満席", closed: "受付終了", left: (count) => "あと" + count + "席" }, en: { open: "Open", mine: "Your booking", exclusive: "Private", until: (time) => "Until " + time, vacant: (count) => count + " open", none: "All taken now", noZones: "No spaces yet", previousDay: "Previous day", nextDay: "Next day", previousHour: "1 hour earlier", nextHour: "1 hour later", play: "Play the day", stop: "Stop", joined: "Joined", full: "Full", closed: "Closed", left: (count) => count + " left" }, }; const FIRST_HOUR = 9; const LAST_HOUR = 20; // 平面図の部屋の位置(320 × 240 の図の中)。7 つ目からの場所は、図の下に並べる。 const ROOMS = [[8, 8, 148, 72], [164, 8, 148, 72], [8, 88, 100, 72], [116, 88, 196, 72], [8, 168, 148, 64], [164, 168, 148, 64]]; const COLOR = { open: "#ffffff", mine: "#bfe3c6", exclusive: "#dadfd2", used: "#f3cfa8", event: "#fbe3a6" }; const style = document.createElement("style"); style.textContent = [ ".plan{padding:12px 16px 16px;background:#f3f7ec;color:#2b3a2e}", ".bar{display:grid;grid-template-columns:48px minmax(0,1fr) 48px;gap:8px;align-items:center;margin-bottom:8px}", ".step{height:48px;border:1px solid #9bb08a;border-radius:12px;background:#ffffff;font-size:18px;font-weight:800;line-height:46px;text-align:center}", ".now{font-size:20px;font-weight:800;text-align:center}", ".sum{margin:0 0 8px;font-size:14px;font-weight:700}", ".map{position:relative;aspect-ratio:4 / 3}", ".map svg{position:absolute;left:0;top:0;width:100%;height:100%}", ".room{position:absolute;padding:8px;overflow:hidden;font-size:13px;font-weight:800;line-height:1.3;overflow-wrap:anywhere}", ".room small,.row small{display:block;max-height:2.6em;overflow:hidden;font-size:12px;font-weight:700;line-height:1.3;color:#4d5c4c}", ".row{min-height:48px;margin-top:8px;padding:8px 12px;border:1px solid #c9d8ba;border-radius:12px;background:#ffffff;font-size:14px;font-weight:800;overflow-wrap:anywhere}", ".play{min-height:48px;margin-top:12px;padding:12px;border-radius:12px;background:#356f46;color:#ffffff;font-size:15px;font-weight:700;text-align:center}", ".bar,.sum,.map,.row,.play{max-width:640px;margin-left:auto;margin-right:auto}", ].concat(ROOMS.map(([x, y, width, height], index) => ".r" + index + "{left:" + (x / 3.2).toFixed(2) + "%;top:" + (y / 2.4).toFixed(2) + "%;width:" + (width / 3.2).toFixed(2) + "%;height:" + (height / 2.4).toFixed(2) + "%}")).join(""); document.head.append(style); const pad = (value) => String(value).padStart(2, "0"); const state = { date: niho.surface.get().today, hour: Math.max(FIRST_HOUR, Math.min(LAST_HOUR, Number(niho.clock.now().slice(11, 13)))), timer: 0, }; const shiftDay = (date, step) => new Date(Date.parse(date + "T00:00:00Z") + step * 86400000).toISOString().slice(0, 10); // その時刻にその場所で起きていること。予定・予約が無ければ空き。 const statusOf = (view, zone, moment, text) => { const covers = (item) => item.startAt <= moment && item.endAt > moment && item.places.includes(zone.name); const event = view.events.find(covers); if (event) return { kind: "event", text: join(event.title, stateOf(event, text)), ref: event.ref }; const booking = view.bookings.find(covers); if (!booking) return { kind: "open", text: text.open, ref: "" }; if (booking.mine) return { kind: "mine", text: text.mine, ref: booking.ref }; return { kind: booking.exclusive ? "exclusive" : "used", text: join(booking.exclusive ? text.exclusive : "", text.until(booking.endAt.slice(11, 16))), ref: booking.ref }; }; const draw = () => { const view = niho.surface.get(); const text = TEXT[view.language] || TEXT.ja; // 見せる日が、渡された期間から外れたら今日へ戻す。今日も外れていたら(月を移したとき)、その月の 1 日にする。 const inRange = (date) => date >= view.range.from && date < view.range.to; if (!inRange(state.date)) state.date = inRange(view.today) ? view.today : shiftDay(view.range.from, 7).slice(0, 8) + "01"; // 動きを控える設定に変わったら、流していた時間を止める。 if (view.reducedMotion && state.timer) { clearInterval(state.timer); state.timer = 0; } const moment = state.date + "T" + pad(state.hour) + ":30:00+09:00"; const statuses = view.zones.map((zone) => statusOf(view, zone, moment, text)); const vacant = statuses.filter((status) => status.kind === "open").length; const step = (kind, sign, label, amount) => tap(el("div", "step", sign), kind, label, { step: String(amount) }); const cell = (zone, status, className) => { const node = el("div", className, zone.name, el("small", "", status.text)); const label = zone.name + " " + status.text; if (status.ref !== "") return tap(node, "open", label, { ref: status.ref }); return view.viewer.canBook ? tap(node, "start", label, { zone: zone.ref }) : node; }; const map = el("div", "map", svg("svg", { viewBox: "0 0 320 240", "aria-hidden": "true" }, svg("rect", { x: "1", y: "1", width: "318", height: "238", rx: "14", fill: "#e1ead6", stroke: "#9bb08a", "stroke-width": "2" }), ...statuses.slice(0, ROOMS.length).flatMap((status, index) => { const [x, y, width, height] = ROOMS[index]; const room = { x: String(x), y: String(y), width: String(width), height: String(height), rx: "8", stroke: "#9bb08a", fill: COLOR[status.kind] }; const table = { x: String(x + width - 44), y: String(y + height - 22), width: "34", height: "12", rx: "4", stroke: "#9bb08a", fill: "none" }; return [svg("rect", room), svg("rect", table)]; })), ...view.zones.slice(0, ROOMS.length).map((zone, index) => cell(zone, statuses[index], "room r" + index))); const page = el("section", "plan", el("div", "bar", step("day", "←", text.previousDay, -1), el("div", "now", dayOf(state.date)), step("day", "→", text.nextDay, 1)), el("div", "bar", step("hour", "←", text.previousHour, -1), el("div", "now", state.hour + ":00"), step("hour", "→", text.nextHour, 1)), el("p", "sum", view.zones.length === 0 ? text.noZones : vacant > 0 ? text.vacant(vacant) : text.none), ...(view.zones.length > 0 ? [map] : []), ...view.zones.slice(ROOMS.length).map((zone, index) => cell(zone, statuses[ROOMS.length + index], "row"))); // 動きを控える設定の人には、時間を流すボタンを出さない。 if (!view.reducedMotion) page.append(tap(el("div", "play", state.timer ? text.stop : text.play), "play", state.timer ? text.stop : text.play)); document.getElementById("before").replaceChildren(page); }; draw(); niho.surface.onChange(draw); niho.ui.onTap("day", ({ data }) => { const { range } = niho.surface.get(); const next = shiftDay(state.date, Number(data["data-step"])); if (next >= range.from && next < range.to) state.date = next; draw(); }); niho.ui.onTap("hour", ({ data }) => { state.hour = Math.max(FIRST_HOUR, Math.min(LAST_HOUR, state.hour + Number(data["data-step"]))); draw(); }); niho.ui.onTap("play", () => { if (state.timer) { clearInterval(state.timer); state.timer = 0; } else state.timer = setInterval(() => { state.hour = state.hour >= LAST_HOUR ? FIRST_HOUR : state.hour + 1; draw(); }, 600); draw(); }); niho.ui.onTap("open", ({ data }) => niho.actions.request({ type: "openBooking", ref: data["data-ref"] })); niho.ui.onTap("start", ({ data }) => niho.actions.request({ type: "startBooking", date: state.date, startHour: state.hour, endHour: state.hour + 1, zoneRef: data["data-zone"] })); await niho.ready(); })();