core.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <script src="../../dist/jsoneditor.js"></script>
  6. </head>
  7. <body>
  8. <textarea class="value" cols="30" rows="10"></textarea>
  9. <button class='get-value'>get value</button>
  10. <button class='set-value'>set value</button>
  11. <button class='get-individual-value'>get individual value</button>
  12. <button class='set-individual-value'>set individual value</button>
  13. <button class='disable-form'>disable form</button>
  14. <button class='enable-form'>enable form</button>
  15. <button class='disable-part'>disable part</button>
  16. <button class='enable-part'>enable part</button>
  17. <button class='destroy'>destroy</button>
  18. <button class='form-changed' style="background: yellow; color: blue; display: none;">form changed</button>
  19. <button class='name-changed' style="background: green; color: white; display: none;">name changed</button>
  20. <div class='container'></div>
  21. <script>
  22. var container = document.querySelector('.container');
  23. var value = document.querySelector('.value');
  24. var getValue = document.querySelector('.get-value');
  25. var setValue = document.querySelector('.set-value');
  26. var getIndividualValue = document.querySelector('.get-individual-value');
  27. var setIndividualValue = document.querySelector('.set-individual-value');
  28. var disableForm = document.querySelector('.disable-form');
  29. var enableForm = document.querySelector('.enable-form');
  30. var disablePart = document.querySelector('.disable-part');
  31. var enablePart = document.querySelector('.enable-part');
  32. var destroy = document.querySelector('.destroy');
  33. var formChanged = document.querySelector('.form-changed');
  34. var nameChanged = document.querySelector('.name-changed');
  35. var schema = {
  36. "title": "Person",
  37. "type": "object",
  38. "properties": {
  39. "age": {
  40. "title": "Age",
  41. "type": "number",
  42. "format": "number",
  43. "default": 18,
  44. "minimum": 0,
  45. "maximum": 80
  46. },
  47. "name": {
  48. "title": "Name",
  49. "type": "string",
  50. "default": "Francesco Avizzano"
  51. }
  52. }
  53. };
  54. var editor = new JSONEditor(container, {
  55. schema: schema
  56. });
  57. var changeCallback = function () {
  58. formChanged.style.display = 'block'
  59. };
  60. editor.on('change', changeCallback);
  61. var watchCallback = function () {
  62. nameChanged.style.display = 'block'
  63. };
  64. editor.watch('root.name', watchCallback);
  65. getValue.addEventListener('click', function () {
  66. value.value = JSON.stringify(editor.getValue());
  67. console.log(editor.getValue());
  68. });
  69. setValue.addEventListener('click', function () {
  70. editor.setValue({name: "John Smith", age: 40});
  71. });
  72. getIndividualValue.addEventListener('click', function () {
  73. var name = editor.getEditor('root.name');
  74. value.value = JSON.stringify(name.getValue());
  75. });
  76. setIndividualValue.addEventListener('click', function () {
  77. var name = editor.getEditor('root.name');
  78. name.setValue("john kaminski");
  79. value.value = JSON.stringify(name.getValue());
  80. });
  81. disableForm.addEventListener('click', function () {
  82. editor.disable();
  83. });
  84. enableForm.addEventListener('click', function () {
  85. editor.enable();
  86. });
  87. disablePart.addEventListener('click', function () {
  88. editor.getEditor('root.name').disable();
  89. });
  90. enablePart.addEventListener('click', function () {
  91. editor.getEditor('root.name').enable();
  92. });
  93. destroy.addEventListener('click', function () {
  94. editor.destroy();
  95. });
  96. </script>
  97. </body>
  98. </html>