recursive.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Recursive JSON Editor Example</title>
  6. <!-- Foundation CSS framework (Bootstrap and jQueryUI also supported) -->
  7. <link rel='stylesheet' href='//cdn.jsdelivr.net/bootstrap/3.2.0/css/bootstrap.css'>
  8. <!-- Font Awesome icons (Bootstrap, Foundation, and jQueryUI also supported) -->
  9. <link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css'>
  10. <link rel='stylesheet' href='//cdn.jsdelivr.net/sceditor/1.4.3/jquery.sceditor.default.min.css'>
  11. <link rel='stylesheet' href='//cdn.jsdelivr.net/sceditor/1.4.3/themes/modern.min.css'>
  12. <script src='//cdn.jsdelivr.net/jquery/2.1.1/jquery.min.js'></script>
  13. <script src='//cdn.jsdelivr.net/sceditor/1.4.3/jquery.sceditor.min.js'></script>
  14. <script src='//cdn.jsdelivr.net/sceditor/1.4.3/plugins/xhtml.js'></script>
  15. <script src="//unpkg.com/json-editor"></script>
  16. </head>
  17. <body>
  18. <div class='container'>
  19. <div class='row' style='padding-bottom: 15px;'>
  20. <div class='col-md-12'>
  21. <h1>Recursive JSON Editor Example</h1>
  22. <p>
  23. This example demonstrates the many ways you can use recursive schemas (aka self-referential or circular schemas).
  24. </p>
  25. <ul>
  26. <li>Within array items as long as minLength is 0. See "coworkers" below.</li>
  27. <li>In non-default properties. See "mother" below (click the "object properties" button and check "mother")</li>
  28. <li>In oneOf as long as it's not the 1st choice. See "bestFriend" below.</li>
  29. <li>In patternProperties. Try adding the property "cousin_1" using the "object properties" button.</li>
  30. </ul>
  31. </div>
  32. </div>
  33. <div class='row' style='padding-bottom: 15px;'>
  34. <div class='col-md-12'>
  35. <button id='submit' class='btn btn-info'>Submit (console.log)</button>
  36. <button id='restore' class='btn btn-info'>Restore to Default</button>
  37. <button id='enable_disable' class='btn btn-info'>Disable/Enable Form</button>
  38. <span id='valid_indicator' class='label label-success'></span>
  39. </div>
  40. </div>
  41. <div class='row'>
  42. <div class='col-md-12'>
  43. <div id='editor_holder'></div>
  44. </div>
  45. </div>
  46. </div>
  47. <script>
  48. JSONEditor.defaults.theme = 'bootstrap3';
  49. JSONEditor.defaults.iconlib = 'fontawesome4';
  50. JSONEditor.plugins.sceditor.style = "//cdn.jsdelivr.net/sceditor/1.4.3/jquery.sceditor.default.min.css";
  51. // Initialize the editor
  52. var editor = new JSONEditor(document.getElementById('editor_holder'),{
  53. // The schema for the editor
  54. schema: {
  55. title: "Person",
  56. $ref: "#/definitions/person",
  57. definitions: {
  58. person: {
  59. type: "object",
  60. id: "person",
  61. // The object will start with only these properties
  62. defaultProperties: [
  63. "fname",
  64. "lname",
  65. "bestFriend",
  66. "coworkers"
  67. ],
  68. patternProperties: {
  69. // Self-referntial schema in patternProperties
  70. "^cousin_[0-9]+$": {
  71. $ref: "#/definitions/person"
  72. }
  73. },
  74. properties: {
  75. fname: {
  76. title: "first name",
  77. type: "string"
  78. },
  79. lname: {
  80. title: "last name",
  81. type: "string"
  82. },
  83. bestFriend: {
  84. title: "best friend",
  85. oneOf: [
  86. {
  87. title: "none",
  88. type: "null"
  89. },
  90. // Self-referential schema as 2nd choice in oneOf
  91. {
  92. title: "person",
  93. $ref: "#/definitions/person"
  94. }
  95. ]
  96. },
  97. coworkers: {
  98. type: "array",
  99. // Self-referential schema in array items
  100. items: {
  101. title: "Coworker",
  102. $ref: "#/definitions/person"
  103. }
  104. },
  105. // Self-referential schemas in non-default properties
  106. mother: {
  107. title: "mother",
  108. $ref: "#/definitions/person"
  109. }
  110. }
  111. },
  112. year: {
  113. type: "integer",
  114. pattern: "^[0-9]{4}$",
  115. minimum: 1900,
  116. maximum: 2100
  117. }
  118. }
  119. }
  120. });
  121. // Hook up the submit button to log to the console
  122. document.getElementById('submit').addEventListener('click',function() {
  123. // Get the value from the editor
  124. console.log(editor.getValue());
  125. });
  126. // Hook up the Restore to Default button
  127. document.getElementById('restore').addEventListener('click',function() {
  128. editor.setValue(starting_value);
  129. });
  130. // Hook up the enable/disable button
  131. document.getElementById('enable_disable').addEventListener('click',function() {
  132. // Enable form
  133. if(!editor.isEnabled()) {
  134. editor.enable();
  135. }
  136. // Disable form
  137. else {
  138. editor.disable();
  139. }
  140. });
  141. // Hook up the validation indicator to update its
  142. // status whenever the editor changes
  143. editor.on('change',function() {
  144. // Get an array of errors from the validator
  145. var errors = editor.validate();
  146. var indicator = document.getElementById('valid_indicator');
  147. // Not valid
  148. if(errors.length) {
  149. indicator.className = 'label label-danger'
  150. indicator.textContent = "not valid";
  151. }
  152. // Valid
  153. else {
  154. indicator.className = 'label label-success'
  155. indicator.textContent = "valid";
  156. }
  157. });
  158. </script>
  159. </body>
  160. </html>