function IsJsonString(str) { if (typeof str !== 'string') return false; try { const result = JSON.parse(str); const type = Object.prototype.toString.call(result); return type === '[object Object]' || type === '[object Array]'; } catch (err) { alert("Json not valide: " + err); return false; } } function submitJson(filename, jsonstring = ""){ if(jsonstring == ""){ jsonstring = $('#jsonfiletxtarea').val(); } if(IsJsonString(jsonstring)){ $.ajax({ type: "PUT", data: jsonstring, headers: {"content-id": "list"}, contentType: "application/json", url: "../server/json/" + filename, success: function(res) { alert("Saved!") } }); } } function showjsonfiles(){ $.getJSON( "../server/json", function( data ) { $.each( data, function(i, item){ var clone = document.importNode(document.querySelector('#JsonFile').content,true); $('[data-class=jsonlink]',clone).text(item); $('[data-class=jsonlink]',clone).attr('href','javascript:openjson("' + item + '");'); $('#JsonFiles').append(clone); }); }); } function openjson(filename){ $.getJSON( "../server/json/" + filename, function( data ) { $('#jsonfiletxtarea').val(JSON.stringify(data, null, 2)); $('#jsonfileform').attr('onsubmit','submitJson("' + filename + '");'); }); } function createProductcodeString(){ ret = "" abk.forEach(function(item){ if(typeof $("#FormPattern3_Form_" + item).val() !== 'undefined' && typeof $("#FormPattern3_Form_" + item).val() !== 'undefined'){ //if rectangle is checked but nothing has been selected if($("#FormPattern3_Form_" + item).find(":selected").text() == ""){ ret += "."; } else{ ret += $("#FormPattern3_Form_" + item).find(":selected").text() + "."; } } }); $("#showcreatedProductcodeString").val(ret.slice(0, -1)); } $(document).ready(function() { $(document).on('submit', '#jsonfileform', function() { //prevent page reload on form submit return false; }); $('#jsonfiletxtarea').val(""); showjsonfiles(); $(document).on('submit', '#createProductcodeString', function() { //prevent page reload on form submit return false; }); });