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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JSON Editor Example</title>
<!-- placeholders for the theme switcher -->
<link rel='stylesheet' id='theme_stylesheet'>
<link rel='stylesheet' id='icon_stylesheet'>
<style>[class*="foundicon-"] {font-family: GeneralFoundicons;font-style: normal;}</style>
<script src="../../../javascript/json-editor/jsoneditor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lz-string@1.4.4/libs/lz-string.min.js"></script>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='span8 col-md-8 columns eight large-8 col s8'>
<h2>Editor</h2>
<p>Below is the editor generated from the JSON Schema.</p>
<div id='editor'></div>
</div>
<div class='span4 col-md-4 columns four large-4 col s4'>
<div>
<a href='#' id='direct_link'>Direct Link</a> (preserves schema, value, and options)
</div>
<h2>JSON Output</h2>
<p>You can also make changes to the JSON here and set the value in the editor by clicking <button class='btn btn-primary' id='setvalue'>Update Form</button></p>
<textarea id='output' style='width: 100%; height: 300px; font-family: monospace;' class='form-control'></textarea>
<h2>Options</h2>
<div id='options_holder'>
<div>
<label>CSS Framework</label>
<select id='theme_switcher' class='form-control browser-default'>
<option value='barebones'>Barebones</option>
<option value='html'>HTML</option>
<option value='jqueryui'>jQuery UI</option>
<option value='bootstrap2'>Bootstrap 2</option>
<option value='bootstrap3'>Bootstrap 3</option>
<option value='bootstrap4'>Bootstrap 4</option>
<option value='foundation3'>Foundation 3</option>
<option value='foundation4'>Foundation 4</option>
<option value='foundation5'>Foundation 5</option>
<option value='foundation6'>Foundation 6</option>
<option value='materialize'>Materialize</option>
</select>
</div>
<div>
<label>Icon Library</label>
<select id='icon_switcher' class='form-control browser-default'>
<option value=''>None</option>
<option value='jqueryui'>jQuery UI</option>
<option value='bootstrap2'>Bootstrap 2 Glyphicons</option>
<option value='bootstrap3'>Bootstrap 3 Glyphicons</option>
<option value='foundation2'>Foundicons 2</option>
<option value='foundation3'>Foundicons 3</option>
<option value='fontawesome3'>FontAwesome 3</option>
<option value='fontawesome4'>FontAwesome 4</option>
<option value='fontawesome5'>FontAwesome 5</option>
<option value='materialicons'>Material Icons</option>
</select>
</div>
<div>
<label>Object Layout</label>
<select id='object_layout' class='form-control browser-default'>
<option value='normal'>normal</option>
<option value='grid'>grid</option>
</select>
</div>
<div>
<label>Show Errors</label>
<select id='show_errors' class='form-control browser-default'>
<option value='interaction'>On Interaction</option>
<option value='change'>On Field Change</option>
<option value='always'>Always</option>
<option value='never'>Never</option>
</select>
</div>
<div>
<label>Boolean options</label>
<select multiple size="9" id="boolean_options" style="width: 100%; height: inherit;" class="form-control browser-default">
<option value='required_by_default'>Object properties required by default</option>
<option value='display_required_only'>Only show required properties by default</option>
<option value='no_additional_properties'>No additional object properties</option>
<option value='ajax'>Allow loading schemas via Ajax</option>
<option value='disable_edit_json'>Disable "Edit JSON" buttons</option>
<option value='disable_collapse'>Disable collapse buttons</option>
<option value='disable_properties'>Disable properties buttons</option>
<option value='disable_array_add'>Disable array add buttons</option>
<option value='disable_array_reorder'>Disable array move buttons</option>
<option value='disable_array_delete'>Disable array delete buttons</option>
<option value='disable_array_delete_all_rows'>Disable array delete all rows buttons</option>
<option value='disable_array_delete_last_row'>Disable array delete last row buttons</option>
</select>
</div>
</div>
<h2>Validation</h2>
<p>This will update whenever the form changes to show validation errors if there are any.</p>
<textarea id='validate' style='width: 100%; height: 100px; font-family: monospace;' readonly disabled class='form-control'></textarea>
</div>
</div>
<div class='row'>
<div class='row'>
<div class='span12 col-md-12 columns twelve large-12 col s12'>
<h2>Schema</h2>
<p>You can change the schema and see how the generated form looks. After you make changes, click <button class='btn btn-primary' id='setschema'>Update Schema</button></p>
<textarea id='schema' style='width: 100%; height: 450px; font-family: monospace;' class='form-control'></textarea>
</div>
</div>
<div class='row'>
<div class='span12 col-md-12 columns twelve large-12 col s12'>
<h2>Code</h2>
<pre><code>// Set default options
JSONEditor.defaults.options.theme = 'bootstrap2';
// Initialize the editor
var editor = new JSONEditor(document.getElementById("editor_holder"),{
schema: {
type: "object",
properties: {
name: { "type": "string" }
}
}
});
// Set the value
editor.setValue({
name: "John Smith"
});
// Get the value
var data = editor.getValue();
console.log(data.name); // "John Smith"
// Validate
var errors = editor.validate();
if(errors.length) {
// Not valid
}
// Listen for changes
editor.on("change", function() {
// Do something...
});</code></pre>
</div>
</div>
</div>
</div>
<script>
(function() {
var schema;
if(window.location.href.match('[?&]schema=([^&]+)')) {
try {
schema = JSON.parse(LZString.decompressFromBase64(window.location.href.match('[?&]schema=([^&]+)')[1]));
}
catch(e) {
console.log('invalid starting schema');
}
}
// Default starting schema
if(!schema) {
schema = {
title: "Person",
type: "object",
"required": [
'name',
'age',
'favorite_color',
'gender',
'location',
'pets'
],
properties: {
name: {
type: "string",
description: "First and Last name",
minLength: 4,
default: "Jeremy Dorn"
},
age: {
type: "integer",
default: 25,
minimum: 18,
maximum: 99
},
favorite_color: {
type: "string",
format: "color",
title: "favorite color",
default: "#ffa500"
},
gender: {
type: "string",
enum: ["male", "female"]
},
location: {
type: "object",
title: "Location",
properties: {
city: {
type: "string",
default: "San Francisco"
},
state: {
type: "string",
default: "CA"
},
citystate: {
type: "string",
description: "This is generated automatically from the previous two fields",
template: "{{city}}, {{state}}",
watch: {
city: 'location.city',
state: 'location.state'
}
}
}
},
pets: {
type: "array",
format: "table",
title: "Pets",
uniqueItems: true,
items: {
type: "object",
title: "Pet",
properties: {
type: {
type: "string",
enum: ["cat","dog","bird","reptile","other"],
default: "dog"
},
name: {
type: "string"
}
}
},
default: [
{
type: "dog",
name: "Walter"
}
]
}
}
}
}
// Divs/textareas on the page
var $schema = document.getElementById('schema');
var $output = document.getElementById('output');
var $editor = document.getElementById('editor');
var $validate = document.getElementById('validate');
// Buttons
var $set_schema_button = document.getElementById('setschema');
var $set_value_button = document.getElementById('setvalue');
var jsoneditor;
var updateDirectLink = function() {
var url = window.location.href.replace(/\?.*/,'');
url += '?schema='+LZString.compressToBase64(JSON.stringify(schema));
url += '&value='+LZString.compressToBase64(JSON.stringify(jsoneditor.getValue()));
for(var i in JSONEditor.defaults.options) {
if(!JSONEditor.defaults.options.hasOwnProperty(i)) continue;
if(JSONEditor.defaults.options[i]===false) continue;
else if(JSONEditor.defaults.options[i]===true) {
url += '&'+i;
}
else {
url += '&'+i+'='+JSONEditor.defaults.options[i];
}
}
document.getElementById('direct_link').href = url;
};
var reload = function(keep_value) {
var startval = (jsoneditor && keep_value)? jsoneditor.getValue() : window.startval;
window.startval = undefined;
if(jsoneditor) jsoneditor.destroy();
jsoneditor = new JSONEditor($editor,{
schema: schema,
startval: startval
});
window.jsoneditor = jsoneditor;
// When the value of the editor changes, update the JSON output and validation message
jsoneditor.on('change',function() {
var json = jsoneditor.getValue();
$output.value = JSON.stringify(json,null,2);
var validation_errors = jsoneditor.validate();
// Show validation errors if there are any
if(validation_errors.length) {
$validate.value = JSON.stringify(validation_errors,null,2);
}
else {
$validate.value = 'valid';
}
updateDirectLink();
// Materialize extra.
if (window.Materialize) window.Materialize.updateTextFields();
});
};
// Start the schema and output textareas with initial values
$schema.value = JSON.stringify(schema,null,2);
$output.value = '';
// When the 'update form' button is clicked, set the editor's value
$set_value_button.addEventListener('click',function() {
jsoneditor.setValue(JSON.parse($output.value));
});
// Update the schema when the button is clicked
$set_schema_button.addEventListener('click',function() {
try {
schema = JSON.parse($schema.value);
}
catch(e) {
alert('Invalid Schema: '+e.message);
return;
}
reload();
});
// Set the theme by loading the right stylesheets
var setTheme = function(theme,no_reload) {
theme = theme || '';
var mapping = {
barebones: '',
html: '',
bootstrap2: '//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css',
bootstrap3: '//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
bootstrap4: '//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
foundation3: '//cdnjs.cloudflare.com/ajax/libs/foundation/3.2.5/stylesheets/foundation.css',
foundation4: '//cdnjs.cloudflare.com/ajax/libs/foundation/4.3.2/css/foundation.min.css',
foundation5: '//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.min.css',
foundation6: '//cdnjs.cloudflare.com/ajax/libs/foundation/6.2.4/foundation.min.css',
jqueryui: '//code.jquery.com/ui/1.10.3/themes/south-street/jquery-ui.css',
materialize: '//cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css'
};
if(typeof mapping[theme] === 'undefined') {
theme = 'bootstrap3';
document.getElementById('theme_switcher').value = theme;
}
var scriptMapping = {
materialize: [
'https://code.jquery.com/jquery-3.2.1.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js'
]
}
var themeScripts = scriptMapping[theme],
head = document.getElementsByTagName('head')[0],
script;
if (typeof themeScripts == 'string') { themeScripts = [themeScripts]; }
if (Array.isArray(themeScripts)) {
for (var i = 0; i < themeScripts.length; i++) {
script = document.createElement('script');
script.type = 'text/javascript';
script.src = themeScripts[i];
head.appendChild(script);
}
}
JSONEditor.defaults.options.theme = theme;
document.getElementById('theme_stylesheet').href = mapping[theme];
document.getElementById('theme_switcher').value = JSONEditor.defaults.options.theme;
if(!no_reload) reload(true);
};
// Set the icontheme
// Set the theme by loading the right stylesheets
var setIconlib = function(iconlib,no_reload) {
iconlib = iconlib || '';
var mapping = {
foundation2: '//cdnjs.cloudflare.com/ajax/libs/foundicons/2.0/stylesheets/general_foundicons.css',
foundation3: '//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css',
fontawesome3: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.css',
fontawesome4: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css',
fontawesome5: 'https://use.fontawesome.com/releases/v5.6.1/css/all.css',
materialicons: 'https://fonts.googleapis.com/icon?family=Material+Icons'
};
JSONEditor.defaults.options.iconlib = iconlib;
document.getElementById('icon_stylesheet').href = mapping[iconlib] || '';
document.getElementById('icon_switcher').value = JSONEditor.defaults.options.iconlib;
if(!no_reload) reload(true);
};
var refreshBooleanOptions = function(no_reload) {
var boolean_options = document.getElementById('boolean_options').children;
for(var i=0; i<boolean_options.length; i++) {
JSONEditor.defaults.options[boolean_options[i].value] = boolean_options[i].selected;
}
if(!no_reload) reload(true);
};
// Change listeners for options
document.getElementById('theme_switcher').addEventListener('change',function() {
setTheme(this.value);
});
document.getElementById('icon_switcher').addEventListener('change',function() {
setIconlib(this.value);
});
document.getElementById('object_layout').addEventListener('change',function() {
JSONEditor.defaults.options.object_layout = this.value;
reload(true);
});
document.getElementById('show_errors').addEventListener('change',function() {
JSONEditor.defaults.options.show_errors = this.value;
reload(true);
});
document.getElementById('boolean_options').addEventListener('change',function() {
refreshBooleanOptions();
});
// Get starting value from url
if(window.location.href.match('[?&]value=([^&]+)')) {
window.startval = JSON.parse(LZString.decompressFromBase64(window.location.href.match('[?&]value=([^&]+)')[1]));
}
// Set options from direct link
setTheme((window.location.href.match(/[?&]theme=([^&]+)/)||[])[1] || 'bootstrap2', true);
setIconlib((window.location.href.match(/[?&]iconlib=([^&]*)/)||[null,'fontawesome4'])[1], true);
document.getElementById('object_layout').value = (window.location.href.match(/[?&]object_layout=([^&]+)/)||[])[1] || 'normal';
JSONEditor.defaults.options.object_layout = document.getElementById('object_layout').value;
document.getElementById('show_errors').value = (window.location.href.match(/[?&]show_errors=([^&]+)/)||[])[1] || 'interaction';
JSONEditor.defaults.options.show_errors = document.getElementById('show_errors').value;
var boolean_options = document.getElementById('boolean_options').children;
for(var i=0; i<boolean_options.length; i++) {
if(window.location.href.match(new RegExp('[?&]'+boolean_options[i].getAttribute('value')+'([&=]|$)'))) {
boolean_options[i].selected = true;
}
}
refreshBooleanOptions(true);
reload();
})();
</script>
</body>
</html>
|