upload.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>JSON Editor Upload Example</title>
  6. <script src="//unpkg.com/json-editor"></script>
  7. </head>
  8. <body>
  9. <h1>JSON Editor Upload Example</h1>
  10. <div id='editor_holder'></div>
  11. <button id='submit'>Submit (console.log)</button>
  12. <script>
  13. // Specify theme
  14. JSONEditor.defaults.options.theme = 'html';
  15. // Specify upload handler
  16. JSONEditor.defaults.options.upload = function(type, file, cbs) {
  17. if (type === 'root.upload_fail') cbs.failure('Upload failed');
  18. else {
  19. var tick = 0;
  20. var tickFunction = function() {
  21. tick += 1;
  22. console.log('progress: ' + tick);
  23. if (tick < 100) {
  24. cbs.updateProgress(tick);
  25. window.setTimeout(tickFunction, 50)
  26. } else if (tick == 100) {
  27. cbs.updateProgress();
  28. window.setTimeout(tickFunction, 500)
  29. } else {
  30. cbs.success('http://www.example.com/images/' + file.name);
  31. }
  32. };
  33. window.setTimeout(tickFunction)
  34. }
  35. };
  36. // Initialize the editor with a JSON schema
  37. var editor = new JSONEditor(document.getElementById('editor_holder'),{
  38. schema: {
  39. type: "object",
  40. title: "Image",
  41. properties: {
  42. upload_default: {
  43. type: "string",
  44. format: "url",
  45. options: {
  46. upload: true
  47. },
  48. "links": [
  49. {
  50. "href": "{{self}}"
  51. }
  52. ]
  53. },
  54. upload_custom_link: {
  55. type: "string",
  56. format: "url",
  57. options: {
  58. upload: true
  59. },
  60. "links": [
  61. {
  62. "href": "{{self}}",
  63. "rel": "view"
  64. }
  65. ]
  66. },
  67. upload_readonly: {
  68. readonly: true,
  69. type: "string",
  70. format: "url",
  71. options: {
  72. upload: true
  73. },
  74. "links": [
  75. {
  76. "href": "{{self}}"
  77. }
  78. ]
  79. },
  80. upload_fail: {
  81. type: "string",
  82. format: "url",
  83. options: {
  84. upload: true
  85. },
  86. "links": [
  87. {
  88. "href": "{{self}}"
  89. }
  90. ]
  91. },
  92. name: {
  93. type: "string"
  94. }
  95. }
  96. }
  97. });
  98. editor.setValue({
  99. upload_default: "",
  100. upload_custom_link: "",
  101. upload_readonly: "http://www.example.com/images/image.jpg",
  102. upload_fail: "",
  103. name: ""
  104. });
  105. // Hook up the submit button to log to the console
  106. document.getElementById('submit').addEventListener('click',function() {
  107. // Get the value from the editor
  108. console.log(editor.getValue());
  109. });
  110. </script>
  111. </body>
  112. </html>