mosaic 1.0.3 → 1.0.6

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