Compare commits

...

5 Commits
1.0.0 ... 1.0.3

Author SHA1 Message Date
youxingzhi
bbbe58a4e0 1.0.3 2018-01-05 10:59:54 +08:00
youxingzhi
97f0db4343 #3 add dragDisabled params 2018-01-05 10:52:09 +08:00
youxingzhi
527bae5bdc 1.0.2 2017-08-31 18:27:39 +08:00
youxingzhi
dce8f74717 #1 2017-08-31 18:25:47 +08:00
ayou
9f286fde0d 1.0.1 for update npm 2017-07-23 11:08:20 +08:00
7 changed files with 103 additions and 69 deletions

File diff suppressed because one or more lines are too long

View File

@@ -127,6 +127,7 @@
name: 'Node 1', name: 'Node 1',
id: 1, id: 1,
pid: 0, pid: 0,
dragDisabled: true,
children: [ children: [
{ {
name: 'Node 1-2', name: 'Node 1-2',
@@ -139,7 +140,8 @@
{ {
name: 'Node 2', name: 'Node 2',
id: 3, id: 3,
pid: 0 pid: 0,
dragDisabled: true
}, },
{ {
name: 'Node 3', name: 'Node 3',
@@ -155,7 +157,7 @@
}, },
addNode: function () { 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 = [] if (!this.data.children) this.data.children = []
this.data.addChildren(node) this.data.addChildren(node)
}, },
@@ -165,10 +167,11 @@
function _dfs (oldNode) { function _dfs (oldNode) {
let newNode = {} let newNode = {}
newNode.name = oldNode.name for (var k in oldNode) {
newNode.pid = oldNode.pid if (k !== 'children' && k !== 'parent') {
newNode.isLeaf = oldNode.isLeaf newNode[k] = oldNode[k]
newNode.id = oldNode.id }
}
if (oldNode.children && oldNode.children.length > 0) { if (oldNode.children && oldNode.children.length > 0) {
newNode.children = [] newNode.children = []

View File

@@ -1,7 +1,7 @@
{ {
"name": "vue-tree-list", "name": "vue-tree-list",
"version": "1.0.0", "version": "1.0.3",
"description": "A vue component for tree structure.", "description": "A vue component for tree structure. Support adding treenode/leafnode, editing node's name and dragging.",
"main": "dist/vue-tree-list.min.js", "main": "dist/vue-tree-list.min.js",
"scripts": { "scripts": {
"test": "karma start --single-run", "test": "karma start --single-run",

View File

@@ -85,4 +85,17 @@ export default {
# props # props
default-tree-node-name: Default name for new treenode. default-tree-node-name: Default name for new treenode.
default-leaf-node-name: Default name for new leafnode. 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,
...
```

View File

@@ -1,40 +1,50 @@
// used to record treenode's change // used to record treenode's change
let Record = {} // let Record = {}
// treenode's id // treenode's id
let nodeId = 1 let nodeId = 1
/** /**
* Tree data struct * Tree data struct
* Created by ayou on 2017/7/20. * Created by ayou on 2017/7/20.
* @param name: treenode's name * @param data: treenode's params
* @param isLeaf: treenode is leaf node or not * name: treenode's name
* @param id * isLeaf: treenode is leaf node or not
* id: id
* dragDisabled: decide if it can be dragged
*/ */
const TreeNode = function (name, isLeaf, id) { const TreeNode = function (data) {
this.name = name const { id, isLeaf } = data
// this.name = name
this.id = (typeof id === 'undefined') ? ('new' + nodeId++) : id this.id = (typeof id === 'undefined') ? ('new' + nodeId++) : id
this.parent = null this.parent = null
this.pid = null // this.pid = null
this.children = null this.children = null
this.isLeaf = !!isLeaf this.isLeaf = !!isLeaf
}
TreeNode.prototype.updateRecordProperty = function () { // other params
if (!Record[this.id]) { for (var k in data) {
Record[this.id] = {} 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) { TreeNode.prototype.changeName = function (name) {
this.name = name this.name = name
this.updateRecordProperty() // this.updateRecordProperty()
Record[this.id].modify = true // Record[this.id].modify = true
} }
TreeNode.prototype.addChildren = function (children, isNew) { TreeNode.prototype.addChildren = function (children, isNew) {
@@ -48,10 +58,10 @@ TreeNode.prototype.addChildren = function (children, isNew) {
child.parent = this child.parent = this
child.pid = this.id child.pid = this.id
if (isNew) { // if (isNew) {
child.updateRecordProperty() // child.updateRecordProperty()
Record[child.id].add = true // Record[child.id].add = true
} // }
} }
this.children.concat(children) this.children.concat(children)
} else { } else {
@@ -60,10 +70,10 @@ TreeNode.prototype.addChildren = function (children, isNew) {
child.pid = this.id child.pid = this.id
this.children.push(child) this.children.push(child)
if (isNew) { // if (isNew) {
child.updateRecordProperty() // child.updateRecordProperty()
Record[child.id].add = true // Record[child.id].add = true
} // }
} }
} }
@@ -73,8 +83,8 @@ TreeNode.prototype.remove = function () {
const index = parent.findChildIndex(this) const index = parent.findChildIndex(this)
parent.children.splice(index, 1) parent.children.splice(index, 1)
this.updateRecordProperty() // this.updateRecordProperty()
Record[this.id].remove = true // Record[this.id].remove = true
} }
// remove child // remove child
@@ -121,10 +131,10 @@ TreeNode.prototype.moveInto = function (target) {
} }
target.children.unshift(this) target.children.unshift(this)
this.updateRecordProperty() // this.updateRecordProperty()
Record[this.id].targetId = target.id // Record[this.id].targetId = target.id
Record[this.id].move = true // Record[this.id].move = true
Record[this.id].moveType = 'inside' // Record[this.id].moveType = 'inside'
} }
TreeNode.prototype.findChildIndex = function (child) { TreeNode.prototype.findChildIndex = function (child) {
@@ -160,10 +170,10 @@ TreeNode.prototype.insertBefore = function (target) {
const pos = target.parent.findChildIndex(target) const pos = target.parent.findChildIndex(target)
target.parent.children.splice(pos, 0, this) target.parent.children.splice(pos, 0, this)
this.updateRecordProperty() // this.updateRecordProperty()
Record[this.id].targetId = target.id // Record[this.id].targetId = target.id
Record[this.id].move = true // Record[this.id].move = true
Record[this.id].moveType = 'before' // Record[this.id].moveType = 'before'
} }
TreeNode.prototype.insertAfter = function (target) { TreeNode.prototype.insertAfter = function (target) {
@@ -172,14 +182,14 @@ TreeNode.prototype.insertAfter = function (target) {
const pos = target.parent.findChildIndex(target) const pos = target.parent.findChildIndex(target)
target.parent.children.splice(pos + 1, 0, this) target.parent.children.splice(pos + 1, 0, this)
this.updateRecordProperty() // this.updateRecordProperty()
Record[this.id].targetId = target.id // Record[this.id].targetId = target.id
Record[this.id].move = true // Record[this.id].move = true
Record[this.id].moveType = 'after' // Record[this.id].moveType = 'after'
} }
function Tree(data) { 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) this.initNode(this.root, data)
return this.root return this.root
} }
@@ -188,7 +198,7 @@ Tree.prototype.initNode = function (node, data) {
for (let i = 0, len = data.length; i < len; i++) { for (let i = 0, len = data.length; i < len; i++) {
var _data = data[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) { if (_data.children && _data.children.length > 0) {
this.initNode(child, _data.children) this.initNode(child, _data.children)
} }
@@ -198,4 +208,4 @@ Tree.prototype.initNode = function (node, data) {
exports.Tree = Tree exports.Tree = Tree
exports.TreeNode = TreeNode exports.TreeNode = TreeNode
exports.Record = Record // exports.Record = Record

View File

@@ -6,10 +6,8 @@
@dragenter="dragEnterUp" @dragenter="dragEnterUp"
@dragover='dragOverUp' @dragover='dragOverUp'
@dragleave="dragLeaveUp"></div> @dragleave="dragLeaveUp"></div>
<div class='tree-node' :id='model.id' :class="{'active': isDragEnterNode}" <div class='tree-node' :id='model.id' :class="{'active': isDragEnterNode}"
draggable="true" :draggable="!model.dragDisabled"
@click=""
@dragstart='dragStart' @dragstart='dragStart'
@dragover='dragOver' @dragover='dragOver'
@dragenter='dragEnter' @dragenter='dragEnter'
@@ -85,7 +83,7 @@
import { Tree, TreeNode } from './Tree.js' import { Tree, TreeNode } from './Tree.js'
import $ from 'jquery' import $ from 'jquery'
let fromComp = '' let fromComp = null
export default { export default {
data: function () { data: function () {
return { return {
@@ -176,15 +174,19 @@
addChild(isLeaf) { addChild(isLeaf) {
const name = isLeaf ? this.defaultLeafNodeName : this.defaultTreeNodeName const name = isLeaf ? this.defaultLeafNodeName : this.defaultTreeNodeName
this.expanded = true this.expanded = true
var node = new TreeNode(name, isLeaf) var node = new TreeNode({ name, isLeaf })
this.model.addChildren(node, true) this.model.addChildren(node, true)
}, },
dragStart(e) { dragStart(e) {
fromComp = this if (!this.model.dragDisabled) {
fromComp = this
e.dataTransfer.effectAllowed = 'move' // for firefox
return true e.dataTransfer.setData("data","data");
e.dataTransfer.effectAllowed = 'move'
return true
}
return false
}, },
dragEnd(e) { dragEnd(e) {
fromComp = null fromComp = null
@@ -194,20 +196,21 @@
return true return true
}, },
dragEnter(e) { dragEnter(e) {
if (this.model.isLeaf) { if (!fromComp) return
return if (this.model.isLeaf) return
}
this.isDragEnterNode = true this.isDragEnterNode = true
}, },
dragLeave(e) { dragLeave(e) {
this.isDragEnterNode = false this.isDragEnterNode = false
}, },
drop(e) { drop(e) {
if (!fromComp) return
fromComp.model.moveInto(this.model) fromComp.model.moveInto(this.model)
this.isDragEnterNode = false this.isDragEnterNode = false
}, },
dragEnterUp () { dragEnterUp () {
if (!fromComp) return
this.isDragEnterUp = true this.isDragEnterUp = true
}, },
dragOverUp (e) { dragOverUp (e) {
@@ -215,14 +218,17 @@
return true return true
}, },
dragLeaveUp () { dragLeaveUp () {
if (!fromComp) return
this.isDragEnterUp = false this.isDragEnterUp = false
}, },
dropUp () { dropUp () {
if (!fromComp) return
fromComp.model.insertBefore(this.model) fromComp.model.insertBefore(this.model)
this.isDragEnterUp = false this.isDragEnterUp = false
}, },
dragEnterBottom () { dragEnterBottom () {
if (!fromComp) return
this.isDragEnterBottom = true this.isDragEnterBottom = true
}, },
dragOverBottom (e) { dragOverBottom (e) {
@@ -230,9 +236,11 @@
return true return true
}, },
dragLeaveBottom () { dragLeaveBottom () {
if (!fromComp) return
this.isDragEnterBottom = false this.isDragEnterBottom = false
}, },
dropBottom () { dropBottom () {
if (!fromComp) return
fromComp.model.insertAfter(this.model) fromComp.model.insertAfter(this.model)
this.isDragEnterBottom = false this.isDragEnterBottom = false
} }

View File

@@ -4,4 +4,4 @@
exports.VueTreeList = require('./VueTreeList.vue') exports.VueTreeList = require('./VueTreeList.vue')
exports.TreeNode = require('./Tree.js').TreeNode exports.TreeNode = require('./Tree.js').TreeNode
exports.Tree = require('./Tree.js').Tree exports.Tree = require('./Tree.js').Tree
exports.Record = require('./Tree.js').Record // exports.Record = require('./Tree.js').Record