From 7311e78b539307541d5c8fd63274b1e3c7a13926 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 19 Jun 2019 11:57:28 +0200 Subject: [PATCH] Implemented `transform` modal for modes `code` and `text` --- HISTORY.md | 5 ++++ src/js/Node.js | 56 ++++++++++++------------------------ src/js/showTransformModal.js | 25 ++++++++-------- src/js/textmode.js | 40 +++++++++++++++++--------- src/js/treemode.js | 6 +--- src/js/util.js | 32 +++++++++++++++++++++ test/util.test.js | 9 ++++++ 7 files changed, 105 insertions(+), 68 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b22c4fe..28ba567 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,11 @@ https://github.com/josdejong/jsoneditor +## not yet published, version 6.1.0 + +- Implemented menu options `sort` and `transform` for modes `code` and `text`. + + ## 2019-06-12, version 6.0.0 - Breaking change: upgraded dependency `ajv@6.10.0`, supporting JSON schema diff --git a/src/js/Node.js b/src/js/Node.js index efd01f4..5af3cc9 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -1459,7 +1459,7 @@ Node.prototype.changeType = function (newType) { this.value = String(this.value); } else { - this.value = this._stringCast(String(this.value)); + this.value = util.parseString(String(this.value)); } this.focus(); @@ -1550,7 +1550,7 @@ Node.prototype._getDomValue = function() { } else { var str = this._unescapeHTML(this.valueInnerText); - value = this._stringCast(str); + value = util.parseString(str); } if (value !== this.value) { this.value = value; @@ -4303,8 +4303,7 @@ Node.prototype.showContextMenu = function (anchor, onClose) { title: translate('transformTitle', {type: this.type}), className: 'jsoneditor-transform', click: function () { - var anchor = node.editor.options.modalAnchor || DEFAULT_MODAL_ANCHOR; - showTransformModal(node, anchor) + node.showTransformModal(); } }); } @@ -4456,7 +4455,7 @@ Node.prototype.showContextMenu = function (anchor, onClose) { /** - * Show advanced sorting modal + * Show sorting modal */ Node.prototype.showSortModal = function () { var node = this; @@ -4479,6 +4478,19 @@ Node.prototype.showSortModal = function () { }) } +/** + * Show transform modal + */ +Node.prototype.showTransformModal = function () { + var node = this; + + var anchor = this.editor.options.modalAnchor || DEFAULT_MODAL_ANCHOR; + var json = node.getValue(); + showTransformModal(anchor, json, function (query) { + node.transform(query); + }); +} + /** * get the type of a value * @param {*} value @@ -4492,45 +4504,13 @@ Node.prototype._getType = function(value) { if (value instanceof Object) { return 'object'; } - if (typeof(value) == 'string' && typeof(this._stringCast(value)) != 'string') { + if (typeof(value) == 'string' && typeof(util.parseString(value)) != 'string') { return 'string'; } return 'auto'; }; -/** - * cast contents of a string to the correct type. This can be a string, - * a number, a boolean, etc - * @param {String} str - * @return {*} castedStr - * @private - */ -Node.prototype._stringCast = function(str) { - var lower = str.toLowerCase(), - num = Number(str), // will nicely fail with '123ab' - numFloat = parseFloat(str); // will nicely fail with ' ' - - if (str == '') { - return ''; - } - else if (lower == 'null') { - return null; - } - else if (lower == 'true') { - return true; - } - else if (lower == 'false') { - return false; - } - else if (!isNaN(num) && !isNaN(numFloat)) { - return num; - } - else { - return str; - } -}; - /** * escape a text, such that it can be displayed safely in an HTML element * @param {String} text diff --git a/src/js/showTransformModal.js b/src/js/showTransformModal.js index 70f1715..156cdd8 100644 --- a/src/js/showTransformModal.js +++ b/src/js/showTransformModal.js @@ -9,12 +9,14 @@ var MAX_PREVIEW_LINES = 100; /** * Show advanced filter and transform modal using JMESPath - * @param {Node} node the node to be transformed * @param {HTMLElement} container The container where to center * the modal and create an overlay + * @param {JSON} json The json data to be transformed + * @param {function} onTransform Callback invoked with the created + * query as callback */ -function showTransformModal (node, container) { - var value = node.getValue(); +function showTransformModal (container, json, onTransform) { + var value = json; var content = '