enigmatic 0.10.1 → 0.11.0
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/README.md +0 -0
- package/{components.mjs → components.js} +3 -4
- package/enigmatic.js +120 -120
- package/index.html +19 -0
- package/package.json +1 -1
package/README.md
ADDED
|
File without changes
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
window.components = {
|
|
2
2
|
'hello-world': {
|
|
3
3
|
style: 'color: red',
|
|
4
|
+
onMount: async x => console.log('mounted h-w'),
|
|
4
5
|
template: 'Hello World'
|
|
5
6
|
},
|
|
6
7
|
'random-users': {
|
|
@@ -9,9 +10,7 @@ window.components = {
|
|
|
9
10
|
beforeData: x => x.results[0].name.first = 'John'
|
|
10
11
|
},
|
|
11
12
|
'tailwind-example': {
|
|
12
|
-
template: '<div class="bg-blue-
|
|
13
|
-
onMount: e => loadCSS('https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css')
|
|
13
|
+
template: '<div class="bg-blue-300 text-white font-bold py-2 px-4 rounded">I am Tailwind</div>',
|
|
14
|
+
onMount: async e => await loadCSS('https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css')
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
-
console.log('components.mjs', components)
|
package/enigmatic.js
CHANGED
|
@@ -1,157 +1,157 @@
|
|
|
1
|
+
/*
|
|
2
|
+
enigmatic v 0.11.0 front end js utils
|
|
3
|
+
|
|
4
|
+
Usage:
|
|
5
|
+
<div id='mykey' transform='func1' fetch='some.site/data' immediate>${name} ${value}</div>
|
|
6
|
+
<div id='mykey2' transform='func2' stream='some.site/data'>${name} ${value}</div>
|
|
7
|
+
|
|
8
|
+
$('selector')
|
|
9
|
+
$$('selector')
|
|
10
|
+
await loadJS('sounds.js')
|
|
11
|
+
await loadCSS('enigmatic.css')
|
|
12
|
+
await wait(1000)
|
|
13
|
+
await ready()
|
|
14
|
+
beep()
|
|
15
|
+
element(beforeData=>beforeData.field, '<div>${o.mykey}</div>')
|
|
16
|
+
window.components
|
|
17
|
+
state
|
|
18
|
+
await get
|
|
19
|
+
await stream
|
|
20
|
+
*/
|
|
21
|
+
|
|
1
22
|
const w = {},
|
|
2
|
-
d = document
|
|
23
|
+
d = document
|
|
3
24
|
|
|
4
|
-
|
|
25
|
+
/////// Helpers
|
|
5
26
|
|
|
6
|
-
w.$ = d.querySelector.bind(d)
|
|
7
|
-
w.$$ = d.querySelectorAll.bind(d)
|
|
27
|
+
w.$ = d.querySelector.bind(d)
|
|
28
|
+
w.$$ = d.querySelectorAll.bind(d)
|
|
8
29
|
w.loadJS = (src) => {
|
|
9
30
|
return new Promise((r, j) => {
|
|
10
|
-
if ($(`script[src="${src}"]`)) return r(true)
|
|
11
|
-
const s = d.createElement('script')
|
|
12
|
-
s.src = src
|
|
13
|
-
s.addEventListener('load', r)
|
|
14
|
-
d.head.appendChild(s)
|
|
15
|
-
})
|
|
16
|
-
}
|
|
31
|
+
if ($(`script[src="${src}"]`)) return r(true)
|
|
32
|
+
const s = d.createElement('script')
|
|
33
|
+
s.src = src
|
|
34
|
+
s.addEventListener('load', r)
|
|
35
|
+
d.head.appendChild(s)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
17
38
|
w.loadCSS = (src) => {
|
|
18
39
|
return new Promise((r, j) => {
|
|
19
|
-
const s = document.createElement('link')
|
|
20
|
-
s.rel = 'stylesheet'
|
|
21
|
-
s.href = src
|
|
22
|
-
s.addEventListener('load', r)
|
|
23
|
-
d.head.appendChild(s)
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
w.wait = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
40
|
+
const s = document.createElement('link')
|
|
41
|
+
s.rel = 'stylesheet'
|
|
42
|
+
s.href = src
|
|
43
|
+
s.addEventListener('load', r)
|
|
44
|
+
d.head.appendChild(s)
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
w.wait = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
27
48
|
w.ready = async () => {
|
|
28
49
|
return new Promise((r) => {
|
|
29
|
-
if (document.readyState === 'complete') r(true)
|
|
50
|
+
if (document.readyState === 'complete') r(true)
|
|
30
51
|
document.onreadystatechange = () => {
|
|
31
|
-
if (document.readyState === 'complete') r()
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
w.child = (type, html) => {
|
|
36
|
-
const e = d.createElement(type);
|
|
37
|
-
e.innerHTML = html;
|
|
38
|
-
d.body.appendChild(e);
|
|
39
|
-
return e;
|
|
40
|
-
};
|
|
52
|
+
if (document.readyState === 'complete') r()
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
}
|
|
41
56
|
|
|
42
|
-
|
|
57
|
+
/////// Custom element
|
|
43
58
|
|
|
44
|
-
w.element = (
|
|
45
|
-
|
|
59
|
+
w.element = (
|
|
60
|
+
name,
|
|
61
|
+
{ onMount = x => x, beforeData = (x) => x, style, template = '' }
|
|
62
|
+
) => {
|
|
46
63
|
customElements.define(
|
|
47
64
|
name,
|
|
48
65
|
class extends HTMLElement {
|
|
49
66
|
connectedCallback(props) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
onMount(this)
|
|
68
|
+
if (style) {
|
|
69
|
+
const s = document.createElement('style')
|
|
70
|
+
s.innerHTML = `${name} {${style}}`
|
|
71
|
+
d.body.appendChild(s)
|
|
72
|
+
}
|
|
73
|
+
this.template = template
|
|
74
|
+
if (!this.template.match('{')) this.innerHTML = this.template
|
|
75
|
+
}
|
|
76
|
+
set(o) {
|
|
77
|
+
this.innerHTML = ''
|
|
78
|
+
o = beforeData(o)
|
|
79
|
+
if (!Array.isArray(o)) o = [o]
|
|
80
|
+
const m = new Function('o', 'return `' + this.template + '`')
|
|
81
|
+
o.map((i) => (this.innerHTML += m(i)))
|
|
82
|
+
return true
|
|
54
83
|
}
|
|
55
84
|
}
|
|
56
|
-
)
|
|
57
|
-
}
|
|
85
|
+
)
|
|
86
|
+
}
|
|
58
87
|
|
|
59
|
-
|
|
88
|
+
if (window.components) {
|
|
89
|
+
for (let name in window.components) w.element(name, window.components[name])
|
|
90
|
+
}
|
|
60
91
|
|
|
61
|
-
|
|
62
|
-
{},
|
|
63
|
-
{
|
|
64
|
-
set: (obj, prop, value) => {
|
|
65
|
-
for (const e of $$(`[data*=${prop}]`)) {
|
|
66
|
-
console.log('setting e', e.tagName, e.id, value);
|
|
67
|
-
if (!e.set) {
|
|
68
|
-
e.set = new Function(
|
|
69
|
-
'o',
|
|
70
|
-
'return this.innerHTML = `' +
|
|
71
|
-
e.innerHTML.replaceAll('{', '${o.') +
|
|
72
|
-
'`'
|
|
73
|
-
);
|
|
74
|
-
console.log(e, 'defaulting set', e.set);
|
|
75
|
-
}
|
|
76
|
-
console.log(value);
|
|
77
|
-
e.set(value);
|
|
78
|
-
}
|
|
79
|
-
obj[prop] = value;
|
|
80
|
-
return value;
|
|
81
|
-
},
|
|
82
|
-
get: (obj, prop, receiver) => {
|
|
83
|
-
if (prop == '_state') return obj;
|
|
84
|
-
return obj[prop];
|
|
85
|
-
},
|
|
86
|
-
}
|
|
87
|
-
);
|
|
92
|
+
/////// State, data, and reactivity
|
|
88
93
|
|
|
89
|
-
w.
|
|
94
|
+
w.state = (key, data) => {
|
|
95
|
+
if (!key && !data)
|
|
96
|
+
return localStorage.clear()
|
|
97
|
+
if (data) {
|
|
98
|
+
const stored_data = { when: new Date().toISOString(), data: data }
|
|
99
|
+
localStorage.setItem(key, JSON.stringify(stored_data))
|
|
100
|
+
for (const e of $$(`[data*=${key}]`)) e.set(stored_data.data)
|
|
101
|
+
return stored_data
|
|
102
|
+
}
|
|
103
|
+
return JSON.parse(localStorage.getItem(key))
|
|
104
|
+
}
|
|
90
105
|
|
|
91
|
-
w.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
106
|
+
w.get = async (
|
|
107
|
+
url,
|
|
108
|
+
options = null,
|
|
109
|
+
transform = null,
|
|
110
|
+
key = 'temp'
|
|
111
|
+
) => {
|
|
112
|
+
let data = await (await fetch(`https://${url}`, options)).json()
|
|
113
|
+
if (transform) data = transform(data)
|
|
114
|
+
state(key, data)
|
|
115
|
+
return data
|
|
116
|
+
}
|
|
97
117
|
|
|
98
|
-
w.
|
|
99
|
-
const ev = new EventSource(url)
|
|
118
|
+
w.stream = async (url, key) => {
|
|
119
|
+
const ev = new EventSource(url)
|
|
100
120
|
ev.onmessage = (ev) => {
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// State changes
|
|
109
|
-
|
|
110
|
-
w.trackStateChanges = () =>
|
|
111
|
-
(w.dataEvent = (o) =>
|
|
112
|
-
localStorage.set(new Date().toISOString(), JSON.stringify(o)));
|
|
113
|
-
w.untrackStateChanges = () =>
|
|
114
|
-
(w.dataEvent = (o) => console.log('dataevent:', o));
|
|
121
|
+
const data = JSON.parse(ev.data)
|
|
122
|
+
state(key, data)
|
|
123
|
+
return data
|
|
124
|
+
}
|
|
125
|
+
}
|
|
115
126
|
|
|
116
|
-
|
|
127
|
+
/////// Startup
|
|
117
128
|
|
|
118
129
|
w.start = async () => {
|
|
119
130
|
await w.ready();
|
|
120
131
|
[...$$('div')].map((e) => {
|
|
121
|
-
if (!e.id)
|
|
122
|
-
e.id = (Math.random() + 1).toString(36).substring(7).toUpperCase();
|
|
123
132
|
e.pr = {};
|
|
124
|
-
[...e.attributes].map((a) => (e.pr[a.name] = a.value))
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
if (
|
|
128
|
-
if (!e.stream && e.pr.stream)
|
|
129
|
-
e.stream = streamJSON.bind(null, e.pr.stream, e.id);
|
|
133
|
+
[...e.attributes].map((a) => (e.pr[a.name] = a.value))
|
|
134
|
+
if (e.pr.fetch) e.fetch = w.get.bind(null, e.pr.fetch, null, window[e.pr.transform], e.id)
|
|
135
|
+
if ('immediate' in e.pr) e.fetch()
|
|
136
|
+
if (e.pr.stream) e.stream = w.stream.bind(null, e.pr.stream, null, window[e.pr.transform], e.id)
|
|
130
137
|
if (e.pr.data) {
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
138
|
+
if (e.innerHTML && e.innerHTML.includes('{')) {
|
|
139
|
+
e.template = e.innerHTML.replaceAll('{', '${')
|
|
140
|
+
e.innerHTML = ''
|
|
141
|
+
}
|
|
142
|
+
e.set = (o) => {
|
|
143
|
+
e.innerHTML = ''
|
|
144
|
+
if (!Array.isArray(o)) o = [o]
|
|
145
|
+
const m = new Function('o', 'return `' + e.template + '`')
|
|
146
|
+
o.map((i) => (e.innerHTML += m(i)))
|
|
134
147
|
}
|
|
135
|
-
this.set = (o) => {
|
|
136
|
-
if (!Array.isArray(o)) o = [o];
|
|
137
|
-
const m = new Function('o', 'return `' + this.template + '`');
|
|
138
|
-
o.map((i) => (this.innerHTML += m(o)));
|
|
139
|
-
};
|
|
140
148
|
}
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
const ch = document.createElement('div');
|
|
144
|
-
ch.id = 'testchild';
|
|
145
|
-
ch.html = html;
|
|
146
|
-
parent.appendChild(ch);
|
|
147
|
-
return ch;
|
|
148
|
-
};
|
|
149
|
-
};
|
|
149
|
+
})
|
|
150
|
+
}
|
|
150
151
|
|
|
151
|
-
w.enigmatic = { version: '2022-
|
|
152
|
+
w.enigmatic = { version: '2022-07-24 0.11.0' }
|
|
152
153
|
Object.assign(window, w);
|
|
153
154
|
|
|
154
|
-
(async() => {
|
|
155
|
+
(async () => {
|
|
155
156
|
await w.start()
|
|
156
|
-
|
|
157
|
-
})();
|
|
157
|
+
})()
|
package/index.html
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<script src='enigmatic.js'></script>
|
|
2
|
+
|
|
3
|
+
<div id='tests'></div>
|
|
4
|
+
|
|
5
|
+
<script type='module'>
|
|
6
|
+
|
|
7
|
+
element('my-custom', data=>data.myvalue, '<div>${o.mykey}</div>')
|
|
8
|
+
const e = document.createElement('my-custom')
|
|
9
|
+
e.setAttribute('data', 'mydata')
|
|
10
|
+
|
|
11
|
+
state('mydata', {myvalue: 'my value'})
|
|
12
|
+
|
|
13
|
+
await get('', null, data=>data.myvalue, 'my-fetched-data')
|
|
14
|
+
state('my-fetched-data')
|
|
15
|
+
|
|
16
|
+
await stream('', 'my-streamed-data')
|
|
17
|
+
state('my-streamed-data')
|
|
18
|
+
|
|
19
|
+
</script>
|
package/package.json
CHANGED