nbhi 0.6.17 → 1.0.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/README.md +385 -201
- package/javascript/component.js +184 -0
- package/javascript/index.js +56 -80
- package/javascript/render.js +141 -0
- package/package.json +18 -10
- package/javascript/attributes.js +0 -133
- package/javascript/element.js +0 -11
- package/javascript/onceVisible.js +0 -11
- package/javascript/update.js +0 -220
- package/javascript/validate.js +0 -39
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { collect, apply, toAttribute, toProperty } from './render.js';
|
|
2
|
+
|
|
3
|
+
export { define };
|
|
4
|
+
|
|
5
|
+
const states = new WeakMap();
|
|
6
|
+
const fieldProperties = ['value', 'checked'];
|
|
7
|
+
const formMembers = ['form', 'validity', 'validationMessage', 'willValidate', 'checkValidity', 'reportValidity'];
|
|
8
|
+
const validityFlags = [
|
|
9
|
+
'valueMissing', 'typeMismatch', 'patternMismatch', 'tooLong', 'tooShort',
|
|
10
|
+
'rangeUnderflow', 'rangeOverflow', 'stepMismatch', 'badInput', 'customError',
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
function define({ name, template, script, styleSheets, observer }) {
|
|
14
|
+
const { bindings, field } = collect([...template.content.children]);
|
|
15
|
+
const isFormControl = Boolean(field);
|
|
16
|
+
const bound = [...new Set(bindings.map(binding => binding.name))];
|
|
17
|
+
|
|
18
|
+
bound.forEach(property => {
|
|
19
|
+
if (property in HTMLElement.prototype || property === 'refs' || (isFormControl && formMembers.includes(property))) {
|
|
20
|
+
throw new Error(`<${ name }> can't bind "${ property }": every element already has a ${ property } property, use another name`);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const properties = [...new Set([...bound, ...(isFormControl ? fieldProperties : [])])];
|
|
25
|
+
|
|
26
|
+
class Component extends HTMLElement {
|
|
27
|
+
static observedAttributes = properties.map(toAttribute);
|
|
28
|
+
static formAssociated = isFormControl;
|
|
29
|
+
|
|
30
|
+
constructor() {
|
|
31
|
+
super();
|
|
32
|
+
|
|
33
|
+
const shadow = this.attachShadow({ mode: 'open', delegatesFocus: isFormControl });
|
|
34
|
+
shadow.adoptedStyleSheets = styleSheets;
|
|
35
|
+
shadow.append(document.importNode(template.content, true));
|
|
36
|
+
observer.observe(shadow, { childList: true, subtree: true });
|
|
37
|
+
|
|
38
|
+
const found = collect([...shadow.children]);
|
|
39
|
+
const state = { values: new Map(), bindings: found.bindings, field: found.field, started: false };
|
|
40
|
+
states.set(this, state);
|
|
41
|
+
this.refs = found.refs;
|
|
42
|
+
found.bindings
|
|
43
|
+
.filter(({ kind }) => kind === 'each')
|
|
44
|
+
.forEach(binding => apply(binding, []));
|
|
45
|
+
|
|
46
|
+
if (isFormControl) {
|
|
47
|
+
state.internals = this.attachInternals();
|
|
48
|
+
state.field.addEventListener('input', () => sync(this));
|
|
49
|
+
state.field.addEventListener('change', () => {
|
|
50
|
+
sync(this);
|
|
51
|
+
this.dispatchEvent(new Event('change', { bubbles: true }));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// A property set before the component was defined is an own property of
|
|
56
|
+
// the element that hides the accessor, so it is moved to the accessor
|
|
57
|
+
properties.forEach(property => {
|
|
58
|
+
if (Object.hasOwn(this, property)) {
|
|
59
|
+
const value = this[property];
|
|
60
|
+
delete this[property];
|
|
61
|
+
this[property] = value;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
connectedCallback() {
|
|
67
|
+
const state = states.get(this);
|
|
68
|
+
if (state.started) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
state.started = true;
|
|
72
|
+
if (isFormControl) {
|
|
73
|
+
state.initial = { value: state.field.value, checked: state.field.checked };
|
|
74
|
+
sync(this);
|
|
75
|
+
}
|
|
76
|
+
if (typeof script === 'function') {
|
|
77
|
+
script(this);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
attributeChangedCallback(attribute, oldValue, newValue) {
|
|
82
|
+
if (oldValue !== newValue) {
|
|
83
|
+
this[toProperty(attribute)] = newValue;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
formResetCallback() {
|
|
88
|
+
const { field, initial } = states.get(this);
|
|
89
|
+
if (initial) {
|
|
90
|
+
field.value = initial.value;
|
|
91
|
+
field.checked = initial.checked;
|
|
92
|
+
sync(this);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
formDisabledCallback(disabled) {
|
|
97
|
+
states.get(this).field.disabled = disabled;
|
|
98
|
+
sync(this);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
properties.forEach(property => {
|
|
103
|
+
Object.defineProperty(Component.prototype, property, {
|
|
104
|
+
configurable: true,
|
|
105
|
+
get() {
|
|
106
|
+
const { values, field } = states.get(this);
|
|
107
|
+
return isFormControl && fieldProperties.includes(property) ? field[property] : values.get(property);
|
|
108
|
+
},
|
|
109
|
+
set(value) {
|
|
110
|
+
write(this, property, value);
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (isFormControl) {
|
|
116
|
+
const internals = element => states.get(element).internals;
|
|
117
|
+
Object.defineProperties(Component.prototype, {
|
|
118
|
+
form: { get() { return internals(this).form; } },
|
|
119
|
+
validity: { get() { return internals(this).validity; } },
|
|
120
|
+
validationMessage: { get() { return internals(this).validationMessage; } },
|
|
121
|
+
willValidate: { get() { return internals(this).willValidate; } },
|
|
122
|
+
checkValidity: { value() { return internals(this).checkValidity(); } },
|
|
123
|
+
reportValidity: { value() { return internals(this).reportValidity(); } },
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
customElements.define(name, Component);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function write(element, property, value) {
|
|
131
|
+
const state = states.get(element);
|
|
132
|
+
state.values.set(property, value);
|
|
133
|
+
const own = state.bindings.filter(binding => binding.name === property);
|
|
134
|
+
|
|
135
|
+
own.forEach(binding => {
|
|
136
|
+
if (binding.kind === 'each' && binding.element.localName === 'select') {
|
|
137
|
+
renderOptions(binding);
|
|
138
|
+
} else {
|
|
139
|
+
apply(binding, value);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
if (state.field) {
|
|
144
|
+
if (fieldProperties.includes(property) && !own.length) {
|
|
145
|
+
state.field[property] = property === 'value' ? value ?? '' : value !== false && value !== null && value !== undefined;
|
|
146
|
+
}
|
|
147
|
+
sync(element);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Rendering options resets the selected one: keep what the user picked when
|
|
151
|
+
// it is still there, or else select the value that was set for it
|
|
152
|
+
function renderOptions(binding) {
|
|
153
|
+
const select = binding.element;
|
|
154
|
+
const previous = select.value;
|
|
155
|
+
apply(binding, value);
|
|
156
|
+
if ([...select.options].some(option => option.value === previous) && previous !== '') {
|
|
157
|
+
select.value = previous;
|
|
158
|
+
} else {
|
|
159
|
+
state.bindings
|
|
160
|
+
.filter(other => other.element === select && other.kind === 'attribute')
|
|
161
|
+
.forEach(other => apply(other, state.values.get(other.name)));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function sync(element) {
|
|
167
|
+
const { field, internals } = states.get(element);
|
|
168
|
+
if (field.type === 'checkbox' || field.type === 'radio') {
|
|
169
|
+
internals.setFormValue(field.checked ? field.value : null);
|
|
170
|
+
} else if (field.localName === 'select' && field.multiple) {
|
|
171
|
+
const data = new FormData();
|
|
172
|
+
[...field.selectedOptions].forEach(option => data.append(element.getAttribute('name') ?? '', option.value));
|
|
173
|
+
internals.setFormValue(data);
|
|
174
|
+
} else {
|
|
175
|
+
internals.setFormValue(field.value);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (field.validity.valid) {
|
|
179
|
+
internals.setValidity({});
|
|
180
|
+
} else {
|
|
181
|
+
const flags = Object.fromEntries(validityFlags.map(flag => [flag, field.validity[flag]]));
|
|
182
|
+
internals.setValidity(flags, field.validationMessage, field);
|
|
183
|
+
}
|
|
184
|
+
}
|
package/javascript/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import validate from './validate.js';
|
|
4
|
-
import onceVisible from './onceVisible.js';
|
|
1
|
+
import { define } from './component.js';
|
|
2
|
+
import { rows } from './render.js';
|
|
5
3
|
|
|
6
|
-
export { initialize,
|
|
4
|
+
export { initialize, rows };
|
|
7
5
|
|
|
8
|
-
async function initialize({ prefix = '
|
|
6
|
+
async function initialize({ prefix = 'nb', source = '/components' } = {}) {
|
|
9
7
|
const registerCache = new Set();
|
|
10
8
|
const fileCache = new Map();
|
|
11
9
|
|
|
@@ -13,33 +11,38 @@ async function initialize({ prefix = 'db', source = '/components' } = {}) {
|
|
|
13
11
|
throw new Error('prefix option needs to be a string of at least one character');
|
|
14
12
|
}
|
|
15
13
|
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
const styleSheets = [...getInheritedStylesheets(), getHiddenStylesheet()];
|
|
15
|
+
const body = document.querySelector('body');
|
|
16
|
+
const observer = new MutationObserver(loadAdded);
|
|
17
|
+
inPage(body);
|
|
18
|
+
observer.observe(body, { childList: true, subtree: true });
|
|
19
|
+
await fromFile(body);
|
|
19
20
|
|
|
20
|
-
function inPage(root
|
|
21
|
+
function inPage(root) {
|
|
21
22
|
root.querySelectorAll('template').forEach(template => {
|
|
22
|
-
if (
|
|
23
|
-
template.
|
|
23
|
+
if (template.id.startsWith(`${ prefix }-`)) {
|
|
24
|
+
inPage(template.content);
|
|
25
|
+
register({ name: template.id, template });
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
if (!template.id) {
|
|
27
|
-
console.log(template, parentId);
|
|
28
|
-
throw new Error('<template> is missing a mandatory id field');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
inPage(template.content, template.id);
|
|
32
|
-
const name = template.id;
|
|
33
|
-
register({ name, template });
|
|
34
27
|
});
|
|
35
28
|
}
|
|
36
29
|
|
|
30
|
+
function loadAdded(mutations) {
|
|
31
|
+
mutations
|
|
32
|
+
.flatMap(mutation => [...mutation.addedNodes])
|
|
33
|
+
.filter(node => node.nodeType === Node.ELEMENT_NODE)
|
|
34
|
+
.forEach(node => {
|
|
35
|
+
fromElements([node, ...node.querySelectorAll('*')]).catch(error => console.error(error));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
37
39
|
function fromFile(root, ancestors = []) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
return fromElements([...root.querySelectorAll('*')], ancestors);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function fromElements(elements, ancestors = []) {
|
|
44
|
+
const names = elements
|
|
45
|
+
.filter(element => element.localName.startsWith(`${ prefix }-`) && element.matches(':not(:defined)'))
|
|
43
46
|
.map(element => element.localName)
|
|
44
47
|
.filter(name => !ancestors.includes(name));
|
|
45
48
|
|
|
@@ -58,23 +61,24 @@ async function initialize({ prefix = 'db', source = '/components' } = {}) {
|
|
|
58
61
|
const html = await fetchFromFile(name);
|
|
59
62
|
const template = document.createElement('template');
|
|
60
63
|
template.content.append(...html.children);
|
|
61
|
-
const script = await
|
|
64
|
+
const script = await importScript(template);
|
|
62
65
|
await fromFile(template.content, [...ancestors, name]);
|
|
63
|
-
inPage(template.content
|
|
66
|
+
inPage(template.content);
|
|
64
67
|
register({ name, template, script });
|
|
65
68
|
|
|
66
|
-
function
|
|
69
|
+
async function importScript(template) {
|
|
67
70
|
const script = template.content.querySelector('script');
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
if (!script) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
script.remove();
|
|
75
|
+
const blob = new Blob([script.textContent], { type: 'text/javascript' });
|
|
76
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
77
|
+
try {
|
|
78
|
+
return (await import(blobUrl)).default;
|
|
79
|
+
} finally {
|
|
80
|
+
URL.revokeObjectURL(blobUrl);
|
|
81
|
+
}
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
async function fetchFromFile(name) {
|
|
@@ -92,48 +96,13 @@ async function initialize({ prefix = 'db', source = '/components' } = {}) {
|
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
function register({ name, template, script }) {
|
|
99
|
+
function register({ name, template, script = null }) {
|
|
96
100
|
if (registerCache.has(name)) {
|
|
97
101
|
return;
|
|
98
102
|
}
|
|
99
|
-
|
|
100
103
|
registerCache.add(name);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const attributeMapping = getAttributeMapping(template.content);
|
|
104
|
-
|
|
105
|
-
customElements.define(name, class extends HTMLElement {
|
|
106
|
-
static observedAttributes = Object.keys(attributeMapping);
|
|
107
|
-
static formAssociated = isFormControl;
|
|
108
|
-
#shadow = null;
|
|
109
|
-
|
|
110
|
-
constructor() {
|
|
111
|
-
super();
|
|
112
|
-
|
|
113
|
-
const clone = document.importNode(template.content, true);
|
|
114
|
-
this.#shadow = this.attachShadow({ mode: 'open' });
|
|
115
|
-
this.#shadow.adoptedStyleSheets = inheritedStylesheets;
|
|
116
|
-
this.#shadow.appendChild(clone);
|
|
117
|
-
|
|
118
|
-
if (isFormControl) {
|
|
119
|
-
this.elementInternals = this.attachInternals();
|
|
120
|
-
this.formElement = this.#shadow.querySelector('input, select, textarea');
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
connectedCallback() {
|
|
125
|
-
if (typeof script === 'function') {
|
|
126
|
-
script(this, path => import(`${ window.location.origin }${ path }`));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
attributeChangedCallback(name, oldValue, newValue) {
|
|
131
|
-
if (oldValue !== newValue) {
|
|
132
|
-
updateAttributes({ name, value: newValue, attributeMapping, root: this.#shadow });
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
};
|
|
104
|
+
define({ name, template, script, styleSheets, observer });
|
|
105
|
+
}
|
|
137
106
|
|
|
138
107
|
function getInheritedStylesheets() {
|
|
139
108
|
return Array.from(document.styleSheets)
|
|
@@ -144,10 +113,17 @@ async function initialize({ prefix = 'db', source = '/components' } = {}) {
|
|
|
144
113
|
const cssText = Array.from(sheet.cssRules).map(rule => rule.cssText).join('\n');
|
|
145
114
|
newSheet.replaceSync(cssText);
|
|
146
115
|
} catch (error) {
|
|
147
|
-
error;
|
|
148
|
-
console.error('Permission denied for sheet:', sheet.href);
|
|
116
|
+
console.error('Permission denied for sheet:', sheet.href, error);
|
|
149
117
|
}
|
|
150
118
|
return newSheet;
|
|
151
119
|
});
|
|
152
120
|
}
|
|
153
|
-
|
|
121
|
+
|
|
122
|
+
// The browser hides [hidden] with a user agent style, which any display in
|
|
123
|
+
// the component's own CSS overrides
|
|
124
|
+
function getHiddenStylesheet() {
|
|
125
|
+
const sheet = new CSSStyleSheet();
|
|
126
|
+
sheet.replaceSync(':host([hidden]) { display: none !important; }');
|
|
127
|
+
return sheet;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
export { collect, apply, rows, toAttribute, toProperty };
|
|
2
|
+
|
|
3
|
+
const fieldTags = ['input', 'select', 'textarea'];
|
|
4
|
+
const fieldProperties = ['value', 'checked', 'selected'];
|
|
5
|
+
const lists = new WeakMap();
|
|
6
|
+
const rowBindings = new WeakMap();
|
|
7
|
+
|
|
8
|
+
// Reads the bindings, refs and form field of the given elements and everything
|
|
9
|
+
// inside them, except the rows of an nb-each element: those bind to records
|
|
10
|
+
function collect(elements) {
|
|
11
|
+
const bindings = [];
|
|
12
|
+
const refs = {};
|
|
13
|
+
let field = null;
|
|
14
|
+
elements.forEach(visit);
|
|
15
|
+
return { bindings, refs, field: refs.field ?? field };
|
|
16
|
+
|
|
17
|
+
function visit(element) {
|
|
18
|
+
[...element.attributes].forEach(({ name, value }) => {
|
|
19
|
+
if (name === 'nb-text') {
|
|
20
|
+
bindings.push({ element, kind: 'text', name: value });
|
|
21
|
+
} else if (name === 'nb-html') {
|
|
22
|
+
bindings.push({ element, kind: 'html', name: value });
|
|
23
|
+
} else if (name === 'nb-each') {
|
|
24
|
+
bindings.push({ element, kind: 'each', name: value });
|
|
25
|
+
} else if (name === 'nb-ref') {
|
|
26
|
+
refs[value] = element;
|
|
27
|
+
} else if (name.startsWith(':')) {
|
|
28
|
+
bindings.push({ element, kind: 'attribute', attribute: name.slice(1), name: value });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (!field && fieldTags.includes(element.localName)) {
|
|
33
|
+
field = element;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!element.hasAttribute('nb-each')) {
|
|
37
|
+
[...element.children].forEach(visit);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function apply({ element, kind, attribute }, value) {
|
|
43
|
+
if (kind === 'text') {
|
|
44
|
+
element.textContent = value ?? '';
|
|
45
|
+
} else if (kind === 'html') {
|
|
46
|
+
element.innerHTML = value ?? '';
|
|
47
|
+
} else if (kind === 'each') {
|
|
48
|
+
rows(element, value ?? []);
|
|
49
|
+
} else {
|
|
50
|
+
setAttribute(element, attribute, value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function setAttribute(element, attribute, value) {
|
|
55
|
+
const isComponent = element.localName.includes('-');
|
|
56
|
+
const isOwnAttribute = attribute.startsWith('data-') || attribute.startsWith('aria-');
|
|
57
|
+
const isOn = value !== false && value !== null && value !== undefined;
|
|
58
|
+
|
|
59
|
+
if (isComponent && !isOwnAttribute) {
|
|
60
|
+
element[toProperty(attribute)] = value;
|
|
61
|
+
} else if (fieldProperties.includes(attribute) && [...fieldTags, 'option'].includes(element.localName)) {
|
|
62
|
+
element[attribute] = attribute === 'value' ? value ?? '' : isOn;
|
|
63
|
+
} else if (isOn) {
|
|
64
|
+
element.setAttribute(attribute, value === true ? '' : value);
|
|
65
|
+
} else {
|
|
66
|
+
element.removeAttribute(attribute);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function rows(element, records, each) {
|
|
71
|
+
const list = lists.get(element) ?? createList();
|
|
72
|
+
const rendered = new Map();
|
|
73
|
+
|
|
74
|
+
records.forEach((input, index) => {
|
|
75
|
+
const record = input !== null && typeof input === 'object' ? input : { id: input, value: input };
|
|
76
|
+
const key = getKey(record, index);
|
|
77
|
+
const row = list.rows.get(key) ?? createRow();
|
|
78
|
+
rowBindings.get(row).forEach(binding => apply(binding, record[binding.name]));
|
|
79
|
+
if (each) {
|
|
80
|
+
each(row, input);
|
|
81
|
+
}
|
|
82
|
+
rendered.set(key, row);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
list.rows.forEach((row, key) => {
|
|
86
|
+
if (!rendered.has(key)) {
|
|
87
|
+
row.remove();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
let reference = list.container.firstElementChild;
|
|
92
|
+
rendered.forEach(row => {
|
|
93
|
+
if (row === reference) {
|
|
94
|
+
reference = row.nextElementSibling;
|
|
95
|
+
} else if (list.container.moveBefore && list.container.isConnected && row.parentNode === list.container) {
|
|
96
|
+
// moveBefore keeps focus and the state of the row, insertBefore would
|
|
97
|
+
// disconnect and reconnect it. Not every browser has it yet
|
|
98
|
+
list.container.moveBefore(row, reference);
|
|
99
|
+
} else {
|
|
100
|
+
list.container.insertBefore(row, reference);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
list.rows = rendered;
|
|
105
|
+
|
|
106
|
+
function getKey(record, index) {
|
|
107
|
+
const key = `id:${ record.id }`;
|
|
108
|
+
if (['string', 'number'].includes(typeof record.id) && !rendered.has(key)) {
|
|
109
|
+
return key;
|
|
110
|
+
}
|
|
111
|
+
return `position:${ index }`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function createRow() {
|
|
115
|
+
const row = list.template.cloneNode(true);
|
|
116
|
+
rowBindings.set(row, collect([row]).bindings);
|
|
117
|
+
return row;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function createList() {
|
|
121
|
+
const container = element.localName === 'table'
|
|
122
|
+
? element.tBodies[0] ?? element.createTBody()
|
|
123
|
+
: element;
|
|
124
|
+
const template = container.firstElementChild;
|
|
125
|
+
if (!template) {
|
|
126
|
+
throw new Error(`rows() needs a first element in <${ element.localName }> to use as the row template`);
|
|
127
|
+
}
|
|
128
|
+
template.remove();
|
|
129
|
+
const created = { container, template, rows: new Map() };
|
|
130
|
+
lists.set(element, created);
|
|
131
|
+
return created;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function toAttribute(property) {
|
|
136
|
+
return property.replace(/[A-Z]/g, letter => `-${ letter.toLowerCase() }`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function toProperty(attribute) {
|
|
140
|
+
return attribute.replace(/-([a-z])/g, (match, letter) => letter.toUpperCase());
|
|
141
|
+
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nbhi",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Web components from plain HTML templates, without build tools. Bindings in a template become properties, rows() renders lists, and a component with a form field is a real form control.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web-components",
|
|
7
|
+
"custom-elements",
|
|
8
|
+
"html-templates",
|
|
7
9
|
"html-includes",
|
|
8
10
|
"no-build",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
11
|
+
"buildless",
|
|
12
|
+
"data-binding",
|
|
13
|
+
"list-rendering",
|
|
14
|
+
"form-associated",
|
|
15
|
+
"form-controls",
|
|
16
|
+
"shadow-dom",
|
|
17
|
+
"vanilla-js"
|
|
15
18
|
],
|
|
16
|
-
"homepage": "https://github.
|
|
19
|
+
"homepage": "https://royniels.github.io/nbhi/",
|
|
17
20
|
"bugs": {
|
|
18
21
|
"url": "https://github.com/royniels/nbhi/issues"
|
|
19
22
|
},
|
|
@@ -31,6 +34,11 @@
|
|
|
31
34
|
"author": "Roy Niels",
|
|
32
35
|
"type": "module",
|
|
33
36
|
"scripts": {
|
|
34
|
-
"test": "
|
|
37
|
+
"test": "playwright test",
|
|
38
|
+
"site": "node site/serve.js",
|
|
39
|
+
"art": "node site/art/draw.mjs"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@playwright/test": "^1.63.0"
|
|
35
43
|
}
|
|
36
44
|
}
|
package/javascript/attributes.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
export { getAttributeMapping, updateAttributes };
|
|
2
|
-
|
|
3
|
-
const mapping = {
|
|
4
|
-
boolean: {
|
|
5
|
-
autofocus: ['button', 'input', 'select', 'textarea'],
|
|
6
|
-
autoplay: ['audio', 'video'],
|
|
7
|
-
checked: ['input'],
|
|
8
|
-
controls: ['audio', 'video'],
|
|
9
|
-
default: ['track'],
|
|
10
|
-
defer: ['script'],
|
|
11
|
-
disabled: ['button', 'input', 'optgroup', 'option', 'select', 'textarea', 'fieldset'],
|
|
12
|
-
formnovalidate: ['button', 'input'],
|
|
13
|
-
ismap: ['img'],
|
|
14
|
-
loop: ['audio', 'video'],
|
|
15
|
-
multiple: ['input', 'select'],
|
|
16
|
-
muted: ['audio', 'video'],
|
|
17
|
-
novalidate: ['form'],
|
|
18
|
-
open: ['details', 'dialog'],
|
|
19
|
-
playsinline: ['video'],
|
|
20
|
-
readonly: ['input', 'textarea'],
|
|
21
|
-
required: ['input', 'select', 'textarea'],
|
|
22
|
-
reversed: ['ol'],
|
|
23
|
-
},
|
|
24
|
-
keyValue: {
|
|
25
|
-
accept: ['input'],
|
|
26
|
-
autocomplete: ['input', 'select', 'textarea'],
|
|
27
|
-
cols: ['textarea'],
|
|
28
|
-
dirname: ['input', 'textarea'],
|
|
29
|
-
max: ['input'],
|
|
30
|
-
maxlength: ['input', 'textarea'],
|
|
31
|
-
min: ['input'],
|
|
32
|
-
minlength: ['input', 'textarea'],
|
|
33
|
-
name: ['input', 'select', 'textarea'],
|
|
34
|
-
pattern: ['input'],
|
|
35
|
-
placeholder: ['input', 'textarea'],
|
|
36
|
-
rows: ['textarea'],
|
|
37
|
-
size: ['input', 'select'],
|
|
38
|
-
step: ['input'],
|
|
39
|
-
type: ['input'],
|
|
40
|
-
value: ['input', 'select'],
|
|
41
|
-
width: ['input'],
|
|
42
|
-
wrap: ['textarea'],
|
|
43
|
-
htmlFor: ['label'],
|
|
44
|
-
for: ['label'],
|
|
45
|
-
href: ['a'],
|
|
46
|
-
target: ['a'],
|
|
47
|
-
src: ['audio', 'img', 'input', 'video'],
|
|
48
|
-
'aria-invalid': ['input', 'textarea', 'select'],
|
|
49
|
-
},
|
|
50
|
-
excludedAttributes: ['class', 'extends', 'id', 'part', 'data-slot', 'child'],
|
|
51
|
-
excludedElements: ['slot', 'template', 'style', 'script'],
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
function getAttributeMapping(element) {
|
|
55
|
-
const { keyValue, excludedAttributes, excludedElements, boolean: binary } = mapping;
|
|
56
|
-
const results = {};
|
|
57
|
-
traverse(element);
|
|
58
|
-
return results;
|
|
59
|
-
|
|
60
|
-
function traverse(element) {
|
|
61
|
-
const tagName = element.localName;
|
|
62
|
-
if (
|
|
63
|
-
element &&
|
|
64
|
-
typeof tagName === 'string' &&
|
|
65
|
-
!excludedElements.includes(element.localName)
|
|
66
|
-
) {
|
|
67
|
-
Object
|
|
68
|
-
.entries(binary)
|
|
69
|
-
.filter(([key]) => !excludedAttributes.includes(key))
|
|
70
|
-
.filter(([, tagNames]) => tagNames.includes(tagName))
|
|
71
|
-
.forEach(([key, tagNames]) => results[key] = { type: 'boolean', tagNames });
|
|
72
|
-
|
|
73
|
-
Object
|
|
74
|
-
.entries(keyValue)
|
|
75
|
-
.filter(([key]) => !excludedAttributes.includes(key))
|
|
76
|
-
.filter(([, tagNames]) => tagNames.includes(tagName))
|
|
77
|
-
.forEach(([key, tagNames]) => results[key] = { type: 'keyValue', tagNames });
|
|
78
|
-
|
|
79
|
-
if (element.hasAttributes()) {
|
|
80
|
-
[...element.attributes].forEach(attribute => {
|
|
81
|
-
const name = attribute.name;
|
|
82
|
-
if (!excludedAttributes.includes(name)) {
|
|
83
|
-
if (!results[name]) {
|
|
84
|
-
results[name] = { type: 'userDefined', tagNames: [] };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (!results[name].tagNames.includes(tagName)) {
|
|
88
|
-
results[name].tagNames.push(tagName);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
[...element.children].forEach(child => traverse(child));
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
function updateAttributes({ name, value, attributeMapping, root }) {
|
|
99
|
-
const { type, tagNames = [] } = attributeMapping[name] ?? [];
|
|
100
|
-
|
|
101
|
-
if (type === 'userDefined') {
|
|
102
|
-
if (root.hasAttribute && root.hasAttribute(name)) {
|
|
103
|
-
process(root);
|
|
104
|
-
} else {
|
|
105
|
-
process(root.querySelector(`[${ name }]`));
|
|
106
|
-
}
|
|
107
|
-
} else {
|
|
108
|
-
tagNames.forEach(tagName => {
|
|
109
|
-
root.querySelectorAll(tagName).forEach(process);
|
|
110
|
-
if (tagName === root?.localName) {
|
|
111
|
-
process(root);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function process(element) {
|
|
117
|
-
if (element) {
|
|
118
|
-
if (
|
|
119
|
-
value === null ||
|
|
120
|
-
value === undefined ||
|
|
121
|
-
(type === 'boolean' && value === false)
|
|
122
|
-
) {
|
|
123
|
-
element.removeAttribute(name);
|
|
124
|
-
} else {
|
|
125
|
-
element.setAttribute(name, value);
|
|
126
|
-
// Triggers <select> to update the selected <option>
|
|
127
|
-
if (name === 'value' && element.localName === 'select') {
|
|
128
|
-
element.value = value;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|