|
9 | 9 | constructor() { |
10 | 10 | // establish prototype chain |
11 | 11 | super(); |
12 | | - this.ajvurl = 'https://cdnjs.cloudflare.com/ajax/libs/ajv/6.5.5/ajv.min.js' |
| 12 | + this.ajv_url = 'https://unpkg.com/ajv@6.5.5/dist/ajv.min.js' |
| 13 | + this.json_editor_url = 'https://unpkg.com/jsoneditor@5.25.0'; |
| 14 | + this.promises = {}; |
13 | 15 | } |
14 | 16 |
|
15 | 17 | static get observedAttributes() { |
16 | | - return ['ajv', 'metaschema', 'schema']; |
| 18 | + return ['jsoneditor', 'metaschema', 'schema']; |
17 | 19 | } |
18 | 20 |
|
19 | 21 | // fires after the element has been attached to the DOM |
20 | 22 | connectedCallback() { |
21 | 23 | const uid = Math.ceil((Math.random() * 10e10).toString()) |
22 | 24 | this.id = `schema-validator-${uid}` |
23 | 25 | this.innerHTML = `<div> |
24 | | - <textarea |
25 | | - onChange="event.target.parentElement.parentElement.validateInput()" |
26 | | - onKeyup="event.target.parentElement.parentElement.validateInput()" |
27 | | - style="box-sizing: border-box; height: 500px; padding: 15px; resize: none; width: 100%;" |
28 | | - ></textarea> |
| 26 | + <div id="jsoneditor" style="box-sizing: border-box; height: 500px; padding: 15px; width: 100%;"></div> |
29 | 27 | <div id="schema-validation-errors" style="box-sizing: border-box; padding: 15px; width: 100%;"></div> |
| 28 | + <link href="${this.json_editor_url}/dist/jsoneditor.min.css" rel="stylesheet" type="text/css"> |
30 | 29 | </div>`; |
31 | 30 | this.update(); |
32 | 31 | } |
|
60 | 59 | } |
61 | 60 |
|
62 | 61 | getAjv() { |
63 | | - if (this.ajv) { |
64 | | - return Promise.resolve(this.ajv); |
65 | | - } else { |
66 | | - return new Promise(resolve => { |
| 62 | + return this.loadScript(this.ajv_url).then(() => { |
| 63 | + if (!this.ajv) { |
| 64 | + this.ajv = new window.Ajv({ allErrors: true, schemaId: 'id' }); |
| 65 | + } |
| 66 | + return this.ajv |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + getJSONEditor() { |
| 71 | + return this.loadScript(this.json_editor_url + '/dist/jsoneditor.min.js') |
| 72 | + .then(() => window.JSONEditor) |
| 73 | + } |
| 74 | + |
| 75 | + loadScript(url) { |
| 76 | + if (!this.promises[url]) { |
| 77 | + this.promises[url] = new Promise(resolve => { |
67 | 78 | const script = document.createElement("script"); |
68 | | - script.src = this.ajvurl; |
69 | | - script.onload = () => { |
70 | | - this.ajv = new window.Ajv({ allErrors: true, schemaId: 'id' }); |
71 | | - resolve(this.ajv) |
72 | | - }; |
| 79 | + script.src = url; |
| 80 | + script.onload = resolve |
73 | 81 | document.body.appendChild(script) |
74 | 82 | }) |
75 | 83 | } |
| 84 | + return Promise.resolve(this.promises[url]) |
76 | 85 | } |
77 | 86 |
|
78 | 87 | getStyle() { |
|
103 | 112 | } |
104 | 113 |
|
105 | 114 | update() { |
106 | | - this.ajvurl = this.getAttribute('ajv') || this.ajvurl |
| 115 | + //this.ajvurl = this.getAttribute('ajv') || this.ajvurl |
107 | 116 | this.schemaurl = this.getAttribute('schema') |
108 | 117 | this.metaschemaurl = this.getAttribute('metaschema') |
109 | | - Promise.all([ |
| 118 | + this.updated = Promise.all([ |
110 | 119 | this.getAjv(), |
| 120 | + this.getJSONEditor(), |
111 | 121 | this.getMetaSchema(), |
112 | 122 | this.getSchema() |
113 | | - ]).then(([ajv, metaschema, schema]) => { |
114 | | - console.log("[ajv, metaschema, schema]:", [ajv, metaschema, schema]) |
| 123 | + ]).then(([ajv, JSONEditor, metaschema, schema]) => { |
| 124 | + |
| 125 | + console.log("[ajv, JSONEditor, metaschema, schema]:", [ajv, JSONEditor, metaschema, schema]) |
115 | 126 | try { |
116 | | - this.ajv.addMetaSchema(metaschema); |
| 127 | + ajv.addMetaSchema(metaschema); |
117 | 128 | } catch (error) { |
118 | 129 | console.warn(error); |
119 | 130 | } |
120 | | - this.validate = this.ajv.compile(schema); |
| 131 | + |
| 132 | + if (!this.editor) { |
| 133 | + // create the editor |
| 134 | + var container = this.querySelector("#jsoneditor"); |
| 135 | + var options = { |
| 136 | + ajv, |
| 137 | + mode: 'text', |
| 138 | + modes: ['text', 'tree'] |
| 139 | + }; |
| 140 | + this.editor = new JSONEditor(container, options, ''); |
| 141 | + this.editor.setSchema(schema) |
| 142 | + this.editor.setText('') |
| 143 | + } |
121 | 144 | }) |
122 | 145 | } |
123 | 146 |
|
|
0 commit comments