-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcustom-element.js
53 lines (43 loc) · 1.78 KB
/
custom-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const Elm = require('./JsonFormCustomElement');
const css = require('../stylesheets/standalone.css').toString();
customElements.define('json-form',
class extends HTMLElement {
static get observedAttributes() {
return ['value', 'schema'];
}
constructor() {
super();
const appRoot = document.createElement('div');
const appStyles = document.createElement('style');
appStyles.textContent = css;
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(appStyles);
shadowRoot.appendChild(appRoot);
const json = this.getAttribute('value');
const schema = JSON.parse(this.getAttribute('schema'));
const value = JSON.parse(json);
const app = Elm.JsonFormCustomElement.embed(appRoot, { schema, value });
this.app = app;
this.muteAttributeChange = false;
app.ports.value.subscribe(({ value, isValid }) => {
const event = new CustomEvent('change', { detail: { value, isValid } });
this.muteAttributeChange = true;
this.setAttribute('value', JSON.stringify(v));
this.muteAttributeChange = false;
this.dispatchEvent(event);
});
}
attributeChangedCallback(name, oldValue, newValue) {
if (this.muteAttributeChange) {
return;
}
switch (name) {
case 'value':
this.app.ports.valueChange.send(JSON.parse(newValue));
break;
case 'schema':
this.app.ports.schemaChange.send(JSON.parse(newValue));
break;
}
}
});