-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
398 lines (340 loc) · 11.4 KB
/
index.html
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8" />
<title>Tower Stats Editor</title>
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict/ace.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/libs/lz-string.min.js"></script>
<link rel='stylesheet' id='theme-link'>
<link rel='stylesheet' id='iconlib-link'>
<style>
#fileInput {
display: none;
}
</style>
</head>
<body>
<div class='container grid-xl' style="padding: 15px 0;">
<div class="row columns md:flex">
<div class='col-8 col-md-8 w-8/12'>
<h1>Editor</h1>
<p>Below is the editor generated from the JSON Schema.</p>
<div id="json-editor-form"></div>
</div>
<div class='col-4 col-md-4 w-4/12'>
<h2>Tower Selection</h2>
<select id='schema-select' name="theme" class='form-control browser-default'>
</select>
<br />
<h2>Validation</h2>
<label for="validate-textarea">This will update whenever the form changes to show validation errors if
there
are any.</label><br>
<textarea id='validate-textarea' readonly disabled class='form-control'></textarea><br />
<h2>JSON Output</h2>
<p id="pretext">You can also make changes to the JSON here and set the value in the editor by clicking
"Update Form"
</p>
<input type="file" id="fileInput" />
<button class='btn btn-primary btn-block' id='downloadvalue'>Download Stats</button>
<button class='btn btn-primary btn-block' onclick="document.getElementById('fileInput').click()">Import
Stats</button>
<button class='btn btn-primary btn-block' id='setvalue'>Update Editor</button>
<textarea id='output-textarea' rows="15" style="width: 100%; font-family: monospace;"
class='form-control'></textarea>
</div>
</div>
</div>
<script>
const schemaSelect = document.querySelector('#schema-select')
const fileSelect = document.querySelector('#fileInput')
var schemaSelectOptions = []
var schemaSelectValue = 'Warden';
var defaultSchema = {}
const isFile = window.location.protocol == 'file:'
const urlBase = isFile ? 'https://raw.githubusercontent.com/paradoxum-games/tower-stats/main/' : ''
const compiledStatsSchema = `${urlBase}CompiledStats_schema.json`
fetch(compiledStatsSchema).then(response => {
return response.json()
}).then(data => {
if (!data.properties) return;
// populate the schema select
schemaSelectOptions = Object.keys(data.properties).map(key => {
return `<option value="${key}">${key}</option>`
})
schemaSelect.innerHTML = schemaSelectOptions.join('')
defaultSchema = data
refreshUI()
})
// parse url -> merge options -> refreshUI() -> initJsoneditor() -> direct link
/* ------------------------------------------------------------------- data */
var data = {}
var defaultOptions = {
iconlib: 'fontawesome5',
object_layout: 'normal',
show_errors: 'interaction',
theme: 'bootstrap5',
disable_edit_json: true,
disable_properties: true,
disable_array_reorder: true,
// disable_array_delete_all_rows: true,
// disable_array_delete_last_row: true,
// disable_array_add: true,
// disable_array_delete: true,
}
var jsoneditor = null
var head = document.getElementsByTagName('head')[0]
var jsonEditorForm = document.querySelector('#json-editor-form')
var setValue = document.querySelector('#setvalue')
var downloadValue = document.querySelector('#downloadvalue')
var themeLink = document.querySelector('#theme-link')
var iconlibLink = document.querySelector('#iconlib-link')
var validateTextarea = document.querySelector('#validate-textarea')
var aceConfig = {
mode: 'ace/mode/json',
selection: false,
maxLines: Infinity,
minLines: 5
}
var outputTextarea = ace.edit('output-textarea', aceConfig);
outputTextarea.setTheme('ace/theme/twilight');
/* -------------------------------------------------------------- parse url */
var parseUrl = function () {
var url = window.location.search
var queryParamsString = url.substring(1, url.length)
var queryParams = queryParamsString.split('&')
if (queryParamsString.length) {
queryParams.forEach(function (queryParam) {
var splittedParam = queryParam.split('=')
var param = splittedParam[0]
var value = splittedParam[1]
// data query param
if (param === 'data') {
// compress schema and value
try {
data = JSON.parse(LZString.decompressFromBase64(value))
} catch (reason) { }
}
})
}
mergeOptions()
}
/* ----------------------------------------------------------- mergeOptions */
var mergeOptions = function () {
data.options = Object.assign(defaultOptions, data.options)
refreshUI()
}
/* -------------------------------------------------------------- refreshUI */
var refreshUI = function () {
// theme
var themeMap = {
bootstrap5: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
}
// iconlLib
var iconLibMap = {
fontawesome5: 'https://use.fontawesome.com/releases/v5.6.1/css/all.css',
}
// link icon and theme
themeLink.href = themeMap[data.options.theme]
iconlibLink.href = iconLibMap[data.options.iconlib]
// libs
var libMapping = {
ace_editor: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/src-min-noconflict/ace.js'
],
css: []
},
choices: {
js: [
'https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js'
],
css: [
'https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css'
]
},
cleavejs: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/cleave.min.js'
],
css: []
},
sceditor: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/minified/sceditor.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/minified/formats/bbcode.js',
'https://cdn.jsdelivr.net/npm/[email protected]/minified/formats/xhtml.js'
],
css: [
'https://cdn.jsdelivr.net/npm/[email protected]/minified/themes/default.min.css'
]
},
simplemde: {
js: [
'https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js'
],
css: [
'https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css'
]
},
select2: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js'
],
css: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css'
]
},
selectize: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/standalone/selectize.min.js'
],
css: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/selectize.min.css',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/selectize.default.min.css'
]
},
flatpickr: {
js: [
'https://cdn.jsdelivr.net/npm/flatpickr'
],
css: [
'https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css'
]
},
signature_pad: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/signature_pad.min.js'
],
css: []
},
mathjs: {
js: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/math.min.js'
],
css: []
},
}
if (data.selectedLibs || data.unselectedLibs) {
// remove libraries
data.unselectedLibs.forEach(function (selectedLib) {
var concat = libMapping[selectedLib].js.concat(libMapping[selectedLib].css)
concat.forEach(function () {
var className = '.external_' + selectedLib
var toRemove = head.querySelector(className)
if (toRemove) {
toRemove.parentNode.removeChild(toRemove)
}
})
})
// add libraries
data.selectedLibs.forEach(function (selectedLib) {
// add js
libMapping[selectedLib].js.forEach(function (js) {
var scriptElement = document.createElement('script')
scriptElement.type = 'text/javascript'
scriptElement.src = js
scriptElement.async = false
scriptElement.classList.add('external_' + selectedLib)
head.appendChild(scriptElement)
})
// add css
libMapping[selectedLib].css.forEach(function (css) {
var linkElement = document.createElement('link')
linkElement.setAttribute('rel', 'stylesheet')
linkElement.setAttribute('type', 'text/css')
linkElement.setAttribute('href', css)
linkElement.classList.add('external_' + selectedLib)
head.appendChild(linkElement)
})
})
}
initJsoneditor()
}
/* --------------------------------------------------------- initJsoneditor */
var initJsoneditor = function () {
// destroy old JSONEditor instance if exists
if (jsoneditor) {
jsoneditor.destroy()
}
if (!defaultSchema || !defaultSchema.properties || !defaultSchema.properties[schemaSelectValue]) {
return
}
// new instance of JSONEditor
jsoneditor = new window.JSONEditor(jsonEditorForm, Object.assign({}, data.options, {
schema: defaultSchema.properties[schemaSelectValue],
}))
jsoneditor.theme.options.object_background = "bg-dark"
console.log(jsoneditor)
// listen for changes
jsoneditor.on('change', function () {
// output
var json = {
[schemaSelectValue]: jsoneditor.getValue()
}
outputTextarea.setValue(JSON.stringify(json, null, 2))
// validate
var validationErrors = jsoneditor.validate()
if (validationErrors.length) {
validateTextarea.value = JSON.stringify(validationErrors, null, 2)
} else {
validateTextarea.value = 'valid'
}
})
}
/* ------------------------------------------------------- saveFile */
function saveFile(filename, data) {
const blob = new Blob([data], {
type: 'application/json'
});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
/* -------------------------------------------------------- event listeners */
setValue.addEventListener('click', function () {
const value = JSON.parse(outputTextarea.getValue());
const data = value[schemaSelectValue];
if (!data) return;
jsoneditor.setValue(data)
})
downloadValue.addEventListener('click', function () {
const value = outputTextarea.getValue();
if (!jsoneditor || jsoneditor.validate().length) return;
saveFile(`${schemaSelectValue.toLowerCase()}-stats.json`, value);
})
fileSelect.addEventListener('change', function () {
var file = fileSelect.files[0]
var reader = new FileReader()
reader.onload = function (e) {
var json = JSON.parse(e.target.result)
if (!json) {
alert('Invalid JSON file');
return;
} else if (!json[schemaSelectValue]) {
alert('Invalid JSON file for ' + schemaSelectValue);
return;
}
outputTextarea.setValue(JSON.stringify(json, null, 2))
jsoneditor.setValue(json[schemaSelectValue])
}
reader.readAsText(file)
})
schemaSelect.addEventListener('change', function () {
schemaSelectValue = schemaSelect.value
refreshUI();
})
parseUrl()
</script>
</body>
</html>