(async () => { // ポスターのような月の予定。写真は「photo」という名前で登録した画像で、無くても地の色と箱の大きさは同じ。 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 yen = (amount) => "¥" + String(amount).replace(/\B(?=(\d{3})+$)/g, ","); const feeOf = (event, text) => event.fee === null ? "" : event.fee === 0 ? text.free : yen(event.fee); 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: { empty: "この月の予定は、まだありません", more: (count) => "ほか " + count + " 件", previous: "前の月", next: "次の月", book: "場所をとる", free: "無料", joined: "参加します", full: "満席", closed: "受付終了", left: (count) => "あと" + count + "席" }, en: { empty: "Nothing planned this month yet", more: (count) => count + " more", previous: "Previous month", next: "Next month", book: "Book a space", free: "Free", joined: "Joined", full: "Full", closed: "Closed", left: (count) => count + " left" }, }; const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; const WEEK = { ja: ["日", "月", "火", "水", "木", "金", "土"], en: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] }; const MAX_ROWS = 24; const style = document.createElement("style"); style.textContent = [ ".poster{padding-bottom:8px;background:#f4ede3;color:#26231f}", ".head{display:flex;align-items:center;gap:8px;padding:12px 16px}", ".month{flex:1 1 0;min-width:0}", ".kicker{max-height:1.4em;overflow:hidden;font-size:13px;font-weight:700;line-height:1.4;color:#a3243a;overflow-wrap:anywhere}", ".name{margin:0;font-size:32px;font-weight:800;line-height:1.2;color:#c22f3e}", ".turn{width:48px;height:48px;border:2px solid #26231f;border-radius:24px;font-size:18px;font-weight:800;line-height:44px;text-align:center}", ".photo{position:relative;height:148px;margin:0 16px;overflow:hidden;border-radius:12px;background:#c22f3e}", ".lantern{position:absolute;top:0;width:28px;height:44px}", ".sway{animation:sway .9s ease-in-out 3 both}", "@keyframes sway{from{transform:rotate(-4deg)}to{transform:rotate(4deg)}}", ".row{display:grid;grid-template-columns:60px minmax(0,1fr);gap:12px;align-items:center;min-height:64px;margin:0 16px;padding:10px 0;border-bottom:2px solid #26231f}", ".day{font-size:30px;font-weight:800;line-height:1;text-align:center}", ".day small{display:block;margin-top:4px;font-size:12px}", ".title{max-height:2.8em;overflow:hidden;font-size:17px;font-weight:800;line-height:1.4;overflow-wrap:anywhere}", ".meta{margin-top:4px;font-size:13px;overflow-wrap:anywhere}", ".mark{display:inline-block;margin-top:4px;padding:2px 8px;border-radius:10px;background:#c22f3e;color:#ffffff;font-size:12px;font-weight:800}", ".note{margin:0;padding:20px 16px;font-size:15px}", ".book{min-height:52px;margin:12px 16px 20px;padding:14px;border-radius:12px;background:#26231f;color:#f4ede3;font-size:16px;font-weight:800;text-align:center}", ].concat([0, 1, 2, 3, 4].map((index) => ".at" + index + "{left:" + (6 + index * 20) + "%}")).join(""); document.head.append(style); const daysBetween = (from, to) => (Date.parse(to + "T00:00:00Z") - Date.parse(from + "T00:00:00Z")) / 86400000; // いま見せる月。「月を移す」の後の期間は、その月の最初の週から 42 日。開いたときは今日の月を見せる。 const monthOf = ({ range, today }) => daysBetween(range.from, range.to) === 42 ? new Date(Date.parse(range.from + "T00:00:00Z") + 7 * 86400000).toISOString().slice(0, 7) : today.slice(0, 7); const shift = (month, step) => { const date = new Date(month + "-01T00:00:00Z"); date.setUTCMonth(date.getUTCMonth() + step); return date.toISOString().slice(0, 7); }; const lantern = (index) => svg("svg", { class: "lantern sway at" + index, viewBox: "0 0 40 60", "aria-hidden": "true" }, svg("path", { d: "M20 0v14", stroke: "#26231f", "stroke-width": "2" }), svg("ellipse", { cx: "20", cy: "34", rx: "17", ry: "20", fill: index % 2 ? "#f7b0a4" : "#ffe2c9" }), svg("path", { d: "M5 28h30M3 34h34M5 40h30", stroke: "#c22f3e", "stroke-width": "1.5" })); const draw = () => { const view = niho.surface.get(); const text = TEXT[view.language] || TEXT.ja; const week = WEEK[view.language] || WEEK.ja; const month = monthOf(view); const number = Number(month.slice(5, 7)); const turn = (name, label, sign) => tap(el("div", "turn", sign), "month", label, { month: name }); const photo = el("div", "photo", ...[0, 1, 2, 3, 4].map(lantern)); photo.dataset.asset = "photo"; photo.dataset.assetFit = "cover"; const page = el("section", "poster", el("header", "head", el("div", "month", el("div", "kicker", view.spot.name), el("h1", "name", view.language === "en" ? MONTHS[number - 1] : number + "月")), turn(shift(month, -1), text.previous, "←"), turn(shift(month, 1), text.next, "→")), photo); const events = view.events.filter((event) => event.startAt.slice(0, 7) === month && event.endAt.slice(0, 10) >= view.today); for (const event of events.slice(0, MAX_ROWS)) { const weekday = week[new Date(event.startAt.slice(0, 10) + "T00:00:00Z").getUTCDay()]; const state = stateOf(event, text); const body = el("div", "", el("div", "title", event.title), el("div", "meta", join(event.startAt.slice(11, 16), event.places.slice(0, 2).join("・"), feeOf(event, text)))); if (state !== "") body.append(el("span", "mark", state)); const row = el("div", "row", el("div", "day", String(Number(event.startAt.slice(8, 10))), el("small", "", weekday)), body); page.append(tap(row, "open", dayOf(event.startAt) + " " + event.title, { ref: event.ref })); } if (events.length === 0) page.append(el("p", "note", text.empty)); if (events.length > MAX_ROWS) page.append(el("p", "note", text.more(events.length - MAX_ROWS))); document.getElementById("before").replaceChildren(page); document.getElementById("after").replaceChildren(...(view.viewer.canBook ? [tap(el("div", "book", text.book), "book", text.book)] : [])); }; draw(); niho.surface.onChange(draw); niho.ui.onTap("open", ({ data }) => niho.actions.request({ type: "openBooking", ref: data["data-ref"] })); niho.ui.onTap("month", ({ data }) => niho.actions.request({ type: "showMonth", month: data["data-month"] })); niho.ui.onTap("book", () => niho.actions.request({ type: "startBooking", date: niho.surface.get().today })); await niho.ready(); })();