Replaced `getPath()` and `props.parent` with `props.path`

This commit is contained in:
jos 2017-01-06 20:33:01 +01:00
parent 1cce254e9c
commit 85ae6d3b5e
2 changed files with 22 additions and 36 deletions

View File

@ -41,7 +41,7 @@ export default class JSONNode extends Component {
renderJSONObject ({prop, index, data, options, events}) { renderJSONObject ({prop, index, data, options, events}) {
const childCount = data.props.length const childCount = data.props.length
const node = h('div', {name: compileJSONPointer(this.getPath()), key: 'node', className: 'jsoneditor-node jsoneditor-object'}, [ const node = h('div', {name: compileJSONPointer(this.props.path), key: 'node', className: 'jsoneditor-node jsoneditor-object'}, [
this.renderExpandButton(), this.renderExpandButton(),
this.renderActionMenuButton(), this.renderActionMenuButton(),
this.renderProperty(prop, index, data, options), this.renderProperty(prop, index, data, options),
@ -55,7 +55,7 @@ export default class JSONNode extends Component {
const props = data.props.map(prop => { const props = data.props.map(prop => {
return h('li', {key: prop.id}, return h('li', {key: prop.id},
h(this.constructor, { h(this.constructor, {
parent: this, path: this.props.path.concat(prop.name),
prop, prop,
data: prop.value, data: prop.value,
options, options,
@ -81,7 +81,7 @@ export default class JSONNode extends Component {
// TODO: extract a function renderChilds shared by both renderJSONObject and renderJSONArray (rename .props and .items to .childs?) // TODO: extract a function renderChilds shared by both renderJSONObject and renderJSONArray (rename .props and .items to .childs?)
renderJSONArray ({prop, index, data, options, events}) { renderJSONArray ({prop, index, data, options, events}) {
const childCount = data.items.length const childCount = data.items.length
const node = h('div', {name: compileJSONPointer(this.getPath()), key: 'node', className: 'jsoneditor-node jsoneditor-array'}, [ const node = h('div', {name: compileJSONPointer(this.props.path), key: 'node', className: 'jsoneditor-node jsoneditor-array'}, [
this.renderExpandButton(), this.renderExpandButton(),
this.renderActionMenuButton(), this.renderActionMenuButton(),
this.renderProperty(prop, index, data, options), this.renderProperty(prop, index, data, options),
@ -95,7 +95,7 @@ export default class JSONNode extends Component {
const items = data.items.map((item, index) => { const items = data.items.map((item, index) => {
return h('li', {key : item.id}, return h('li', {key : item.id},
h(this.constructor, { h(this.constructor, {
parent: this, path: this.props.path.concat(String(index)),
index, index,
data: item.value, data: item.value,
options, options,
@ -118,7 +118,7 @@ export default class JSONNode extends Component {
} }
renderJSONValue ({prop, index, data, options}) { renderJSONValue ({prop, index, data, options}) {
return h('div', {name: compileJSONPointer(this.getPath()), className: 'jsoneditor-node'}, [ return h('div', {name: compileJSONPointer(this.props.path), className: 'jsoneditor-node'}, [
this.renderPlaceholder(), this.renderPlaceholder(),
this.renderActionMenuButton(), this.renderActionMenuButton(),
this.renderProperty(prop, index, data, options), this.renderProperty(prop, index, data, options),
@ -165,7 +165,7 @@ export default class JSONNode extends Component {
}, rootName) }, rootName)
} }
const editable = !isIndex && (!options.isPropertyEditable || options.isPropertyEditable(this.getPath())) const editable = !isIndex && (!options.isPropertyEditable || options.isPropertyEditable(this.props.path))
const emptyClassName = (prop && prop.name.length === 0) ? ' jsoneditor-empty' : '' const emptyClassName = (prop && prop.name.length === 0) ? ' jsoneditor-empty' : ''
const searchClassName = prop ? JSONNode.getSearchResultClass(prop.searchResult) : '' const searchClassName = prop ? JSONNode.getSearchResultClass(prop.searchResult) : ''
@ -199,7 +199,7 @@ export default class JSONNode extends Component {
const itsAnUrl = isUrl(value) const itsAnUrl = isUrl(value)
const isEmpty = escapedValue.length === 0 const isEmpty = escapedValue.length === 0
const editable = !options.isValueEditable || options.isValueEditable(this.getPath()) const editable = !options.isValueEditable || options.isValueEditable(this.props.path)
if (editable) { if (editable) {
return h('div', { return h('div', {
key: 'value', key: 'value',
@ -358,7 +358,7 @@ export default class JSONNode extends Component {
renderActionMenuButton () { renderActionMenuButton () {
return h(ActionButton, { return h(ActionButton, {
key: 'action', key: 'action',
path: this.getPath(), path: this.props.path,
type: this.props.data.type, type: this.props.data.type,
events: this.props.events events: this.props.events
}) })
@ -367,7 +367,7 @@ export default class JSONNode extends Component {
renderAppendMenuButton () { renderAppendMenuButton () {
return h(AppendActionButton, { return h(AppendActionButton, {
key: 'append', key: 'append',
path: this.getPath(), path: this.props.path,
events: this.props.events events: this.props.events
}) })
} }
@ -393,14 +393,14 @@ export default class JSONNode extends Component {
componentDidUpdate (prevProps, prevState) { componentDidUpdate (prevProps, prevState) {
if (this.props.prop && this.props.prop.focus && if (this.props.prop && this.props.prop.focus &&
!(prevProps.props.prop && prevProps.props.prop.focus)) { !(prevProps.props.prop && prevProps.props.prop.focus)) {
console.log('focus property', this.getPath()) // TODO: cleanup console.log('focus property', this.props.path) // TODO: cleanup
if (this.refs.property) { if (this.refs.property) {
this.refs.property.focus() this.refs.property.focus()
} }
} }
if (this.props.data.focus && !prevProps.data.focus) { if (this.props.data.focus && !prevProps.data.focus) {
console.log('focus value', this.getPath()) // TODO: cleanup console.log('focus value', this.props.path) // TODO: cleanup
if (this.refs.value) { if (this.refs.value) {
this.refs.value.focus() this.refs.value.focus()
} }
@ -417,7 +417,7 @@ export default class JSONNode extends Component {
/** @private */ /** @private */
handleChangeProperty = (event) => { handleChangeProperty = (event) => {
const parentPath = this.props.parent.getPath() const parentPath = allButLast(this.props.path)
const oldProp = this.props.prop.name const oldProp = this.props.prop.name
const newProp = unescapeHTML(getInnerText(event.target)) const newProp = unescapeHTML(getInnerText(event.target))
@ -431,7 +431,7 @@ export default class JSONNode extends Component {
const value = this.getValueFromEvent(event) const value = this.getValueFromEvent(event)
if (value !== this.props.data.value) { if (value !== this.props.data.value) {
this.props.events.onChangeValue(this.getPath(), value) this.props.events.onChangeValue(this.props.path, value)
} }
} }
@ -454,7 +454,7 @@ export default class JSONNode extends Component {
const recurse = event.ctrlKey const recurse = event.ctrlKey
const expanded = !this.props.data.expanded const expanded = !this.props.data.expanded
this.props.events.onExpand(this.getPath(), expanded, recurse) this.props.events.onExpand(this.props.path, expanded, recurse)
} }
/** @private */ /** @private */
@ -533,27 +533,6 @@ export default class JSONNode extends Component {
} }
} }
/**
* Get the path of this JSONNode
* @return {Path}
*/
// TODO: get rid of getPath, it's not nice having a reference to the parent in the child
getPath () {
const path = this.props.parent
? this.props.parent.getPath()
: []
if (typeof this.props.index === 'number') {
path.push(String(this.props.index))
}
if (this.props.prop) {
path.push(this.props.prop.name)
}
return path
}
/** /**
* Get the value of the target of an event, and convert it to it's type * Get the value of the target of an event, and convert it to it's type
* @param event * @param event
@ -593,3 +572,10 @@ export default class JSONNode extends Component {
} }
} }
/**
* Returns a copy of the array having the last item removed
*/
function allButLast (array: []): any {
return array.slice(0, array.length - 1)
}

View File

@ -141,7 +141,7 @@ export default class TreeMode extends Component {
data, data,
events: state.events, events: state.events,
options: props, options: props,
parent: null, path: [],
prop: null prop: null
}) })
) )