@akshayanimateddsa/site-ui 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Booking slots, pure and headless: a person's available blocks (one-off or repeating weekly)
3
+ * expanded into dated occurrences, then cut into bookable slots of a given length, minus the times
4
+ * already taken. The booking calendar shows these to a student, and a site's server imports the
5
+ * same functions to check that a requested slot is really open (one rule, one copy).
6
+ */
7
+ import { addDays, dayKeyOf, daysBetween, minuteOfDay, zonedTime } from './dates.js';
8
+ /** Whether two spans share any time (touching ends don't count). */
9
+ export const overlaps = (a, b) => a.start < b.end && b.start < a.end;
10
+ /** Every occurrence of `blocks` that overlaps `range`, in start order. */
11
+ export function expandAvailability(blocks, range, timeZone) {
12
+ const found = [];
13
+ for (const block of blocks) {
14
+ if (block.end <= block.start)
15
+ continue;
16
+ if (!block.repeatWeekly) {
17
+ if (overlaps(block, range))
18
+ found.push({ id: block.id, start: block.start, end: block.end });
19
+ continue;
20
+ }
21
+ const firstDay = dayKeyOf(block.start, timeZone);
22
+ const startMinute = minuteOfDay(block.start, timeZone);
23
+ const length = block.end - block.start;
24
+ // Weeks from the first occurrence to the one just before the range starts (never negative).
25
+ const weeksIn = Math.max(0, Math.floor(daysBetween(firstDay, dayKeyOf(range.start, timeZone)) / 7) - 1);
26
+ for (let week = weeksIn;; week++) {
27
+ const day = addDays(firstDay, week * 7);
28
+ if (block.until && day > block.until)
29
+ break;
30
+ const start = zonedTime(day, startMinute, timeZone);
31
+ if (start >= range.end)
32
+ break;
33
+ const occurrence = { id: block.id, start, end: start + length };
34
+ if (overlaps(occurrence, range))
35
+ found.push(occurrence);
36
+ }
37
+ }
38
+ return found.sort((a, b) => a.start - b.start || a.end - b.end);
39
+ }
40
+ /**
41
+ * The bookable slots: each occurrence cut from its start into back-to-back slots of `minutes`
42
+ * (a remainder too short for a session is dropped), minus slots that overlap a busy time or start
43
+ * before `now`; in start order, each start once.
44
+ */
45
+ export function openSlots(query) {
46
+ const length = query.minutes * 60_000;
47
+ if (!(length > 0))
48
+ return [];
49
+ const starts = new Set();
50
+ const slots = [];
51
+ for (const occurrence of expandAvailability(query.availability, query.range, query.timeZone)) {
52
+ for (let start = occurrence.start; start + length <= occurrence.end; start += length) {
53
+ const slot = { start, end: start + length };
54
+ if (start < query.now || starts.has(start))
55
+ continue;
56
+ if (slot.start < query.range.start || slot.end > query.range.end)
57
+ continue;
58
+ if (query.busy.some((taken) => overlaps(taken, slot)))
59
+ continue;
60
+ starts.add(start);
61
+ slots.push(slot);
62
+ }
63
+ }
64
+ return slots.sort((a, b) => a.start - b.start);
65
+ }
66
+ /** Whether `slot` is exactly one of the open slots (the server's check before taking a booking). */
67
+ export function isOpenSlot(slot, query) {
68
+ if (slot.end - slot.start !== query.minutes * 60_000)
69
+ return false;
70
+ return openSlots({ ...query, range: slot }).some((open) => open.start === slot.start && open.end === slot.end);
71
+ }
@@ -0,0 +1,23 @@
1
+ /** A button at the bottom of a dialog. */
2
+ export interface DialogAction {
3
+ label: string;
4
+ /** primary (filled accent), danger (red) or ghost (plain; the default). */
5
+ kind?: 'primary' | 'danger' | 'ghost';
6
+ /** Runs on click; the dialog closes after it unless it returns false or throws. */
7
+ run?: () => unknown;
8
+ }
9
+ /** What `openDialog` shows. */
10
+ export interface DialogOptions {
11
+ title: string;
12
+ body?: Node | string;
13
+ actions?: DialogAction[];
14
+ /** Called once the dialog has closed, however it closed. */
15
+ onClose?: () => void;
16
+ }
17
+ /** An open dialog: its element, and `close()` to close it from outside. */
18
+ export interface Dialog {
19
+ element: HTMLDialogElement;
20
+ close: () => void;
21
+ }
22
+ /** Opens a modal dialog in `document.body`; it removes itself when closed. */
23
+ export declare function openDialog(options: DialogOptions): Dialog;
package/dist/dialog.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A small modal dialog on the native `<dialog>` element (focus trap, Esc to close, backdrop),
3
+ * styled by css/dialog.css with the same `--ui-*` / `--cal-*` tokens. Each action button may be
4
+ * async: while it runs the buttons are disabled, a thrown error is shown inside the dialog and it
5
+ * stays open; returning `false` also keeps it open. The booking calendar uses it for its forms.
6
+ */
7
+ import { append, el } from './dom.js';
8
+ /** Opens a modal dialog in `document.body`; it removes itself when closed. */
9
+ export function openDialog(options) {
10
+ const error = el('p', { class: 'ui-dialog-error', role: 'alert', hidden: true });
11
+ const buttons = (options.actions ?? [{ label: 'Close' }]).map((action) => {
12
+ const button = el('button', { type: 'button', class: `ui-btn ui-btn--${action.kind ?? 'ghost'}` }, action.label);
13
+ button.addEventListener('click', () => void runAction(action));
14
+ return button;
15
+ });
16
+ const titleId = `ui-dialog-${Math.random().toString(36).slice(2)}`;
17
+ // The padding is on the inner box, so a click on the dialog element itself is a backdrop click.
18
+ const dialog = el('dialog', { class: 'ui-dialog', 'aria-labelledby': titleId }, el('div', { class: 'ui-dialog-box' }, el('h2', { class: 'ui-dialog-title', id: titleId }, options.title), el('div', { class: 'ui-dialog-body' }, options.body ?? null), error, el('div', { class: 'ui-dialog-actions' }, ...buttons)));
19
+ async function runAction(action) {
20
+ error.hidden = true;
21
+ for (const button of buttons)
22
+ button.disabled = true;
23
+ try {
24
+ const result = await action.run?.();
25
+ if (result !== false)
26
+ dialog.close();
27
+ }
28
+ catch (problem) {
29
+ error.textContent = problem instanceof Error ? problem.message : String(problem);
30
+ error.hidden = false;
31
+ }
32
+ finally {
33
+ for (const button of buttons)
34
+ button.disabled = false;
35
+ }
36
+ }
37
+ dialog.addEventListener('close', () => {
38
+ dialog.remove();
39
+ options.onClose?.();
40
+ });
41
+ dialog.addEventListener('click', (event) => {
42
+ if (event.target === dialog)
43
+ dialog.close(); // a click on the backdrop
44
+ });
45
+ append(document.body, dialog);
46
+ dialog.showModal();
47
+ return { element: dialog, close: () => dialog.close() };
48
+ }
package/dist/dom.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The package's own small DOM helper (internal, not exported): builds elements from a tag,
3
+ * properties and children. Text always goes in as text nodes, never as HTML, so event titles,
4
+ * names and notes from a site's data can't inject markup.
5
+ */
6
+ type Child = Node | string | number | false | null | undefined;
7
+ type Props = Record<string, string | number | boolean | EventListener | undefined | null>;
8
+ /**
9
+ * A new element. `class`, `data-*`, `aria-*` and other attributes are set as attributes; `on<event>`
10
+ * keys add listeners; boolean `true` sets an empty attribute and `false` / null / undefined skip it.
11
+ */
12
+ export declare function el<K extends keyof HTMLElementTagNameMap>(tag: K, props?: Props, ...children: Child[]): HTMLElementTagNameMap[K];
13
+ /** Appends children, skipping empty ones; strings and numbers become text nodes. */
14
+ export declare function append(parent: Node, ...children: Child[]): void;
15
+ export {};
package/dist/dom.js ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The package's own small DOM helper (internal, not exported): builds elements from a tag,
3
+ * properties and children. Text always goes in as text nodes, never as HTML, so event titles,
4
+ * names and notes from a site's data can't inject markup.
5
+ */
6
+ /**
7
+ * A new element. `class`, `data-*`, `aria-*` and other attributes are set as attributes; `on<event>`
8
+ * keys add listeners; boolean `true` sets an empty attribute and `false` / null / undefined skip it.
9
+ */
10
+ export function el(tag, props = {}, ...children) {
11
+ const node = document.createElement(tag);
12
+ for (const [key, value] of Object.entries(props)) {
13
+ if (value === undefined || value === null || value === false)
14
+ continue;
15
+ if (typeof value === 'function')
16
+ node.addEventListener(key.slice(2).toLowerCase(), value);
17
+ else
18
+ node.setAttribute(key, value === true ? '' : String(value));
19
+ }
20
+ append(node, ...children);
21
+ return node;
22
+ }
23
+ /** Appends children, skipping empty ones; strings and numbers become text nodes. */
24
+ export function append(parent, ...children) {
25
+ for (const child of children) {
26
+ if (child === false || child === null || child === undefined)
27
+ continue;
28
+ parent.appendChild(typeof child === 'object' ? child : document.createTextNode(String(child)));
29
+ }
30
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @akshayanimateddsa/site-ui: generic site components every site of the workspace can share (the
3
+ * broad package for site UI: a new generic component goes here, not into a new package). Plain
4
+ * DOM, no framework, no runtime dependencies; nothing touches the DOM at import time, and the pure
5
+ * parts (dates, slots, layout) run anywhere, a site's server included. Today: an Outlook-style
6
+ * calendar (Day / Week / Month), a booking calendar on top of it, and a modal dialog. Styles:
7
+ * `css/calendar.css`, `css/dialog.css`; every colour is a `--cal-*` / `--ui-*` token a site sets
8
+ * to its own theme. Every export is listed with one line in `catalogue.json` (checked by a test).
9
+ */
10
+ /** The calendar view: Day, Week and Month, a small month, drag to pick a span. */
11
+ export { createCalendar, CALENDAR_VIEWS, type Calendar, type CalendarOptions, type CalendarRange, type CalendarView, } from './calendar/calendar.js';
12
+ /** The booking calendar: availability, session lengths and requests (admin), open slots (student). */
13
+ export { createBookingCalendar, NOTE_MAX, type BookingCalendar, type BookingCalendarOptions, type BookingData, type BookingKind, type BookingMode, type BookingRequest, type BookingStatus, type NewAvailability, type NewRequest, } from './calendar/booking.js';
14
+ /** Booking slots: availability expanded, cut to a length, minus taken times (also for servers). */
15
+ export { expandAvailability, openSlots, isOpenSlot, overlaps, type Availability, type Interval, type Occurrence, type SlotQuery, } from './calendar/slots.js';
16
+ /** Where events go: the events of a day, and their spans and columns on the time grid. */
17
+ export { eventsOnDay, layoutDay, daySpan, type CalendarEvent, type DaySegment } from './calendar/layout.js';
18
+ /** Calendar date maths on 'YYYY-MM-DD' days and time zones. */
19
+ export { zonedParts, zonedTime, dayKeyOf, minuteOfDay, parseDayKey, addDays, addMonths, weekdayOf, daysBetween, startOfWeek, weekDays, startOfMonth, monthGrid, startOfDay, type DayKey, type ZonedParts, } from './calendar/dates.js';
20
+ /** A modal dialog with async action buttons. */
21
+ export { openDialog, type Dialog, type DialogAction, type DialogOptions } from './dialog.js';
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @akshayanimateddsa/site-ui: generic site components every site of the workspace can share (the
3
+ * broad package for site UI: a new generic component goes here, not into a new package). Plain
4
+ * DOM, no framework, no runtime dependencies; nothing touches the DOM at import time, and the pure
5
+ * parts (dates, slots, layout) run anywhere, a site's server included. Today: an Outlook-style
6
+ * calendar (Day / Week / Month), a booking calendar on top of it, and a modal dialog. Styles:
7
+ * `css/calendar.css`, `css/dialog.css`; every colour is a `--cal-*` / `--ui-*` token a site sets
8
+ * to its own theme. Every export is listed with one line in `catalogue.json` (checked by a test).
9
+ */
10
+ /** The calendar view: Day, Week and Month, a small month, drag to pick a span. */
11
+ export { createCalendar, CALENDAR_VIEWS, } from './calendar/calendar.js';
12
+ /** The booking calendar: availability, session lengths and requests (admin), open slots (student). */
13
+ export { createBookingCalendar, NOTE_MAX, } from './calendar/booking.js';
14
+ /** Booking slots: availability expanded, cut to a length, minus taken times (also for servers). */
15
+ export { expandAvailability, openSlots, isOpenSlot, overlaps, } from './calendar/slots.js';
16
+ /** Where events go: the events of a day, and their spans and columns on the time grid. */
17
+ export { eventsOnDay, layoutDay, daySpan } from './calendar/layout.js';
18
+ /** Calendar date maths on 'YYYY-MM-DD' days and time zones. */
19
+ export { zonedParts, zonedTime, dayKeyOf, minuteOfDay, parseDayKey, addDays, addMonths, weekdayOf, daysBetween, startOfWeek, weekDays, startOfMonth, monthGrid, startOfDay, } from './calendar/dates.js';
20
+ /** A modal dialog with async action buttons. */
21
+ export { openDialog } from './dialog.js';
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@akshayanimateddsa/site-ui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Generic site components: an Outlook-style calendar (day, week, month) with availability and 1:1 session booking, a modal dialog, and the pure date and slot maths behind them. Plain DOM, no framework, themeable with CSS variables.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/Akshay-Gupta-19/root-config.git",
10
+ "directory": "packages/site-ui"
11
+ },
12
+ "keywords": ["calendar", "booking", "scheduling", "dialog", "ui"],
13
+ "sideEffects": ["*.css"],
14
+ "files": ["dist", "css", "catalogue.json"],
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./slots": {
23
+ "types": "./dist/calendar/slots.d.ts",
24
+ "import": "./dist/calendar/slots.js"
25
+ },
26
+ "./css/*": "./css/*",
27
+ "./catalogue.json": "./catalogue.json"
28
+ },
29
+ "engines": {
30
+ "node": ">=20"
31
+ },
32
+ "scripts": {
33
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
34
+ "test": "node --test \"test/*.test.js\"",
35
+ "check": "npm run build && npm test && npm pack --dry-run"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "provenance": true
40
+ }
41
+ }