settings.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. function IsJsonString(str) {
  2. if (typeof str !== 'string') return false;
  3. try {
  4. const result = JSON.parse(str);
  5. const type = Object.prototype.toString.call(result);
  6. return type === '[object Object]'
  7. || type === '[object Array]';
  8. } catch (err) {
  9. alert("Json not valide: " + err);
  10. return false;
  11. }
  12. }
  13. function submitJson(filename, jsonstring = ""){
  14. if(jsonstring == ""){
  15. jsonstring = $('#jsonfiletxtarea').val();
  16. }
  17. if(IsJsonString(jsonstring)){
  18. $.ajax({
  19. type: "PUT",
  20. data: jsonstring,
  21. headers: {"content-id": "list"},
  22. contentType: "application/json",
  23. url: "../server/json/" + filename,
  24. success: function(res) {
  25. alert("Saved!")
  26. }
  27. });
  28. }
  29. }
  30. function showjsonfiles(){
  31. $.getJSON( "../server/json", function( data ) {
  32. $.each( data, function(i, item){
  33. var clone = document.importNode(document.querySelector('#JsonFile').content,true);
  34. $('[data-class=jsonlink]',clone).text(item);
  35. $('[data-class=jsonlink]',clone).attr('href','javascript:openjson("' + item + '");');
  36. $('#JsonFiles').append(clone);
  37. });
  38. });
  39. }
  40. function openjson(filename){
  41. $.getJSON( "../server/json/" + filename, function( data ) {
  42. $('#jsonfiletxtarea').val(JSON.stringify(data, null, 2));
  43. $('#jsonfileform').attr('onsubmit','submitJson("' + filename + '");');
  44. });
  45. }
  46. $(document).ready(function() {
  47. $(document).on('submit', '#jsonfileform', function() { //prevent page reload on form submit
  48. return false;
  49. });
  50. $('#jsonfiletxtarea').val("");
  51. showjsonfiles();
  52. });