#3 add dragDisabled params
This commit is contained in:
96
src/Tree.js
96
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
|
||||
|
Reference in New Issue
Block a user