index.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>JSON Editor Example</title>
  6. <!-- placeholders for the theme switcher -->
  7. <link rel='stylesheet' id='theme_stylesheet'>
  8. <link rel='stylesheet' id='icon_stylesheet'>
  9. <style>[class*="foundicon-"] {font-family: GeneralFoundicons;font-style: normal;}</style>
  10. <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor/dist/jsoneditor.min.js"></script>
  11. <script>
  12. /**
  13. * LZString compression library
  14. * Used to create direct links to the demo.
  15. * NOT REQUIRED for JSON Editor.
  16. * https://github.com/pieroxy/lz-string
  17. */
  18. var LZString={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",_f:String.fromCharCode,compressToBase64:function(e){if(e==null)return"";var t="";var n,r,i,s,o,u,a;var f=0;e=LZString.compress(e);while(f<e.length*2){if(f%2==0){n=e.charCodeAt(f/2)>>8;r=e.charCodeAt(f/2)&255;if(f/2+1<e.length)i=e.charCodeAt(f/2+1)>>8;else i=NaN}else{n=e.charCodeAt((f-1)/2)&255;if((f+1)/2<e.length){r=e.charCodeAt((f+1)/2)>>8;i=e.charCodeAt((f+1)/2)&255}else r=i=NaN}f+=3;s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+LZString._keyStr.charAt(s)+LZString._keyStr.charAt(o)+LZString._keyStr.charAt(u)+LZString._keyStr.charAt(a)}return t},decompressFromBase64:function(e){if(e==null)return"";var t="",n=0,r,i,s,o,u,a,f,l,c=0,h=LZString._f;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(c<e.length){u=LZString._keyStr.indexOf(e.charAt(c++));a=LZString._keyStr.indexOf(e.charAt(c++));f=LZString._keyStr.indexOf(e.charAt(c++));l=LZString._keyStr.indexOf(e.charAt(c++));i=u<<2|a>>4;s=(a&15)<<4|f>>2;o=(f&3)<<6|l;if(n%2==0){r=i<<8;if(f!=64){t+=h(r|s)}if(l!=64){r=o<<8}}else{t=t+h(r|i);if(f!=64){r=s<<8}if(l!=64){t+=h(r|o)}}n+=3}return LZString.decompress(t)},compressToUTF16:function(e){if(e==null)return"";var t="",n,r,i,s=0,o=LZString._f;e=LZString.compress(e);for(n=0;n<e.length;n++){r=e.charCodeAt(n);switch(s++){case 0:t+=o((r>>1)+32);i=(r&1)<<14;break;case 1:t+=o(i+(r>>2)+32);i=(r&3)<<13;break;case 2:t+=o(i+(r>>3)+32);i=(r&7)<<12;break;case 3:t+=o(i+(r>>4)+32);i=(r&15)<<11;break;case 4:t+=o(i+(r>>5)+32);i=(r&31)<<10;break;case 5:t+=o(i+(r>>6)+32);i=(r&63)<<9;break;case 6:t+=o(i+(r>>7)+32);i=(r&127)<<8;break;case 7:t+=o(i+(r>>8)+32);i=(r&255)<<7;break;case 8:t+=o(i+(r>>9)+32);i=(r&511)<<6;break;case 9:t+=o(i+(r>>10)+32);i=(r&1023)<<5;break;case 10:t+=o(i+(r>>11)+32);i=(r&2047)<<4;break;case 11:t+=o(i+(r>>12)+32);i=(r&4095)<<3;break;case 12:t+=o(i+(r>>13)+32);i=(r&8191)<<2;break;case 13:t+=o(i+(r>>14)+32);i=(r&16383)<<1;break;case 14:t+=o(i+(r>>15)+32,(r&32767)+32);s=0;break}}return t+o(i+32)},decompressFromUTF16:function(e){if(e==null)return"";var t="",n,r,i=0,s=0,o=LZString._f;while(s<e.length){r=e.charCodeAt(s)-32;switch(i++){case 0:n=r<<1;break;case 1:t+=o(n|r>>14);n=(r&16383)<<2;break;case 2:t+=o(n|r>>13);n=(r&8191)<<3;break;case 3:t+=o(n|r>>12);n=(r&4095)<<4;break;case 4:t+=o(n|r>>11);n=(r&2047)<<5;break;case 5:t+=o(n|r>>10);n=(r&1023)<<6;break;case 6:t+=o(n|r>>9);n=(r&511)<<7;break;case 7:t+=o(n|r>>8);n=(r&255)<<8;break;case 8:t+=o(n|r>>7);n=(r&127)<<9;break;case 9:t+=o(n|r>>6);n=(r&63)<<10;break;case 10:t+=o(n|r>>5);n=(r&31)<<11;break;case 11:t+=o(n|r>>4);n=(r&15)<<12;break;case 12:t+=o(n|r>>3);n=(r&7)<<13;break;case 13:t+=o(n|r>>2);n=(r&3)<<14;break;case 14:t+=o(n|r>>1);n=(r&1)<<15;break;case 15:t+=o(n|r);i=0;break}s++}return LZString.decompress(t)},compressToUint8Array:function(e){var t=LZString.compress(e);var n=new Uint8Array(t.length*2);for(var r=0,i=t.length;r<i;r++){var s=t.charCodeAt(r);n[r*2]=s>>>8;n[r*2+1]=s%256}return n},decompressFromUint8Array:function(e){if(e===null||e===undefined){return LZString.decompress(e)}else{var t=new Array(e.length/2);for(var n=0,r=t.length;n<r;n++){t[n]=e[n*2]*256+e[n*2+1]}return LZString.decompress(String.fromCharCode.apply(null,t))}},compressToEncodedURIComponent:function(e){return LZString.compressToBase64(e).replace(/=/g,"$").replace(/\//g,"-")},decompressFromEncodedURIComponent:function(e){if(e)e=e.replace(/$/g,"=").replace(/-/g,"/");return LZString.decompressFromBase64(e)},compress:function(e){if(e==null)return"";var t,n,r={},i={},s="",o="",u="",a=2,f=3,l=2,c="",h=0,p=0,d,v=LZString._f;for(d=0;d<e.length;d+=1){s=e.charAt(d);if(!Object.prototype.hasOwnProperty.call(r,s)){r[s]=f++;i[s]=true}o=u+s;if(Object.prototype.hasOwnProperty.call(r,o)){u=o}else{if(Object.prototype.hasOwnProperty.call(i,u)){if(u.charCodeAt(0)<256){for(t=0;t<l;t++){h=h<<1;if(p==15){p=0;c+=v(h);h=0}else{p++}}n=u.charCodeAt(0);for(t=0;t<8;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}}else{n=1;for(t=0;t<l;t++){h=h<<1|n;if(p==15){p=0;c+=v(h);h=0}else{p++}n=0}n=u.charCodeAt(0);for(t=0;t<16;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}}a--;if(a==0){a=Math.pow(2,l);l++}delete i[u]}else{n=r[u];for(t=0;t<l;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}}a--;if(a==0){a=Math.pow(2,l);l++}r[o]=f++;u=String(s)}}if(u!==""){if(Object.prototype.hasOwnProperty.call(i,u)){if(u.charCodeAt(0)<256){for(t=0;t<l;t++){h=h<<1;if(p==15){p=0;c+=v(h);h=0}else{p++}}n=u.charCodeAt(0);for(t=0;t<8;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}}else{n=1;for(t=0;t<l;t++){h=h<<1|n;if(p==15){p=0;c+=v(h);h=0}else{p++}n=0}n=u.charCodeAt(0);for(t=0;t<16;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}}a--;if(a==0){a=Math.pow(2,l);l++}delete i[u]}else{n=r[u];for(t=0;t<l;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}}a--;if(a==0){a=Math.pow(2,l);l++}}n=2;for(t=0;t<l;t++){h=h<<1|n&1;if(p==15){p=0;c+=v(h);h=0}else{p++}n=n>>1}while(true){h=h<<1;if(p==15){c+=v(h);break}else p++}return c},decompress:function(e){if(e==null)return"";if(e=="")return null;var t=[],n,r=4,i=4,s=3,o="",u="",a,f,l,c,h,p,d,v=LZString._f,m={string:e,val:e.charCodeAt(0),position:32768,index:1};for(a=0;a<3;a+=1){t[a]=a}l=0;h=Math.pow(2,2);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}switch(n=l){case 0:l=0;h=Math.pow(2,8);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}d=v(l);break;case 1:l=0;h=Math.pow(2,16);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}d=v(l);break;case 2:return""}t[3]=d;f=u=d;while(true){if(m.index>m.string.length){return""}l=0;h=Math.pow(2,s);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}switch(d=l){case 0:l=0;h=Math.pow(2,8);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}t[i++]=v(l);d=i-1;r--;break;case 1:l=0;h=Math.pow(2,16);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}t[i++]=v(l);d=i-1;r--;break;case 2:return u}if(r==0){r=Math.pow(2,s);s++}if(t[d]){o=t[d]}else{if(d===i){o=f+f.charAt(0)}else{return null}}u+=o;t[i++]=f+o.charAt(0);r--;f=o;if(r==0){r=Math.pow(2,s);s++}}}};if(typeof module!=="undefined"&&module!=null){module.exports=LZString}
  19. </script>
  20. </head>
  21. <body>
  22. <div class='container'>
  23. <div class='row'>
  24. <div class='span8 col-md-8 columns eight large-8 col s8'>
  25. <h2>Editor</h2>
  26. <p>Below is the editor generated from the JSON Schema.</p>
  27. <div id='editor'></div>
  28. </div>
  29. <div class='span4 col-md-4 columns four large-4 col s4'>
  30. <div>
  31. <a href='#' id='direct_link'>Direct Link</a> (preserves schema, value, and options)
  32. </div>
  33. <h2>JSON Output</h2>
  34. <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>
  35. <textarea id='output' style='width: 100%; height: 300px; font-family: monospace;' class='form-control'></textarea>
  36. <h2>Options</h2>
  37. <div id='options_holder'>
  38. <div>
  39. <label>CSS Framework</label>
  40. <select id='theme_switcher' class='form-control browser-default'>
  41. <option value='barebones'>Barebones</option>
  42. <option value='html'>HTML</option>
  43. <option value='jqueryui'>jQuery UI</option>
  44. <option value='bootstrap2'>Bootstrap 2</option>
  45. <option value='bootstrap3'>Bootstrap 3</option>
  46. <option value='bootstrap4'>Bootstrap 4</option>
  47. <option value='foundation3'>Foundation 3</option>
  48. <option value='foundation4'>Foundation 4</option>
  49. <option value='foundation5'>Foundation 5</option>
  50. <option value='foundation6'>Foundation 6</option>
  51. <option value='materialize'>Materialize</option>
  52. </select>
  53. </div>
  54. <div>
  55. <label>Icon Library</label>
  56. <select id='icon_switcher' class='form-control browser-default'>
  57. <option value=''>None</option>
  58. <option value='jqueryui'>jQuery UI</option>
  59. <option value='bootstrap2'>Bootstrap 2 Glyphicons</option>
  60. <option value='bootstrap3'>Bootstrap 3 Glyphicons</option>
  61. <option value='foundation2'>Foundicons 2</option>
  62. <option value='foundation3'>Foundicons 3</option>
  63. <option value='fontawesome3'>FontAwesome 3</option>
  64. <option value='fontawesome4'>FontAwesome 4</option>
  65. <option value='materialicons'>Material Icons</option>
  66. </select>
  67. </div>
  68. <div>
  69. <label>Object Layout</label>
  70. <select id='object_layout' class='form-control browser-default'>
  71. <option value='normal'>normal</option>
  72. <option value='grid'>grid</option>
  73. </select>
  74. </div>
  75. <div>
  76. <label>Show Errors</label>
  77. <select id='show_errors' class='form-control browser-default'>
  78. <option value='interaction'>On Interaction</option>
  79. <option value='change'>On Field Change</option>
  80. <option value='always'>Always</option>
  81. <option value='never'>Never</option>
  82. </select>
  83. </div>
  84. <div>
  85. <label>Boolean options</label>
  86. <select multiple size="9" id="boolean_options" style="width: 100%; height: inherit;" class="form-control browser-default">
  87. <option value='required_by_default'>Object properties required by default</option>
  88. <option value='display_required_only'>Only show required properties by default</option>
  89. <option value='no_additional_properties'>No additional object properties</option>
  90. <option value='ajax'>Allow loading schemas via Ajax</option>
  91. <option value='disable_edit_json'>Disable "Edit JSON" buttons</option>
  92. <option value='disable_collapse'>Disable collapse buttons</option>
  93. <option value='disable_properties'>Disable properties buttons</option>
  94. <option value='disable_array_add'>Disable array add buttons</option>
  95. <option value='disable_array_reorder'>Disable array move buttons</option>
  96. <option value='disable_array_delete'>Disable array delete buttons</option>
  97. <option value='disable_array_delete_all_rows'>Disable array delete all rows buttons</option>
  98. <option value='disable_array_delete_last_row'>Disable array delete last row buttons</option>
  99. </select>
  100. </div>
  101. </div>
  102. <h2>Validation</h2>
  103. <p>This will update whenever the form changes to show validation errors if there are any.</p>
  104. <textarea id='validate' style='width: 100%; height: 100px; font-family: monospace;' readonly disabled class='form-control'></textarea>
  105. </div>
  106. </div>
  107. <div class='row'>
  108. <div class='row'>
  109. <div class='span12 col-md-12 columns twelve large-12 col s12'>
  110. <h2>Schema</h2>
  111. <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>
  112. <textarea id='schema' style='width: 100%; height: 450px; font-family: monospace;' class='form-control'></textarea>
  113. </div>
  114. </div>
  115. <div class='row'>
  116. <div class='span12 col-md-12 columns twelve large-12 col s12'>
  117. <h2>Code</h2>
  118. <pre><code>// Set default options
  119. JSONEditor.defaults.options.theme = 'bootstrap2';
  120. // Initialize the editor
  121. var editor = new JSONEditor(document.getElementById("editor_holder"),{
  122. schema: {
  123. type: "object",
  124. properties: {
  125. name: { "type": "string" }
  126. }
  127. }
  128. });
  129. // Set the value
  130. editor.setValue({
  131. name: "John Smith"
  132. });
  133. // Get the value
  134. var data = editor.getValue();
  135. console.log(data.name); // "John Smith"
  136. // Validate
  137. var errors = editor.validate();
  138. if(errors.length) {
  139. // Not valid
  140. }
  141. // Listen for changes
  142. editor.on("change", function() {
  143. // Do something...
  144. });</code></pre>
  145. </div>
  146. </div>
  147. </div>
  148. </div>
  149. <script>
  150. (function() {
  151. var schema;
  152. if(window.location.href.match('[?&]schema=([^&]+)')) {
  153. try {
  154. schema = JSON.parse(LZString.decompressFromBase64(window.location.href.match('[?&]schema=([^&]+)')[1]));
  155. }
  156. catch(e) {
  157. console.log('invalid starting schema');
  158. }
  159. }
  160. // Default starting schema
  161. if(!schema) {
  162. schema = {
  163. title: "Person",
  164. type: "object",
  165. "required": [
  166. 'name',
  167. 'age',
  168. 'favorite_color',
  169. 'gender',
  170. 'location',
  171. 'pets'
  172. ],
  173. properties: {
  174. name: {
  175. type: "string",
  176. description: "First and Last name",
  177. minLength: 4,
  178. default: "Jeremy Dorn"
  179. },
  180. age: {
  181. type: "integer",
  182. default: 25,
  183. minimum: 18,
  184. maximum: 99
  185. },
  186. favorite_color: {
  187. type: "string",
  188. format: "color",
  189. title: "favorite color",
  190. default: "#ffa500"
  191. },
  192. gender: {
  193. type: "string",
  194. enum: ["male", "female"]
  195. },
  196. location: {
  197. type: "object",
  198. title: "Location",
  199. properties: {
  200. city: {
  201. type: "string",
  202. default: "San Francisco"
  203. },
  204. state: {
  205. type: "string",
  206. default: "CA"
  207. },
  208. citystate: {
  209. type: "string",
  210. description: "This is generated automatically from the previous two fields",
  211. template: "{{city}}, {{state}}",
  212. watch: {
  213. city: 'location.city',
  214. state: 'location.state'
  215. }
  216. }
  217. }
  218. },
  219. pets: {
  220. type: "array",
  221. format: "table",
  222. title: "Pets",
  223. uniqueItems: true,
  224. items: {
  225. type: "object",
  226. title: "Pet",
  227. properties: {
  228. type: {
  229. type: "string",
  230. enum: ["cat","dog","bird","reptile","other"],
  231. default: "dog"
  232. },
  233. name: {
  234. type: "string"
  235. }
  236. }
  237. },
  238. default: [
  239. {
  240. type: "dog",
  241. name: "Walter"
  242. }
  243. ]
  244. }
  245. }
  246. }
  247. }
  248. // Divs/textareas on the page
  249. var $schema = document.getElementById('schema');
  250. var $output = document.getElementById('output');
  251. var $editor = document.getElementById('editor');
  252. var $validate = document.getElementById('validate');
  253. // Buttons
  254. var $set_schema_button = document.getElementById('setschema');
  255. var $set_value_button = document.getElementById('setvalue');
  256. var jsoneditor;
  257. var updateDirectLink = function() {
  258. var url = window.location.href.replace(/\?.*/,'');
  259. url += '?schema='+LZString.compressToBase64(JSON.stringify(schema));
  260. url += '&value='+LZString.compressToBase64(JSON.stringify(jsoneditor.getValue()));
  261. for(var i in JSONEditor.defaults.options) {
  262. if(!JSONEditor.defaults.options.hasOwnProperty(i)) continue;
  263. if(JSONEditor.defaults.options[i]===false) continue;
  264. else if(JSONEditor.defaults.options[i]===true) {
  265. url += '&'+i;
  266. }
  267. else {
  268. url += '&'+i+'='+JSONEditor.defaults.options[i];
  269. }
  270. }
  271. document.getElementById('direct_link').href = url;
  272. };
  273. var reload = function(keep_value) {
  274. var startval = (jsoneditor && keep_value)? jsoneditor.getValue() : window.startval;
  275. window.startval = undefined;
  276. if(jsoneditor) jsoneditor.destroy();
  277. jsoneditor = new JSONEditor($editor,{
  278. schema: schema,
  279. startval: startval
  280. });
  281. window.jsoneditor = jsoneditor;
  282. // When the value of the editor changes, update the JSON output and validation message
  283. jsoneditor.on('change',function() {
  284. var json = jsoneditor.getValue();
  285. $output.value = JSON.stringify(json,null,2);
  286. var validation_errors = jsoneditor.validate();
  287. // Show validation errors if there are any
  288. if(validation_errors.length) {
  289. $validate.value = JSON.stringify(validation_errors,null,2);
  290. }
  291. else {
  292. $validate.value = 'valid';
  293. }
  294. updateDirectLink();
  295. // Materialize extra.
  296. if (window.Materialize) window.Materialize.updateTextFields();
  297. });
  298. };
  299. // Start the schema and output textareas with initial values
  300. $schema.value = JSON.stringify(schema,null,2);
  301. $output.value = '';
  302. // When the 'update form' button is clicked, set the editor's value
  303. $set_value_button.addEventListener('click',function() {
  304. jsoneditor.setValue(JSON.parse($output.value));
  305. });
  306. // Update the schema when the button is clicked
  307. $set_schema_button.addEventListener('click',function() {
  308. try {
  309. schema = JSON.parse($schema.value);
  310. }
  311. catch(e) {
  312. alert('Invalid Schema: '+e.message);
  313. return;
  314. }
  315. reload();
  316. });
  317. // Set the theme by loading the right stylesheets
  318. var setTheme = function(theme,no_reload) {
  319. theme = theme || '';
  320. var mapping = {
  321. barebones: '',
  322. html: '',
  323. bootstrap2: '//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css',
  324. bootstrap3: '//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
  325. bootstrap4: '//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
  326. foundation3: '//cdnjs.cloudflare.com/ajax/libs/foundation/3.2.5/stylesheets/foundation.css',
  327. foundation4: '//cdnjs.cloudflare.com/ajax/libs/foundation/4.3.2/css/foundation.min.css',
  328. foundation5: '//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.min.css',
  329. foundation6: '//cdnjs.cloudflare.com/ajax/libs/foundation/6.2.4/foundation.min.css',
  330. jqueryui: '//code.jquery.com/ui/1.10.3/themes/south-street/jquery-ui.css',
  331. materialize: '//cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css'
  332. };
  333. if(typeof mapping[theme] === 'undefined') {
  334. theme = 'bootstrap3';
  335. document.getElementById('theme_switcher').value = theme;
  336. }
  337. var scriptMapping = {
  338. materialize: [
  339. 'https://code.jquery.com/jquery-3.2.1.min.js',
  340. 'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js'
  341. ]
  342. }
  343. var themeScripts = scriptMapping[theme],
  344. head = document.getElementsByTagName('head')[0],
  345. script;
  346. if (typeof themeScripts == 'string') { themeScripts = [themeScripts]; }
  347. if (Array.isArray(themeScripts)) {
  348. for (var i = 0; i < themeScripts.length; i++) {
  349. script = document.createElement('script');
  350. script.type = 'text/javascript';
  351. script.src = themeScripts[i];
  352. head.appendChild(script);
  353. }
  354. }
  355. JSONEditor.defaults.options.theme = theme;
  356. document.getElementById('theme_stylesheet').href = mapping[theme];
  357. document.getElementById('theme_switcher').value = JSONEditor.defaults.options.theme;
  358. if(!no_reload) reload(true);
  359. };
  360. // Set the icontheme
  361. // Set the theme by loading the right stylesheets
  362. var setIconlib = function(iconlib,no_reload) {
  363. iconlib = iconlib || '';
  364. var mapping = {
  365. foundation2: '//cdnjs.cloudflare.com/ajax/libs/foundicons/2.0/stylesheets/general_foundicons.css',
  366. foundation3: '//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css',
  367. fontawesome3: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.css',
  368. fontawesome4: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css',
  369. materialicons: 'https://fonts.googleapis.com/icon?family=Material+Icons'
  370. };
  371. JSONEditor.defaults.options.iconlib = iconlib;
  372. document.getElementById('icon_stylesheet').href = mapping[iconlib] || '';
  373. document.getElementById('icon_switcher').value = JSONEditor.defaults.options.iconlib;
  374. if(!no_reload) reload(true);
  375. };
  376. var refreshBooleanOptions = function(no_reload) {
  377. var boolean_options = document.getElementById('boolean_options').children;
  378. for(var i=0; i<boolean_options.length; i++) {
  379. JSONEditor.defaults.options[boolean_options[i].value] = boolean_options[i].selected;
  380. }
  381. if(!no_reload) reload(true);
  382. };
  383. // Change listeners for options
  384. document.getElementById('theme_switcher').addEventListener('change',function() {
  385. setTheme(this.value);
  386. });
  387. document.getElementById('icon_switcher').addEventListener('change',function() {
  388. setIconlib(this.value);
  389. });
  390. document.getElementById('object_layout').addEventListener('change',function() {
  391. JSONEditor.defaults.options.object_layout = this.value;
  392. reload(true);
  393. });
  394. document.getElementById('show_errors').addEventListener('change',function() {
  395. JSONEditor.defaults.options.show_errors = this.value;
  396. reload(true);
  397. });
  398. document.getElementById('boolean_options').addEventListener('change',function() {
  399. refreshBooleanOptions();
  400. });
  401. // Get starting value from url
  402. if(window.location.href.match('[?&]value=([^&]+)')) {
  403. window.startval = JSON.parse(LZString.decompressFromBase64(window.location.href.match('[?&]value=([^&]+)')[1]));
  404. }
  405. // Set options from direct link
  406. setTheme((window.location.href.match(/[?&]theme=([^&]+)/)||[])[1] || 'bootstrap2', true);
  407. setIconlib((window.location.href.match(/[?&]iconlib=([^&]*)/)||[null,'fontawesome4'])[1], true);
  408. document.getElementById('object_layout').value = (window.location.href.match(/[?&]object_layout=([^&]+)/)||[])[1] || 'normal';
  409. JSONEditor.defaults.options.object_layout = document.getElementById('object_layout').value;
  410. document.getElementById('show_errors').value = (window.location.href.match(/[?&]show_errors=([^&]+)/)||[])[1] || 'interaction';
  411. JSONEditor.defaults.options.show_errors = document.getElementById('show_errors').value;
  412. var boolean_options = document.getElementById('boolean_options').children;
  413. for(var i=0; i<boolean_options.length; i++) {
  414. if(window.location.href.match(new RegExp('[?&]'+boolean_options[i].getAttribute('value')+'([&=]|$)'))) {
  415. boolean_options[i].selected = true;
  416. }
  417. }
  418. refreshBooleanOptions(true);
  419. reload();
  420. })();
  421. </script>
  422. </body>
  423. </html>