From 97f0db4343c83ec6d26e536abfca34438c510e14 Mon Sep 17 00:00:00 2001 From: youxingzhi Date: Fri, 5 Jan 2018 10:52:09 +0800 Subject: [PATCH] #3 add dragDisabled params --- examples/demo1.html | 15 ++++--- readme.md | 13 ++++++ src/Tree.js | 96 +++++++++++++++++++++++++-------------------- src/VueTreeList.vue | 34 +++++++++------- src/index.js | 2 +- 5 files changed, 96 insertions(+), 64 deletions(-) diff --git a/examples/demo1.html b/examples/demo1.html index 985c260..4db8372 100644 --- a/examples/demo1.html +++ b/examples/demo1.html @@ -127,6 +127,7 @@ name: 'Node 1', id: 1, pid: 0, + dragDisabled: true, children: [ { name: 'Node 1-2', @@ -139,7 +140,8 @@ { name: 'Node 2', id: 3, - pid: 0 + pid: 0, + dragDisabled: true }, { name: 'Node 3', @@ -155,7 +157,7 @@ }, addNode: function () { - var node = new VueTreeList.TreeNode('new node', false) + var node = new VueTreeList.TreeNode({ name: 'new node', isLeaf: false }) if (!this.data.children) this.data.children = [] this.data.addChildren(node) }, @@ -165,10 +167,11 @@ function _dfs (oldNode) { let newNode = {} - newNode.name = oldNode.name - newNode.pid = oldNode.pid - newNode.isLeaf = oldNode.isLeaf - newNode.id = oldNode.id + for (var k in oldNode) { + if (k !== 'children' && k !== 'parent') { + newNode[k] = oldNode[k] + } + } if (oldNode.children && oldNode.children.length > 0) { newNode.children = [] diff --git a/readme.md b/readme.md index 6fbb920..81646f0 100644 --- a/readme.md +++ b/readme.md @@ -85,4 +85,17 @@ export default { # props default-tree-node-name: Default name for new treenode. + default-leaf-node-name: Default name for new leafnode. + +# Forbid dragging +Use `dragDisabled` to forbid dragging: +```javascript +data: new Tree([ + { + name: 'Node 1', + id: 1, + pid: 0, + dragDisabled: true, + ... +``` \ No newline at end of file diff --git a/src/Tree.js b/src/Tree.js index 7257c3e..d058268 100644 --- a/src/Tree.js +++ b/src/Tree.js @@ -1,40 +1,50 @@ // used to record treenode's change -let Record = {} +// let Record = {} // treenode's id let nodeId = 1 /** * Tree data struct * Created by ayou on 2017/7/20. - * @param name: treenode's name - * @param isLeaf: treenode is leaf node or not - * @param id + * @param data: treenode's params + * name: treenode's name + * isLeaf: treenode is leaf node or not + * id: id + * dragDisabled: decide if it can be dragged */ -const TreeNode = function (name, isLeaf, id) { - this.name = name +const TreeNode = function (data) { + const { id, isLeaf } = data + // this.name = name this.id = (typeof id === 'undefined') ? ('new' + nodeId++) : id this.parent = null - this.pid = null + // this.pid = null this.children = null this.isLeaf = !!isLeaf -} -TreeNode.prototype.updateRecordProperty = function () { - if (!Record[this.id]) { - Record[this.id] = {} + // other params + for (var k in data) { + if (k !== 'id' && k !== 'children' && k !== 'isLeaf') { + this[k] = data[k] + } } - - Record[this.id].name = this.name - Record[this.id].id = this.id - Record[this.id].pid = this.pid - Record[this.id].isLeaf = this.isLeaf } +// TreeNode.prototype.updateRecordProperty = function () { +// if (!Record[this.id]) { +// Record[this.id] = {} +// } +// +// Record[this.id].name = this.name +// Record[this.id].id = this.id +// Record[this.id].pid = this.pid +// Record[this.id].isLeaf = this.isLeaf +// } + TreeNode.prototype.changeName = function (name) { this.name = name - this.updateRecordProperty() - Record[this.id].modify = true + // this.updateRecordProperty() + // Record[this.id].modify = true } TreeNode.prototype.addChildren = function (children, isNew) { @@ -48,10 +58,10 @@ TreeNode.prototype.addChildren = function (children, isNew) { child.parent = this child.pid = this.id - if (isNew) { - child.updateRecordProperty() - Record[child.id].add = true - } + // if (isNew) { + // child.updateRecordProperty() + // Record[child.id].add = true + // } } this.children.concat(children) } else { @@ -60,10 +70,10 @@ TreeNode.prototype.addChildren = function (children, isNew) { child.pid = this.id this.children.push(child) - if (isNew) { - child.updateRecordProperty() - Record[child.id].add = true - } + // if (isNew) { + // child.updateRecordProperty() + // Record[child.id].add = true + // } } } @@ -73,8 +83,8 @@ TreeNode.prototype.remove = function () { const index = parent.findChildIndex(this) parent.children.splice(index, 1) - this.updateRecordProperty() - Record[this.id].remove = true + // this.updateRecordProperty() + // Record[this.id].remove = true } // remove child @@ -121,10 +131,10 @@ TreeNode.prototype.moveInto = function (target) { } target.children.unshift(this) - this.updateRecordProperty() - Record[this.id].targetId = target.id - Record[this.id].move = true - Record[this.id].moveType = 'inside' + // this.updateRecordProperty() + // Record[this.id].targetId = target.id + // Record[this.id].move = true + // Record[this.id].moveType = 'inside' } TreeNode.prototype.findChildIndex = function (child) { @@ -160,10 +170,10 @@ TreeNode.prototype.insertBefore = function (target) { const pos = target.parent.findChildIndex(target) target.parent.children.splice(pos, 0, this) - this.updateRecordProperty() - Record[this.id].targetId = target.id - Record[this.id].move = true - Record[this.id].moveType = 'before' + // this.updateRecordProperty() + // Record[this.id].targetId = target.id + // Record[this.id].move = true + // Record[this.id].moveType = 'before' } TreeNode.prototype.insertAfter = function (target) { @@ -172,14 +182,14 @@ TreeNode.prototype.insertAfter = function (target) { const pos = target.parent.findChildIndex(target) target.parent.children.splice(pos + 1, 0, this) - this.updateRecordProperty() - Record[this.id].targetId = target.id - Record[this.id].move = true - Record[this.id].moveType = 'after' + // this.updateRecordProperty() + // Record[this.id].targetId = target.id + // Record[this.id].move = true + // Record[this.id].moveType = 'after' } function Tree(data) { - this.root = new TreeNode('root', false, 0) + this.root = new TreeNode({ name: 'root', isLeaf: false, id: 0 }) this.initNode(this.root, data) return this.root } @@ -188,7 +198,7 @@ Tree.prototype.initNode = function (node, data) { for (let i = 0, len = data.length; i < len; i++) { var _data = data[i] - var child = new TreeNode(_data.name, _data.isLeaf, _data.id) + var child = new TreeNode(_data) if (_data.children && _data.children.length > 0) { this.initNode(child, _data.children) } @@ -198,4 +208,4 @@ Tree.prototype.initNode = function (node, data) { exports.Tree = Tree exports.TreeNode = TreeNode -exports.Record = Record +// exports.Record = Record diff --git a/src/VueTreeList.vue b/src/VueTreeList.vue index a8f6b55..3eeddf7 100644 --- a/src/VueTreeList.vue +++ b/src/VueTreeList.vue @@ -6,10 +6,8 @@ @dragenter="dragEnterUp" @dragover='dragOverUp' @dragleave="dragLeaveUp"> -