Fixed showing multiple context menus

This commit is contained in:
jos 2016-07-16 14:31:01 +02:00
parent cd08e16789
commit a1859a9aff
4 changed files with 30 additions and 16 deletions

View File

@ -30,7 +30,7 @@ export default class JSONNode extends Component {
} }
} }
renderJSONObject ({data, index, options, events}) { renderJSONObject ({data, index, options, events, menu}) {
const childCount = data.childs.length const childCount = data.childs.length
const contents = [ const contents = [
h('div', {class: 'jsoneditor-node jsoneditor-object'}, [ h('div', {class: 'jsoneditor-node jsoneditor-object'}, [
@ -47,7 +47,8 @@ export default class JSONNode extends Component {
return h(JSONNode, { return h(JSONNode, {
data: child, data: child,
options, options,
events events,
menu
}) })
}) })
@ -57,7 +58,7 @@ export default class JSONNode extends Component {
return h('li', {}, contents) return h('li', {}, contents)
} }
renderJSONArray ({data, index, options, events}) { renderJSONArray ({data, index, options, events, menu}) {
const childCount = data.childs.length const childCount = data.childs.length
const contents = [ const contents = [
h('div', {class: 'jsoneditor-node jsoneditor-array'}, [ h('div', {class: 'jsoneditor-node jsoneditor-array'}, [
@ -75,7 +76,8 @@ export default class JSONNode extends Component {
data: child, data: child,
index, index,
options, options,
events events,
menu
}) })
}) })
@ -145,11 +147,12 @@ export default class JSONNode extends Component {
} }
renderContextMenuButton () { renderContextMenuButton () {
const childs = this.props.data.menu ? [ const visible = this.contextMenuVisible()
h(ContextMenu) const childs = visible
] : null ? [ h(ContextMenu) ]
: null
const className = `jsoneditor-button jsoneditor-contextmenu` const className = `jsoneditor-button jsoneditor-contextmenu` + (visible ? ' jsoneditor-visible' : '')
return h('div', {class: 'jsoneditor-button-container'}, return h('div', {class: 'jsoneditor-button-container'},
h('button', {class: className, onClick: this.handleContextMenu}, childs) h('button', {class: className, onClick: this.handleContextMenu}, childs)
) )
@ -159,6 +162,10 @@ export default class JSONNode extends Component {
return Object.keys(nextProps).some(prop => this.props[prop] !== nextProps[prop]) return Object.keys(nextProps).some(prop => this.props[prop] !== nextProps[prop])
} }
contextMenuVisible () {
return this.props.menu === this.props.data.path
}
static _rootName (data, options) { static _rootName (data, options) {
return typeof options.name === 'string' return typeof options.name === 'string'
? options.name ? options.name
@ -198,7 +205,12 @@ export default class JSONNode extends Component {
} }
handleContextMenu (event) { handleContextMenu (event) {
this.props.events.onContextMenu(this.props.data.path, !this.props.data.menu) // toggle visibility of the context menu
const path = this.contextMenuVisible()
? null
: this.props.data.path
this.props.events.onContextMenu(path)
} }
_openLinkIfUrl (event) { _openLinkIfUrl (event) {

View File

@ -29,7 +29,7 @@ export default class Main extends Component {
onContextMenu: this._onContextMenu.bind(this) onContextMenu: this._onContextMenu.bind(this)
}, },
menu: null, menu: null, // json pointer to the node having menu visible
search: null search: null
} }
@ -80,11 +80,14 @@ export default class Main extends Component {
}) })
} }
_onContextMenu(path, visible) { /**
const modelPath = Main._pathToModelPath(this.state.data, Main._parsePath(path)).concat('menu') * Set ContextMenu to a json pointer, or hide the context menu by passing null
* @param {string | null} path
* @private
*/
_onContextMenu(path) {
this.setState({ this.setState({
data: setIn(this.state.data, modelPath, visible) menu: path || null
}) })
} }

View File

@ -163,7 +163,7 @@ button.jsoneditor-button.jsoneditor-contextmenu {
button.jsoneditor-button.jsoneditor-contextmenu:hover, button.jsoneditor-button.jsoneditor-contextmenu:hover,
button.jsoneditor-button.jsoneditor-contextmenu:focus, button.jsoneditor-button.jsoneditor-contextmenu:focus,
button.jsoneditor-button.jsoneditor-contextmenu.jsoneditor-selected { button.jsoneditor-button.jsoneditor-contextmenu.jsoneditor-visible {
background-position: -50px -50px; background-position: -50px -50px;
} }

View File

@ -3,7 +3,6 @@
* @typedef {{ * @typedef {{
* type: string, * type: string,
* expanded: boolean?, * expanded: boolean?,
* menu: boolean?,
* path: string, * path: string,
* prop: string?, * prop: string?,
* value: *?, * value: *?,