Skip to content

Commit

Permalink
Improving behavior API
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Desmaisons committed Aug 27, 2019
1 parent 5d4ed12 commit d8a0ee8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions example/tree/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
</template>
</template> -->

<template #behavior="{nodes, actions}">
<StandardBehavior v-bind="{nodes, actions}"/>
<template #behavior="{on, actions}">
<StandardBehavior v-bind="{on, actions}"/>
</template>
</tree>
</div>
Expand Down
22 changes: 12 additions & 10 deletions src/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,19 @@ export default {
},
render (h) {
const {setSelected, collapse, collapseAll, expand, expandAll, show, toggleExpandCollapse} = this
const rawActions = {setSelected, collapse, collapseAll, expand, expandAll, show, toggleExpandCollapse}
this.actions = Object.keys(rawActions).reduce((current, key) => {
current[key] = rawActions[key].bind(this)
return current
}, {})
const getProps = () => {
const {actions, graphNodes: nodes} = this
return {nodes, actions}
if (!this._behaviour) {
const {setSelected, collapse, collapseAll, expand, expandAll, show, toggleExpandCollapse} = this
const rawActions = {setSelected, collapse, collapseAll, expand, expandAll, show, toggleExpandCollapse}
this.actions = Object.keys(rawActions).reduce((current, key) => {
current[key] = rawActions[key].bind(this)
return current
}, {})
const getProps = () => {
const {actions, graphNodes: nodes, $on: on} = this
return {nodes, actions, on: on.bind(this)}
}
this._behaviour = renderTemplateSlot(getProps, this.$scopedSlots.behavior, standardBehavior)
}
this._behaviour = renderTemplateSlot(getProps, this.$scopedSlots.behavior, standardBehavior)
return h('div', {class: 'viewport treeclass', directives: [{name: 'resize', value: this.resize}]})
},
Expand Down
24 changes: 13 additions & 11 deletions src/behaviors/StandardBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export default {
name: 'standardBehavior',
props: {
/**
* Tree nodes. Typically provided by tree behavior slot.
* Parent event listener.
*/
nodes: {
required: false,
type: Object
on: {
required: true,
type: Function
},
/**
* Tree actions. Typically provided by tree behavior slot.
Expand All @@ -26,13 +26,15 @@ export default {
return null
},

watch: {
'nodes.clickedText': function (node) {
node && this.actions.setSelected(node.data)
},
created () {
const {on, actions} = this

'nodes.clickedNode': function (node) {
node && this.actions.toggleExpandCollapse(node)
}
on('clickedText', ({element}) => {
actions.setSelected(element)
})

on('clickedNode', ({element}) => {
actions.toggleExpandCollapse(element)
})
}
}

0 comments on commit d8a0ee8

Please sign in to comment.