settings.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. function createProductcodeString(){
  47. ret = ""
  48. abk.forEach(function(item){
  49. if(typeof $("#FormPattern3_Form_" + item).val() !== 'undefined' && typeof $("#FormPattern3_Form_" + item).val() !== 'undefined'){
  50. //if rectangle is checked but nothing has been selected
  51. if($("#FormPattern3_Form_" + item).find(":selected").text() == ""){
  52. ret += ".";
  53. }
  54. else{
  55. ret += $("#FormPattern3_Form_" + item).find(":selected").text() + ".";
  56. }
  57. }
  58. });
  59. $("#showcreatedProductcodeString").val(ret.slice(0, -1));
  60. }
  61. $(document).ready(function() {
  62. $(document).on('submit', '#jsonfileform', function() { //prevent page reload on form submit
  63. return false;
  64. });
  65. $('#jsonfiletxtarea').val("");
  66. showjsonfiles();
  67. $(document).on('submit', '#createProductcodeString', function() { //prevent page reload on form submit
  68. return false;
  69. });
  70. });