mosaic 1.0.9 → 1.0.12

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/render.js DELETED
@@ -1,369 +0,0 @@
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 render = (
14
- target,
15
- { getState, dispatch },
16
- template,
17
- updatedCallback,
18
- beforeMountCallback
19
- ) => {
20
- let observer = () => {
21
- let subscribers = new Set();
22
- return {
23
- publish: (cb) => {
24
- for (let fn of subscribers) {
25
- fn();
26
- }
27
- cb?.();
28
- },
29
- subscribe(fn) {
30
- subscribers.add(fn);
31
- },
32
- }
33
- };
34
-
35
- const createSubscription = {
36
- [constants.TEXT]: ({ value, node, context }, { getState }) => {
37
- return {
38
- handler: () => {
39
- let state = context ? context.wrap(getState()) : getState();
40
- let a = node.textContent;
41
- let b = token.getValueFromParts(state, token.getParts(value));
42
- if (a !== b) node.textContent = b;
43
- },
44
- }
45
- },
46
- [constants.ATTRIBUTE]: ({ value, node, name, context }, { getState }) => {
47
- return {
48
- handler: () => {
49
- let state = context ? context.wrap(getState()) : getState();
50
- let b = token.getValueFromParts(state, token.getParts(value));
51
-
52
- attribute.applyAttribute(node, name, b);
53
-
54
- if (node.nodeName === "OPTION") {
55
- let path = node.parentNode.getAttribute("name");
56
- let selected = helpers.getValueAtPath(path, state);
57
- node.selected = selected === b;
58
- }
59
- },
60
- }
61
- },
62
- [constants.INPUT]: ({ node, path, context }, { getState, dispatch }) => {
63
- node.addEventListener("input", () => {
64
- let value =
65
- node.getAttribute("type") === "checkbox" ? node.checked : node.value;
66
-
67
- if (value.trim?.().length && !isNaN(value)) value = +value;
68
-
69
- if (context) {
70
- let state = context.wrap(getState());
71
- state[path] = value;
72
- dispatch({
73
- type: "MERGE",
74
- payload: state,
75
- });
76
- } else {
77
- dispatch({
78
- type: "SET",
79
- payload: {
80
- name: path,
81
- value,
82
- context,
83
- },
84
- });
85
- }
86
- });
87
-
88
- return {
89
- handler: () => {
90
- let state = context ? context.wrap(getState()) : getState();
91
- formControl.updateFormControl(node, helpers.getValueAtPath(path, state));
92
- },
93
- }
94
- },
95
- [constants.EVENT]: (
96
- { node, eventType, actionType, context },
97
- { dispatch, getState }
98
- ) => {
99
- node.addEventListener(eventType, (event) => {
100
- let action = {
101
- type: actionType,
102
- event,
103
- context: context ? context.wrap(getState()) : getState(),
104
- };
105
-
106
- dispatch(action);
107
- });
108
- return {
109
- handler: () => {},
110
- }
111
- },
112
- [constants.REPEAT]: (
113
- {
114
- node,
115
- context: context$1,
116
- map,
117
- path,
118
- identifier,
119
- index,
120
- key,
121
- blockIndex,
122
- hydrate,
123
- pickupNode,
124
- },
125
- { getState }
126
- ) => {
127
- let oldValue;
128
- node.$t = blockIndex - 1;
129
-
130
- const initialiseBlock = (rootNode, i, k, exitNode) => {
131
- helpers.walk(
132
- rootNode,
133
- multi(
134
- (node) => {
135
- if (node === exitNode) return false
136
- },
137
- bindAll(
138
- map,
139
- hydrate,
140
- context.createContext(
141
- (context$1?.get() || []).concat({
142
- path,
143
- identifier,
144
- key,
145
- index,
146
- i,
147
- k,
148
- })
149
- )
150
- ),
151
- (child) => (child.$t = blockIndex)
152
- )
153
- );
154
- };
155
-
156
- function firstChild(v) {
157
- return (v.nodeType === v.DOCUMENT_FRAGMENT_NODE && v.firstChild) || v
158
- }
159
-
160
- const createListItem = (datum, i) => {
161
- let k = datum[key];
162
- let frag = helpers.fragmentFromTemplate(node);
163
- initialiseBlock(firstChild(frag), i, k);
164
- return frag
165
- };
166
-
167
- if (hydrate) {
168
- let x = helpers.getValueAtPath(path, getState());
169
- let blocks = list.getBlocks(node);
170
-
171
- blocks.forEach((block, i) => {
172
- let datum = x[i];
173
- let k = datum?.[key];
174
- initialiseBlock(block[0], i, k, helpers.last(block).nextSibling);
175
- });
176
-
177
- pickupNode = helpers.last(helpers.last(blocks)).nextSibling;
178
- }
179
-
180
- return {
181
- handler: () => {
182
- let state = context$1 ? context$1.wrap(getState()) : getState();
183
-
184
- const newValue = Object.entries(helpers.getValueAtPath(path, state) || []);
185
- const delta = list.compareKeyedLists(key, oldValue, newValue);
186
-
187
- if (delta) {
188
- list.updateList(node, delta, newValue, createListItem);
189
- }
190
- oldValue = newValue.slice(0);
191
- },
192
- pickupNode,
193
- }
194
- },
195
- };
196
-
197
- const mediator = () => {
198
- const o = observer();
199
- return {
200
- bind(v) {
201
- let s = createSubscription[v.type](v, { getState, dispatch });
202
- o.subscribe(s.handler);
203
- return s
204
- },
205
- // scheduleUpdate: debounce((state, cb) => {
206
- // return o.publish(state, cb)
207
- // }),
208
- update(cb) {
209
- return o.publish(cb)
210
- },
211
- }
212
- };
213
-
214
- const { bind, update } = mediator();
215
-
216
- let blockCount = 0;
217
-
218
- const parse = (frag) => {
219
- let index = 0;
220
- let map = {};
221
-
222
- helpers.walk(frag, (node) => {
223
- let x = [];
224
- let pickupNode;
225
- switch (node.nodeType) {
226
- case node.TEXT_NODE: {
227
- let value = node.textContent;
228
- if (token.hasMustache(value)) {
229
- x.push({
230
- type: constants.TEXT,
231
- value,
232
- });
233
- }
234
- break
235
- }
236
- case node.ELEMENT_NODE: {
237
- let each = list.parseEach(node);
238
-
239
- if (each) {
240
- let ns = node.namespaceURI;
241
- let m;
242
-
243
- if (ns.endsWith("/svg")) {
244
- node.removeAttribute("each");
245
- let tpl = document.createElementNS(ns, "defs");
246
- tpl.innerHTML = node.outerHTML;
247
- node.parentNode.replaceChild(tpl, node);
248
- node = tpl;
249
- m = parse(node.firstChild);
250
- } else {
251
- if (node.nodeName !== "TEMPLATE") {
252
- node.removeAttribute("each");
253
- let tpl = document.createElement("template");
254
-
255
- tpl.innerHTML = node.outerHTML;
256
- node.parentNode.replaceChild(tpl, node);
257
- node = tpl;
258
- }
259
- m = parse(node.content.firstChild);
260
- }
261
-
262
- pickupNode = node.nextSibling;
263
-
264
- x.push({
265
- type: constants.REPEAT,
266
- map: m,
267
- blockIndex: blockCount++,
268
- ...each,
269
- pickupNode,
270
- });
271
-
272
- break
273
- }
274
-
275
- let attrs = node.attributes;
276
- let i = attrs.length;
277
- while (i--) {
278
- let { name, value } = attrs[i];
279
-
280
- if (
281
- name === ":name" &&
282
- value &&
283
- (node.nodeName === "INPUT" ||
284
- node.nodeName === "SELECT" ||
285
- node.nodeName === "TEXTAREA")
286
- ) {
287
- x.push({
288
- type: constants.INPUT,
289
- path: value,
290
- });
291
-
292
- node.removeAttribute(name);
293
- node.setAttribute("name", value);
294
- } else if (name.startsWith(":on")) {
295
- node.removeAttribute(name);
296
- let eventType = name.split(":on")[1];
297
- x.push({
298
- type: constants.EVENT,
299
- eventType,
300
- actionType: value,
301
- });
302
- } else if (name.startsWith(":")) {
303
- let prop = name.slice(1);
304
-
305
- let v = value || prop;
306
-
307
- if (!v.includes("{{")) v = `{{${v}}}`;
308
-
309
- x.push({
310
- type: constants.ATTRIBUTE,
311
- name: prop,
312
- value: v,
313
- });
314
- node.removeAttribute(name);
315
- }
316
- }
317
- }
318
- }
319
- if (x.length) map[index] = x;
320
- index++;
321
- return pickupNode
322
- });
323
-
324
- return map
325
- };
326
-
327
- const multi =
328
- (...fns) =>
329
- (...args) => {
330
- for (let fn of fns) {
331
- let v = fn(...args);
332
- if (v === false) return false
333
- }
334
- };
335
-
336
- const bindAll = (bMap, hydrate = 0, context) => {
337
- let index = 0;
338
- return (node) => {
339
- let k = index;
340
- let p;
341
- if (k in bMap) {
342
- bMap[k].forEach((v) => {
343
- let x = bind({
344
- ...v,
345
- node,
346
- context,
347
- hydrate,
348
- });
349
- p = x.pickupNode;
350
- });
351
- node.$i = index;
352
- }
353
- index++;
354
- return p
355
- }
356
- };
357
-
358
- let frag = helpers.fragmentFromTemplate(template);
359
- let map = parse(frag);
360
-
361
- helpers.walk(frag, bindAll(map));
362
- beforeMountCallback?.(frag);
363
- target.prepend(frag);
364
- update();
365
-
366
- return helpers.debounce(() => update(updatedCallback))
367
- };
368
-
369
- exports.render = render;
package/cjs/token.js DELETED
@@ -1,63 +0,0 @@
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;
package/cjs/update.js DELETED
@@ -1,140 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function systemReducer(state, action) {
6
- switch (action.type) {
7
- case "SET": {
8
- const { name, value } = action.payload;
9
- return {
10
- ...state,
11
- [name]: value,
12
- }
13
- }
14
- case "MERGE": {
15
- return {
16
- ...state,
17
- ...action.payload,
18
- }
19
- }
20
- }
21
- }
22
-
23
- /*
24
-
25
- Notes
26
-
27
- - any call to dispatch will immediately invoke the reducer
28
- - any call to dispatch within middleware is also immediately processed
29
- - SET and MERGE are not interceptable by middleware
30
- - middleware functions are assumed to call next synchronously
31
- - the render function (subscriber) is invoked once for every call to dispatch
32
- - the render function is debounced
33
-
34
- */
35
-
36
- function configure(
37
- { update = {}, middleware = [], derivations = {}, initialState = {} },
38
- node
39
- ) {
40
- let subscribers = [];
41
- let state;
42
- let updatedCallback = () => {};
43
-
44
- let dollarKeys = Object.keys(update)
45
- .concat(...middleware.map(Object.keys))
46
- .filter((v) => v.startsWith("$"));
47
-
48
- function updateState(o) {
49
- state = { ...o };
50
- for (let k in derivations) {
51
- state[k] = derivations[k](o);
52
- }
53
- }
54
-
55
- updateState(initialState);
56
-
57
- function getState() {
58
- return { ...state }
59
- }
60
-
61
- function subscribe(fn) {
62
- subscribers.push(fn);
63
- }
64
-
65
- function flush() {
66
- subscribers.forEach((fn) => fn());
67
- subscribers = [];
68
- }
69
-
70
- function onUpdate(fn) {
71
- updatedCallback = fn;
72
- }
73
-
74
- function dispatch(action) {
75
- const { type } = action;
76
-
77
- if (type.startsWith("$") && !(type in update)) {
78
- node.dispatchEvent(
79
- new CustomEvent(type, {
80
- detail: action,
81
- bubbles: true,
82
- })
83
- );
84
- }
85
-
86
- if (type === "SET" || type === "MERGE") {
87
- updateState(systemReducer(getState(), action));
88
- } else {
89
- if (middleware.length) {
90
- let mw = middleware.slice();
91
-
92
- let next = (action) => {
93
- if (mw.length) {
94
- let x = mw.shift();
95
-
96
- if (action.type in x) {
97
- x[action.type](action, next, {
98
- getState,
99
- dispatch,
100
- afterNextRender: subscribe,
101
- });
102
- } else {
103
- next(action);
104
- }
105
- } else if (action.type in update) {
106
- updateState(update[action.type](getState(), action));
107
- }
108
- };
109
-
110
- let x = mw.shift();
111
-
112
- if (type in x) {
113
- x[type](action, next, {
114
- getState,
115
- dispatch,
116
- afterNextRender: subscribe,
117
- });
118
- } else {
119
- next(action);
120
- }
121
- } else if (type in update) {
122
- updateState(update[type](getState(), action));
123
- }
124
- }
125
- updatedCallback();
126
- }
127
-
128
- for (let actionName of dollarKeys) {
129
- node.addEventListener(actionName, ({ detail }) => dispatch(detail));
130
- }
131
-
132
- return {
133
- dispatch,
134
- getState,
135
- onUpdate,
136
- flush,
137
- }
138
- }
139
-
140
- exports.configure = configure;