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
package/src/proxy.js ADDED
@@ -0,0 +1,36 @@
1
+ import { typeOf } from "./helpers.js"
2
+
3
+ export const proxy = (root, callback) => {
4
+ let proxyCache = new WeakMap()
5
+
6
+ function createProxy(target, handler) {
7
+ let proxy = proxyCache.get(target)
8
+ if (proxy === undefined) {
9
+ proxy = new Proxy(target, handler)
10
+ proxyCache.set(target, proxy)
11
+ }
12
+ return proxy
13
+ }
14
+
15
+ const handler = {
16
+ get(target, property) {
17
+ if (["Object", "Array"].includes(typeOf(target[property]))) {
18
+ return createProxy(target[property], handler)
19
+ } else {
20
+ return Reflect.get(...arguments)
21
+ }
22
+ },
23
+ set(target, property, value) {
24
+ if (value === target[property]) return true
25
+
26
+ callback()
27
+ return Reflect.set(...arguments)
28
+ },
29
+ deleteProperty() {
30
+ callback()
31
+ return Reflect.deleteProperty(...arguments)
32
+ },
33
+ }
34
+
35
+ return new Proxy(root, handler)
36
+ }
package/src/render.js ADDED
@@ -0,0 +1,386 @@
1
+ import { TEXT, ATTRIBUTE, INPUT, EVENT, REPEAT } from "./constants.js"
2
+ import { updateFormControl } from "./formControl.js"
3
+ import {
4
+ debounce,
5
+ fragmentFromTemplate,
6
+ getValueAtPath,
7
+ last,
8
+ walk,
9
+ } from "./helpers.js"
10
+ import { compareKeyedLists, getBlocks, parseEach, updateList } from "./list.js"
11
+ import { getParts, getValueFromParts, hasMustache } from "./token.js"
12
+ import { applyAttribute } from "./attribute.js"
13
+ import { createContext } from "./context.js"
14
+
15
+ export 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 _ = (a, b) => (a === undefined ? b : a)
38
+
39
+ const createSubscription = {
40
+ [TEXT]: ({ value, node, context }, { getState }) => {
41
+ return {
42
+ handler: () => {
43
+ let state = context ? context.wrap(getState()) : getState()
44
+ let a = node.textContent
45
+ let b = getValueFromParts(state, getParts(value))
46
+ if (a !== b) node.textContent = b
47
+ },
48
+ }
49
+ },
50
+ [ATTRIBUTE]: ({ value, node, name, context }, { getState }) => {
51
+ return {
52
+ handler: () => {
53
+ let state = context ? context.wrap(getState()) : getState()
54
+ let b = getValueFromParts(state, getParts(value))
55
+
56
+ applyAttribute(node, name, b)
57
+
58
+ if (node.nodeName === "OPTION") {
59
+ let path = node.parentNode.getAttribute("name")
60
+ let selected = getValueAtPath(path, state)
61
+ node.selected = selected === b
62
+ }
63
+ },
64
+ }
65
+ },
66
+ [INPUT]: ({ node, path, context }, { getState, dispatch }) => {
67
+ node.addEventListener("input", () => {
68
+ let value =
69
+ node.getAttribute("type") === "checkbox" ? node.checked : node.value
70
+
71
+ if (value.trim?.().length && !isNaN(value)) value = +value
72
+
73
+ if (context) {
74
+ let state = context.wrap(getState())
75
+ state[path] = value
76
+ dispatch({
77
+ type: "MERGE",
78
+ payload: state,
79
+ })
80
+ } else {
81
+ dispatch({
82
+ type: "SET",
83
+ payload: {
84
+ name: path,
85
+ value,
86
+ context,
87
+ },
88
+ })
89
+ }
90
+ })
91
+
92
+ return {
93
+ handler: () => {
94
+ let state = context ? context.wrap(getState()) : getState()
95
+ updateFormControl(node, getValueAtPath(path, state))
96
+ },
97
+ }
98
+ },
99
+ [EVENT]: (
100
+ { node, eventType, actionType, context },
101
+ { dispatch, getState }
102
+ ) => {
103
+ /*
104
+
105
+ NB that context is only passed for local actions not prefixed with "$"
106
+
107
+ */
108
+
109
+ node.addEventListener(eventType, (event) => {
110
+ let action = {
111
+ type: actionType,
112
+ event,
113
+ context: context ? context.wrap(getState()) : getState(),
114
+ }
115
+
116
+ dispatch(action)
117
+
118
+ if (actionType.startsWith("$")) {
119
+ node.dispatchEvent(
120
+ new CustomEvent(actionType, {
121
+ detail: action,
122
+ bubbles: true,
123
+ })
124
+ )
125
+ }
126
+ })
127
+ return {
128
+ handler: () => {},
129
+ }
130
+ },
131
+ [REPEAT]: (
132
+ {
133
+ node,
134
+ context,
135
+ map,
136
+ path,
137
+ identifier,
138
+ index,
139
+ key,
140
+ blockIndex,
141
+ hydrate,
142
+ pickupNode,
143
+ },
144
+ { getState }
145
+ ) => {
146
+ let oldValue
147
+ node.$t = blockIndex - 1
148
+
149
+ const initialiseBlock = (rootNode, i, k, exitNode) => {
150
+ walk(
151
+ rootNode,
152
+ multi(
153
+ (node) => {
154
+ if (node === exitNode) return false
155
+ },
156
+ bindAll(
157
+ map,
158
+ hydrate,
159
+ createContext(
160
+ (context?.get() || []).concat({
161
+ path,
162
+ identifier,
163
+ key,
164
+ index,
165
+ i,
166
+ k,
167
+ })
168
+ )
169
+ ),
170
+ (child) => (child.$t = blockIndex)
171
+ )
172
+ )
173
+ }
174
+
175
+ function firstChild(v) {
176
+ return (v.nodeType === v.DOCUMENT_FRAGMENT_NODE && v.firstChild) || v
177
+ }
178
+
179
+ const createListItem = (datum, i) => {
180
+ let k = datum[key]
181
+ let frag = fragmentFromTemplate(node)
182
+ initialiseBlock(firstChild(frag), i, k)
183
+ return frag
184
+ }
185
+
186
+ if (hydrate) {
187
+ let x = getValueAtPath(path, getState())
188
+ let blocks = getBlocks(node)
189
+
190
+ blocks.forEach((block, i) => {
191
+ let datum = x[i]
192
+ let k = datum?.[key]
193
+ initialiseBlock(block[0], i, k, last(block).nextSibling)
194
+ })
195
+
196
+ pickupNode = last(last(blocks)).nextSibling
197
+ }
198
+
199
+ return {
200
+ handler: () => {
201
+ let state = context ? context.wrap(getState()) : getState()
202
+
203
+ const newValue = Object.entries(getValueAtPath(path, state) || [])
204
+ const delta = compareKeyedLists(key, oldValue, newValue)
205
+
206
+ if (delta) {
207
+ updateList(node, delta, newValue, createListItem)
208
+ }
209
+ oldValue = newValue.slice(0)
210
+ },
211
+ pickupNode,
212
+ }
213
+ },
214
+ }
215
+
216
+ const mediator = () => {
217
+ const o = observer()
218
+ return {
219
+ bind(v) {
220
+ let s = createSubscription[v.type](v, { getState, dispatch })
221
+ o.subscribe(s.handler)
222
+ return s
223
+ },
224
+ // scheduleUpdate: debounce((state, cb) => {
225
+ // return o.publish(state, cb)
226
+ // }),
227
+ update(cb) {
228
+ return o.publish(cb)
229
+ },
230
+ }
231
+ }
232
+
233
+ const { bind, update } = mediator()
234
+
235
+ let blockCount = 0
236
+
237
+ const parse = (frag) => {
238
+ let index = 0
239
+ let map = {}
240
+
241
+ walk(frag, (node) => {
242
+ let x = []
243
+ let pickupNode
244
+ switch (node.nodeType) {
245
+ case node.TEXT_NODE: {
246
+ let value = node.textContent
247
+ if (hasMustache(value)) {
248
+ x.push({
249
+ type: TEXT,
250
+ value,
251
+ })
252
+ }
253
+ break
254
+ }
255
+ case node.ELEMENT_NODE: {
256
+ let each = parseEach(node)
257
+
258
+ if (each) {
259
+ let ns = node.namespaceURI
260
+ let m
261
+
262
+ if (ns.endsWith("/svg")) {
263
+ node.removeAttribute("each")
264
+ let tpl = document.createElementNS(ns, "defs")
265
+ tpl.innerHTML = node.outerHTML
266
+ node.parentNode.replaceChild(tpl, node)
267
+ node = tpl
268
+ m = parse(node.firstChild)
269
+ } else {
270
+ if (node.nodeName !== "TEMPLATE") {
271
+ node.removeAttribute("each")
272
+ let tpl = document.createElement("template")
273
+
274
+ tpl.innerHTML = node.outerHTML
275
+ node.parentNode.replaceChild(tpl, node)
276
+ node = tpl
277
+ }
278
+ m = parse(node.content.firstChild)
279
+ }
280
+
281
+ pickupNode = node.nextSibling
282
+
283
+ x.push({
284
+ type: REPEAT,
285
+ map: m,
286
+ blockIndex: blockCount++,
287
+ ...each,
288
+ pickupNode,
289
+ })
290
+
291
+ break
292
+ }
293
+
294
+ let attrs = node.attributes
295
+ let i = attrs.length
296
+ while (i--) {
297
+ let { name, value } = attrs[i]
298
+
299
+ if (
300
+ name === ":name" &&
301
+ value &&
302
+ (node.nodeName === "INPUT" ||
303
+ node.nodeName === "SELECT" ||
304
+ node.nodeName === "TEXTAREA")
305
+ ) {
306
+ x.push({
307
+ type: INPUT,
308
+ path: value,
309
+ })
310
+
311
+ node.removeAttribute(name)
312
+ node.setAttribute("name", value)
313
+ } else if (name.startsWith(":on")) {
314
+ node.removeAttribute(name)
315
+ let eventType = name.split(":on")[1]
316
+ x.push({
317
+ type: EVENT,
318
+ eventType,
319
+ actionType: value,
320
+ })
321
+ } else if (name.startsWith(":")) {
322
+ let prop = name.slice(1)
323
+
324
+ let v = value || prop
325
+
326
+ if (!v.includes("{{")) v = `{{${v}}}`
327
+
328
+ x.push({
329
+ type: ATTRIBUTE,
330
+ name: prop,
331
+ value: v,
332
+ })
333
+ node.removeAttribute(name)
334
+ }
335
+ }
336
+ }
337
+ }
338
+ if (x.length) map[index] = x
339
+ index++
340
+ return pickupNode
341
+ })
342
+
343
+ return map
344
+ }
345
+
346
+ const multi =
347
+ (...fns) =>
348
+ (...args) => {
349
+ for (let fn of fns) {
350
+ let v = fn(...args)
351
+ if (v === false) return false
352
+ }
353
+ }
354
+
355
+ const bindAll = (bMap, hydrate = 0, context) => {
356
+ let index = 0
357
+ return (node) => {
358
+ let k = index
359
+ let p
360
+ if (k in bMap) {
361
+ bMap[k].forEach((v) => {
362
+ let x = bind({
363
+ ...v,
364
+ node,
365
+ context,
366
+ hydrate,
367
+ })
368
+ p = x.pickupNode
369
+ })
370
+ node.$i = index
371
+ }
372
+ index++
373
+ return p
374
+ }
375
+ }
376
+
377
+ let frag = fragmentFromTemplate(template)
378
+ let map = parse(frag)
379
+
380
+ walk(frag, bindAll(map))
381
+ beforeMountCallback?.(frag)
382
+ target.prepend(frag)
383
+ update()
384
+
385
+ return debounce(() => update(updatedCallback))
386
+ }
package/src/token.js ADDED
@@ -0,0 +1,55 @@
1
+ import { getValueAtPath } from "./helpers.js"
2
+
3
+ const VALUE = 1
4
+ const KEY = 2
5
+ const FUNCTION = 3
6
+
7
+ export const hasMustache = (v) => v.match(/({{[^{}]+}})/)
8
+
9
+ export const getParts = (value) =>
10
+ value
11
+ .trim()
12
+ .split(/({{[^{}]+}})/)
13
+ .filter((v) => v)
14
+ .map((value) => {
15
+ let match = value.match(/{{([^{}]+)}}/)
16
+
17
+ if (!match)
18
+ return {
19
+ type: VALUE,
20
+ value,
21
+ negated: false,
22
+ }
23
+
24
+ value = match[1].trim()
25
+ let negated = value.charAt(0) === "!"
26
+ if (negated) value = value.slice(1)
27
+
28
+ return {
29
+ type: KEY,
30
+ value,
31
+ negated,
32
+ }
33
+ })
34
+
35
+ export const getValueFromParts = (target, parts) => {
36
+ return parts.reduce((a, part) => {
37
+ let { type, value, negated } = part
38
+
39
+ let v
40
+
41
+ if (type === VALUE) v = value
42
+ if (type === KEY) {
43
+ v = getValueAtPath(value, target)
44
+ }
45
+ if (type === FUNCTION) {
46
+ let args = part.args.map((value) => getValueAtPath(value, target))
47
+
48
+ v = getValueAtPath(part.method, target)?.(...args)
49
+ }
50
+
51
+ if (negated) v = !v
52
+
53
+ return a || a === 0 ? a + v : v
54
+ }, "")
55
+ }
package/src/update.js ADDED
@@ -0,0 +1,122 @@
1
+ function systemReducer(state, action) {
2
+ switch (action.type) {
3
+ case "SET": {
4
+ const { name, value } = action.payload
5
+ return {
6
+ ...state,
7
+ [name]: value,
8
+ }
9
+ }
10
+ case "MERGE": {
11
+ return {
12
+ ...state,
13
+ ...action.payload,
14
+ }
15
+ }
16
+ }
17
+ }
18
+
19
+ /*
20
+
21
+ Notes
22
+
23
+ - any call to dispatch will immediately invoke the reducer
24
+ - any call to dispatch within middleware is also immediately processed
25
+ - SET and MERGE are not interceptable by middleware
26
+ - middleware functions are assumed to call next synchronously
27
+ - the render function (subscriber) is invoked once for every call to dispatch
28
+ - the render function is debounced
29
+
30
+ */
31
+
32
+ export function configure(
33
+ { update = {}, middleware = [], derivations = {}, initialState = {} },
34
+ node
35
+ ) {
36
+ let subscribers = []
37
+ let state
38
+ let updatedCallback = () => {}
39
+
40
+ function updateState(o) {
41
+ state = { ...o }
42
+ for (let k in derivations) {
43
+ state[k] = derivations[k](o)
44
+ }
45
+ }
46
+
47
+ updateState(initialState)
48
+
49
+ function getState() {
50
+ return { ...state }
51
+ }
52
+
53
+ function subscribe(fn) {
54
+ subscribers.push(fn)
55
+ }
56
+
57
+ function flush() {
58
+ subscribers.forEach((fn) => fn())
59
+ subscribers = []
60
+ }
61
+
62
+ function onUpdate(fn) {
63
+ updatedCallback = fn
64
+ }
65
+
66
+ function dispatch(action) {
67
+ const { type } = action
68
+ if (type === "SET" || type === "MERGE") {
69
+ updateState(systemReducer(getState(), action))
70
+ } else {
71
+ if (middleware.length) {
72
+ let mw = middleware.slice()
73
+
74
+ let next = (action) => {
75
+ if (mw.length) {
76
+ let x = mw.shift()
77
+
78
+ if (action.type in x) {
79
+ x[action.type](action, next, {
80
+ getState,
81
+ dispatch,
82
+ afterNextRender: subscribe,
83
+ })
84
+ } else {
85
+ next(action)
86
+ }
87
+ } else if (action.type in update) {
88
+ updateState(update[action.type](getState(), action))
89
+ }
90
+ }
91
+
92
+ let x = mw.shift()
93
+
94
+ if (type in x) {
95
+ x[type](action, next, {
96
+ getState,
97
+ dispatch,
98
+ afterNextRender: subscribe,
99
+ })
100
+ } else {
101
+ next(action)
102
+ }
103
+ } else if (type in update) {
104
+ updateState(update[type](getState(), action))
105
+ }
106
+ }
107
+ updatedCallback()
108
+ }
109
+
110
+ for (let actionName in update) {
111
+ if (actionName.startsWith("$")) {
112
+ node.addEventListener(actionName, ({ detail }) => dispatch(detail))
113
+ }
114
+ }
115
+
116
+ return {
117
+ dispatch,
118
+ getState,
119
+ onUpdate,
120
+ flush,
121
+ }
122
+ }
package/SCRATCH.md DELETED
@@ -1,31 +0,0 @@
1
- # SCRATCH (Mosaic)
2
-
3
- Realised yesterday that TS can try to work with JS but also
4
- has great support for JSDoc.
5
-
6
- The main reasoning for using TS i hear is generally
7
- intellisense (or equiv), however types without docs aren't
8
- really that useful.
9
-
10
- If TS can understand JS Doc comments, then potentially you
11
- can have a workflow where...
12
-
13
- - typings are taken from the doc comments and `tsc` is used
14
- to generate a declaration file.
15
- - the doc comments are used to generate....documentation!
16
-
17
- ## compatibility
18
-
19
- There are things that you can write in doc comments that TSC
20
- will understand but not JSDoc.
21
-
22
- This is also true in the other direction.
23
-
24
- There is a project tsdoc that is supposed to work with
25
- anything supported by TSC
26
-
27
- ## goals
28
-
29
- The goal is to have a single script that runs both tsc to
30
- generate a declaration file, and also generate beautiful
31
- docs, either with JSdoc, or TSdoc, I don't really care.
package/cli.js DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env node
2
- import path from 'path';
3
-
4
- import mosaic from './index.js';
5
-
6
- const CWD = process.env.INIT_CWD || process.env.PWD;
7
-
8
- const [key] = process.argv.slice(2);
9
-
10
- const configure = (key) =>
11
- import(path.join(CWD, './mosaic.config.js'))
12
- .then((v) => v.default)
13
- .then((v) => (typeof v === 'function' ? v() : v))
14
- .then((v) => (key ? v[key] : v))
15
- .then(mosaic);
16
-
17
- configure(key);
package/conf.json DELETED
File without changes