index.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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='//unpkg.com/json-editor'></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. properties: {
  166. name: {
  167. type: "string",
  168. description: "First and Last name",
  169. minLength: 4,
  170. default: "Jeremy Dorn"
  171. },
  172. age: {
  173. type: "integer",
  174. default: 25,
  175. minimum: 18,
  176. maximum: 99
  177. },
  178. favorite_color: {
  179. type: "string",
  180. format: "color",
  181. title: "favorite color",
  182. default: "#ffa500"
  183. },
  184. gender: {
  185. type: "string",
  186. enum: ["male", "female"]
  187. },
  188. location: {
  189. type: "object",
  190. title: "Location",
  191. properties: {
  192. city: {
  193. type: "string",
  194. default: "San Francisco"
  195. },
  196. state: {
  197. type: "string",
  198. default: "CA"
  199. },
  200. citystate: {
  201. type: "string",
  202. description: "This is generated automatically from the previous two fields",
  203. template: "{{city}}, {{state}}",
  204. watch: {
  205. city: 'location.city',
  206. state: 'location.state'
  207. }
  208. }
  209. }
  210. },
  211. pets: {
  212. type: "array",
  213. format: "table",
  214. title: "Pets",
  215. uniqueItems: true,
  216. items: {
  217. type: "object",
  218. title: "Pet",
  219. properties: {
  220. type: {
  221. type: "string",
  222. enum: ["cat","dog","bird","reptile","other"],
  223. default: "dog"
  224. },
  225. name: {
  226. type: "string"
  227. }
  228. }
  229. },
  230. default: [
  231. {
  232. type: "dog",
  233. name: "Walter"
  234. }
  235. ]
  236. }
  237. }
  238. }
  239. }
  240. // Divs/textareas on the page
  241. var $schema = document.getElementById('schema');
  242. var $output = document.getElementById('output');
  243. var $editor = document.getElementById('editor');
  244. var $validate = document.getElementById('validate');
  245. // Buttons
  246. var $set_schema_button = document.getElementById('setschema');
  247. var $set_value_button = document.getElementById('setvalue');
  248. var jsoneditor;
  249. var updateDirectLink = function() {
  250. var url = window.location.href.replace(/\?.*/,'');
  251. url += '?schema='+LZString.compressToBase64(JSON.stringify(schema));
  252. url += '&value='+LZString.compressToBase64(JSON.stringify(jsoneditor.getValue()));
  253. for(var i in JSONEditor.defaults.options) {
  254. if(!JSONEditor.defaults.options.hasOwnProperty(i)) continue;
  255. if(JSONEditor.defaults.options[i]===false) continue;
  256. else if(JSONEditor.defaults.options[i]===true) {
  257. url += '&'+i;
  258. }
  259. else {
  260. url += '&'+i+'='+JSONEditor.defaults.options[i];
  261. }
  262. }
  263. document.getElementById('direct_link').href = url;
  264. };
  265. var reload = function(keep_value) {
  266. var startval = (jsoneditor && keep_value)? jsoneditor.getValue() : window.startval;
  267. window.startval = undefined;
  268. if(jsoneditor) jsoneditor.destroy();
  269. jsoneditor = new JSONEditor($editor,{
  270. schema: schema,
  271. startval: startval
  272. });
  273. window.jsoneditor = jsoneditor;
  274. // When the value of the editor changes, update the JSON output and validation message
  275. jsoneditor.on('change',function() {
  276. var json = jsoneditor.getValue();
  277. $output.value = JSON.stringify(json,null,2);
  278. var validation_errors = jsoneditor.validate();
  279. // Show validation errors if there are any
  280. if(validation_errors.length) {
  281. $validate.value = JSON.stringify(validation_errors,null,2);
  282. }
  283. else {
  284. $validate.value = 'valid';
  285. }
  286. updateDirectLink();
  287. // Materialize extra.
  288. if (window.Materialize) window.Materialize.updateTextFields();
  289. });
  290. };
  291. // Start the schema and output textareas with initial values
  292. $schema.value = JSON.stringify(schema,null,2);
  293. $output.value = '';
  294. // When the 'update form' button is clicked, set the editor's value
  295. $set_value_button.addEventListener('click',function() {
  296. jsoneditor.setValue(JSON.parse($output.value));
  297. });
  298. // Update the schema when the button is clicked
  299. $set_schema_button.addEventListener('click',function() {
  300. try {
  301. schema = JSON.parse($schema.value);
  302. }
  303. catch(e) {
  304. alert('Invalid Schema: '+e.message);
  305. return;
  306. }
  307. reload();
  308. });
  309. // Set the theme by loading the right stylesheets
  310. var setTheme = function(theme,no_reload) {
  311. theme = theme || '';
  312. var mapping = {
  313. barebones: '',
  314. html: '',
  315. bootstrap2: '//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css',
  316. bootstrap3: '//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
  317. bootstrap4: '//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css',
  318. foundation3: '//cdnjs.cloudflare.com/ajax/libs/foundation/3.2.5/stylesheets/foundation.css',
  319. foundation4: '//cdnjs.cloudflare.com/ajax/libs/foundation/4.3.2/css/foundation.min.css',
  320. foundation5: '//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.min.css',
  321. foundation6: '//cdnjs.cloudflare.com/ajax/libs/foundation/6.2.4/foundation.min.css',
  322. jqueryui: '//code.jquery.com/ui/1.10.3/themes/south-street/jquery-ui.css',
  323. materialize: '//cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css'
  324. };
  325. if(typeof mapping[theme] === 'undefined') {
  326. theme = 'bootstrap3';
  327. document.getElementById('theme_switcher').value = theme;
  328. }
  329. var scriptMapping = {
  330. materialize: [
  331. 'https://code.jquery.com/jquery-3.2.1.min.js',
  332. 'https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js'
  333. ]
  334. }
  335. var themeScripts = scriptMapping[theme],
  336. head = document.getElementsByTagName('head')[0],
  337. script;
  338. if (typeof themeScripts == 'string') { themeScripts = [themeScripts]; }
  339. if (Array.isArray(themeScripts)) {
  340. for (var i = 0; i < themeScripts.length; i++) {
  341. script = document.createElement('script');
  342. script.type = 'text/javascript';
  343. script.src = themeScripts[i];
  344. head.appendChild(script);
  345. }
  346. }
  347. JSONEditor.defaults.options.theme = theme;
  348. document.getElementById('theme_stylesheet').href = mapping[theme];
  349. document.getElementById('theme_switcher').value = JSONEditor.defaults.options.theme;
  350. if(!no_reload) reload(true);
  351. };
  352. // Set the icontheme
  353. // Set the theme by loading the right stylesheets
  354. var setIconlib = function(iconlib,no_reload) {
  355. iconlib = iconlib || '';
  356. var mapping = {
  357. foundation2: '//cdnjs.cloudflare.com/ajax/libs/foundicons/2.0/stylesheets/general_foundicons.css',
  358. foundation3: '//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css',
  359. fontawesome3: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/3.2.1/css/font-awesome.css',
  360. fontawesome4: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css',
  361. materialicons: 'https://fonts.googleapis.com/icon?family=Material+Icons'
  362. };
  363. JSONEditor.defaults.options.iconlib = iconlib;
  364. document.getElementById('icon_stylesheet').href = mapping[iconlib] || '';
  365. document.getElementById('icon_switcher').value = JSONEditor.defaults.options.iconlib;
  366. if(!no_reload) reload(true);
  367. };
  368. var refreshBooleanOptions = function(no_reload) {
  369. var boolean_options = document.getElementById('boolean_options').children;
  370. for(var i=0; i<boolean_options.length; i++) {
  371. JSONEditor.defaults.options[boolean_options[i].value] = boolean_options[i].selected;
  372. }
  373. if(!no_reload) reload(true);
  374. };
  375. // Change listeners for options
  376. document.getElementById('theme_switcher').addEventListener('change',function() {
  377. setTheme(this.value);
  378. });
  379. document.getElementById('icon_switcher').addEventListener('change',function() {
  380. setIconlib(this.value);
  381. });
  382. document.getElementById('object_layout').addEventListener('change',function() {
  383. JSONEditor.defaults.options.object_layout = this.value;
  384. reload(true);
  385. });
  386. document.getElementById('show_errors').addEventListener('change',function() {
  387. JSONEditor.defaults.options.show_errors = this.value;
  388. reload(true);
  389. });
  390. document.getElementById('boolean_options').addEventListener('change',function() {
  391. refreshBooleanOptions();
  392. });
  393. // Get starting value from url
  394. if(window.location.href.match('[?&]value=([^&]+)')) {
  395. window.startval = JSON.parse(LZString.decompressFromBase64(window.location.href.match('[?&]value=([^&]+)')[1]));
  396. }
  397. // Set options from direct link
  398. setTheme((window.location.href.match(/[?&]theme=([^&]+)/)||[])[1] || 'bootstrap2', true);
  399. setIconlib((window.location.href.match(/[?&]iconlib=([^&]*)/)||[null,'fontawesome4'])[1], true);
  400. document.getElementById('object_layout').value = (window.location.href.match(/[?&]object_layout=([^&]+)/)||[])[1] || 'normal';
  401. JSONEditor.defaults.options.object_layout = document.getElementById('object_layout').value;
  402. document.getElementById('show_errors').value = (window.location.href.match(/[?&]show_errors=([^&]+)/)||[])[1] || 'interaction';
  403. JSONEditor.defaults.options.show_errors = document.getElementById('show_errors').value;
  404. var boolean_options = document.getElementById('boolean_options').children;
  405. for(var i=0; i<boolean_options.length; i++) {
  406. if(window.location.href.match(new RegExp('[?&]'+boolean_options[i].getAttribute('value')+'([&=]|$)'))) {
  407. boolean_options[i].selected = true;
  408. }
  409. }
  410. refreshBooleanOptions(true);
  411. reload();
  412. })();
  413. </script>
  414. </body>
  415. </html>