Change `showTransformModal` to pass an object with properties. Fix transform not working in `text` and `preview` mode.

This commit is contained in:
jos 2020-01-04 10:39:34 +01:00
parent aba0a58383
commit e09fde628b
6 changed files with 34 additions and 21 deletions

View File

@ -75,7 +75,9 @@
const execute = new Function('data', 'return ' + query) const execute = new Function('data', 'return ' + query)
return execute(json) return execute(json)
} },
queryDescription: 'Enter a JavaScript query to filter, sort, or transform the JSON data.<br/>' +
'The <a href="https://lodash.com/" target="_blank">Lodash</a> library is available via <code>_</code> to facilitate this.'
} }
const json = [] const json = []
for (let i = 0; i < 100; i++) { for (let i = 0; i < 100; i++) {

View File

@ -185,7 +185,7 @@ JSONEditor.VALID_OPTIONS = [
'sortObjectKeys', 'navigationBar', 'statusBar', 'mainMenuBar', 'languages', 'language', 'enableSort', 'enableTransform', 'sortObjectKeys', 'navigationBar', 'statusBar', 'mainMenuBar', 'languages', 'language', 'enableSort', 'enableTransform',
'maxVisibleChilds', 'onValidationError', 'maxVisibleChilds', 'onValidationError',
'modalAnchor', 'popupAnchor', 'modalAnchor', 'popupAnchor',
'createQuery', 'executeQuery' 'createQuery', 'executeQuery', 'queryDescription'
] ]
/** /**

View File

@ -3860,17 +3860,16 @@ export class Node {
* Show transform modal * Show transform modal
*/ */
showTransformModal () { showTransformModal () {
const node = this const { modalAnchor, createQuery, executeQuery, queryDescription } = this.editor.options
const json = this.getValue()
const anchor = this.editor.options.modalAnchor || DEFAULT_MODAL_ANCHOR
const json = node.getValue()
const { createQuery, executeQuery } = this.editor.options
showTransformModal({ showTransformModal({
anchor, anchor: modalAnchor || DEFAULT_MODAL_ANCHOR,
json, json,
queryDescription, // can be undefined
createQuery, createQuery,
executeQuery, executeQuery,
onTransform: query => { node.transform(query) } onTransform: query => { this.transform(query) }
}) })
} }

View File

@ -388,20 +388,20 @@ previewmode._showSortModal = function () {
*/ */
previewmode._showTransformModal = function () { previewmode._showTransformModal = function () {
this.executeWithBusyMessage(() => { this.executeWithBusyMessage(() => {
const anchor = this.options.modalAnchor || DEFAULT_MODAL_ANCHOR const { createQuery, executeQuery, modalAnchor, queryDescription } = this.options
const json = this.get() const json = this.get()
const { createQuery, executeQuery } = this.options
this._renderPreview() // update array count this._renderPreview() // update array count
showTransformModal({ showTransformModal({
anchor, anchor: modalAnchor || DEFAULT_MODAL_ANCHOR,
json, json,
queryDescription, // can be undefined
createQuery, createQuery,
executeQuery, executeQuery,
onTransform: query => { onTransform: query => {
this.executeWithBusyMessage(() => { this.executeWithBusyMessage(() => {
const updatedJson = this.options.executeQuery(json, query) const updatedJson = executeQuery(json, query)
this._setAndFireOnChange(updatedJson) this._setAndFireOnChange(updatedJson)
}, 'transforming...') }, 'transforming...')
} }

View File

@ -5,27 +5,39 @@ import { stringifyPartial } from './jsonUtils'
import { getChildPaths, debounce } from './util' import { getChildPaths, debounce } from './util'
import { MAX_PREVIEW_CHARACTERS } from './constants' import { MAX_PREVIEW_CHARACTERS } from './constants'
const DEFAULT_DESCRIPTION =
'Enter a <a href="http://jmespath.org" target="_blank">JMESPath</a> query to filter, sort, or transform the JSON data.<br/>' +
'To learn JMESPath, go to <a href="http://jmespath.org/tutorial.html" target="_blank">the interactive tutorial</a>.'
/** /**
* Show advanced filter and transform modal using JMESPath * Show advanced filter and transform modal using JMESPath
* @param {Object} params * @param {Object} params
* @property {HTMLElement} container The container where to center * @property {HTMLElement} container The container where to center
* the modal and create an overlay * the modal and create an overlay
* @property {JSON} json The json data to be transformed * @property {JSON} json The json data to be transformed
* @property {string} [queryDescription] Optional custom description explaining
* the transform functionality
* @property {function} createQuery Function called to create a query * @property {function} createQuery Function called to create a query
* from the wizard form * from the wizard form
* @property {function} executeQuery Execute a query for the preview pane * @property {function} executeQuery Execute a query for the preview pane
* @property {function} onTransform Callback invoked with the created * @property {function} onTransform Callback invoked with the created
* query as callback * query as callback
*/ */
export function showTransformModal ({ container, json, createQuery, executeQuery, onTransform }) { export function showTransformModal (
{
container,
json,
queryDescription = DEFAULT_DESCRIPTION,
createQuery,
executeQuery,
onTransform
}
) {
const value = json const value = json
const content = '<label class="pico-modal-contents">' + const content = '<label class="pico-modal-contents">' +
'<div class="pico-modal-header">' + translate('transform') + '</div>' + '<div class="pico-modal-header">' + translate('transform') + '</div>' +
'<p>' + '<p>' + queryDescription + '</p>' +
'Enter a <a href="http://jmespath.org" target="_blank">JMESPath</a> query to filter, sort, or transform the JSON data.<br/>' +
'To learn JMESPath, go to <a href="http://jmespath.org/tutorial.html" target="_blank">the interactive tutorial</a>.' +
'</p>' +
'<div class="jsoneditor-jmespath-label">' + translate('transformWizardLabel') + ' </div>' + '<div class="jsoneditor-jmespath-label">' + translate('transformWizardLabel') + ' </div>' +
'<div id="wizard" class="jsoneditor-jmespath-block jsoneditor-jmespath-wizard">' + '<div id="wizard" class="jsoneditor-jmespath-block jsoneditor-jmespath-wizard">' +
' <table class="jsoneditor-jmespath-wizard-table">' + ' <table class="jsoneditor-jmespath-wizard-table">' +

View File

@ -427,17 +427,17 @@ textmode._showSortModal = function () {
* @private * @private
*/ */
textmode._showTransformModal = function () { textmode._showTransformModal = function () {
const anchor = this.options.modalAnchor || DEFAULT_MODAL_ANCHOR const { modalAnchor, createQuery, executeQuery, queryDescription } = this.options
const json = this.get() const json = this.get()
const { createQuery, executeQuery } = this.options
showTransformModal({ showTransformModal({
anchor, anchor: modalAnchor || DEFAULT_MODAL_ANCHOR,
json, json,
queryDescription, // can be undefined
createQuery, createQuery,
executeQuery, executeQuery,
onTransform: query => { onTransform: query => {
const updatedJson = this.options.executeQuery(json, query) const updatedJson = executeQuery(json, query)
this.set(updatedJson) this.set(updatedJson)
} }
}) })