From dd4b19dff786b82a09ebf97cf182bc1bf75334fa Mon Sep 17 00:00:00 2001 From: fefescript84 <106695387+fefescript84@users.noreply.github.com> Date: Wed, 1 Jun 2022 20:29:19 +0200 Subject: [PATCH] Update JSONedtr.js update on reset and default added type language keys --- src/JSONedtr.js | 702 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 493 insertions(+), 209 deletions(-) diff --git a/src/JSONedtr.js b/src/JSONedtr.js index 9d829a0..e258a2a 100755 --- a/src/JSONedtr.js +++ b/src/JSONedtr.js @@ -1,213 +1,497 @@ -function JSONedtr( data, outputElement, config = {} ){ - - if (! window.jQuery) { +/** + * https://github.com/LorincJuraj/JSONedtr + * https://www.jqueryscript.net/demo/visual-json-editor-jsonedtr/ + * + * Usage: + var editor = new JSONedtr( + '{"Number":1,"Array":[1,2,3,"four"],"Object":{"aa":11,"bb":22.22},"String":"Hello World!","Boolean":true}', + '#id-of-el', + { + runFunctionOnUpdate: function (editor) { + //console.log('JSONedtr', editor); + console.log('JSONedtr', editor.getDataString()); + //console.log('JSONedtr', editor.getData()); + }, + instantChange: true, + labelSave: 'Mentés', + labelCancel: 'Mégsem', + labelKey: 'kulcs', + labelValue: 'érték', + labelDefault: 'Alapállás', + labelClearAll: 'Mind töröl', + label_type_string: 'Szöveg', + label_type_number: 'Szám', + label_type_boolean: 'Igaz/Hamis', + label_type_array: 'Tömb', + label_type_object: 'Objektum', + arrayAdditionDisabled: false, + objectAdditionDisabled: false + }); + + * + * @type {type} + */ + +function JSONedtr(data, outputElement, config = {}){ + + if (!window.jQuery) { console.error("JSONedtr requires jQuery"); return; } - var JSONedtr = {}; - - JSONedtr.config = config; - - if( JSONedtr.config.instantChange == null ) - JSONedtr.config.instantChange = true ; - - JSONedtr.level = function ( node, lvl=0 ) { - var output = ''; - - $.each( node , function( key, value ) { - JSONedtr.i++; - - if( typeof key == 'string' ) - key = key.replace(/\"/g,"""); - - if( typeof value == 'object' ) { - var type = typeof value; - - if( Array.isArray( value ) ) - type = 'array'; - - output += '
: (' + type + ')'; - output += JSONedtr.level( value, lvl+1 ); - output += '
'; - } else { - if( typeof value == 'string' ) - value = value.replace(/\"/g,"""); - output += '
: (' + typeof value + ')
'; - } - }) - - output += '
'; - - return output; - } - - JSONedtr.getData = function( node = $( JSONedtr.outputElement + ' > .jse--row > input' ) ){ - var result = {}; - $.each( node, function() { - - if( $(this).hasClass( 'jse--value' ) ) { - result[ $(this).data( 'key' ) ] = $(this).val(); - } - - if( $(this).hasClass( 'jse--object' ) || $(this).hasClass( 'jse--array' ) ) { - var selector = '#' + $(this).parent().attr('id') + ' > .jse--row > input'; - result[ $(this).val( ) ] = JSONedtr.getData( $( selector ) ); - } - }); - return result; - } - - JSONedtr.getDataString = function( node = $( JSONedtr.outputElement + ' > .jse--row > input' ) ){ - return JSON.stringify( JSONedtr.getData() ); - } - - JSONedtr.addRowForm = function( plus ) { - var lvl = $( plus ).data('level'); - // - // TODO: add support for array, reference and number - // - // var typeofHTML = ''; - // - - var typeofHTML = ''; - - $( plus ).html(' : ( ' + typeofHTML + ' )'); - $( plus ).children('.jse--key').focus(); - - $( plus ).find( 'select.jse--typeof' ).change(function(){ - switch ( $(this).val() ) { - case 'text': - $(this).parent().siblings( '.jse--value__new' ).replaceWith( '' ); - $(this).parent().siblings( '.jse--value__new' ).focus(); - break; - case 'boolean': - $(this).parent().siblings( '.jse--value__new' ).replaceWith( '' ); - $(this).parent().siblings( '.jse--value__new' ).focus(); - break; - case 'object': - $(this).parent().siblings( '.jse--value__new' ).replaceWith( '' ); - break; - } - }) - - $( '.jse--row.jse--add .jse--save' ).click(function( e ){ - JSONedtr.addRow( e.currentTarget.parentElement ) - }) - - $( '.jse--row.jse--add .jse--cancel' ).click(function( e ){ - var x = e.currentTarget.parentElement - $( e.currentTarget.parentElement ).html(''); - $( x ).find( '.jse--plus' ).click( function(e){ - JSONedtr.addRowForm( e.currentTarget.parentElement ); - }); - }) - } - - JSONedtr.addRow = function( row ) { - - var typeOf = $( row ).find( 'select.jse--typeof option:selected' ).val(); - var ii = $( JSONedtr.outputElement ).data('i'); - ii++; - $( JSONedtr.outputElement ).data('i', ii); - var lvl = $( row ).data('level'); - $( row ).removeClass( 'jse--add' ).attr('id', 'jse--row-' + ii ); - $( row ).find( 'span.jse--typeof' ).html('(' + typeOf +')'); - var key = $( row ).find( '.jse--key' ).val() - switch ( typeOf ) { - case 'text': - $( row ).find( '.jse--value__new' ).data( 'key', key ).removeClass( 'jse--value__new' ); - break; - case 'boolean': - if ($( row ).find( '.jse--value__new' ).is(':checked')) { - $( row ).find( '.jse--value__new' ).replaceWith( '' ); - } else { - $( row ).find( '.jse--value__new' ).replaceWith( '' ); - } - break; - case 'object': - $( row ).find( '.jse--key' ).addClass( 'jse--object' ); - $( row ).append( '
' ); - $( row ).addClass( 'jse--row-object' ); - break; - } - - $( row ).append( '
' ); - - $( row ).find( '.jse--delete' ).click(function( e ){ - JSONedtr.deleteRow( e.currentTarget.parentElement ); - }) - - $( row ).children( '.jse--save, .jse--cancel' ).remove(); - $( row ).after( '
' ); - $( row ).parent().find( '.jse--row.jse--add .jse--plus' ).click( function(e){ JSONedtr.addRowForm( e.currentTarget.parentElement ) }); - - $( row ).find( 'input' ).on( 'change input', function( e ){ - if ( JSONedtr.config.runFunctionOnUpdate ) { - if( JSONedtr.config.instantChange || 'change' == e.type ) - JSONedtr.executeFunctionByName( JSONedtr.config.runFunctionOnUpdate , window, JSONedtr); - } - }); - - if ( JSONedtr.config.runFunctionOnUpdate ) { - JSONedtr.executeFunctionByName( JSONedtr.config.runFunctionOnUpdate , window, JSONedtr); - } - - } - - JSONedtr.deleteRow = function( row ) { - $( row ).remove(); - if ( JSONedtr.config.runFunctionOnUpdate ) { - JSONedtr.executeFunctionByName( JSONedtr.config.runFunctionOnUpdate , window, JSONedtr); - } - } - - JSONedtr.executeFunctionByName = function(functionName, context /*, args */) { - var args = Array.prototype.slice.call(arguments, 2); - var namespaces = functionName.split("."); - var func = namespaces.pop(); - for(var i = 0; i < namespaces.length; i++) { - context = context[namespaces[i]]; - } - return context[func].apply(context, args); - } - - JSONedtr.init = function( data, outputElement ) { - data = JSON.parse( data ); - JSONedtr.i = 0; - JSONedtr.outputElement = outputElement; - var html = JSONedtr.level( data ); - - $( outputElement ).addClass('jse--output').html( html ).data('i', JSONedtr.i); - - $( outputElement + ' .jse--row.jse--add .jse--plus' ).click(function( e ){ - JSONedtr.addRowForm( e.currentTarget.parentElement ); - }) - - $( outputElement + ' .jse--row .jse--delete' ).click(function( e ){ - JSONedtr.deleteRow( e.currentTarget.parentElement ); - }) - - $( outputElement + ' .jse--row input' ).on( 'change input', function( e ){ - if ( JSONedtr.config.runFunctionOnUpdate ) { - if( JSONedtr.config.instantChange || 'change' == e.type ) - JSONedtr.executeFunctionByName( JSONedtr.config.runFunctionOnUpdate , window, JSONedtr); - } - }); - } - - JSONedtr.init( data, outputElement ); - - return JSONedtr; -}; + var JSONedtr = {}; + + JSONedtr.saveData = data; /* SAVE DEFAULT */ + + //Default values + JSONedtr.config = $.extend( + { + instantChange: true, /* True: call runFunctionOnUpdate on INPUT && CHANGE event, False: call runFunctionOnUpdate on CHANGE event */ + runFunctionOnUpdate: null, /* call on update string || function */ + labelSave: 'Save', + labelCancel: 'Cancel', + labelKey: 'key', + labelValue: 'value', + labelDefault: 'Default', + labelClearAll: 'Clear all', + label_type_string: 'String', + label_type_number: 'Number', + label_type_boolean: 'Boolean', + label_type_array: 'Array', + label_type_object: 'Object', + arrayAdditionDisabled: false, /* True: Can't add new Array */ + objectAdditionDisabled: false /* True: Can't add new Object */ + }, config); + + JSONedtr.getConfigValue = function (key, def = null) { + if ( + typeof JSONedtr.config === 'object' && + typeof JSONedtr.config[key] !== 'undefined' + ) { + return JSONedtr.config[key]; + } + return def; + } + + JSONedtr.setConfigValue = function (key, value) { + if (typeof JSONedtr.config !== 'object') { + JSONedtr.config = {}; + } + JSONedtr.config[key] = value; + } + + if (JSONedtr.getConfigValue('instantChange') === null) { + JSONedtr.setConfigValue('instantChange', true); + } + + JSONedtr.level = function (node, lvl = 0, isArrayParam ) { + var output = ''; + + var isArray = false; + if ( typeof isArrayParam !== 'undefined' && isArrayParam !== null ) { + isArray = (isArrayParam? true:false); + } + + var lblKey = JSONedtr.getConfigValue('labelKey', 'key'); + var lblValue = JSONedtr.getConfigValue('labelValue', 'value'); + + $.each(node, function (key, value) { + JSONedtr.i++; + + if (typeof key === 'string' && key) { + key = key.replace(/\"/g, """); + } + + var type = typeof value; + var typeKey = 'label_type_' + type; +//console.log('key', key, 'value', value, 'type', type); + if (type === 'object') { /* Object || Array */ + if (Array.isArray(value)) { + type = 'array'; + } + typeKey = 'label_type_' + type; + + var labelText = JSONedtr.getConfigValue(typeKey, '?'); + + var rowClass = 'jse--row-' + type; + + output += '
: (' + labelText + ')'; + //output += JSONedtr.level(value, lvl + 1); + output += JSONedtr.level(value, lvl + 1, (type=='array'? true:false)); + output += '
'; + } else { /* string || number || boolean */ + var labelText = JSONedtr.getConfigValue(typeKey, '?'); + var inputValueType = 'text'; + var checked = ''; + var readonly = (isArray?'readonly="true"':''); + if (type === 'string') { + value = value.replace(/\"/g, """); + } else if (type === 'number') { + inputValueType = 'number'; + } else if (type === 'boolean') { + inputValueType = 'checkbox'; + if (value) { + checked = 'checked="true"'; + } + } + output += '
: (' + labelText + ')
'; + } + }) + + output += '
'; + + return output; + } + + JSONedtr.getData = function (node = $(JSONedtr.outputElement + ' > .jse--row > input'), isArrayParam) { + var isArray = false; + if ( typeof isArrayParam !== 'undefined' && isArrayParam !== null ) { + isArray = (isArrayParam? true:false); + } + + if ( isArray ) { + var result = []; + } else { + var result = {}; + } + + $.each(node, function () { + var o = $(this); + var oRow = o.parent(); + //var oRow = o.closest('.jse--row'); + var oRowId = oRow.attr('id'); + var sTypeOf = oRow.find('.jse--typeof'); + var typeOf = sTypeOf.data('type'); + +//console.log(o, typeOf, o.val() ); + if (o.hasClass('jse--key')) { + if (typeOf == 'array' || typeOf == 'object') { + //if ( oRow.hasClass( 'jse--row-object' ) || oRow.hasClass( 'jse--row-array' ) ) { + var selector = '#' + oRowId + ' > .jse--row > input'; + if ( isArray ) { + result.push( JSONedtr.getData($(selector), (typeOf=='array'? true:false) ) ); + } else { + result[ o.val( ) ] = JSONedtr.getData($(selector), (typeOf=='array'? true:false) ); + } + } + } else if (o.hasClass('jse--value')) { /* MSG: (array && object) esetén nincs érték */ + if ( typeof typeOf === 'undefined' || !typeOf ) { + typeOf = 'string'; + } + var iKey = oRow.find('.jse--key'); + var key = iKey.val(); + + var oVal = o.val(); + if (typeOf == 'number') { + oVal = parseFloat( oVal ); + if ( isNaN(oVal) ) { + oVal = null; + } + } else if (typeOf == 'boolean') { + if (o.attr('type') === 'checkbox') { + oVal = (o.is(':checked') ? true : false); + } else { + var oVal = ( oVal ).toLowerCase(); + oVal = $.trim(oVal); + if ( + oVal == '' || + oVal == 'false' || + oVal == 'null' || + parseFloat(oVal) == 0 + ) { + oVal = false; + } else { + oVal = true; + } + } + } + if ( isArray ) { + result.push( oVal ); + } else { + result[ key ] = oVal; + } + + } + + }); + return result; + } + + JSONedtr.getDataString = function (node = $(JSONedtr.outputElement + ' > .jse--row > input')) { + return JSON.stringify(JSONedtr.getData()); + } + + JSONedtr.addRowForm = function (plus) { + var plusO = $(plus); +//console.log(plusO); + var plusParent = plusO.parent(); + var lvl = plusO.data('level'); + var arrayAdditionDisabled = JSONedtr.getConfigValue('arrayAdditionDisabled'); + var objectAdditionDisabled = JSONedtr.getConfigValue('objectAdditionDisabled'); + var lblSave = JSONedtr.getConfigValue('labelSave', 'Save'); + var lblCancel = JSONedtr.getConfigValue('labelCancel', 'Cancel'); + var lblKey = JSONedtr.getConfigValue('labelKey', 'key'); + var lblValue = JSONedtr.getConfigValue('labelValue', 'value'); + + var typeofHTML = ''; + + plusO.html(' : ( ' + typeofHTML + ' )'); + + var jseKey = plusO.children('.jse--key'); + if ( plusParent.hasClass('jse--row') && plusParent.hasClass('jse--row-array') ) { + var rows = plusParent.find('> [id].jse--row:not(.jse--add)'); + //console.log('rows', rows); + rows.each(function (id, el) { + var o = $(this); + o.find('>.jse--key').first().val(id).prop('readonly', true); + }); + jseKey.val(rows.length); + jseKey.prop('readonly', true); + plusO.children('.jse--value').focus(); + } else { + jseKey.focus(); + } + //$(plus).children('.jse--key').focus(); + + //$(plus).find('select.jse--typeof').change(function () { + plusO.find('select.jse--typeof').change(function () { + var o = $(this); + var p = o.parent(); + switch (o.val()) { + case 'number': + p.siblings('.jse--value__new').replaceWith(''); + p.siblings('.jse--value__new').focus(); + break; + case 'string': + p.siblings('.jse--value__new').replaceWith(''); + p.siblings('.jse--value__new').focus(); + break; + case 'boolean': + p.siblings('.jse--value__new').replaceWith('   '); + p.siblings('.jse--value__new').focus(); + break; + case 'array': + case 'object': + p.siblings('.jse--value__new').replaceWith(''); + break; + } + }) + + $('.jse--row.jse--add .jse--save').click(function (e) { + JSONedtr.addRow(e.currentTarget.parentElement); + }) + + $('.jse--row.jse--add .jse--cancel').click(function (e) { + var x = e.currentTarget.parentElement; + $(e.currentTarget.parentElement).html(''); + $(x).find('.jse--plus').click(function (e) { + JSONedtr.addRowForm(e.currentTarget.parentElement); + }); + }) + } + + JSONedtr.addRow = function (row) { + + var typeOf = $(row).find('select.jse--typeof option:selected').val(); + var ii = $(JSONedtr.outputElement).data('i'); + ii++; + $(JSONedtr.outputElement).data('i', ii); + var lvl = $(row).data('level'); + $(row).removeClass('jse--add').attr('id', 'jse--row-' + ii); + var labelTypeKey = 'label_type_' + typeOf; + var labelTypeText = JSONedtr.getConfigValue(labelTypeKey, typeOf); + + $(row).find('span.jse--typeof').html('(' + labelTypeText + ')').attr('data-type', typeOf); + var key = $(row).find('.jse--key').val(); + switch (typeOf) { + case 'number': + case 'string': + $(row).find('.jse--value__new').removeClass('jse--value__new'); + break; + case 'boolean': + var isChecked = $(row).find('.jse--value__new').is(':checked'); + var iValue = ''; + var checked = ''; + if ( isChecked ) { + iValue = 'true'; + checked = 'checked="true"'; + } else { + iValue = 'false'; + } + //$(row).find('.jse--value__new').replaceWith(''); + $(row).find('.jse--value__new').replaceWith(''); + break; + case 'array': + $(row).append('
'); + $(row).addClass('jse--row-array'); + break; + case 'object': + $(row).append('
'); + $(row).addClass('jse--row-object'); + break; + } + + $(row).append('
'); + + $(row).find('.jse--delete').click(function (e) { + JSONedtr.deleteRow(e.currentTarget.parentElement); + }); + + $(row).children('.jse--save, .jse--cancel').remove(); + $(row).after('
'); + $(row).parent().find('.jse--row.jse--add .jse--plus').click(function (e) { + JSONedtr.addRowForm(e.currentTarget.parentElement) + }); + + if (JSONedtr.getConfigValue('runFunctionOnUpdate')) { + $(row).find('input').on('change input', function (e) { + if ( + JSONedtr.getConfigValue('instantChange') || + 'change' == e.type + ) { + JSONedtr.executeFunction(JSONedtr.config.runFunctionOnUpdate, window, JSONedtr); + } + }); + + JSONedtr.executeFunction(JSONedtr.config.runFunctionOnUpdate, window, JSONedtr); + } + + } + + JSONedtr.deleteRow = function (row) { + var oRow = $(row); + if ( oRow.hasClass('jse--row') ) { + var pRow = oRow.parent(); + oRow.remove(); + if ( pRow.hasClass('jse--row') && pRow.hasClass('jse--row-array') ) { + var rows = pRow.find('> [id].jse--row:not(.jse--add)'); + rows.each(function (id, el) { + var o = $(this); + o.find('> .jse--key').first().val(id).prop('readonly', true); + }); + } + if (JSONedtr.getConfigValue('runFunctionOnUpdate')) { + JSONedtr.executeFunction(JSONedtr.config.runFunctionOnUpdate, window, JSONedtr); + } + } + } + + /** + * functionName can be function type + * @param {type} functionName + * @param {type} context + * @return {unresolved} + */ + JSONedtr.executeFunction = function (functionName, context /*, args */) { + var args = Array.prototype.slice.call(arguments, 2); + //console.log('args' ,args ); + if (typeof functionName === 'function') { + //return functionName.apply(null, args); + return functionName.apply(context, args); + } else if (typeof functionName === 'string') { + var namespaces = functionName.split("."); + var func = namespaces.pop(); + for (var i = 0; i < namespaces.length; i++) { + context = context[namespaces[i]]; + } + return context[func].apply(context, args); + } + return null; + } + + JSONedtr.reset = function () { + var lblClearAll = JSONedtr.getConfigValue('labelClearAll', 'Clear all'); + var cRet = confirm(lblClearAll); + if ( + cRet && + JSONedtr.outputElement + ) { + JSONedtr.init('{}', JSONedtr.outputElement, true); + } + return false; + } + + JSONedtr.default = function () { + var lblDefault = JSONedtr.getConfigValue('labelDefault', 'Default'); + var cRet = confirm(lblDefault); + if ( + cRet && + JSONedtr.outputElement && + typeof JSONedtr.saveData !== 'undefined' + ) { + JSONedtr.init( JSONedtr.saveData, JSONedtr.outputElement, true ); + } + return false; + } + + JSONedtr.init = function (data, outputElement, update) { + if (data === null || !data) { + data = '{}'; + } + data = JSON.parse(data); + JSONedtr.i = 0; + JSONedtr.outputElement = outputElement; + //var html = JSONedtr.level(data, 0, (Array.isArray(data)? true:false)); + var html = JSONedtr.level(data, 0, false); + + var lblClearAll = JSONedtr.getConfigValue('labelClearAll', 'Clear all'); + var lblDefault = JSONedtr.getConfigValue('labelDefault', 'Default'); + html += '
'; + + $(outputElement).addClass('jse--output').html(html).data('i', JSONedtr.i); + + $(outputElement + ' .jse--row.jse--add .jse--plus').click(function (e) { + JSONedtr.addRowForm(e.currentTarget.parentElement); + }); + + $(outputElement + ' .jse--row .jse--delete').click(function (e) { + JSONedtr.deleteRow(e.currentTarget.parentElement); + }); + + $(outputElement + ' .jse--reset').click(function (e) { + JSONedtr.reset(e); + e.preventDefault(); + return false; + }); + + $(outputElement + ' .jse--default').click(function (e) { + JSONedtr.default(e); + e.preventDefault(); + return false; + }); + + if (JSONedtr.getConfigValue('runFunctionOnUpdate')) { + $(outputElement + ' .jse--row input').on('change input', function (e) { + //console.log($(this), e); + if ( + JSONedtr.getConfigValue('instantChange') || + 'change' == e.type + ) { + JSONedtr.executeFunction(JSONedtr.config.runFunctionOnUpdate, window, JSONedtr); + } + }); + + if ( typeof update !== 'undefined' && update ) { + JSONedtr.executeFunction(JSONedtr.config.runFunctionOnUpdate, window, JSONedtr); + } + } + } + + JSONedtr.init(data, outputElement); + + return JSONedtr; +} +;