domma-cms 1.35.0 → 1.37.0
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.
- package/CHANGELOG.md +12 -0
- package/admin/js/api.js +1 -1
- package/admin/js/app.js +2 -2
- package/admin/js/lib/image-editor.js +1 -1
- package/admin/js/lib/media-arrange.js +1 -1
- package/admin/js/lib/media-picker.js +2 -2
- package/admin/js/lib/media-upload.js +1 -1
- package/admin/js/lib/seo-redirects.js +1 -1
- package/admin/js/templates/docs/usage-media.html +6 -0
- package/admin/js/templates/media.html +1 -0
- package/admin/js/views/media.js +3 -3
- package/bin/lib/left-free-tier.js +4 -1
- package/bin/update.js +4 -0
- package/docs/DommaFeatures.md +10 -5
- package/docs/plugin-development.md +2 -2
- package/package.json +1 -1
- package/plugins/docs/tests/plugin.test.js +2 -0
- package/plugins/free-tier.lock.json +2 -50
- package/server/routes/api/media.js +3 -1
- package/server/routes/api/sidebar.js +2 -2
- package/server/services/content.js +26 -2
- package/server/services/hooks.js +27 -4
- package/server/services/plugins.js +9 -3
- package/server/services/renderer.js +3 -1
- package/server/services/siteGitignore.js +1 -1
- package/plugins/booking/CLAUDE.md +0 -204
- package/plugins/booking/admin/css/index.css +0 -192
- package/plugins/booking/admin/lib/details.js +0 -58
- package/plugins/booking/admin/lib/diary-layout.js +0 -158
- package/plugins/booking/admin/lib/drafts.js +0 -139
- package/plugins/booking/admin/lib/email.js +0 -191
- package/plugins/booking/admin/lib/extension-sections.js +0 -160
- package/plugins/booking/admin/lib/kit.js +0 -471
- package/plugins/booking/admin/templates/booking.html +0 -1
- package/plugins/booking/admin/views/booking.js +0 -216
- package/plugins/booking/admin/views/bookings.js +0 -132
- package/plugins/booking/admin/views/diary.js +0 -382
- package/plugins/booking/admin/views/panels.js +0 -603
- package/plugins/booking/admin/views/people.js +0 -318
- package/plugins/booking/admin/views/services.js +0 -264
- package/plugins/booking/admin/views/settings.js +0 -222
- package/plugins/booking/collections/booking-bookables.json +0 -17
- package/plugins/booking/collections/booking-bookings.json +0 -18
- package/plugins/booking/collections/booking-services.json +0 -17
- package/plugins/booking/config.js +0 -36
- package/plugins/booking/docs/guide.md +0 -73
- package/plugins/booking/inject/body.html +0 -1
- package/plugins/booking/lib/call.js +0 -88
- package/plugins/booking/lib/extensions.js +0 -169
- package/plugins/booking/lib/mail.js +0 -113
- package/plugins/booking/lib/store.js +0 -392
- package/plugins/booking/plugin.js +0 -905
- package/plugins/booking/plugin.json +0 -81
- package/plugins/booking/plugin.public.js +0 -175
- package/plugins/booking/public/booking.css +0 -159
- package/plugins/booking/public/booking.js +0 -808
- package/plugins/booking/public/lib/clean.js +0 -141
- package/plugins/booking/public/lib/page-format.js +0 -121
- package/plugins/booking/public/lib/slots.js +0 -247
- package/plugins/booking/public/lib/time.js +0 -131
- package/plugins/booking/templates/booking.html +0 -5
- package/plugins/booking/tests/admin-diary.test.js +0 -81
- package/plugins/booking/tests/admin-drafts.test.js +0 -67
- package/plugins/booking/tests/api.test.js +0 -651
- package/plugins/booking/tests/email.test.js +0 -69
- package/plugins/booking/tests/public.test.js +0 -72
- package/plugins/booking/tests/pure.test.js +0 -189
- package/plugins/booking/tests/seo.test.js +0 -71
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Booking admin - drafts, the pure part (no DOM, no storage; tested in
|
|
3
|
-
* tests/admin-drafts.test.js). kit.js `openPanel({draft})` does the rest.
|
|
4
|
-
*
|
|
5
|
-
* A draft is what a slideover form held when it was left (the screen changed,
|
|
6
|
-
* the tab closed): `{values, at, was, label}`. `was` is the record as the form
|
|
7
|
-
* began - its `updatedAt`, or a `fingerprint()` of it when it has none (the
|
|
8
|
-
* settings) - so the next opening can say the record changed since. All of a
|
|
9
|
-
* person's drafts live in one stored map, keyed `service:<id|new>`,
|
|
10
|
-
* `bookable:<id|new>`, `new-booking`, `note:<booking id>`, `settings`.
|
|
11
|
-
*
|
|
12
|
-
* @module booking/admin/lib/drafts
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/** A draft older than this is not offered back. */
|
|
16
|
-
export const DRAFT_MAX_DAYS = 14;
|
|
17
|
-
|
|
18
|
-
/** JSON with object keys sorted, so equal data is equal text. */
|
|
19
|
-
export function stableStringify(value) {
|
|
20
|
-
if (Array.isArray(value)) return `[${value.map(v => stableStringify(v === undefined ? null : v)).join(',')}]`;
|
|
21
|
-
if (value && typeof value === 'object') {
|
|
22
|
-
return `{${Object.keys(value).sort().filter(k => value[k] !== undefined)
|
|
23
|
-
.map(k => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(',')}}`;
|
|
24
|
-
}
|
|
25
|
-
return JSON.stringify(value ?? null);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** A short, stable fingerprint of some data (FNV-1a over its stable JSON). */
|
|
29
|
-
export function fingerprint(value) {
|
|
30
|
-
const s = stableStringify(value);
|
|
31
|
-
let h = 0x811c9dc5;
|
|
32
|
-
for (let i = 0; i < s.length; i++) {
|
|
33
|
-
h ^= s.charCodeAt(i);
|
|
34
|
-
h = Math.imul(h, 0x01000193) >>> 0;
|
|
35
|
-
}
|
|
36
|
-
return `f${h.toString(36)}${s.length.toString(36)}`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* A stored draft, if it is one a form can use: `values` a plain object, not
|
|
41
|
-
* older than `maxAgeDays`. Anything else (an older build's, a hand-edited
|
|
42
|
-
* store) is not trusted.
|
|
43
|
-
*
|
|
44
|
-
* @returns {{values: object, at: string, was: string, label: string}|null}
|
|
45
|
-
*/
|
|
46
|
-
export function usableDraft(raw, {now = Date.now(), maxAgeDays = DRAFT_MAX_DAYS} = {}) {
|
|
47
|
-
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
|
|
48
|
-
const v = raw.values;
|
|
49
|
-
if (!v || typeof v !== 'object' || Array.isArray(v)) return null;
|
|
50
|
-
const at = Date.parse(raw.at);
|
|
51
|
-
if (!Number.isFinite(at) || now - at > maxAgeDays * 86_400_000) return null;
|
|
52
|
-
return {values: v, at: String(raw.at), was: typeof raw.was === 'string' ? raw.was : '', label: typeof raw.label === 'string' ? raw.label : ''};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* What to do with a stored draft when a form opens.
|
|
57
|
-
*
|
|
58
|
-
* 'none' no draft, or it holds just what the form opened with
|
|
59
|
-
* 'auto' put it back and say so - the record is as it was
|
|
60
|
-
* 'changed' ask: the record was saved by someone (or somewhere) since
|
|
61
|
-
* 'prefilled' ask: the form was opened for something specific (a contact,
|
|
62
|
-
* a time clicked in the Diary), which the draft would replace
|
|
63
|
-
*
|
|
64
|
-
* @param {{draft: object|null, was?: string, opening?: object, prefilled?: boolean}} o
|
|
65
|
-
* `was` - the record as the form opens now ('' for a new one); `opening` -
|
|
66
|
-
* the values the form opened with.
|
|
67
|
-
*/
|
|
68
|
-
export function restorePlan({draft, was = '', opening, prefilled = false}) {
|
|
69
|
-
if (!draft) return 'none';
|
|
70
|
-
if (opening !== undefined && stableStringify(draft.values) === stableStringify(opening)) return 'none';
|
|
71
|
-
if ((draft.was || '') !== (was || '')) return 'changed';
|
|
72
|
-
return prefilled ? 'prefilled' : 'auto';
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** "3 min ago", "yesterday", "2 days ago". */
|
|
76
|
-
export function agoText(at, now = Date.now()) {
|
|
77
|
-
const t = Date.parse(at);
|
|
78
|
-
if (!Number.isFinite(t)) return 'earlier';
|
|
79
|
-
const s = Math.max(0, (now - t) / 1000);
|
|
80
|
-
if (s < 60) return 'just now';
|
|
81
|
-
if (s < 3600) return `${Math.round(s / 60)} min ago`;
|
|
82
|
-
if (s < 86400) return `${Math.round(s / 3600)} h ago`;
|
|
83
|
-
const d = Math.round(s / 86400);
|
|
84
|
-
return d === 1 ? 'yesterday' : `${d} days ago`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* The question (plain text - the caller escapes it) for a plan that asks, or
|
|
89
|
-
* the toast for 'auto'.
|
|
90
|
-
*
|
|
91
|
-
* @param {'auto'|'changed'|'prefilled'|'deleted'} plan
|
|
92
|
-
* @param {{label?: string, at?: string, now?: number, kind?: string}} o - `kind` names the form for 'prefilled' ("a new booking")
|
|
93
|
-
*/
|
|
94
|
-
export function restoreText(plan, {label = '', at = '', now = Date.now(), kind = 'something'} = {}) {
|
|
95
|
-
const to = label ? ` to “${label}”` : '';
|
|
96
|
-
const ago = agoText(at, now);
|
|
97
|
-
if (plan === 'auto') return `Unsaved changes from ${ago} put back${label ? ` (${label})` : ''}.`;
|
|
98
|
-
// The questions put DISCARD on the confirm button: Escape (and leaving) is Cancel, which keeps the work.
|
|
99
|
-
if (plan === 'changed') return `You left unsaved changes${to} ${ago}, and it has been saved since - by someone else, or in another tab. Discard your changes, or keep them over it (check them before saving)?`;
|
|
100
|
-
if (plan === 'deleted') return `You left unsaved changes${to} ${ago}, but it has been deleted since. Discard them, or start a new one from them?`;
|
|
101
|
-
if (plan === 'prefilled') return `You started ${kind}${label ? ` (${label})` : ''} ${ago} and did not finish it. Discard it and start this one, or carry on with it?`;
|
|
102
|
-
return '';
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** The map with `key` set to `draft` (a new object - stored maps are not changed in place). */
|
|
106
|
-
export function draftsWith(map, key, draft) {
|
|
107
|
-
return {...(map && typeof map === 'object' && !Array.isArray(map) ? map : {}), [key]: draft};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** The map without `key`. */
|
|
111
|
-
export function draftsWithout(map, key) {
|
|
112
|
-
const out = {...(map && typeof map === 'object' && !Array.isArray(map) ? map : {})};
|
|
113
|
-
delete out[key];
|
|
114
|
-
return out;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Drafts under `prefix` (e.g. 'service:') whose record is gone - not 'new' and
|
|
119
|
-
* not one of `liveIds`. Unusable drafts are skipped.
|
|
120
|
-
*
|
|
121
|
-
* @returns {{key: string, id: string, draft: object}[]}
|
|
122
|
-
*/
|
|
123
|
-
export function orphanDrafts(map, prefix, liveIds, opts) {
|
|
124
|
-
const live = new Set(liveIds);
|
|
125
|
-
const out = [];
|
|
126
|
-
for (const [key, raw] of Object.entries(map && typeof map === 'object' ? map : {})) {
|
|
127
|
-
if (!key.startsWith(prefix)) continue;
|
|
128
|
-
const id = key.slice(prefix.length);
|
|
129
|
-
if (!id || id === 'new' || live.has(id)) continue;
|
|
130
|
-
const draft = usableDraft(raw, opts);
|
|
131
|
-
if (draft) out.push({key, id, draft});
|
|
132
|
-
}
|
|
133
|
-
return out;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Stored drafts past their age, to drop. */
|
|
137
|
-
export function staleDraftKeys(map, opts) {
|
|
138
|
-
return Object.entries(map && typeof map === 'object' ? map : {}).filter(([, raw]) => !usableDraft(raw, opts)).map(([k]) => k);
|
|
139
|
-
}
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Email someone from this Tool: Email Pro's compose window when the user can
|
|
3
|
-
* send from it, otherwise the device's mail program through `mailto:`.
|
|
4
|
-
*
|
|
5
|
-
* Core makes the choice (`/admin/js/lib/mail-compose.js`, CMS 1.6.61+). It is
|
|
6
|
-
* imported at RUN time, never statically, so this Tool still loads on an older
|
|
7
|
-
* core - where every action keeps the plain `mailto:` it always had.
|
|
8
|
-
*
|
|
9
|
-
* `emailTo()` is for menu actions; `routeMailLinks()` is for a visible
|
|
10
|
-
* `<a href="mailto:…">` - the link stays a link (copy, hover, middle-click all
|
|
11
|
-
* still work) and a plain click goes the same way as the menu action.
|
|
12
|
-
*
|
|
13
|
-
* @module booking/admin/lib/email
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const CORE = '/admin/js/lib/mail-compose.js';
|
|
17
|
-
let core = null;
|
|
18
|
-
|
|
19
|
-
/** The core module, or null on a core without it. Asked once. */
|
|
20
|
-
const loadCore = () => (core ??= import(CORE).catch(() => null));
|
|
21
|
-
|
|
22
|
-
/** Longest subject or body carried (as core's). */
|
|
23
|
-
const MAX_TEXT = 2000;
|
|
24
|
-
/** Longest mailto: handed to the device - mail programs cut or refuse longer ones. */
|
|
25
|
-
const MAX_MAILTO = 1800;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Something that can only be an email address: one @, no spaces or line
|
|
29
|
-
* breaks, nothing that would start another recipient or a query.
|
|
30
|
-
*
|
|
31
|
-
* @param {*} value
|
|
32
|
-
* @returns {boolean}
|
|
33
|
-
*/
|
|
34
|
-
export function looksLikeAddress(value) {
|
|
35
|
-
return typeof value === 'string' && /^[^\s@<>,;"?&]+@[^\s@<>,;"?&]+$/.test(value);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Valid addresses only, once each (ignoring case), from a string, `{address}`/`{email}`, or a list. */
|
|
39
|
-
function addressesOf(list) {
|
|
40
|
-
const seen = new Set();
|
|
41
|
-
return (Array.isArray(list) ? list : list == null ? [] : [list])
|
|
42
|
-
.map(x => String(typeof x === 'string' ? x : (x?.address ?? x?.email ?? '')).trim())
|
|
43
|
-
.filter(a => looksLikeAddress(a) && !seen.has(a.toLowerCase()) && seen.add(a.toLowerCase()));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Trim and cap; a subject loses its line breaks (they would be a header). */
|
|
47
|
-
function cleanText(value, {oneLine = false} = {}) {
|
|
48
|
-
let text = String(value ?? '');
|
|
49
|
-
if (oneLine) text = text.replace(/[\r\n]+/g, ' ');
|
|
50
|
-
return text.slice(0, MAX_TEXT).trim();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The request as a `mailto:` link (the fallback on an older core).
|
|
55
|
-
*
|
|
56
|
-
* @param {{to?: *, cc?: *, bcc?: *, subject?: string, body?: string}} request
|
|
57
|
-
* @returns {string}
|
|
58
|
-
*/
|
|
59
|
-
export function mailtoHref({to, cc, bcc, subject, body} = {}) {
|
|
60
|
-
const enc = (list) => addressesOf(list).map(a => encodeURIComponent(a).replace(/%40/g, '@')).join(',');
|
|
61
|
-
const s = cleanText(subject, {oneLine: true});
|
|
62
|
-
const b = cleanText(body);
|
|
63
|
-
const query = [];
|
|
64
|
-
if (enc(cc)) query.push(`cc=${enc(cc)}`);
|
|
65
|
-
if (enc(bcc)) query.push(`bcc=${enc(bcc)}`);
|
|
66
|
-
if (s) query.push(`subject=${encodeURIComponent(s)}`);
|
|
67
|
-
if (b) query.push(`body=${encodeURIComponent(b)}`);
|
|
68
|
-
return `mailto:${enc(to)}${query.length ? `?${query.join('&')}` : ''}`;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Copy text: the clipboard, else the old copy command, else a box to copy it from by hand. */
|
|
72
|
-
async function copyText(text) {
|
|
73
|
-
try {
|
|
74
|
-
await navigator.clipboard.writeText(text);
|
|
75
|
-
return true;
|
|
76
|
-
} catch { /* next */ }
|
|
77
|
-
const box = document.createElement('textarea');
|
|
78
|
-
box.className = 'form-input';
|
|
79
|
-
box.readOnly = true;
|
|
80
|
-
box.rows = 6;
|
|
81
|
-
box.value = text;
|
|
82
|
-
try {
|
|
83
|
-
box.style.position = 'fixed';
|
|
84
|
-
box.style.opacity = '0';
|
|
85
|
-
document.body.appendChild(box);
|
|
86
|
-
box.select();
|
|
87
|
-
if (document.execCommand('copy')) return true;
|
|
88
|
-
} catch { /* next */ } finally {
|
|
89
|
-
box.remove();
|
|
90
|
-
box.style.position = '';
|
|
91
|
-
box.style.opacity = '';
|
|
92
|
-
}
|
|
93
|
-
const modal = E.modal({title: 'Copy the addresses', size: 'medium', buttons: [{id: 'close', text: 'Close', variant: 'secondary'}]});
|
|
94
|
-
modal.element.appendChild(box); // light DOM: setContent would strip the textarea
|
|
95
|
-
modal.open();
|
|
96
|
-
box.select();
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Hand the request to the device's mail program - unless the link would be
|
|
102
|
-
* too long for it, when the addresses are copied instead and the toast says
|
|
103
|
-
* what did not go.
|
|
104
|
-
*/
|
|
105
|
-
async function openMailto(request) {
|
|
106
|
-
const href = mailtoHref(request);
|
|
107
|
-
if (href.length <= MAX_MAILTO) {
|
|
108
|
-
location.href = href;
|
|
109
|
-
return 'mailto';
|
|
110
|
-
}
|
|
111
|
-
const all = addressesOf([...addressesOf(request.to), ...addressesOf(request.cc), ...addressesOf(request.bcc)]);
|
|
112
|
-
const copied = all.length ? await copyText(all.join(', ')) : false;
|
|
113
|
-
const lost = [cleanText(request.subject) && 'the subject', cleanText(request.body) && 'the message'].filter(Boolean);
|
|
114
|
-
E.toast(`Too long for your mail program to open. ${all.length
|
|
115
|
-
? `${copied ? 'The' : 'Copy the'} ${all.length} address${all.length === 1 ? '' : 'es'}${copied ? ' are copied' : ''} - paste them into Bcc of a new email.` : ''}${lost.length ? ` Not carried over: ${lost.join(' and ')}.` : ''}`,
|
|
116
|
-
{type: 'warning', duration: 9000});
|
|
117
|
-
return 'copied';
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const decode = (s) => { try { return decodeURIComponent(s); } catch { return s; } };
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* A `mailto:` link read back into a request (what a visible link carries).
|
|
124
|
-
* When the address part holds anything that is not an address, the query
|
|
125
|
-
* is ignored: an address stored as `me@x.com?body=…` must not prefill a message.
|
|
126
|
-
*
|
|
127
|
-
* @param {string} href
|
|
128
|
-
* @returns {null|{to: string[], cc: string[], bcc: string[], subject: string, body: string}}
|
|
129
|
-
*/
|
|
130
|
-
export function parseMailto(href) {
|
|
131
|
-
const m = String(href ?? '').match(/^mailto:([^?]*)(?:\?(.*))?$/i);
|
|
132
|
-
if (!m) return null;
|
|
133
|
-
const split = (s) => decode(s || '').split(/[,;]/).map(x => x.trim()).filter(Boolean);
|
|
134
|
-
const named = split(m[1]);
|
|
135
|
-
const out = {to: addressesOf(named), cc: [], bcc: [], subject: '', body: ''};
|
|
136
|
-
if (named.some(a => !looksLikeAddress(a))) return out;
|
|
137
|
-
for (const pair of (m[2] || '').split('&')) {
|
|
138
|
-
const i = pair.indexOf('=');
|
|
139
|
-
if (i < 1) continue;
|
|
140
|
-
const key = pair.slice(0, i).toLowerCase();
|
|
141
|
-
const value = pair.slice(i + 1);
|
|
142
|
-
if (key === 'to') out.to = addressesOf([...out.to, ...split(value)]);
|
|
143
|
-
else if (key === 'cc' || key === 'bcc') out[key] = addressesOf([...out[key], ...split(value)]);
|
|
144
|
-
else if (key === 'subject') out.subject = cleanText(decode(value), {oneLine: true});
|
|
145
|
-
else if (key === 'body') out.body = cleanText(decode(value));
|
|
146
|
-
}
|
|
147
|
-
return out;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Email someone. Recipients are `{name, address}`, `{name, email}` or bare
|
|
152
|
-
* addresses; several people belong in `bcc`, so nobody sees the others.
|
|
153
|
-
* With nobody to address (a link to pass on) compose still opens, To empty.
|
|
154
|
-
*
|
|
155
|
-
* @param {{to?: *, cc?: *, bcc?: *, subject?: string, body?: string}} request
|
|
156
|
-
* @returns {Promise<'compose'|'mailto'|'copied'|'none'>}
|
|
157
|
-
*/
|
|
158
|
-
export async function emailTo(request = {}) {
|
|
159
|
-
const mail = await loadCore();
|
|
160
|
-
if (!mail) return openMailto(request);
|
|
161
|
-
const r = mail.composeRequest(request);
|
|
162
|
-
if (r.to.length || r.cc.length || r.bcc.length) return mail.openEmail(r);
|
|
163
|
-
if (await mail.emailReady()) {
|
|
164
|
-
location.hash = mail.composeHash(r);
|
|
165
|
-
return 'compose';
|
|
166
|
-
}
|
|
167
|
-
location.href = mail.mailtoHref(r);
|
|
168
|
-
return 'mailto';
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Send plain clicks on the `mailto:` links inside `root` through `emailTo()`.
|
|
173
|
-
* A modified click (new tab/window) or one already handled is left alone.
|
|
174
|
-
*
|
|
175
|
-
* @param {Element} root
|
|
176
|
-
* @returns {() => void} removes the listener
|
|
177
|
-
*/
|
|
178
|
-
export function routeMailLinks(root) {
|
|
179
|
-
if (!root) return () => {};
|
|
180
|
-
const onClick = (e) => {
|
|
181
|
-
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
|
182
|
-
const a = e.target?.closest?.('a[href^="mailto:" i]');
|
|
183
|
-
if (!a || !root.contains(a)) return;
|
|
184
|
-
const request = parseMailto(a.getAttribute('href'));
|
|
185
|
-
if (!request) return;
|
|
186
|
-
e.preventDefault();
|
|
187
|
-
emailTo(request);
|
|
188
|
-
};
|
|
189
|
-
root.addEventListener('click', onClick);
|
|
190
|
-
return () => root.removeEventListener('click', onClick);
|
|
191
|
-
}
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sections other Tools add to Booking's own slideovers - the host.
|
|
3
|
-
*
|
|
4
|
-
* THE CONTRACT (Booking 1.1; Booking Pro is the first to use it)
|
|
5
|
-
*
|
|
6
|
-
* GET /api/plugins/booking/bootstrap returns
|
|
7
|
-
* `sections: [{owner, serviceEditor?, newBooking?, bookingPanel?}]`
|
|
8
|
-
* - module paths under /plugins/<owner>/, from what an extension registered
|
|
9
|
-
* (Booking's lib/extensions.js `admin`).
|
|
10
|
-
*
|
|
11
|
-
* Each module exports `async mount(el, ctx)` and returns an object with any of:
|
|
12
|
-
* values() serviceEditor: the owner's settings for the service - saved as
|
|
13
|
-
* `service.ext[owner]`. newBooking: `{answers?, ext?}` - the host sends
|
|
14
|
-
* `body.answers` (merged over every section) and `body.ext[owner]`.
|
|
15
|
-
* validate() '' when all is well, else a message; the host shows it and does not save.
|
|
16
|
-
* after(booking) newBooking only: awaited after the booking is made (a failure is
|
|
17
|
-
* toasted; the booking stays made).
|
|
18
|
-
* dirty() true when the section holds changes (the slideover asks before closing).
|
|
19
|
-
* destroy() called when the slideover closes.
|
|
20
|
-
*
|
|
21
|
-
* ctx = {
|
|
22
|
-
* kind: 'serviceEditor' | 'newBooking' | 'bookingPanel',
|
|
23
|
-
* owner,
|
|
24
|
-
* service, serviceEditor: the service being edited (null when new);
|
|
25
|
-
* newBooking: the service picked so far (null when none)
|
|
26
|
-
* onServiceChange(fn) newBooking: fn(service|null) whenever the pick changes
|
|
27
|
-
* booking, bookingPanel: the booking shown
|
|
28
|
-
* bookables, services, settings,
|
|
29
|
-
* api: {request(method, path, body), get(path), post(path, body), put(path, body), delete(path)}
|
|
30
|
-
* the admin's request helper; paths are under /api - e.g. '/plugins/booking-pro/series'.
|
|
31
|
-
* A failure throws with `status` and `body` (the server's answer).
|
|
32
|
-
* kit, the admin's tool-kit (openMenuAt, onceClosed, helpPopovers, ...)
|
|
33
|
-
* helpPopovers(el), wire the help popovers (data-help="...") inside el (returns the teardown)
|
|
34
|
-
* onChange(), mark the slideover as changed
|
|
35
|
-
* refresh() bookingPanel: reopen the booking with fresh data
|
|
36
|
-
* }
|
|
37
|
-
*
|
|
38
|
-
* The host puts one bordered `<section class="bk-ext" data-owner="...">` per
|
|
39
|
-
* extension after Booking's own fields. A module that fails to load or mount
|
|
40
|
-
* leaves a short muted note - never a broken slideover.
|
|
41
|
-
*
|
|
42
|
-
* @module booking/admin/lib/extension-sections
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
import {loadKit, request} from './kit.js';
|
|
46
|
-
|
|
47
|
-
const say = (el, text) => {
|
|
48
|
-
el.innerHTML = '';
|
|
49
|
-
const p = document.createElement('p');
|
|
50
|
-
p.className = 'bk-note';
|
|
51
|
-
p.textContent = text;
|
|
52
|
-
el.appendChild(p);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const sectionApi = {
|
|
56
|
-
request,
|
|
57
|
-
get: (p) => request('GET', p),
|
|
58
|
-
post: (p, b) => request('POST', p, b ?? {}),
|
|
59
|
-
put: (p, b) => request('PUT', p, b ?? {}),
|
|
60
|
-
delete: (p) => request('DELETE', p)
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Mount every extension's section of one kind into `box`.
|
|
65
|
-
*
|
|
66
|
-
* @param {HTMLElement} box
|
|
67
|
-
* @param {'serviceEditor'|'newBooking'|'bookingPanel'} kind
|
|
68
|
-
* @param {object} opts - {boot, service?, booking?, onChange?, refresh?}
|
|
69
|
-
* @returns {Promise<{ready: Promise<void>, validate(): string, values(): Object<string, object>, answers(): object, extFor(): object,
|
|
70
|
-
* after(booking): Promise<void>, dirty(): boolean, serviceChanged(service): void, destroy(): void}>}
|
|
71
|
-
*/
|
|
72
|
-
export function mountSections(box, kind, {boot, service = null, booking = null, onChange = () => {}, refresh = () => {}}) {
|
|
73
|
-
const mounted = []; // {owner, api}
|
|
74
|
-
const listeners = [];
|
|
75
|
-
let gone = false;
|
|
76
|
-
const entries = (boot?.sections || []).filter(s => s && typeof s[kind] === 'string' && s[kind].startsWith(`/plugins/${s.owner}/`));
|
|
77
|
-
|
|
78
|
-
const ready = (async () => {
|
|
79
|
-
if (!entries.length) return;
|
|
80
|
-
const kit = await loadKit();
|
|
81
|
-
for (const entry of entries) {
|
|
82
|
-
if (gone) return;
|
|
83
|
-
const el = document.createElement('section');
|
|
84
|
-
el.className = 'bk-ext';
|
|
85
|
-
el.dataset.owner = entry.owner;
|
|
86
|
-
box.appendChild(el);
|
|
87
|
-
const ctx = {
|
|
88
|
-
kind, owner: entry.owner, service, booking,
|
|
89
|
-
onServiceChange: (fn) => { if (typeof fn === 'function') listeners.push(fn); },
|
|
90
|
-
bookables: boot.bookables, services: boot.services, settings: boot.settings,
|
|
91
|
-
api: sectionApi, kit, helpPopovers: (el2) => kit.helpPopovers(el2), onChange, refresh
|
|
92
|
-
};
|
|
93
|
-
try {
|
|
94
|
-
const mod = await import(entry[kind]);
|
|
95
|
-
if (gone) return;
|
|
96
|
-
const mount = mod.mount || mod.default?.mount;
|
|
97
|
-
if (typeof mount !== 'function') throw new Error('no mount()');
|
|
98
|
-
const api = (await mount(el, ctx)) || {};
|
|
99
|
-
if (gone) { try { api.destroy?.(); } catch { /* never */ } return; }
|
|
100
|
-
mounted.push({owner: entry.owner, api});
|
|
101
|
-
window.I?.scan?.(el);
|
|
102
|
-
} catch (err) {
|
|
103
|
-
console.warn(`[booking] ${entry.owner} section (${kind}) could not load:`, err);
|
|
104
|
-
say(el, `This part (from ${entry.owner}) could not be loaded just now.`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
})();
|
|
108
|
-
|
|
109
|
-
const call = (m, name, ...args) => { try { return m.api[name]?.(...args); } catch (err) { console.warn(`[booking] ${m.owner}.${name}:`, err); return undefined; } };
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
ready,
|
|
113
|
-
/** The first problem any section reports, or ''. */
|
|
114
|
-
validate() {
|
|
115
|
-
for (const m of mounted) {
|
|
116
|
-
const why = call(m, 'validate');
|
|
117
|
-
if (typeof why === 'string' && why) return why;
|
|
118
|
-
}
|
|
119
|
-
return '';
|
|
120
|
-
},
|
|
121
|
-
/** {owner: values()} for every section that gives any. */
|
|
122
|
-
values() {
|
|
123
|
-
const out = {};
|
|
124
|
-
for (const m of mounted) {
|
|
125
|
-
const v = call(m, 'values');
|
|
126
|
-
if (v && typeof v === 'object') out[m.owner] = v;
|
|
127
|
-
}
|
|
128
|
-
return out;
|
|
129
|
-
},
|
|
130
|
-
/** newBooking: every section's answers, merged. */
|
|
131
|
-
answers() {
|
|
132
|
-
const out = {};
|
|
133
|
-
for (const v of Object.values(this.values())) if (v.answers && typeof v.answers === 'object') Object.assign(out, v.answers);
|
|
134
|
-
return out;
|
|
135
|
-
},
|
|
136
|
-
/** newBooking: {owner: ext} for body.ext. */
|
|
137
|
-
extFor() {
|
|
138
|
-
const out = {};
|
|
139
|
-
for (const [owner, v] of Object.entries(this.values())) if (v.ext && typeof v.ext === 'object') out[owner] = v.ext;
|
|
140
|
-
return out;
|
|
141
|
-
},
|
|
142
|
-
async after(b) {
|
|
143
|
-
for (const m of mounted) {
|
|
144
|
-
if (typeof m.api.after !== 'function') continue;
|
|
145
|
-
try { await m.api.after(b); } catch (err) {
|
|
146
|
-
E.toast(`Booked, but ${m.owner} could not finish: ${err?.message || err}`, {type: 'warning', duration: 8000});
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
dirty: () => mounted.some(m => call(m, 'dirty') === true),
|
|
151
|
-
serviceChanged(svc) {
|
|
152
|
-
for (const fn of listeners) { try { fn(svc || null); } catch (err) { console.warn('[booking] onServiceChange:', err); } }
|
|
153
|
-
},
|
|
154
|
-
destroy() {
|
|
155
|
-
gone = true;
|
|
156
|
-
for (const m of mounted) call(m, 'destroy');
|
|
157
|
-
mounted.length = 0;
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
}
|