mosaic 1.0.10 → 1.0.11

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/cjs/attribute.js DELETED
@@ -1,90 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var helpers = require('./helpers.js');
6
-
7
- const pascalToKebab = (string) =>
8
- string.replace(/[\w]([A-Z])/g, function (m) {
9
- return m[0] + "-" + m[1].toLowerCase()
10
- });
11
-
12
- const kebabToPascal = (string) =>
13
- string.replace(/[\w]-([\w])/g, function (m) {
14
- return m[0] + m[2].toUpperCase()
15
- });
16
-
17
- const parseStyles = (value) => {
18
- let type = typeof value;
19
-
20
- if (type === "string")
21
- return value.split(";").reduce((o, value) => {
22
- const [k, v] = value.split(":").map((v) => v.trim());
23
- if (k) o[k] = v;
24
- return o
25
- }, {})
26
-
27
- if (type === "object") return value
28
-
29
- return {}
30
- };
31
-
32
- const joinStyles = (value) =>
33
- Object.entries(value)
34
- .map(([k, v]) => `${k}: ${v};`)
35
- .join(" ");
36
-
37
- const convertStyles = (o) =>
38
- Object.keys(o).reduce((a, k) => {
39
- a[pascalToKebab(k)] = o[k];
40
- return a
41
- }, {});
42
-
43
- const applyAttribute = (node, name, value) => {
44
- if (name === "style") {
45
- value = joinStyles(
46
- convertStyles({
47
- ...parseStyles(node.getAttribute("style")),
48
- ...parseStyles(value),
49
- })
50
- );
51
- } else if (name === "class") {
52
- switch (helpers.typeOf(value)) {
53
- case "Array":
54
- value = value.join(" ");
55
- break
56
- case "Object":
57
- value = Object.keys(value)
58
- .reduce((a, k) => {
59
- if (value[k]) a.push(k);
60
- return a
61
- }, [])
62
- .join(" ");
63
- break
64
- }
65
- } else if (!helpers.isPrimitive(value)) {
66
- return (node[kebabToPascal(name)] = value)
67
- }
68
-
69
- name = pascalToKebab(name);
70
-
71
- if (typeof value === "boolean") {
72
- if (name.startsWith("aria-")) {
73
- value = "" + value;
74
- } else if (value) {
75
- value = "";
76
- }
77
- }
78
-
79
- let current = node.getAttribute(name);
80
-
81
- if (value === current) return
82
-
83
- if (typeof value === "string" || typeof value === "number") {
84
- node.setAttribute(name, value);
85
- } else {
86
- node.removeAttribute(name);
87
- }
88
- };
89
-
90
- exports.applyAttribute = applyAttribute;
package/cjs/constants.js DELETED
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const TEXT = 1;
6
- const ATTRIBUTE = 2;
7
- const INPUT = 3;
8
- const EVENT = 4;
9
- const REPEAT = 5;
10
-
11
- exports.ATTRIBUTE = ATTRIBUTE;
12
- exports.EVENT = EVENT;
13
- exports.INPUT = INPUT;
14
- exports.REPEAT = REPEAT;
15
- exports.TEXT = TEXT;
package/cjs/context.js DELETED
@@ -1,66 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var helpers = require('./helpers.js');
6
-
7
- const handler = ({ path, identifier, key, index, i, k }) => ({
8
- get(target, property) {
9
- let x = helpers.getValueAtPath(path, target);
10
-
11
- // x === the collection
12
-
13
- if (property === identifier) {
14
- for (let n in x) {
15
- let v = x[n];
16
- if (key) {
17
- if (v[key] === k) return v
18
- } else {
19
- if (n == i) return v
20
- }
21
- }
22
- }
23
-
24
- if (property === index) {
25
- for (let n in x) {
26
- let v = x[n];
27
- if (key) {
28
- if (v[key] === k) return n
29
- } else {
30
- if (n == i) return n
31
- }
32
- }
33
- }
34
-
35
- let t = key ? x.find((v) => v[key] === k) : x[i];
36
- if (t?.hasOwnProperty?.(property)) return t[property]
37
-
38
- return Reflect.get(...arguments)
39
- },
40
- set(target, property, value) {
41
- let x = helpers.getValueAtPath(path, target);
42
- let t = key ? x.find((v) => v[key] === k) : x[i];
43
- if (t && !helpers.isPrimitive(t)) {
44
- t[property] = value;
45
- return true
46
- }
47
-
48
- return Reflect.set(...arguments)
49
- },
50
- });
51
-
52
- const createContext = (v = []) => {
53
- let context = v;
54
- return {
55
- get: () => context,
56
- push: (v) => context.push(v),
57
- wrap: (state) => {
58
- return context.reduce(
59
- (target, ctx) => new Proxy(target, handler(ctx)),
60
- state
61
- )
62
- },
63
- }
64
- };
65
-
66
- exports.createContext = createContext;
package/cjs/define.js DELETED
@@ -1,112 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var update = require('./update.js');
6
- var helpers = require('./helpers.js');
7
- var render = require('./render.js');
8
- var mergeSlots = require('./mergeSlots.js');
9
-
10
- const define = (name, factory, template) =>
11
- customElements.define(
12
- name,
13
- class extends HTMLElement {
14
- async connectedCallback() {
15
- if (!this.initialised) {
16
- let config = factory(this);
17
-
18
- if (config instanceof Promise) config = await config;
19
-
20
- let { subscribe, shadow, initialState = {}, observe = [] } = config;
21
-
22
- this.connectedCallback = config.connectedCallback;
23
- this.disconnectedCallback = config.disconnectedCallback;
24
-
25
- const { dispatch, getState, onUpdate, flush } = update.configure(
26
- config,
27
- this
28
- );
29
-
30
- initialState = getState();
31
-
32
- observe = observe.map(helpers.kebabToPascal);
33
-
34
- let observedAttributes = observe.map(helpers.pascalToKebab);
35
-
36
- let sa = this.setAttribute;
37
- this.setAttribute = (k, v) => {
38
- if (observedAttributes.includes(k)) {
39
- let { name, value } = helpers.attributeToProp(k, v);
40
- dispatch({
41
- type: "SET",
42
- payload: { name, value },
43
- });
44
- }
45
- sa.apply(this, [k, v]);
46
- };
47
-
48
- observedAttributes.forEach((name) => {
49
- let property = helpers.attributeToProp(name).name;
50
- let value;
51
-
52
- if (this.hasAttribute(name)) {
53
- value = this.getAttribute(name);
54
- } else {
55
- value = this[property] || initialState[property];
56
- }
57
-
58
- Object.defineProperty(this, property, {
59
- get() {
60
- return getState()[property]
61
- },
62
- set(v) {
63
- dispatch({
64
- type: "SET",
65
- payload: { name: property, value: v },
66
- });
67
- if (helpers.isPrimitive(v)) {
68
- helpers.applyAttribute(this, property, v);
69
- }
70
- },
71
- });
72
-
73
- this[property] = value;
74
- });
75
-
76
- let beforeMountCallback;
77
-
78
- if (shadow) {
79
- this.attachShadow({
80
- mode: shadow,
81
- });
82
- } else {
83
- beforeMountCallback = (frag) => mergeSlots.mergeSlots(this, frag);
84
- }
85
-
86
- onUpdate(
87
- render.render(
88
- this.shadowRoot || this,
89
- { getState, dispatch },
90
- template,
91
- () => {
92
- observe.forEach((k) => {
93
- let v = getState()[k];
94
- if (helpers.isPrimitive(v)) helpers.applyAttribute(this, k, v);
95
- });
96
- subscribe?.(getState());
97
- flush();
98
- },
99
- beforeMountCallback
100
- )
101
- );
102
- this.initialised = true;
103
- }
104
- this.connectedCallback?.();
105
- }
106
- disconnectedCallback() {
107
- this.disconnectedCallback?.();
108
- }
109
- }
110
- );
111
-
112
- exports.define = define;
@@ -1,42 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const updateFormControl = (node, value) => {
6
- if (node.nodeName === "SELECT") {
7
- Array.from(node.querySelectorAll("option")).forEach((option) => {
8
- option.selected = value.includes(option.value);
9
- });
10
- return
11
- }
12
-
13
- let checked;
14
-
15
- switch (node.getAttribute("type")) {
16
- case "checkbox":
17
- checked = value;
18
- if (node.checked === checked) break
19
- if (checked) {
20
- node.setAttribute("checked", "");
21
- } else {
22
- node.removeAttribute("checked");
23
- }
24
- break
25
- case "radio":
26
- checked = value === node.getAttribute("value");
27
- if (node.checked === checked) break
28
- node.checked = checked;
29
- if (checked) {
30
- node.setAttribute("checked", "");
31
- } else {
32
- node.removeAttribute("checked");
33
- }
34
- break
35
- default:
36
- if (node.value === value) break
37
- node.setAttribute("value", (node.value = value || ""));
38
- break
39
- }
40
- };
41
-
42
- exports.updateFormControl = updateFormControl;
package/cjs/helpers.js DELETED
@@ -1,140 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const last = (v = []) => v[v.length - 1];
6
-
7
- const isWhitespace = (node) => {
8
- return node.nodeType === node.TEXT_NODE && node.nodeValue.trim() === ""
9
- };
10
-
11
- const walk = (node, callback, deep = true) => {
12
- if (!node) return
13
- if (!isWhitespace(node)) {
14
- let v = callback(node);
15
- if (v === false) return
16
- if (v?.nodeName) return walk(v, callback, deep)
17
- }
18
- if (deep) walk(node.firstChild, callback, deep);
19
- walk(node.nextSibling, callback, deep);
20
- };
21
-
22
- const transformBrackets = (str = "") => {
23
- let parts = str.split(/(\[[^\]]+\])/).filter((v) => v);
24
- return parts.reduce((a, part) => {
25
- let v = part.charAt(0) === "[" ? "." + part.replace(/\./g, ":") : part;
26
- return a + v
27
- }, "")
28
- };
29
-
30
- const getTarget = (path, target) => {
31
- let parts = transformBrackets(path)
32
- .split(".")
33
- .map((k) => {
34
- if (k.charAt(0) === "[") {
35
- let p = k.slice(1, -1).replace(/:/g, ".");
36
- return getValueAtPath(p, target)
37
- } else {
38
- return k
39
- }
40
- });
41
-
42
- let t =
43
- parts.slice(0, -1).reduce((o, k) => {
44
- return o && o[k]
45
- }, target) || target;
46
- return [t, last(parts)]
47
- };
48
-
49
- const getValueAtPath = (path, target) => {
50
- let [a, b] = getTarget(path, target);
51
- let v = a?.[b];
52
- if (typeof v === "function") return v.bind(a)
53
- return v
54
- };
55
-
56
- const fragmentFromTemplate = (v) => {
57
- if (typeof v === "string") {
58
- let tpl = document.createElement("template");
59
- tpl.innerHTML = v.trim();
60
- return tpl.content
61
- }
62
- if (v.nodeName === "TEMPLATE") return v.cloneNode(true).content
63
- if (v.nodeName === "defs") return v.firstElementChild.cloneNode(true)
64
- };
65
-
66
- const debounce = (fn) => {
67
- let wait = false;
68
- let invoke = false;
69
- return () => {
70
- if (wait) {
71
- invoke = true;
72
- } else {
73
- wait = true;
74
- fn();
75
- requestAnimationFrame(() => {
76
- if (invoke) fn();
77
- wait = false;
78
- });
79
- }
80
- }
81
- };
82
-
83
- const isPrimitive = (v) => v === null || typeof v !== "object";
84
-
85
- const typeOf = (v) =>
86
- Object.prototype.toString.call(v).match(/\s(.+[^\]])/)[1];
87
-
88
- const pascalToKebab = (string) =>
89
- string.replace(/[\w]([A-Z])/g, function (m) {
90
- return m[0] + "-" + m[1].toLowerCase()
91
- });
92
-
93
- const kebabToPascal = (string) =>
94
- string.replace(/[\w]-([\w])/g, function (m) {
95
- return m[0] + m[2].toUpperCase()
96
- });
97
-
98
- const applyAttribute = (node, name, value) => {
99
- name = pascalToKebab(name);
100
-
101
- if (typeof value === "boolean") {
102
- if (name.startsWith("aria-")) {
103
- value = "" + value;
104
- } else if (value) {
105
- value = "";
106
- }
107
- }
108
-
109
- if (typeof value === "string" || typeof value === "number") {
110
- node.setAttribute(name, value);
111
- } else {
112
- node.removeAttribute(name);
113
- }
114
- };
115
-
116
- const attributeToProp = (k, v) => {
117
- let name = kebabToPascal(k);
118
- if (v === "") v = true;
119
- if (k.startsWith("aria-")) {
120
- if (v === "true") v = true;
121
- if (v === "false") v = false;
122
- }
123
- return {
124
- name,
125
- value: v,
126
- }
127
- };
128
-
129
- exports.applyAttribute = applyAttribute;
130
- exports.attributeToProp = attributeToProp;
131
- exports.debounce = debounce;
132
- exports.fragmentFromTemplate = fragmentFromTemplate;
133
- exports.getValueAtPath = getValueAtPath;
134
- exports.isPrimitive = isPrimitive;
135
- exports.isWhitespace = isWhitespace;
136
- exports.kebabToPascal = kebabToPascal;
137
- exports.last = last;
138
- exports.pascalToKebab = pascalToKebab;
139
- exports.typeOf = typeOf;
140
- exports.walk = walk;
package/cjs/index.js DELETED
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var define = require('./define.js');
6
-
7
-
8
-
9
- exports.define = define.define;
package/cjs/list.js DELETED
@@ -1,109 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var helpers = require('./helpers.js');
6
-
7
- function parseEach(node) {
8
- let each = node.getAttribute("each");
9
- let m = each?.match(/(.+)\s+in\s+(.+)/);
10
- if (!m) {
11
- if (!each) return m
12
- return {
13
- path: each.trim(),
14
- key: node.getAttribute("key"),
15
- }
16
- }
17
- let [_, left, right] = m;
18
- let parts = left.match(/\(([^\)]+)\)/);
19
- let [a, b] = (parts ? parts[1].split(",") : [left]).map((v) => v.trim());
20
-
21
- return {
22
- path: right.trim(),
23
- identifier: b ? b : a,
24
- index: b ? a : b,
25
- key: node.getAttribute("key"),
26
- }
27
- }
28
-
29
- const getBlockSize = (template) => {
30
- let i = 0;
31
- helpers.walk(template.content?.firstChild || template.firstChild, () => i++, false);
32
- return i
33
- };
34
-
35
- const nextNonWhitespaceSibling = (node) => {
36
- return helpers.isWhitespace(node.nextSibling)
37
- ? nextNonWhitespaceSibling(node.nextSibling)
38
- : node.nextSibling
39
- };
40
-
41
- const getBlockFragments = (template, numBlocks) => {
42
- let blockSize = getBlockSize(template);
43
-
44
- let r = [];
45
- if (numBlocks) {
46
- while (numBlocks--) {
47
- let f = document.createDocumentFragment();
48
- let n = blockSize;
49
- while (n--) {
50
- f.appendChild(nextNonWhitespaceSibling(template));
51
- }
52
- r.push(f);
53
- }
54
- }
55
- return r
56
- };
57
-
58
- const getBlocks = (template) => {
59
- let numBlocks = template.getAttribute("length");
60
- let blockSize = getBlockSize(template);
61
- let r = [];
62
- let node = template;
63
- if (numBlocks) {
64
- while (numBlocks--) {
65
- let f = [];
66
- let n = blockSize;
67
- while (n--) {
68
- node = nextNonWhitespaceSibling(node);
69
- f.push(node);
70
- }
71
- r.push(f);
72
- }
73
- }
74
- return r
75
- };
76
-
77
- const compareKeyedLists = (key, a = [], b = []) => {
78
- let delta = b.map(([k, item]) =>
79
- !key ? (k in a ? k : -1) : a.findIndex(([_, v]) => v[key] === item[key])
80
- );
81
- if (a.length !== b.length || !delta.every((a, b) => a === b)) return delta
82
- };
83
-
84
- function lastChild(v) {
85
- return (v.nodeType === v.DOCUMENT_FRAGMENT_NODE && v.lastChild) || v
86
- }
87
-
88
- const updateList = (template, delta, entries, createListItem) => {
89
- let n = template.getAttribute("length") || 0;
90
- let blocks = getBlockFragments(template, n);
91
- let t = template;
92
-
93
- delta.forEach((i, newIndex) => {
94
- let frag =
95
- i === -1
96
- ? createListItem(entries[newIndex][1], entries[newIndex][0])
97
- : blocks[i];
98
- let x = lastChild(frag);
99
- t.after(frag);
100
- t = x;
101
- });
102
-
103
- template.setAttribute("length", delta.length);
104
- };
105
-
106
- exports.compareKeyedLists = compareKeyedLists;
107
- exports.getBlocks = getBlocks;
108
- exports.parseEach = parseEach;
109
- exports.updateList = updateList;
package/cjs/mergeSlots.js DELETED
@@ -1,37 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const childNodes = (node) => {
6
- let frag = document.createDocumentFragment();
7
- while (node.firstChild) {
8
- frag.appendChild(node.firstChild);
9
- }
10
- return frag
11
- };
12
-
13
- const mergeSlots = (targetNode, sourceNode) => {
14
- let namedSlots = sourceNode.querySelectorAll("slot[name]");
15
-
16
- namedSlots.forEach((slot) => {
17
- let name = slot.attributes.name.value;
18
- let node = targetNode.querySelector(`[slot="${name}"]`);
19
- if (!node) {
20
- slot.parentNode.replaceChild(childNodes(slot), slot);
21
- return
22
- }
23
- node.removeAttribute("slot");
24
- slot.parentNode.replaceChild(node, slot);
25
- });
26
-
27
- let defaultSlot = sourceNode.querySelector("slot:not([name])");
28
-
29
- if (defaultSlot) {
30
- defaultSlot.parentNode.replaceChild(
31
- childNodes(targetNode.innerHTML.trim() ? targetNode : defaultSlot),
32
- defaultSlot
33
- );
34
- }
35
- };
36
-
37
- exports.mergeSlots = mergeSlots;
package/cjs/proxy.js DELETED
@@ -1,42 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var helpers = require('./helpers.js');
6
-
7
- const proxy = (root, callback) => {
8
- let proxyCache = new WeakMap();
9
-
10
- function createProxy(target, handler) {
11
- let proxy = proxyCache.get(target);
12
- if (proxy === undefined) {
13
- proxy = new Proxy(target, handler);
14
- proxyCache.set(target, proxy);
15
- }
16
- return proxy
17
- }
18
-
19
- const handler = {
20
- get(target, property) {
21
- if (["Object", "Array"].includes(helpers.typeOf(target[property]))) {
22
- return createProxy(target[property], handler)
23
- } else {
24
- return Reflect.get(...arguments)
25
- }
26
- },
27
- set(target, property, value) {
28
- if (value === target[property]) return true
29
-
30
- callback();
31
- return Reflect.set(...arguments)
32
- },
33
- deleteProperty() {
34
- callback();
35
- return Reflect.deleteProperty(...arguments)
36
- },
37
- };
38
-
39
- return new Proxy(root, handler)
40
- };
41
-
42
- exports.proxy = proxy;