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.
- 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 +117 -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 +384 -0
- package/cjs/token.js +63 -0
- package/cjs/update.js +128 -0
- package/dist/mosaic.js +1096 -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 +116 -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 +386 -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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Matt Donkin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,18 +1,73 @@
|
|
|
1
1
|
# mosaic
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
combining HTML assets. You can use it for things like
|
|
5
|
-
creating dev/test servers and build pipelines.
|
|
3
|
+
## [](http://npm.im/mosaic) [](https://travis-ci.com/defx/mosaic) [](https://coveralls.io/github/defx/mosaic?branch=master) []()
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
they want to perform when bundling files together.
|
|
5
|
+
Mosaic is a framework for building user interfaces for the web
|
|
9
6
|
|
|
10
|
-
##
|
|
7
|
+
## Features
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
- Simple templates for declarative data & event binding
|
|
10
|
+
- Reactive data bindings update your view efficiently and
|
|
11
|
+
automatically
|
|
12
|
+
- Full component workflow using standard Custom Elements
|
|
13
|
+
- Small footprint (~4k)
|
|
14
|
+
- No special compiler, plugins, required
|
|
15
|
+
- Minimal learning curve (almost entirely standard HTML, JS,
|
|
16
|
+
and CSS!)
|
|
17
|
+
- Interoperable with other libraries and frameworks
|
|
18
|
+
|
|
19
|
+
[Learn how to use Mosaic in your own project](https://mosaicjs.org/learn/introduction).
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Mosaic is available from npm:
|
|
13
24
|
|
|
14
25
|
```bash
|
|
15
26
|
$ npm i mosaic
|
|
16
27
|
```
|
|
17
28
|
|
|
18
|
-
|
|
29
|
+
You can also import Mosaic directly in the browser via CDN:
|
|
30
|
+
|
|
31
|
+
```html
|
|
32
|
+
<script type="module">
|
|
33
|
+
import { define } from "https://unpkg.com/mosaic@1.0.4"
|
|
34
|
+
</script>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Browser Support
|
|
38
|
+
|
|
39
|
+
(todo)
|
|
40
|
+
|
|
41
|
+
## Documentation
|
|
42
|
+
|
|
43
|
+
(todo)
|
|
44
|
+
|
|
45
|
+
## Example
|
|
46
|
+
|
|
47
|
+
### Step 1. Define your custom element
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script type="module">
|
|
51
|
+
import { define } from "https://unpkg.com/mosaic@1.0.4"
|
|
52
|
+
|
|
53
|
+
define("hello-world", () => ({ name }), "<p>Hello {{ name }}!</p>")
|
|
54
|
+
</script>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Step 2. Use the Custom Element
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<hello-world name="kimberley"></hello-world>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This example will render "Hello Kimberley!" into a container
|
|
64
|
+
on the page.
|
|
65
|
+
|
|
66
|
+
You'll notice that everything here is valid HTML and JS, and
|
|
67
|
+
you can copy and paste this example and run it directly in
|
|
68
|
+
the browser with no need to compile or install anything
|
|
69
|
+
special to make it work.
|
|
70
|
+
|
|
71
|
+
### License
|
|
72
|
+
|
|
73
|
+
Mosaic is [MIT licensed](./LICENSE).
|
package/cjs/attribute.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
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 = {} } = 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
|
+
let observedProps = Object.keys(initialState).filter(
|
|
33
|
+
(v) => v.charAt(0) === "$"
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
let observedAttributes = observedProps
|
|
37
|
+
.map((v) => v.slice(1))
|
|
38
|
+
.map(helpers.pascalToKebab);
|
|
39
|
+
|
|
40
|
+
let sa = this.setAttribute;
|
|
41
|
+
this.setAttribute = (k, v) => {
|
|
42
|
+
if (observedAttributes.includes(k)) {
|
|
43
|
+
let { name, value } = helpers.attributeToProp(k, v);
|
|
44
|
+
dispatch({
|
|
45
|
+
type: "SET",
|
|
46
|
+
payload: { name: "$" + name, value },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
sa.apply(this, [k, v]);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
observedAttributes.forEach((name) => {
|
|
53
|
+
let property = helpers.attributeToProp(name).name;
|
|
54
|
+
|
|
55
|
+
let value;
|
|
56
|
+
|
|
57
|
+
if (this.hasAttribute(name)) {
|
|
58
|
+
value = this.getAttribute(name);
|
|
59
|
+
} else {
|
|
60
|
+
value = this[property] || initialState["$" + property];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
Object.defineProperty(this, property, {
|
|
64
|
+
get() {
|
|
65
|
+
return getState()["$" + property]
|
|
66
|
+
},
|
|
67
|
+
set(v) {
|
|
68
|
+
dispatch({
|
|
69
|
+
type: "SET",
|
|
70
|
+
payload: { name: "$" + property, value: v },
|
|
71
|
+
});
|
|
72
|
+
if (helpers.isPrimitive(v)) {
|
|
73
|
+
helpers.applyAttribute(this, property, v);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
this[property] = value;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
let beforeMountCallback;
|
|
82
|
+
|
|
83
|
+
if (shadow) {
|
|
84
|
+
this.attachShadow({
|
|
85
|
+
mode: shadow,
|
|
86
|
+
});
|
|
87
|
+
} else {
|
|
88
|
+
beforeMountCallback = (frag) => mergeSlots.mergeSlots(this, frag);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
onUpdate(
|
|
92
|
+
render.render(
|
|
93
|
+
this.shadowRoot || this,
|
|
94
|
+
{ getState, dispatch },
|
|
95
|
+
template,
|
|
96
|
+
() => {
|
|
97
|
+
observedProps.forEach((k) => {
|
|
98
|
+
let v = getState()[k];
|
|
99
|
+
if (helpers.isPrimitive(v)) helpers.applyAttribute(this, k.slice(1), v);
|
|
100
|
+
});
|
|
101
|
+
subscribe?.(getState());
|
|
102
|
+
flush();
|
|
103
|
+
},
|
|
104
|
+
beforeMountCallback
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
this.initialised = true;
|
|
108
|
+
}
|
|
109
|
+
this.connectedCallback?.();
|
|
110
|
+
}
|
|
111
|
+
disconnectedCallback() {
|
|
112
|
+
this.disconnectedCallback?.();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
exports.define = define;
|
|
@@ -0,0 +1,42 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
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
ADDED
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;
|