settings.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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){
  14. jsonstring = $('#jsonfiletxtarea').val();
  15. if(IsJsonString(jsonstring)){
  16. $.ajax({
  17. type: "PUT",
  18. data: jsonstring,
  19. headers: {"content-id": "list"},
  20. contentType: "application/json",
  21. url: "../server/json/" + filename,
  22. success: function(res) {
  23. alert("Saved!")
  24. }
  25. });
  26. }
  27. }
  28. function showjsonfiles(){
  29. $.getJSON( "../server/json", function( data ) {
  30. $.each( data, function(i, item){
  31. var clone = document.importNode(document.querySelector('#JsonFile').content,true);
  32. $('[data-class=jsonlink]',clone).text(item);
  33. $('[data-class=jsonlink]',clone).attr('href','javascript:openjson("' + item + '");');
  34. $('#JsonFiles').append(clone);
  35. });
  36. });
  37. }
  38. function openjson(filename){
  39. $.getJSON( "../server/json/" + filename, function( data ) {
  40. $('#jsonfiletxtarea').val(JSON.stringify(data, null, 2));
  41. $('#jsonfileform').attr('onsubmit','submitJson("' + filename + '");');
  42. });
  43. }
  44. $(document).ready(function() {
  45. $(document).on('submit', '#jsonfileform', function() { //prevent page reload on form submit
  46. return false;
  47. });
  48. $('#jsonfiletxtarea').val("");
  49. showjsonfiles();
  50. });