123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>JSON Editor Upload Example</title>
- <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor/dist/jsoneditor.min.js"></script>
- </head>
- <body>
- <h1>JSON Editor Upload Example</h1>
-
- <div id='editor_holder'></div>
- <button id='submit'>Submit (console.log)</button>
-
- <script>
- // Specify theme
- JSONEditor.defaults.options.theme = 'html';
- // Specify upload handler
- JSONEditor.defaults.options.upload = function(type, file, cbs) {
- if (type === 'root.upload_fail') cbs.failure('Upload failed');
- else {
- var tick = 0;
- var tickFunction = function() {
- tick += 1;
- console.log('progress: ' + tick);
- if (tick < 100) {
- cbs.updateProgress(tick);
- window.setTimeout(tickFunction, 50)
- } else if (tick == 100) {
- cbs.updateProgress();
- window.setTimeout(tickFunction, 500)
- } else {
- cbs.success('http://www.example.com/images/' + file.name);
- }
- };
- window.setTimeout(tickFunction)
- }
- };
- // Initialize the editor with a JSON schema
- var editor = new JSONEditor(document.getElementById('editor_holder'),{
- schema: {
- type: "object",
- title: "Image",
- properties: {
- upload_default: {
- type: "string",
- format: "url",
- options: {
- upload: true
- },
- "links": [
- {
- "href": "{{self}}"
- }
- ]
- },
- upload_custom_link: {
- type: "string",
- format: "url",
- options: {
- upload: true
- },
- "links": [
- {
- "href": "{{self}}",
- "rel": "view"
- }
- ]
- },
- upload_readonly: {
- readonly: true,
- type: "string",
- format: "url",
- options: {
- upload: true
- },
- "links": [
- {
- "href": "{{self}}"
- }
- ]
- },
- upload_fail: {
- type: "string",
- format: "url",
- options: {
- upload: true
- },
- "links": [
- {
- "href": "{{self}}"
- }
- ]
- },
- name: {
- type: "string"
- }
- }
- }
- });
- editor.setValue({
- upload_default: "",
- upload_custom_link: "",
- upload_readonly: "http://www.example.com/images/image.jpg",
- upload_fail: "",
- name: ""
- });
-
- // Hook up the submit button to log to the console
- document.getElementById('submit').addEventListener('click',function() {
- // Get the value from the editor
- console.log(editor.getValue());
- });
- </script>
- </body>
- </html>
|