@gramlot/gramlot 0.2.20 → 0.3.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.
- package/dist/gramlot.js +1343 -303
- package/dist/gramlot.min.js +363 -24
- package/package.json +6 -3
- package/src/binding/ask-dialog.js +2 -2
- package/src/binding/inline.js +3 -10
- package/src/binding/installation.js +2 -1
- package/src/binding/pointer.js +12 -148
- package/src/binding/providers.js +2 -1
- package/src/binding/runtime.js +2 -1
- package/src/bootstrap.d.ts +2 -0
- package/src/bootstrap.js +9 -2
- package/src/builder/gramlot-builder.js +21 -4
- package/src/builder/source.js +11 -10
- package/src/components/bases.js +182 -0
- package/src/components/buttons.js +254 -0
- package/src/components/choice-editor.js +221 -0
- package/src/components/choices.js +13 -0
- package/src/components/date-calendar.js +139 -0
- package/src/components/field-state.js +166 -0
- package/src/components/formlet-labels.js +80 -0
- package/src/components/index.js +5 -0
- package/src/components/inputs.js +196 -0
- package/src/components/labelbox.js +74 -0
- package/src/components/layout.js +190 -0
- package/src/components/parameters.js +6 -0
- package/src/components/popup.js +22 -0
- package/src/components/registry.js +83 -0
- package/src/components/typed-editor.js +260 -0
- package/src/components/validation.js +138 -0
- package/src/format.js +13 -6
- package/src/gramlot.d.ts +5 -1
- package/src/gramlot.js +8 -1
- package/src/handlers/dom-handler.d.ts +1 -0
- package/src/handlers/dom-handler.js +7 -0
- package/src/index-page-runtime.js +78 -30
- package/src/index-page-styles.js +2 -2
- package/src/index-theme.js +1 -1
- package/src/renderer/attributes.js +3 -40
- package/src/renderer/gramlot-html-renderer.js +4 -3
- package/src/renderer/gramlot-renderer.js +62 -4
- package/src/server/gramlot-server.d.ts +3 -0
- package/src/server/gramlot-server.js +10 -3
- package/src/server/index-page.d.ts +1 -0
- package/src/server/index-page.js +8 -3
- package/src/server/page-base.js +1 -0
- package/src/server/page.d.ts +1 -0
- package/src/server/resources.d.ts +2 -0
- package/src/view/button.js +17 -13
- package/src/view/controls.js +152 -4
- package/src/view/html.js +18 -0
- package/themes/gramlot-base/README.md +6 -0
- package/themes/gramlot-base/theme.css +34 -6
- package/themes/gramlot-compact/README.md +6 -0
- package/themes/gramlot-compact/theme.css +8 -4
- package/themes/joanna/README.md +23 -0
- package/themes/joanna/theme.css +89 -0
- package/themes/mimi/README.md +23 -0
- package/themes/mimi/theme.css +89 -0
- package/tools/gramlot-tools-inspector.js +56 -23
- package/tools/inspector-agent.js +8 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Local choices share the same code:caption grammar in editors and groups. */
|
|
2
|
+
export function parseChoices(values) {
|
|
3
|
+
if (values === '') return [];
|
|
4
|
+
const ids = new Set();
|
|
5
|
+
return values.split(',').map(item => {
|
|
6
|
+
const colon = item.indexOf(':');
|
|
7
|
+
const id = colon < 0 ? item : item.slice(0, colon);
|
|
8
|
+
const caption = colon < 0 ? item : item.slice(colon + 1);
|
|
9
|
+
if (!id || ids.has(id)) throw new Error('Invalid or duplicate choice code');
|
|
10
|
+
ids.add(id);
|
|
11
|
+
return {id, caption};
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/** Civil-date calendar shared by the inline control and the text editor popup. */
|
|
2
|
+
import {controlElement} from './bases.js';
|
|
3
|
+
|
|
4
|
+
export function civilDate(year, month, day) {
|
|
5
|
+
const value = new Date(0); value.setUTCFullYear(year, month, day); return value;
|
|
6
|
+
}
|
|
7
|
+
export function checkCivilDate(value) {
|
|
8
|
+
if (value !== null && (!(value instanceof Date) || !Number.isFinite(value.getTime()) ||
|
|
9
|
+
value.getUTCHours() || value.getUTCMinutes() || value.getUTCSeconds() || value.getUTCMilliseconds())) {
|
|
10
|
+
throw new TypeError('Expected a civil Date at UTC midnight');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const equal = (a, b) => a === b || a instanceof Date && b instanceof Date && a.getTime() === b.getTime();
|
|
14
|
+
const iso = value => value.toISOString().slice(0, 10);
|
|
15
|
+
|
|
16
|
+
export class CalendarView {
|
|
17
|
+
constructor(container, select) {
|
|
18
|
+
this.container = container; this.select = select; this.value = null;
|
|
19
|
+
const doc = container.ownerDocument;
|
|
20
|
+
const style = doc.createElement('style');
|
|
21
|
+
style.textContent = `.calendar{width:var(--gramlot-calendar-width,18em);max-width:100%;font:inherit;color:inherit}
|
|
22
|
+
.calendar button{min-height:var(--gramlot-touch-target,0px);font:inherit;color:inherit;border:1px solid transparent;border-radius:3px;background:transparent;cursor:pointer;padding:.25em}
|
|
23
|
+
.calendar button:focus-visible{outline:2px solid var(--gramlot-focus-ring,#4369c5);outline-offset:-2px}
|
|
24
|
+
.calendar button:disabled{opacity:.5;cursor:default}
|
|
25
|
+
.calendar-header,.calendar-footer{display:flex;align-items:center;justify-content:space-between;gap:.4em}
|
|
26
|
+
.calendar-month{font-weight:600;text-align:center}
|
|
27
|
+
.calendar-grid{display:grid;grid-template-columns:repeat(7,minmax(0,1fr));gap:2px;margin:.4em 0}
|
|
28
|
+
.calendar-weekday{text-align:center;font-size:.85em;color:var(--gramlot-text-secondary,#64748b)}
|
|
29
|
+
.calendar button[aria-pressed=true]{background:var(--gramlot-brand-blue,#4369c5);color:white}
|
|
30
|
+
.calendar button[data-today=true]{border-color:var(--gramlot-brand-blue,#4369c5)}
|
|
31
|
+
.calendar button:hover:not(:disabled){background:var(--gramlot-selected,#e5ebf7);color:var(--gramlot-ink,#203047)}`;
|
|
32
|
+
container.classList.add('calendar');
|
|
33
|
+
this.header = doc.createElement('div'); this.header.className = 'calendar-header';
|
|
34
|
+
this.month = doc.createElement('span'); this.month.className = 'calendar-month'; this.month.setAttribute('aria-live', 'polite');
|
|
35
|
+
this.previous = this.button('‹', 'Previous month', () => this.moveMonth(-1));
|
|
36
|
+
this.next = this.button('›', 'Next month', () => this.moveMonth(1));
|
|
37
|
+
this.header.append(this.previous, this.month, this.next);
|
|
38
|
+
this.grid = doc.createElement('div'); this.grid.className = 'calendar-grid';
|
|
39
|
+
this.footer = doc.createElement('div'); this.footer.className = 'calendar-footer';
|
|
40
|
+
this.todayButton = this.button('Today', 'Today', () => this.choose(this.today));
|
|
41
|
+
this.clearButton = this.button('Clear', 'Clear date', () => this.choose(null));
|
|
42
|
+
this.footer.append(this.todayButton, this.clearButton);
|
|
43
|
+
container.append(style, this.header, this.grid, this.footer);
|
|
44
|
+
this.grid.addEventListener('keydown', event => {
|
|
45
|
+
if (this.locked || !event.target.dataset.date) return;
|
|
46
|
+
const offsets = {ArrowLeft:-1, ArrowRight:1, ArrowUp:-7, ArrowDown:7};
|
|
47
|
+
if (event.key in offsets) {
|
|
48
|
+
event.preventDefault(); this.active = civilDate(this.active.getUTCFullYear(), this.active.getUTCMonth(), this.active.getUTCDate() + offsets[event.key]);
|
|
49
|
+
} else if (event.key === 'Home' || event.key === 'End') {
|
|
50
|
+
event.preventDefault(); const weekday = (this.active.getUTCDay() + 6) % 7;
|
|
51
|
+
this.active = civilDate(this.active.getUTCFullYear(), this.active.getUTCMonth(), this.active.getUTCDate() + (event.key === 'Home' ? -weekday : 6 - weekday));
|
|
52
|
+
} else if (event.key === 'PageUp' || event.key === 'PageDown') {
|
|
53
|
+
event.preventDefault(); this.moveMonth(event.key === 'PageUp' ? -1 : 1, true); return;
|
|
54
|
+
} else return;
|
|
55
|
+
this.render(); this.focusDay();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
button(text, label, action) {
|
|
59
|
+
const button = this.container.ownerDocument.createElement('button'); button.type = 'button';
|
|
60
|
+
button.textContent = text; button.setAttribute('aria-label', label);
|
|
61
|
+
button.addEventListener('click', action); return button;
|
|
62
|
+
}
|
|
63
|
+
configure({value, locale, workdate, locked = false}) {
|
|
64
|
+
checkCivilDate(value);
|
|
65
|
+
const now = new Date();
|
|
66
|
+
this.today = workdate ? civilDate(...workdate.split('-').map((part, i) => Number(part) - (i === 1 ? 1 : 0)))
|
|
67
|
+
: civilDate(now.getFullYear(), now.getMonth(), now.getDate());
|
|
68
|
+
if (!this.active || !equal(this.value, value)) this.active = value ?? this.today;
|
|
69
|
+
this.value = value; this.locale = locale; this.locked = locked;
|
|
70
|
+
const focused = this.grid.contains(this.container.getRootNode().activeElement);
|
|
71
|
+
this.render(); if (focused && !locked) this.focusDay();
|
|
72
|
+
}
|
|
73
|
+
choose(value) {
|
|
74
|
+
if (this.locked) return;
|
|
75
|
+
this.value = value; this.active = value ?? this.today; this.render(); this.focusDay(); this.select(value);
|
|
76
|
+
}
|
|
77
|
+
moveMonth(delta, focus = false) {
|
|
78
|
+
if (this.locked) return;
|
|
79
|
+
const y = this.active.getUTCFullYear(), m = this.active.getUTCMonth() + delta;
|
|
80
|
+
this.active = civilDate(y, m, Math.min(this.active.getUTCDate(), civilDate(y, m + 1, 0).getUTCDate()));
|
|
81
|
+
this.render(); if (focus) this.focusDay();
|
|
82
|
+
}
|
|
83
|
+
render() {
|
|
84
|
+
const y = this.active.getUTCFullYear(), m = this.active.getUTCMonth();
|
|
85
|
+
this.month.textContent = new Intl.DateTimeFormat(this.locale, {month:'long',year:'numeric',timeZone:'UTC'}).format(this.active);
|
|
86
|
+
this.grid.replaceChildren();
|
|
87
|
+
const weekday = new Intl.DateTimeFormat(this.locale, {weekday:'short',timeZone:'UTC'});
|
|
88
|
+
for (let i = 0; i < 7; i++) {
|
|
89
|
+
const caption = this.container.ownerDocument.createElement('span'); caption.className = 'calendar-weekday';
|
|
90
|
+
caption.textContent = weekday.format(civilDate(2024, 0, 1 + i)); this.grid.append(caption);
|
|
91
|
+
}
|
|
92
|
+
const offset = (civilDate(y,m,1).getUTCDay() + 6) % 7;
|
|
93
|
+
for (let i = 0; i < offset; i++) this.grid.append(this.container.ownerDocument.createElement('span'));
|
|
94
|
+
const full = new Intl.DateTimeFormat(this.locale, {dateStyle:'full',timeZone:'UTC'});
|
|
95
|
+
for (let day = 1; day <= civilDate(y,m+1,0).getUTCDate(); day++) {
|
|
96
|
+
const value = civilDate(y,m,day), button = this.button(String(day), full.format(value), () => this.choose(value));
|
|
97
|
+
button.dataset.date = iso(value); button.dataset.today = String(equal(value,this.today));
|
|
98
|
+
button.setAttribute('aria-pressed', String(equal(value,this.value))); button.disabled = this.locked;
|
|
99
|
+
button.tabIndex = day === this.active.getUTCDate() ? 0 : -1; this.grid.append(button);
|
|
100
|
+
}
|
|
101
|
+
for (const button of [this.previous,this.next,this.todayButton,this.clearButton]) button.disabled = this.locked;
|
|
102
|
+
}
|
|
103
|
+
focusDay() { this.grid.querySelector('button[tabindex="0"]')?.focus(); }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function CalendarControl(HTMLElement) {
|
|
107
|
+
return class extends controlElement(HTMLElement) {
|
|
108
|
+
#value = null;
|
|
109
|
+
constructor() {
|
|
110
|
+
super(); this.control = this.ownerDocument.createElement('div'); this.control.id = 'control';
|
|
111
|
+
this.control.setAttribute('role','group'); this.controlBox.append(this.control);
|
|
112
|
+
this.calendarView = new CalendarView(this.control, value => {
|
|
113
|
+
this.#value = value;
|
|
114
|
+
this.control.dispatchEvent(new this.ownerDocument.defaultView.Event('change', {bubbles:true}));
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
get commitOnChange() { return true; }
|
|
118
|
+
readControlValue() { return this.#value; }
|
|
119
|
+
sameControlValue(a,b) { return equal(a,b); }
|
|
120
|
+
writeControlValue(value) { checkCivilDate(value); this.#value = value; this.syncCalendar(); }
|
|
121
|
+
projectAttributes(attrs, adapt) {
|
|
122
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
123
|
+
this.control.disabled = Boolean(this.parameters.disabled); this.control.readOnly = Boolean(this.parameters.readonly);
|
|
124
|
+
this.syncCalendar(); return rest;
|
|
125
|
+
}
|
|
126
|
+
syncCalendar() {
|
|
127
|
+
if (!this.pageContext || !this.parameters) return;
|
|
128
|
+
this.calendarView.configure({value:this.#value, locale:this.parameters.locale ?? this.pageContext.locale,
|
|
129
|
+
workdate:this.parameters.workdate, locked:this.control.disabled || this.control.readOnly});
|
|
130
|
+
}
|
|
131
|
+
focus() { this.calendarView.focusDay(); }
|
|
132
|
+
releaseControl() { this.calendarView.locked = true; }
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
CalendarControl.parameters = {
|
|
136
|
+
value:{dtype:'D',nullable:true,binding:'twoWay'},
|
|
137
|
+
disabled:{dtype:'B',default:false,binding:'read'}, readonly:{dtype:'B',default:false,binding:'read'},
|
|
138
|
+
locale:{dtype:'T',nullable:true,binding:'read'}, workdate:{dtype:'T',nullable:true,binding:'read'}
|
|
139
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {validateInput, validationParameters} from './validation.js';
|
|
2
|
+
import {positionPopup} from './popup.js';
|
|
3
|
+
|
|
4
|
+
const MESSAGE_DURATION = 1800;
|
|
5
|
+
const MESSAGE_FADE_DURATION = 200;
|
|
6
|
+
|
|
7
|
+
/** Shared candidate validation and presentation; ControlAdapter alone writes Data. */
|
|
8
|
+
export function FieldState(Base) {
|
|
9
|
+
return class extends Base {
|
|
10
|
+
#rejected = false;
|
|
11
|
+
#projected;
|
|
12
|
+
#messageTimer;
|
|
13
|
+
#messageEvents;
|
|
14
|
+
#validation;
|
|
15
|
+
#validationEpoch = 0;
|
|
16
|
+
#validationCurrent;
|
|
17
|
+
#rules = {};
|
|
18
|
+
validationResult = null;
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.message = this.ownerDocument.createElement('small');
|
|
22
|
+
this.message.id = 'field-message'; this.message.hidden = true;
|
|
23
|
+
this.message.setAttribute('aria-live', 'polite');
|
|
24
|
+
this.message.setAttribute('popover', 'manual');
|
|
25
|
+
this.message.setAttribute('role', 'tooltip');
|
|
26
|
+
const style = this.ownerDocument.createElement('style');
|
|
27
|
+
style.textContent = `#control[aria-invalid="true"]{background:var(--gramlot-error-background,#fff1f0)}
|
|
28
|
+
input#control[aria-invalid="true"],textarea#control[aria-invalid="true"]{color:var(--gramlot-error,#8c1d18)}
|
|
29
|
+
#field-message{position:fixed;inset:auto;margin:0;padding:.4em .65em;border:1px solid var(--gramlot-error,#b3261e);border-radius:var(--gramlot-radius-sm,4px);
|
|
30
|
+
color:var(--gramlot-error,#8c1d18);background:var(--gramlot-error-background,#fff1f0);font:inherit;font-size:.85em;box-shadow:var(--gramlot-shadow,0 2px 8px #0003);
|
|
31
|
+
max-width:min(28em,var(--gramlot-popup-available-width,100vw));max-height:var(--gramlot-popup-available-height,100vh);overflow:auto;overflow-wrap:anywhere;opacity:1;transition:opacity ${MESSAGE_FADE_DURATION}ms ease-out}
|
|
32
|
+
#field-message[data-severity="warning"]{color:var(--gramlot-warning,#805600);border-color:var(--gramlot-warning,#805600);background:var(--gramlot-warning-background,#fff8df)}
|
|
33
|
+
#field-message[data-fading]{opacity:0}
|
|
34
|
+
@media(prefers-reduced-motion:reduce){#field-message{transition:none}}`;
|
|
35
|
+
this.shadowRoot.append(style);
|
|
36
|
+
this.control.setAttribute('aria-describedby', this.message.id);
|
|
37
|
+
this.controlBox.append(this.message);
|
|
38
|
+
this.control.addEventListener('input', () => this.cancelValidation());
|
|
39
|
+
this.control.addEventListener('compositionstart', () => this.cancelValidation());
|
|
40
|
+
this.control.addEventListener('focusin', () => this.#showMessage());
|
|
41
|
+
this.control.addEventListener('mouseenter', () => this.#showMessage());
|
|
42
|
+
this.control.addEventListener('keydown', event => {
|
|
43
|
+
if (event.key === 'Escape') { this.cancelValidation(); this.#hideMessage(); }
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
#hideMessage() {
|
|
47
|
+
this.ownerDocument.defaultView.clearTimeout(this.#messageTimer);
|
|
48
|
+
this.#messageEvents?.abort(); this.#messageEvents = null;
|
|
49
|
+
if (!this.message.hidden) this.message.hidePopover();
|
|
50
|
+
this.message.hidden = true;
|
|
51
|
+
this.message.removeAttribute('data-fading');
|
|
52
|
+
}
|
|
53
|
+
#showMessage() {
|
|
54
|
+
if (!this.message.textContent || !this.isConnected) return;
|
|
55
|
+
this.#hideMessage();
|
|
56
|
+
const view = this.ownerDocument.defaultView;
|
|
57
|
+
this.message.hidden = false;
|
|
58
|
+
this.message.showPopover();
|
|
59
|
+
positionPopup(this.message, this.control);
|
|
60
|
+
this.#messageTimer = view.setTimeout(() => {
|
|
61
|
+
if (this.message.hidden) return;
|
|
62
|
+
this.message.setAttribute('data-fading', '');
|
|
63
|
+
this.#messageTimer = view.setTimeout(() => this.#hideMessage(), MESSAGE_FADE_DURATION);
|
|
64
|
+
}, MESSAGE_DURATION);
|
|
65
|
+
this.#messageEvents = new view.AbortController();
|
|
66
|
+
const options = {capture:true, signal:this.#messageEvents.signal};
|
|
67
|
+
view.addEventListener('scroll', () => this.#hideMessage(), options);
|
|
68
|
+
view.addEventListener('resize', () => this.#hideMessage(), options);
|
|
69
|
+
}
|
|
70
|
+
releaseControl() { this.cancelValidation(); this.#hideMessage(); super.releaseControl(); }
|
|
71
|
+
disconnectedCallback() { this.releaseControl(); }
|
|
72
|
+
setFieldState({invalid = false, pending = false} = {}) {
|
|
73
|
+
this.control.setAttribute('aria-invalid', String(invalid));
|
|
74
|
+
this.control.setAttribute('aria-busy', String(pending));
|
|
75
|
+
}
|
|
76
|
+
error(message) {
|
|
77
|
+
this.#rejected = false;
|
|
78
|
+
if (['input', 'textarea'].includes(this.control.localName)) this.control.setCustomValidity(message);
|
|
79
|
+
this.control.setAttribute('aria-invalid', String(Boolean(message)));
|
|
80
|
+
this.message.textContent = message;
|
|
81
|
+
if (message) this.#showMessage(); else this.#hideMessage();
|
|
82
|
+
}
|
|
83
|
+
cancelValidation() {
|
|
84
|
+
this.#validationEpoch++;
|
|
85
|
+
this.#validation?.abort(); this.#validation = null; this.#validationCurrent = null;
|
|
86
|
+
this.control.setAttribute('aria-busy', 'false');
|
|
87
|
+
}
|
|
88
|
+
validateCandidate(value, accept) {
|
|
89
|
+
this.cancelValidation();
|
|
90
|
+
const controller = new AbortController(); this.#validation = controller;
|
|
91
|
+
const epoch = this.#validationEpoch;
|
|
92
|
+
const rules = this.#rules;
|
|
93
|
+
const current = this.pageContext.validationCurrent(); this.#validationCurrent = current;
|
|
94
|
+
const valid = () => this.#validationEpoch === epoch && !controller.signal.aborted && current();
|
|
95
|
+
const failure = error => ({value, modified:false, error:error.message, warnings:[], issues:[], datachanges:[]});
|
|
96
|
+
const finish = result => {
|
|
97
|
+
if (!valid()) {
|
|
98
|
+
if (this.#validation === controller) this.cancelValidation();
|
|
99
|
+
return {ok:false};
|
|
100
|
+
}
|
|
101
|
+
this.#validation = null; this.#validationCurrent = null;
|
|
102
|
+
this.control.setAttribute('aria-busy', 'false');
|
|
103
|
+
try { if (!result.error) accept(result.value); }
|
|
104
|
+
catch (error) { result = failure(error); }
|
|
105
|
+
this.validationResult = result;
|
|
106
|
+
this.error(result.error || ''); this.message.dataset.severity = result.error ? 'error' : 'warning'; this.#rejected = Boolean(result.error);
|
|
107
|
+
if (!result.error && result.warnings.length) {
|
|
108
|
+
this.message.textContent = result.warnings.join('\n'); this.#showMessage();
|
|
109
|
+
}
|
|
110
|
+
const outcome = {ok:!result.error, value:result.value, result, rules, isCurrent:valid};
|
|
111
|
+
if (!outcome.ok) this.finishValidation(outcome);
|
|
112
|
+
return outcome;
|
|
113
|
+
};
|
|
114
|
+
let result;
|
|
115
|
+
try {
|
|
116
|
+
if (rules.validate_timeout != null && (!Number.isInteger(rules.validate_timeout) || rules.validate_timeout <= 0)) throw new TypeError('validate_timeout must be a positive integer in milliseconds.');
|
|
117
|
+
result = validateInput(value, rules, {...this.pageContext, signal:controller.signal, selection:value => this.validateSelection(value)});
|
|
118
|
+
}
|
|
119
|
+
catch (error) { return finish(failure(error)); }
|
|
120
|
+
if (!(result instanceof Promise)) return finish(result);
|
|
121
|
+
this.control.setAttribute('aria-busy', 'true');
|
|
122
|
+
return new Promise(resolve => {
|
|
123
|
+
let timer;
|
|
124
|
+
const abort = () => { clearTimeout(timer); resolve({ok:false}); };
|
|
125
|
+
controller.signal.addEventListener('abort', abort, {once:true});
|
|
126
|
+
const settle = answer => {
|
|
127
|
+
clearTimeout(timer); controller.signal.removeEventListener('abort', abort);
|
|
128
|
+
resolve(finish(answer));
|
|
129
|
+
};
|
|
130
|
+
if (rules.validate_timeout != null) timer = setTimeout(() => {
|
|
131
|
+
settle(failure(new Error('Validation timed out.'))); controller.abort();
|
|
132
|
+
}, rules.validate_timeout);
|
|
133
|
+
result.then(settle, error => settle(failure(error)));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
finishValidation(outcome) {
|
|
137
|
+
if (!outcome.result) return;
|
|
138
|
+
this.pageContext.finishValidation(outcome);
|
|
139
|
+
}
|
|
140
|
+
shouldProjectControlValue(value) {
|
|
141
|
+
if (this.#validationCurrent && !this.#validationCurrent()) this.cancelValidation();
|
|
142
|
+
const same = this.sameControlValue(value, this.#projected);
|
|
143
|
+
if (same && this.#rejected) return false;
|
|
144
|
+
if (!same) this.cancelValidation();
|
|
145
|
+
this.#projected = value;
|
|
146
|
+
if (!same && this.#rejected) this.error('');
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
projectAttributes(attrs, adapt) {
|
|
150
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
151
|
+
const rules = Object.fromEntries(Object.entries(attrs).filter(([name]) => name.startsWith('validate_')));
|
|
152
|
+
const keys = new Set([...Object.keys(rules), ...Object.keys(this.#rules)]);
|
|
153
|
+
if ([...keys].some(key => !Object.is(rules[key], this.#rules[key])) || attrs.disabled || attrs.readonly) this.cancelValidation();
|
|
154
|
+
this.#rules = rules;
|
|
155
|
+
for (const key of Object.keys(rules)) delete rest[key];
|
|
156
|
+
const required = Boolean(this.parameters.validate_notnull);
|
|
157
|
+
this.control.setAttribute('aria-required', String(required));
|
|
158
|
+
// required on a checkbox would mean "must be true", not "must be present".
|
|
159
|
+
if (this.control.localName === 'textarea' || this.control.localName === 'input' && ['text', 'password'].includes(this.control.type)) {
|
|
160
|
+
this.control.required = required;
|
|
161
|
+
}
|
|
162
|
+
return rest;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
FieldState.parameters = validationParameters;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** Intrinsic label columns for a formlet's directly slotted fields. */
|
|
2
|
+
export class FormletLabels {
|
|
3
|
+
#fields = new Set();
|
|
4
|
+
#observer;
|
|
5
|
+
#frame;
|
|
6
|
+
constructor(formlet) {
|
|
7
|
+
this.formlet = formlet;
|
|
8
|
+
this.window = formlet.ownerDocument.defaultView;
|
|
9
|
+
this.slot = formlet.control.querySelector('slot');
|
|
10
|
+
}
|
|
11
|
+
connect() {
|
|
12
|
+
this.#observer = new this.window.ResizeObserver(this.schedule);
|
|
13
|
+
this.#observer.observe(this.formlet.control);
|
|
14
|
+
this.slot.addEventListener('slotchange', this.schedule);
|
|
15
|
+
this.formlet.addEventListener('gramlot-label-layout', this.schedule);
|
|
16
|
+
this.formlet.ownerDocument.fonts.addEventListener('loadingdone', this.schedule);
|
|
17
|
+
this.schedule();
|
|
18
|
+
}
|
|
19
|
+
disconnect() {
|
|
20
|
+
this.#observer.disconnect();
|
|
21
|
+
this.window.cancelAnimationFrame(this.#frame);
|
|
22
|
+
this.#frame = undefined;
|
|
23
|
+
this.slot.removeEventListener('slotchange', this.schedule);
|
|
24
|
+
this.formlet.removeEventListener('gramlot-label-layout', this.schedule);
|
|
25
|
+
this.formlet.ownerDocument.fonts.removeEventListener('loadingdone', this.schedule);
|
|
26
|
+
for (const field of this.#fields) this.#clear(field);
|
|
27
|
+
this.#fields.clear();
|
|
28
|
+
}
|
|
29
|
+
schedule = () => {
|
|
30
|
+
if (!this.formlet.isConnected || this.#frame !== undefined) return;
|
|
31
|
+
this.#frame = this.window.requestAnimationFrame(() => {
|
|
32
|
+
this.#frame = undefined;
|
|
33
|
+
this.#align();
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
#clear(field) {
|
|
37
|
+
delete field.box.dataset.formletAligned;
|
|
38
|
+
field.box.style.removeProperty('--gramlot-formlet-label-width');
|
|
39
|
+
}
|
|
40
|
+
#align() {
|
|
41
|
+
// Labelbox is the capability; nested formlets own their own fields.
|
|
42
|
+
const fields = new Set(this.slot.assignedElements().filter(field =>
|
|
43
|
+
'fieldLabel' in field && field.box.dataset.position === 'L'));
|
|
44
|
+
for (const field of this.#fields) if (!fields.has(field)) {
|
|
45
|
+
this.#observer.unobserve(field.fieldLabel);
|
|
46
|
+
this.#clear(field);
|
|
47
|
+
}
|
|
48
|
+
for (const field of fields) {
|
|
49
|
+
field.box.dataset.formletAligned = '';
|
|
50
|
+
if (!this.#fields.has(field)) this.#observer.observe(field.fieldLabel);
|
|
51
|
+
}
|
|
52
|
+
this.#fields = fields;
|
|
53
|
+
const columns = new Map();
|
|
54
|
+
for (const field of fields) {
|
|
55
|
+
if (!field.getClientRects().length) continue;
|
|
56
|
+
const host = field.getBoundingClientRect();
|
|
57
|
+
const label = field.labelBox.getBoundingClientRect();
|
|
58
|
+
const style = this.window.getComputedStyle(field.labelBox);
|
|
59
|
+
const boxStyle = this.window.getComputedStyle(field.box);
|
|
60
|
+
const gap = parseFloat(boxStyle.columnGap) || 0;
|
|
61
|
+
const labelLeft = field.labelBox.hidden
|
|
62
|
+
? field.box.getBoundingClientRect().left + parseFloat(boxStyle.paddingLeft) + parseFloat(boxStyle.borderLeftWidth)
|
|
63
|
+
: label.left;
|
|
64
|
+
const inset = labelLeft - host.left + gap;
|
|
65
|
+
const width = field.box.style.getPropertyValue('--gramlot-label-width') ? label.width :
|
|
66
|
+
field.fieldLabel.getBoundingClientRect().width +
|
|
67
|
+
parseFloat(style.paddingLeft) + parseFloat(style.paddingRight) +
|
|
68
|
+
parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);
|
|
69
|
+
const column = columns.get(host.left) ?? [];
|
|
70
|
+
column.push({field, inset, width});
|
|
71
|
+
columns.set(host.left, column);
|
|
72
|
+
}
|
|
73
|
+
for (const column of columns.values()) {
|
|
74
|
+
const edge = Math.max(...column.map(({inset, width}) => inset + width));
|
|
75
|
+
for (const {field, inset} of column) {
|
|
76
|
+
field.box.style.setProperty('--gramlot-formlet-label-width', `${edge - inset}px`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Component authoring primitives. Importing this module never registers DOM tags. */
|
|
2
|
+
export {defineComponent, defineCollection, ComponentCollection} from './registry.js';
|
|
3
|
+
export {NativeControl, ChoiceGroup} from './bases.js';
|
|
4
|
+
export {Labelbox} from './labelbox.js';
|
|
5
|
+
export {FieldState} from './field-state.js';
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {defineComponent, defineCollection} from './registry.js';
|
|
2
|
+
import {NativeControl, ChoiceGroup} from './bases.js';
|
|
3
|
+
import {Labelbox} from './labelbox.js';
|
|
4
|
+
import {TypedEditor, numberEditor, dateEditor, timeEditor} from './typed-editor.js';
|
|
5
|
+
import {CalendarControl} from './date-calendar.js';
|
|
6
|
+
import {FieldState} from './field-state.js';
|
|
7
|
+
import {ChoiceEditor, localChoices, callbackChoices} from './choice-editor.js';
|
|
8
|
+
|
|
9
|
+
export const Textbox = defineComponent({
|
|
10
|
+
name: 'textbox', tag: 'gramlot-textbox', base: NativeControl, mixins: [Labelbox, FieldState],
|
|
11
|
+
parameters: {
|
|
12
|
+
value: {dtype: 'T', nullable: true, binding: 'twoWay'},
|
|
13
|
+
placeholder: {dtype: 'T', qualification: 'intl_str', default: '', target: 'control'}
|
|
14
|
+
},
|
|
15
|
+
implementation: textEditor('input', 'text')
|
|
16
|
+
});
|
|
17
|
+
export const Checkbox = defineComponent({
|
|
18
|
+
name: 'checkbox', tag: 'gramlot-checkbox', base: NativeControl, mixins: [Labelbox, FieldState],
|
|
19
|
+
parameters: {
|
|
20
|
+
value: {dtype: 'B', binding: 'twoWay'},
|
|
21
|
+
mode: {dtype: 'T', default: 'checkbox', binding: 'read', enum: ['checkbox', 'switch']},
|
|
22
|
+
switch_color: {dtype: 'T', nullable: true, default: null, binding: 'read'},
|
|
23
|
+
label: {dtype: 'T', qualification: 'intl_str', default: '', target: 'optionCaption'}
|
|
24
|
+
},
|
|
25
|
+
implementation: Base => class extends Base {
|
|
26
|
+
createControl() { const input = this.ownerDocument.createElement('input'); input.type = 'checkbox'; return input; }
|
|
27
|
+
constructor() {
|
|
28
|
+
super();
|
|
29
|
+
const style = this.ownerDocument.createElement('style');
|
|
30
|
+
style.textContent = `input[role=switch]{appearance:none;position:relative;flex:none;cursor:pointer;
|
|
31
|
+
width:var(--gramlot-switch-width,2.5em);height:var(--gramlot-switch-height,1.4em);min-width:0;min-height:0;
|
|
32
|
+
margin:0;border:1px solid var(--gramlot-border-control,#888);border-radius:2em;
|
|
33
|
+
background:var(--gramlot-switch-off,var(--gramlot-field-readonly-background,#bbb));transition:background 120ms}
|
|
34
|
+
input[role=switch]::before{content:'';position:absolute;top:var(--gramlot-switch-inset,2px);bottom:var(--gramlot-switch-inset,2px);left:var(--gramlot-switch-inset,2px);width:calc(var(--gramlot-switch-height,1.4em) - 2 * var(--gramlot-switch-inset,2px) - 2px);
|
|
35
|
+
border-radius:50%;background:var(--gramlot-switch-thumb,#fff);box-shadow:0 1px 2px #0004;transition:left 120ms}
|
|
36
|
+
input[role=switch]:checked{background:var(--gramlot-switch-on,var(--gramlot-brand-blue,Highlight))}
|
|
37
|
+
input[role=switch]:checked::before{left:calc(100% - var(--gramlot-switch-height,1.4em) + var(--gramlot-switch-inset,2px) + 2px)}
|
|
38
|
+
input[role=switch]:disabled{cursor:not-allowed}
|
|
39
|
+
@media(prefers-reduced-motion:reduce){input[role=switch],input[role=switch]::before{transition:none}}`;
|
|
40
|
+
this.shadowRoot.append(style);
|
|
41
|
+
this.control.addEventListener('click', event => { if (this.control.readOnly) event.preventDefault(); });
|
|
42
|
+
}
|
|
43
|
+
projectAttributes(attrs, adapt) {
|
|
44
|
+
const rest = super.projectAttributes(attrs, adapt);
|
|
45
|
+
const mode = this.parameters.mode;
|
|
46
|
+
if (!this.constructor.gramlotComponent.parameters.mode.enum.includes(mode)) throw new TypeError('checkbox mode must be checkbox or switch');
|
|
47
|
+
if (mode === 'switch') this.control.setAttribute('role', 'switch');
|
|
48
|
+
else this.control.removeAttribute('role');
|
|
49
|
+
if (mode === 'switch' && this.parameters.switch_color != null) {
|
|
50
|
+
this.control.style.setProperty('--gramlot-switch-on', this.parameters.switch_color);
|
|
51
|
+
} else this.control.style.removeProperty('--gramlot-switch-on');
|
|
52
|
+
return rest;
|
|
53
|
+
}
|
|
54
|
+
readControlValue() { return this.control.checked; }
|
|
55
|
+
writeControlValue(value) { this.control.checked = value; }
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
export const RadioButtonText = defineComponent({
|
|
59
|
+
name: 'radioButtonText', tag: 'gramlot-radio-button-text', base: ChoiceGroup, mixins: [Labelbox, FieldState],
|
|
60
|
+
parameters: {
|
|
61
|
+
value: {dtype: 'T', nullable: true, binding: 'twoWay'},
|
|
62
|
+
values: {dtype: 'T', binding: 'read'},
|
|
63
|
+
cols: {dtype: 'L', default: 1, binding: 'read'}
|
|
64
|
+
},
|
|
65
|
+
implementation: Base => class extends Base {
|
|
66
|
+
get choiceType() { return 'radio'; }
|
|
67
|
+
get multiple() { return false; }
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
// Text variants keep the same editing contract, including IME and live/commit.
|
|
71
|
+
function textEditor(tag, type) {
|
|
72
|
+
return Base => class extends Base {
|
|
73
|
+
createControl() {
|
|
74
|
+
const control = this.ownerDocument.createElement(tag);
|
|
75
|
+
if (tag === 'input') control.type = type;
|
|
76
|
+
return control;
|
|
77
|
+
}
|
|
78
|
+
readControlValue() { return this.control.value; }
|
|
79
|
+
writeControlValue(value) { if (this.control.value !== (value ?? '')) this.control.value = value ?? ''; }
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export const TextBoxArea = defineComponent({
|
|
83
|
+
name: 'textBoxArea', tag: 'gramlot-text-box-area', base: NativeControl, mixins: [Labelbox, FieldState],
|
|
84
|
+
parameters: {
|
|
85
|
+
value: {dtype: 'T', nullable: true, binding: 'twoWay'},
|
|
86
|
+
placeholder: {dtype: 'T', qualification: 'intl_str', default: '', target: 'control'},
|
|
87
|
+
rows: {dtype: 'L', default: 4, binding: 'read', target: 'control'},
|
|
88
|
+
cols: {dtype: 'L', default: 24, binding: 'read', target: 'control'}
|
|
89
|
+
},
|
|
90
|
+
implementation: textEditor('textarea')
|
|
91
|
+
});
|
|
92
|
+
export const Passwordbox = defineComponent({
|
|
93
|
+
name: 'passwordbox', tag: 'gramlot-passwordbox', base: NativeControl, mixins: [Labelbox, FieldState],
|
|
94
|
+
parameters: {
|
|
95
|
+
value: {dtype: 'T', nullable: true, binding: 'twoWay'},
|
|
96
|
+
placeholder: {dtype: 'T', qualification: 'intl_str', default: '', target: 'control'}
|
|
97
|
+
},
|
|
98
|
+
implementation: textEditor('input', 'password')
|
|
99
|
+
});
|
|
100
|
+
export const RadioButton = defineComponent({
|
|
101
|
+
name: 'radioButton', tag: 'gramlot-radio-button', base: NativeControl, mixins: [Labelbox, FieldState],
|
|
102
|
+
parameters: {
|
|
103
|
+
value: {dtype: 'B', default: false, binding: 'twoWay'},
|
|
104
|
+
label: {dtype: 'T', qualification: 'intl_str', default: '', target: 'optionCaption'},
|
|
105
|
+
group: {dtype: 'T', nullable: true, binding: 'read'}
|
|
106
|
+
},
|
|
107
|
+
implementation: Base => class extends Base {
|
|
108
|
+
createControl() { const input = this.ownerDocument.createElement('input'); input.type = 'radio'; return input; }
|
|
109
|
+
readControlValue() { return this.control.checked; }
|
|
110
|
+
writeControlValue(value) { this.control.checked = value; }
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
export const Colorpicker = defineComponent({
|
|
114
|
+
name: 'colorpicker', tag: 'gramlot-colorpicker', base: NativeControl, mixins: [Labelbox, FieldState],
|
|
115
|
+
parameters: {value: {dtype: 'T', default: '#000000', binding: 'twoWay'}},
|
|
116
|
+
implementation: textEditor('input', 'color')
|
|
117
|
+
});
|
|
118
|
+
const rangeParameters = {
|
|
119
|
+
value: {dtype: 'R', default: 0, binding: 'twoWay'},
|
|
120
|
+
min: {dtype: 'R', default: 0, binding: 'read', target: 'control'},
|
|
121
|
+
max: {dtype: 'R', default: 100, binding: 'read', target: 'control'},
|
|
122
|
+
step: {dtype: 'R', default: 1, binding: 'read', target: 'control'}
|
|
123
|
+
};
|
|
124
|
+
function rangeEditor(vertical) {
|
|
125
|
+
return Base => class extends Base {
|
|
126
|
+
createControl() {
|
|
127
|
+
const control = this.ownerDocument.createElement('input');
|
|
128
|
+
control.type = 'range';
|
|
129
|
+
if (vertical) {
|
|
130
|
+
control.style.cssText = 'writing-mode:vertical-lr;direction:rtl;width:var(--gramlot-slider-width,1.5rem);height:var(--gramlot-slider-height,10rem)';
|
|
131
|
+
control.setAttribute('aria-orientation', 'vertical');
|
|
132
|
+
} else control.style.width = '100%';
|
|
133
|
+
return control;
|
|
134
|
+
}
|
|
135
|
+
readControlValue() { return this.control.valueAsNumber; }
|
|
136
|
+
writeControlValue(value) { if (this.control.valueAsNumber !== value) this.control.value = String(value); }
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export const HorizontalSlider = defineComponent({
|
|
140
|
+
name: 'horizontalSlider', tag: 'gramlot-horizontal-slider', base: NativeControl,
|
|
141
|
+
mixins: [Labelbox, FieldState], parameters: rangeParameters, implementation: rangeEditor(false)
|
|
142
|
+
});
|
|
143
|
+
export const VerticalSlider = defineComponent({
|
|
144
|
+
name: 'verticalSlider', tag: 'gramlot-vertical-slider', base: NativeControl,
|
|
145
|
+
mixins: [Labelbox, FieldState], parameters: rangeParameters, implementation: rangeEditor(true)
|
|
146
|
+
});
|
|
147
|
+
export const NumberTextBox = defineComponent({
|
|
148
|
+
name: 'numberTextBox', tag: 'gramlot-number-text-box', base: TypedEditor, mixins: [Labelbox, FieldState],
|
|
149
|
+
parameters: {value: {dtype: ['L', 'R', 'N'], nullable: true, binding: 'twoWay'},
|
|
150
|
+
dtype: {dtype: 'T', default: 'R'}, min: {dtype: 'R', nullable: true, binding: 'read'},
|
|
151
|
+
max: {dtype: 'R', nullable: true, binding: 'read'}}, implementation: numberEditor
|
|
152
|
+
});
|
|
153
|
+
export const DateTextBox = defineComponent({
|
|
154
|
+
name: 'dateTextBox', tag: 'gramlot-date-text-box', base: TypedEditor, mixins: [Labelbox, FieldState],
|
|
155
|
+
parameters: {value: {dtype: 'D', nullable: true, binding: 'twoWay'},
|
|
156
|
+
workdate: {dtype: 'T', nullable: true, binding: 'read'},
|
|
157
|
+
calendar: {dtype: 'B', default: true, binding: 'read'}}, implementation: dateEditor
|
|
158
|
+
});
|
|
159
|
+
export const DateCalendar = defineComponent({
|
|
160
|
+
name:'dateCalendar', tag:'gramlot-date-calendar', base:CalendarControl, mixins:[Labelbox, FieldState],
|
|
161
|
+
parameters:{}, implementation:Base => class extends Base {}
|
|
162
|
+
});
|
|
163
|
+
export const TimeTextBox = defineComponent({
|
|
164
|
+
name: 'timeTextBox', tag: 'gramlot-time-text-box', base: TypedEditor, mixins: [Labelbox, FieldState],
|
|
165
|
+
parameters: {value: {dtype: 'H', nullable: true, binding: 'twoWay'}}, implementation: timeEditor
|
|
166
|
+
});
|
|
167
|
+
export const FilteringSelect = defineComponent({
|
|
168
|
+
name: 'filteringSelect', tag: 'gramlot-filtering-select', base: ChoiceEditor, mixins: [Labelbox, FieldState],
|
|
169
|
+
parameters: {values: {dtype: 'T', default: '', binding: 'read'}}, implementation: localChoices
|
|
170
|
+
});
|
|
171
|
+
export const CallbackSelect = defineComponent({
|
|
172
|
+
name: 'callbackSelect', tag: 'gramlot-callback-select', base: ChoiceEditor, mixins: [Labelbox, FieldState],
|
|
173
|
+
parameters: {callback: {dtype: 'T'}}, implementation: callbackChoices
|
|
174
|
+
});
|
|
175
|
+
export const ComboBox = defineComponent({
|
|
176
|
+
name: 'comboBox', tag: 'gramlot-combo-box', base: ChoiceEditor, mixins: [Labelbox, FieldState],
|
|
177
|
+
parameters: {values: {dtype: 'T', default: '', binding: 'read'}},
|
|
178
|
+
implementation: Base => class extends localChoices(Base) {
|
|
179
|
+
get constrained() { return false; }
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
export const CheckBoxText = defineComponent({
|
|
183
|
+
name: 'checkBoxText', tag: 'gramlot-check-box-text', base: ChoiceGroup, mixins: [Labelbox, FieldState],
|
|
184
|
+
parameters: {
|
|
185
|
+
value: {dtype: 'T', nullable: true, binding: 'twoWay'},
|
|
186
|
+
values: {dtype: 'T', binding: 'read'},
|
|
187
|
+
cols: {dtype: 'L', default: 1, binding: 'read'}
|
|
188
|
+
},
|
|
189
|
+
implementation: Base => class extends Base {
|
|
190
|
+
get choiceType() { return 'checkbox'; }
|
|
191
|
+
get multiple() { return true; }
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
export default defineCollection({name: 'inputs', components: [Textbox, Checkbox, RadioButtonText,
|
|
195
|
+
TextBoxArea, Passwordbox, RadioButton, Colorpicker, HorizontalSlider, VerticalSlider,
|
|
196
|
+
NumberTextBox, DateTextBox, TimeTextBox, FilteringSelect, CallbackSelect, ComboBox, CheckBoxText, DateCalendar]});
|