mosaic 1.0.3 → 1.0.4
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/LICENSE +21 -0
- package/README.md +63 -8
- package/cjs/attribute.js +90 -0
- package/cjs/constants.js +15 -0
- package/cjs/context.js +66 -0
- package/cjs/define.js +152 -0
- package/cjs/formControl.js +42 -0
- package/cjs/helpers.js +140 -0
- package/cjs/index.js +9 -0
- package/cjs/list.js +109 -0
- package/cjs/mergeSlots.js +37 -0
- package/cjs/proxy.js +42 -0
- package/cjs/render.js +395 -0
- package/cjs/token.js +63 -0
- package/cjs/update.js +128 -0
- package/dist/mosaic.js +1142 -0
- package/dist/mosaic.min.js +1 -0
- package/package.json +42 -25
- package/src/attribute.js +84 -0
- package/src/constants.js +5 -0
- package/src/context.js +60 -0
- package/src/define.js +158 -0
- package/src/formControl.js +36 -0
- package/src/helpers.js +123 -0
- package/src/index.js +1 -0
- package/src/list.js +100 -0
- package/src/mergeSlots.js +31 -0
- package/src/proxy.js +36 -0
- package/src/render.js +397 -0
- package/src/token.js +55 -0
- package/src/update.js +122 -0
- package/SCRATCH.md +0 -31
- package/cli.js +0 -17
- package/conf.json +0 -0
- package/docs/assets/css/main.css +0 -2638
- package/docs/assets/images/icons.png +0 -0
- package/docs/assets/images/icons@2x.png +0 -0
- package/docs/assets/images/widgets.png +0 -0
- package/docs/assets/images/widgets@2x.png +0 -0
- package/docs/assets/js/main.js +0 -248
- package/docs/assets/js/search.js +0 -1
- package/docs/index.html +0 -111
- package/docs/modules.html +0 -131
- package/example/components/x-drawer.html +0 -41
- package/example/mosaic/templates/mocha/index.html +0 -39
- package/example/mosaic/tests.d.ts +0 -13
- package/example/mosaic/tests.js +0 -28
- package/example/mosaic/website.d.ts +0 -31
- package/example/mosaic/website.js +0 -64
- package/example/mosaic.config.d.ts +0 -7
- package/example/mosaic.config.js +0 -8
- package/example/package-lock.json +0 -899
- package/example/package.json +0 -20
- package/example/pages/index.html +0 -12
- package/example/test/x-drawer.d.ts +0 -0
- package/example/test/x-drawer.js +0 -94
- package/index.d.ts +0 -85
- package/index.js +0 -125
- package/index.wdocs.js +0 -37
- package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
- package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
- package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
- package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Light-webfont.eot +0 -0
- package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
- package/out/fonts/OpenSans-Light-webfont.woff +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
- package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
- package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/out/global.html +0 -912
- package/out/index.html +0 -65
- package/out/index.js.html +0 -239
- package/out/scripts/linenumber.js +0 -25
- package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/out/scripts/prettify/lang-css.js +0 -2
- package/out/scripts/prettify/prettify.js +0 -28
- package/out/styles/jsdoc-default.css +0 -358
- package/out/styles/prettify-jsdoc.css +0 -111
- package/out/styles/prettify-tomorrow.css +0 -132
- package/tsconfig.json +0 -11
package/cjs/list.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
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;
|
|
@@ -0,0 +1,37 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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;
|
package/cjs/render.js
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var constants = require('./constants.js');
|
|
6
|
+
var formControl = require('./formControl.js');
|
|
7
|
+
var helpers = require('./helpers.js');
|
|
8
|
+
var list = require('./list.js');
|
|
9
|
+
var token = require('./token.js');
|
|
10
|
+
var attribute = require('./attribute.js');
|
|
11
|
+
var context = require('./context.js');
|
|
12
|
+
|
|
13
|
+
const HYDRATE_ATTR = "mosaic-hydrate";
|
|
14
|
+
|
|
15
|
+
const render = (
|
|
16
|
+
target,
|
|
17
|
+
{ getState, dispatch },
|
|
18
|
+
template,
|
|
19
|
+
updatedCallback,
|
|
20
|
+
beforeMountCallback
|
|
21
|
+
) => {
|
|
22
|
+
let observer = () => {
|
|
23
|
+
let subscribers = new Set();
|
|
24
|
+
return {
|
|
25
|
+
publish: (cb) => {
|
|
26
|
+
for (let fn of subscribers) {
|
|
27
|
+
fn();
|
|
28
|
+
}
|
|
29
|
+
cb?.();
|
|
30
|
+
},
|
|
31
|
+
subscribe(fn) {
|
|
32
|
+
subscribers.add(fn);
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const createSubscription = {
|
|
38
|
+
[constants.TEXT]: ({ value, node, context }, { getState }) => {
|
|
39
|
+
return {
|
|
40
|
+
handler: () => {
|
|
41
|
+
let state = context ? context.wrap(getState()) : getState();
|
|
42
|
+
let a = node.textContent;
|
|
43
|
+
let b = token.getValueFromParts(state, token.getParts(value));
|
|
44
|
+
if (a !== b) node.textContent = b;
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
[constants.ATTRIBUTE]: ({ value, node, name, context }, { getState }) => {
|
|
49
|
+
return {
|
|
50
|
+
handler: () => {
|
|
51
|
+
let state = context ? context.wrap(getState()) : getState();
|
|
52
|
+
let b = token.getValueFromParts(state, token.getParts(value));
|
|
53
|
+
|
|
54
|
+
attribute.applyAttribute(node, name, b);
|
|
55
|
+
|
|
56
|
+
if (node.nodeName === "OPTION") {
|
|
57
|
+
let path = node.parentNode.getAttribute("name");
|
|
58
|
+
let selected = helpers.getValueAtPath(path, state);
|
|
59
|
+
node.selected = selected === b;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
[constants.INPUT]: ({ node, path, context }, { getState, dispatch }) => {
|
|
65
|
+
node.addEventListener("input", () => {
|
|
66
|
+
let value =
|
|
67
|
+
node.getAttribute("type") === "checkbox" ? node.checked : node.value;
|
|
68
|
+
|
|
69
|
+
if (value.trim?.().length && !isNaN(value)) value = +value;
|
|
70
|
+
|
|
71
|
+
if (context) {
|
|
72
|
+
let state = context.wrap(getState());
|
|
73
|
+
state[path] = value;
|
|
74
|
+
dispatch({
|
|
75
|
+
type: "MERGE",
|
|
76
|
+
payload: state,
|
|
77
|
+
});
|
|
78
|
+
} else {
|
|
79
|
+
dispatch({
|
|
80
|
+
type: "SET",
|
|
81
|
+
payload: {
|
|
82
|
+
name: path,
|
|
83
|
+
value,
|
|
84
|
+
context,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
handler: () => {
|
|
92
|
+
let state = context ? context.wrap(getState()) : getState();
|
|
93
|
+
formControl.updateFormControl(node, helpers.getValueAtPath(path, state));
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
[constants.EVENT]: (
|
|
98
|
+
{ node, eventType, actionType, context },
|
|
99
|
+
{ dispatch, getState }
|
|
100
|
+
) => {
|
|
101
|
+
/*
|
|
102
|
+
|
|
103
|
+
NB that context is only passed for local actions not prefixed with "$"
|
|
104
|
+
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
node.addEventListener(eventType, (event) => {
|
|
108
|
+
let isGlobal = actionType.startsWith("$");
|
|
109
|
+
|
|
110
|
+
let action = {
|
|
111
|
+
type: actionType,
|
|
112
|
+
event,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
if (!isGlobal)
|
|
116
|
+
action.context = context ? context.wrap(getState()) : getState();
|
|
117
|
+
|
|
118
|
+
dispatch(action);
|
|
119
|
+
|
|
120
|
+
if (isGlobal) {
|
|
121
|
+
node.dispatchEvent(
|
|
122
|
+
new CustomEvent(actionType, {
|
|
123
|
+
detail: action,
|
|
124
|
+
bubbles: true,
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
handler: () => {},
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
[constants.REPEAT]: (
|
|
134
|
+
{
|
|
135
|
+
node,
|
|
136
|
+
context: context$1,
|
|
137
|
+
map,
|
|
138
|
+
path,
|
|
139
|
+
identifier,
|
|
140
|
+
index,
|
|
141
|
+
key,
|
|
142
|
+
blockIndex,
|
|
143
|
+
hydrate,
|
|
144
|
+
pickupNode,
|
|
145
|
+
},
|
|
146
|
+
{ getState }
|
|
147
|
+
) => {
|
|
148
|
+
let oldValue;
|
|
149
|
+
node.$t = blockIndex - 1;
|
|
150
|
+
|
|
151
|
+
const initialiseBlock = (rootNode, i, k, exitNode) => {
|
|
152
|
+
helpers.walk(
|
|
153
|
+
rootNode,
|
|
154
|
+
multi(
|
|
155
|
+
(node) => {
|
|
156
|
+
if (node === exitNode) return false
|
|
157
|
+
},
|
|
158
|
+
bindAll(
|
|
159
|
+
map,
|
|
160
|
+
hydrate,
|
|
161
|
+
context.createContext(
|
|
162
|
+
(context$1?.get() || []).concat({
|
|
163
|
+
path,
|
|
164
|
+
identifier,
|
|
165
|
+
key,
|
|
166
|
+
index,
|
|
167
|
+
i,
|
|
168
|
+
k,
|
|
169
|
+
})
|
|
170
|
+
)
|
|
171
|
+
),
|
|
172
|
+
(child) => (child.$t = blockIndex)
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
function firstChild(v) {
|
|
178
|
+
return (v.nodeType === v.DOCUMENT_FRAGMENT_NODE && v.firstChild) || v
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const createListItem = (datum, i) => {
|
|
182
|
+
let k = datum[key];
|
|
183
|
+
let frag = helpers.fragmentFromTemplate(node);
|
|
184
|
+
initialiseBlock(firstChild(frag), i, k);
|
|
185
|
+
return frag
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
if (hydrate) {
|
|
189
|
+
let x = helpers.getValueAtPath(path, getState());
|
|
190
|
+
let blocks = list.getBlocks(node);
|
|
191
|
+
|
|
192
|
+
blocks.forEach((block, i) => {
|
|
193
|
+
let datum = x[i];
|
|
194
|
+
let k = datum?.[key];
|
|
195
|
+
initialiseBlock(block[0], i, k, helpers.last(block).nextSibling);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
pickupNode = helpers.last(helpers.last(blocks)).nextSibling;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
handler: () => {
|
|
203
|
+
let state = context$1 ? context$1.wrap(getState()) : getState();
|
|
204
|
+
|
|
205
|
+
const newValue = Object.entries(helpers.getValueAtPath(path, state) || []);
|
|
206
|
+
const delta = list.compareKeyedLists(key, oldValue, newValue);
|
|
207
|
+
|
|
208
|
+
if (delta) {
|
|
209
|
+
list.updateList(node, delta, newValue, createListItem);
|
|
210
|
+
}
|
|
211
|
+
oldValue = newValue.slice(0);
|
|
212
|
+
},
|
|
213
|
+
pickupNode,
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const mediator = () => {
|
|
219
|
+
const o = observer();
|
|
220
|
+
return {
|
|
221
|
+
bind(v) {
|
|
222
|
+
let s = createSubscription[v.type](v, { getState, dispatch });
|
|
223
|
+
o.subscribe(s.handler);
|
|
224
|
+
return s
|
|
225
|
+
},
|
|
226
|
+
// scheduleUpdate: debounce((state, cb) => {
|
|
227
|
+
// return o.publish(state, cb)
|
|
228
|
+
// }),
|
|
229
|
+
update(cb) {
|
|
230
|
+
return o.publish(cb)
|
|
231
|
+
},
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const { bind, update } = mediator();
|
|
236
|
+
|
|
237
|
+
let blockCount = 0;
|
|
238
|
+
|
|
239
|
+
const parse = (frag) => {
|
|
240
|
+
let index = 0;
|
|
241
|
+
let map = {};
|
|
242
|
+
|
|
243
|
+
helpers.walk(frag, (node) => {
|
|
244
|
+
let x = [];
|
|
245
|
+
let pickupNode;
|
|
246
|
+
switch (node.nodeType) {
|
|
247
|
+
case node.TEXT_NODE: {
|
|
248
|
+
let value = node.textContent;
|
|
249
|
+
if (token.hasMustache(value)) {
|
|
250
|
+
x.push({
|
|
251
|
+
type: constants.TEXT,
|
|
252
|
+
value,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
case node.ELEMENT_NODE: {
|
|
258
|
+
let each = list.parseEach(node);
|
|
259
|
+
|
|
260
|
+
if (each) {
|
|
261
|
+
let ns = node.namespaceURI;
|
|
262
|
+
let m;
|
|
263
|
+
|
|
264
|
+
if (ns.endsWith("/svg")) {
|
|
265
|
+
node.removeAttribute("each");
|
|
266
|
+
let tpl = document.createElementNS(ns, "defs");
|
|
267
|
+
tpl.innerHTML = node.outerHTML;
|
|
268
|
+
node.parentNode.replaceChild(tpl, node);
|
|
269
|
+
node = tpl;
|
|
270
|
+
m = parse(node.firstChild);
|
|
271
|
+
} else {
|
|
272
|
+
if (node.nodeName !== "TEMPLATE") {
|
|
273
|
+
node.removeAttribute("each");
|
|
274
|
+
let tpl = document.createElement("template");
|
|
275
|
+
|
|
276
|
+
tpl.innerHTML = node.outerHTML;
|
|
277
|
+
node.parentNode.replaceChild(tpl, node);
|
|
278
|
+
node = tpl;
|
|
279
|
+
}
|
|
280
|
+
m = parse(node.content.firstChild);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
pickupNode = node.nextSibling;
|
|
284
|
+
|
|
285
|
+
x.push({
|
|
286
|
+
type: constants.REPEAT,
|
|
287
|
+
map: m,
|
|
288
|
+
blockIndex: blockCount++,
|
|
289
|
+
...each,
|
|
290
|
+
pickupNode,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
break
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let attrs = node.attributes;
|
|
297
|
+
let i = attrs.length;
|
|
298
|
+
while (i--) {
|
|
299
|
+
let { name, value } = attrs[i];
|
|
300
|
+
|
|
301
|
+
if (
|
|
302
|
+
name === ":name" &&
|
|
303
|
+
value &&
|
|
304
|
+
(node.nodeName === "INPUT" ||
|
|
305
|
+
node.nodeName === "SELECT" ||
|
|
306
|
+
node.nodeName === "TEXTAREA")
|
|
307
|
+
) {
|
|
308
|
+
x.push({
|
|
309
|
+
type: constants.INPUT,
|
|
310
|
+
path: value,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
node.removeAttribute(name);
|
|
314
|
+
node.setAttribute("name", value);
|
|
315
|
+
} else if (name.startsWith(":on")) {
|
|
316
|
+
node.removeAttribute(name);
|
|
317
|
+
let eventType = name.split(":on")[1];
|
|
318
|
+
x.push({
|
|
319
|
+
type: constants.EVENT,
|
|
320
|
+
eventType,
|
|
321
|
+
actionType: value,
|
|
322
|
+
});
|
|
323
|
+
} else if (name.startsWith(":")) {
|
|
324
|
+
let prop = name.slice(1);
|
|
325
|
+
|
|
326
|
+
let v = value || prop;
|
|
327
|
+
|
|
328
|
+
if (!v.includes("{{")) v = `{{${v}}}`;
|
|
329
|
+
|
|
330
|
+
x.push({
|
|
331
|
+
type: constants.ATTRIBUTE,
|
|
332
|
+
name: prop,
|
|
333
|
+
value: v,
|
|
334
|
+
});
|
|
335
|
+
node.removeAttribute(name);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (x.length) map[index] = x;
|
|
341
|
+
index++;
|
|
342
|
+
return pickupNode
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
return map
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const multi =
|
|
349
|
+
(...fns) =>
|
|
350
|
+
(...args) => {
|
|
351
|
+
for (let fn of fns) {
|
|
352
|
+
let v = fn(...args);
|
|
353
|
+
if (v === false) return false
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const bindAll = (bMap, hydrate = 0, context) => {
|
|
358
|
+
let index = 0;
|
|
359
|
+
return (node) => {
|
|
360
|
+
let k = index;
|
|
361
|
+
let p;
|
|
362
|
+
if (k in bMap) {
|
|
363
|
+
bMap[k].forEach((v) => {
|
|
364
|
+
let x = bind({
|
|
365
|
+
...v,
|
|
366
|
+
node,
|
|
367
|
+
context,
|
|
368
|
+
hydrate,
|
|
369
|
+
});
|
|
370
|
+
p = x.pickupNode;
|
|
371
|
+
});
|
|
372
|
+
node.$i = index;
|
|
373
|
+
}
|
|
374
|
+
index++;
|
|
375
|
+
return p
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
let frag = helpers.fragmentFromTemplate(template);
|
|
380
|
+
let map = parse(frag);
|
|
381
|
+
let hydrate = target.hasAttribute?.(HYDRATE_ATTR);
|
|
382
|
+
if (hydrate) {
|
|
383
|
+
helpers.walk(target, bindAll(map, 1));
|
|
384
|
+
} else {
|
|
385
|
+
helpers.walk(frag, bindAll(map));
|
|
386
|
+
beforeMountCallback?.(frag);
|
|
387
|
+
target.prepend(frag);
|
|
388
|
+
update();
|
|
389
|
+
target.setAttribute?.(HYDRATE_ATTR, 1);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return helpers.debounce(() => update(updatedCallback))
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
exports.render = render;
|
package/cjs/token.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var helpers = require('./helpers.js');
|
|
6
|
+
|
|
7
|
+
const VALUE = 1;
|
|
8
|
+
const KEY = 2;
|
|
9
|
+
const FUNCTION = 3;
|
|
10
|
+
|
|
11
|
+
const hasMustache = (v) => v.match(/({{[^{}]+}})/);
|
|
12
|
+
|
|
13
|
+
const getParts = (value) =>
|
|
14
|
+
value
|
|
15
|
+
.trim()
|
|
16
|
+
.split(/({{[^{}]+}})/)
|
|
17
|
+
.filter((v) => v)
|
|
18
|
+
.map((value) => {
|
|
19
|
+
let match = value.match(/{{([^{}]+)}}/);
|
|
20
|
+
|
|
21
|
+
if (!match)
|
|
22
|
+
return {
|
|
23
|
+
type: VALUE,
|
|
24
|
+
value,
|
|
25
|
+
negated: false,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
value = match[1].trim();
|
|
29
|
+
let negated = value.charAt(0) === "!";
|
|
30
|
+
if (negated) value = value.slice(1);
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
type: KEY,
|
|
34
|
+
value,
|
|
35
|
+
negated,
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const getValueFromParts = (target, parts) => {
|
|
40
|
+
return parts.reduce((a, part) => {
|
|
41
|
+
let { type, value, negated } = part;
|
|
42
|
+
|
|
43
|
+
let v;
|
|
44
|
+
|
|
45
|
+
if (type === VALUE) v = value;
|
|
46
|
+
if (type === KEY) {
|
|
47
|
+
v = helpers.getValueAtPath(value, target);
|
|
48
|
+
}
|
|
49
|
+
if (type === FUNCTION) {
|
|
50
|
+
let args = part.args.map((value) => helpers.getValueAtPath(value, target));
|
|
51
|
+
|
|
52
|
+
v = helpers.getValueAtPath(part.method, target)?.(...args);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (negated) v = !v;
|
|
56
|
+
|
|
57
|
+
return a || a === 0 ? a + v : v
|
|
58
|
+
}, "")
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
exports.getParts = getParts;
|
|
62
|
+
exports.getValueFromParts = getValueFromParts;
|
|
63
|
+
exports.hasMustache = hasMustache;
|