Skip to content

Commit

Permalink
#55: Add states like draggable, dropable; Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
amsik committed Oct 13, 2018
1 parent 4bad450 commit 39052df
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 31 deletions.
4 changes: 2 additions & 2 deletions demo/pages/dnd.html
Expand Up @@ -88,9 +88,9 @@
MY_TEXT: 'JS: The Right Way',
OPTIONS: { expanded: true },
KIDS: [
{ MY_TEXT: 'Getting Started', OPTIONS: { checked: true, draggable: false } },
{ MY_TEXT: 'Getting Started (NOT DRAGGABLE)', OPTIONS: { checked: true, draggable: false } },
{ MY_TEXT: 'JavaScript Code Style', OPTIONS: { selected: true } },
{ MY_TEXT: 'MVC Pattern' },
{ MY_TEXT: 'MVC Pattern (NOT DROPABLE)', OPTIONS: {dropable: false} },
{ MY_TEXT: 'MVP Pattern' },
{ MY_TEXT: 'MVVM Pattern' },
{ MY_TEXT: 'The Good Parts', KIDS: [
Expand Down
2 changes: 1 addition & 1 deletion src/components/TreeNode.vue
Expand Up @@ -30,7 +30,7 @@
class="tree-children">
<node
v-for="child in node.children"
v-if="child.visible()"
v-if="child && child.visible()"

:key="child.id"
:node="child"
Expand Down
2 changes: 1 addition & 1 deletion src/components/TreeRoot.vue
Expand Up @@ -18,7 +18,7 @@
<template v-else>
<TreeNode
v-for="node in model"
v-if="node.visible()"
v-if="node && node.visible()"

:key="node.id"
:node="node"
Expand Down
33 changes: 17 additions & 16 deletions src/lib/Node.js
Expand Up @@ -391,6 +391,14 @@ export default class Node {
return this.expand()
}

isDropable () {
return this.enabled() && this.state('dropable')
}

isDraggable () {
return this.enabled() && this.state('draggable')
}

startDragging () {
if (this.disabled() || this.state('draggable') === false || this.state('dragging')) {
return false
Expand All @@ -409,6 +417,10 @@ export default class Node {
}

finishDragging (destination, destinationPosition) {
if (!destination.isDropable()) {
return
}

const tree = this.tree
const clone = this.clone()
const parent = this.parent
Expand All @@ -432,8 +444,9 @@ export default class Node {

this.remove()

parent.refreshIndeterminateState()
parent && parent.refreshIndeterminateState()
tree.__silence = false

this.state('dragging', false)
this.$emit('dragging:finish')
}
Expand Down Expand Up @@ -574,7 +587,7 @@ export default class Node {
}

find (criteria, deep) {
if (criteria instanceof Node) {
if (this.tree.isNode(criteria)) {
return criteria
}

Expand All @@ -599,27 +612,15 @@ export default class Node {
}

clone () {
const node = new Node(
this.tree,
this.toJSON()
)

node.children = node.children.map(child => {
const c = new Node(this.tree, child)
c.parent = node
c.children = c.children.map(child => new Node(this.tree, child))
return c
})

return node
return this.tree.objectToNode(this.toJSON())
}

toJSON () {
return {
text: this.text,
data: this.data,
state: this.states,
children: this.children.map(node => node.toJSON())
children: this.children.map(node => this.tree.objectToNode(node).toJSON())
}
}
}
4 changes: 2 additions & 2 deletions src/lib/Tree.js
Expand Up @@ -646,7 +646,7 @@ export default class Tree {
}

find (criteria, multiple) {
if (criteria instanceof Node) {
if (this.isNode(criteria)) {
return criteria
}

Expand Down Expand Up @@ -677,7 +677,7 @@ export default class Tree {
}

getNode (node) {
if (node instanceof Node) {
if (this.isNode(node)) {
return node
}

Expand Down
17 changes: 15 additions & 2 deletions src/mixins/DndMixin.js
Expand Up @@ -79,6 +79,10 @@ export default {
},

startDragging (node, event) {
if (!node.isDraggable()) {
return
}

this.$$startDragPosition = [event.clientX, event.clientY]
this.$$possibleDragNode = node

Expand All @@ -88,6 +92,11 @@ export default {
initDragListeners () {
let dropPosition

const removeListeners = () => {
window.removeEventListener('mouseup', onMouseUp, true)
window.removeEventListener('mousemove', onMouseMove, true)
}

const onMouseUp = (e) => {
if (!this.$$startDragPosition) {
e.stopPropagation()
Expand All @@ -103,8 +112,7 @@ export default {
this.$$possibleDragNode = null
this.$set(this, 'draggableNode', null)

window.removeEventListener('mouseup', onMouseUp, true)
window.removeEventListener('mousemove', onMouseMove, true)
removeListeners()
}

const onMouseMove = (e) => {
Expand Down Expand Up @@ -138,6 +146,11 @@ export default {

if (!this.$$dropDestination || this.$$dropDestination.id !== dropDestinationId) {
this.$$dropDestination = this.tree.getNodeById(dropDestinationId)

if (!this.$$dropDestination.isDropable()) {
this.$$dropDestination = null
return
}
}

dropPosition = highlightDropDestination(e, dropDestination)
Expand Down
12 changes: 7 additions & 5 deletions src/mixins/TreeMixin.js
Expand Up @@ -30,16 +30,18 @@ function initEvents (vm) {
}

tree.$on('node:added', (targetNode, newNode) => {
const node = newNode || targetNode

if (checkbox) {
if (newNode.state('checked') && !tree.checkedNodes.has(newNode)) {
tree.checkedNodes.add(newNode)
if (node.state('checked') && !tree.checkedNodes.has(node)) {
tree.checkedNodes.add(node)
}

newNode.refreshIndeterminateState()
node.refreshIndeterminateState()
}

if (newNode.state('selected') && !tree.selectedNodes.has(newNode)) {
tree.select(newNode)
if (node.state('selected') && !tree.selectedNodes.has(node)) {
tree.select(node)
}

emitter()
Expand Down
4 changes: 3 additions & 1 deletion src/utils/objectToNode.js
Expand Up @@ -14,7 +14,9 @@ const nodeStates = {
indeterminate: false,
matched: false,
editable: true,
dragging: false
dragging: false,
draggable: true,
dropable: true
}

function merge (state = {}) {
Expand Down
4 changes: 3 additions & 1 deletion test/utils/objectToNode.spec.js
Expand Up @@ -11,7 +11,9 @@ const nodeStates = {
indeterminate: false,
matched: false,
editable: true,
dragging: false
dragging: false,
draggable: true,
dropable: true
}

const tree = {}
Expand Down

0 comments on commit 39052df

Please sign in to comment.