Compare commits

..

45 Commits

Author SHA1 Message Date
995cea4b0d removed husky and git-cz 2022-04-07 02:26:44 +02:00
440d2782a1 upgrade to yarn 2 2022-04-07 02:21:54 +02:00
youxingzhi
7b677a6589 1.5.0 2020-11-28 22:11:56 +08:00
youxingzhi
a21f4b8adf chore: 🤖 rm npx 2020-11-28 22:11:42 +08:00
youxingzhi
5b2fea32ad feat: 🎸 #84, add default title props 2020-11-28 22:11:42 +08:00
ayou
347a927e90 Merge pull request #93 from TheFoot/feature/node-expand-collapse
Feature: Added toggle() method to the node passed to TreeNode.click()…
2020-10-28 09:56:11 +08:00
Barry Jones
0f04f7edba Feature: Added toggle() method to the node passed to TreeNode.click() handler. 2020-09-08 15:35:17 +01:00
youxingzhi
5500a36492 1.4.6 2020-07-02 09:59:35 +08:00
ayou
66bc19baaa Merge pull request #78 from zoispag/feat/74_slot_for_display_name
Feat: Add slot for display node/leaf name 
2020-07-02 09:57:17 +08:00
Zois Pagoulatos
97daf67f9a Add slot for display node/leaf name (#74) 2020-07-01 11:26:10 +02:00
youxingzhi
d1e12aad85 1.4.5 2020-05-30 22:13:14 +08:00
youxingzhi
d5dd9b88c9 fix: 🐛 #72 2020-05-30 22:12:00 +08:00
youxingzhi
ccb1067713 1.4.4 2020-04-25 19:44:54 +08:00
ayou
d5705f92e8 Merge pull request #68 from laashub/master
update: after blur emit the event for changeName
2020-04-10 11:48:34 +08:00
tristan-tsl
ae494ce25f update: after blur emit the event for changeName 2020-04-06 13:11:15 +08:00
tristan-tsl
c59015fdf7 update: after blur emit the event for changeName 2020-04-06 13:05:32 +08:00
ayou
5f94ebf51b Merge pull request #65 from ParadeTo/feature-code-format
chore: 🤖 add code format tool
2020-01-30 13:36:42 +08:00
youxingzhi
53320cc235 chore: 🤖 change lint-staged conf 2020-01-30 13:34:24 +08:00
youxingzhi
d73b4c1829 chore: 🤖 add code format tool 2020-01-30 12:01:36 +08:00
ayou
15f33d187d Merge pull request #64 from ParadeTo/fix-#62
fix: 🐛 #62
2020-01-30 10:44:55 +08:00
youxingzhi
a67e39ce31 fix: 🐛 #62 2020-01-30 10:43:51 +08:00
youxingzhi
69ffc1da0f 1.4.3 2020-01-07 11:44:57 +08:00
ayou
31c9225441 Merge pull request #56 from ParadeTo/bugfix/style-including
bugfix: extracted css file
2020-01-07 11:44:07 +08:00
ayou
64c56af961 Merge pull request #57 from ParadeTo/feature/plugin-export
feat: plugin export
2020-01-07 11:43:46 +08:00
Binbin Sun
a792ee3910 chore: 🤖 move core-js & vue to devDependencies 2020-01-07 11:38:23 +08:00
Binbin Sun
aa3359155f docs: ✏️ install doc 2020-01-07 11:27:16 +08:00
Binbin Sun
c1270b880b feat: 🎸 export default with the install method 2020-01-07 11:21:17 +08:00
Binbin Sun
d8a5da1e0e feat: 🎸 extracted css file
Closes: #55
2020-01-07 10:59:06 +08:00
youxingzhi
780d42c6ea 1.4.2 2020-01-06 17:40:23 +08:00
ayou
9c2d25e313 Merge pull request #54 from ParadeTo/feature-#47
feat: 🎸 #47
2020-01-06 17:37:43 +08:00
youxingzhi
fbd370e9e5 feat: 🎸 #47 2020-01-03 23:19:55 +08:00
ayou
332402dee6 Merge pull request #53 from weiqian93/master
feat: support different icon for opened node
2020-01-03 21:33:58 +08:00
qian.wei
61ae848898 feat: 🎸 support different icon for opened node
Closes: #47
2020-01-03 19:44:42 +08:00
ayou
82e87e493a Merge pull request #51 from ParadeTo/feature-add-tests
Feature add tests
2019-12-31 09:18:31 +08:00
ayou
2427f47201 Merge pull request #52 from ParadeTo/add-license-1
Create LICENSE
2019-12-30 22:48:45 +08:00
ayou
fab494cedf Create LICENSE 2019-12-30 22:46:31 +08:00
youxingzhi
4e37591f10 test: 💍 update snapshot 2019-12-30 22:45:07 +08:00
youxingzhi
8b23327cee ci: 🎡 change test:unit to test:coverage 2019-12-30 22:39:13 +08:00
youxingzhi
ad6ad1b255 1.4.1 2019-12-30 21:51:20 +08:00
youxingzhi
2fe4da8605 docs: ✏️ add action badge 2019-12-30 21:48:06 +08:00
youxingzhi
d545e6bdbb Merge remote-tracking branch 'origin/master' into feature-add-tests 2019-12-30 21:35:46 +08:00
youxingzhi
d4f911b7b5 test: 💍 add some tests 2019-12-30 12:17:51 +08:00
ayou
9ded7fe914 Merge pull request #50 from ParadeTo/feature/github-actions
Create test.yml
2019-12-29 22:45:39 +08:00
Binbin Sun
b07e14b9ff chore: 🤖 update test.yml 2019-12-29 22:40:08 +08:00
Binbin Sun
fa2b7cb9dd Create test.yml 2019-12-29 22:31:23 +08:00
29 changed files with 17530 additions and 14547 deletions

View File

@@ -1,2 +0,0 @@
extends:
- "@commitlint/config-conventional"

3
.czrc
View File

@@ -1,3 +0,0 @@
{
"path": "cz-conventional-changelog"
}

29
.eslintrc Normal file
View File

@@ -0,0 +1,29 @@
{
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"plugin:prettier/recommended",
"eslint:recommended"
],
"rules": {
"prettier/prettier": "error",
"no-console": "warn"
},
"parserOptions": {
"parser": "babel-eslint"
},
"overrides": [
{
"files": [
"**/__tests__/*.{j,t}s?(x)",
"**/tests/unit/**/*.spec.{j,t}s?(x)"
],
"env": {
"jest": true
}
}
]
}

15
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,15 @@
name: Test
on: [pull_request, push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: npm install, lint and test
run: |
npm install
npm run lint
npm run test:coverage

25
.gitignore vendored
View File

@@ -19,3 +19,28 @@ yarn-error.log*
*.njsproj
*.sln
*.sw?
coverage
# Created by https://www.toptal.com/developers/gitignore/api/yarn
# Edit at https://www.toptal.com/developers/gitignore?templates=yarn
### yarn ###
# https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
.yarn/*
!.yarn/releases
!.yarn/patches
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
# if you are NOT using Zero-installs, then:
# comment the following lines
#!.yarn/cache
# and uncomment the following lines
.pnp.*
# End of https://www.toptal.com/developers/gitignore/api/yarn

View File

@@ -1,4 +0,0 @@
hooks:
pre-commit:
- npm run standard
commit-msg: commitlint -E HUSKY_GIT_PARAMS

File diff suppressed because one or more lines are too long

785
.yarn/releases/yarn-3.2.0.cjs vendored Executable file

File diff suppressed because one or more lines are too long

7
.yarnrc.yml Normal file
View File

@@ -0,0 +1,7 @@
nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
yarnPath: .yarn/releases/yarn-3.2.0.cjs

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 ayou
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,5 +1,3 @@
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
presets: ['@vue/cli-plugin-babel/preset']
}

View File

@@ -12,13 +12,37 @@
:model="data"
default-tree-node-name="new node"
default-leaf-node-name="new leaf"
v-bind:default-expanded="false">
<span class="icon" slot="addTreeNodeIcon">📂</span>
<span class="icon" slot="addLeafNodeIcon"></span>
<span class="icon" slot="editNodeIcon">📃</span>
<span class="icon" slot="delNodeIcon"></span>
<span class="icon" slot="leafNodeIcon">🍃</span>
<span class="icon" slot="treeNodeIcon">🌲</span>
v-bind:default-expanded="false"
>
<template v-slot:leafNameDisplay="slotProps">
<span>
{{ slotProps.model.name }} <span class="muted">#{{ slotProps.model.id }}</span>
</span>
</template>
<template v-slot:addTreeNodeIcon="slotProps">
<span class="icon">📂</span>
</template>
<template v-slot:addLeafNodeIcon="slotProps">
<span class="icon"></span>
</template>
<template v-slot:editNodeIcon="slotProps">
<span class="icon">📃</span>
</template>
<template v-slot:delNodeIcon="slotProps">
<span class="icon"></span>
</template>
<template v-slot:leafNodeIcon="slotProps">
<span class="icon">🍃</span>
</template>
<template v-slot:treeNodeIcon="slotProps">
<span class="icon">
{{
slotProps.model.children && slotProps.model.children.length > 0 && !slotProps.expanded
? '🌲'
: ''
}}</span
>
</template>
</vue-tree-list>
<button @click="getNewTree">Get new tree</button>
<pre>
@@ -70,31 +94,38 @@
},
methods: {
onDel(node) {
// eslint-disable-next-line no-console
console.log(node)
node.remove()
},
onChangeName(params) {
// eslint-disable-next-line no-console
console.log(params)
},
onAddNode(params) {
// eslint-disable-next-line no-console
console.log(params)
},
onClick(params) {
// eslint-disable-next-line no-console
console.log(params)
},
drop: function({ node, src, target }) {
// eslint-disable-next-line no-console
console.log('drop', node, src, target)
},
dropBefore: function({ node, src, target }) {
// eslint-disable-next-line no-console
console.log('drop-before', node, src, target)
},
dropAfter: function({ node, src, target }) {
// eslint-disable-next-line no-console
console.log('drop-after', node, src, target)
},
@@ -149,4 +180,9 @@
cursor: pointer;
}
}
.muted {
color: gray;
font-size: 80%;
}
</style>

View File

@@ -1,4 +1,6 @@
module.exports = {
preset: "@vue/cli-plugin-unit-jest",
snapshotSerializers: ["jest-serializer-vue"]
};
preset: '@vue/cli-plugin-unit-jest',
snapshotSerializers: ['jest-serializer-vue'],
collectCoverageFrom: ['src/**/*.{js,vue}'],
coveragePathIgnorePatterns: ['src/index.js']
}

13806
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,21 +1,18 @@
{
"name": "vue-tree-list",
"version": "1.4.1",
"version": "1.5.0",
"description": "A vue component for tree structure. Support adding treenode/leafnode, editing node's name and dragging.",
"author": "ayou",
"scripts": {
"serve": "vue-cli-service serve dev",
"build": "vue-cli-service build --target lib src/index.js",
"test:unit": "vue-cli-service test:unit",
"test:unit": "vue-cli-service test:unit --watch",
"test:coverage": "vue-cli-service test:unit --coverage",
"lint": "vue-cli-service lint",
"commit": "npx git-cz",
"lint-staged": "lint-staged",
"prepublish": "npm run build"
},
"main": "dist/vue-tree-list.umd.min.js",
"dependencies": {
"core-js": "^3.4.3",
"vue": "^2.6.10"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.1.0",
"@vue/cli-plugin-eslint": "^4.1.0",
@@ -23,38 +20,23 @@
"@vue/cli-service": "^4.1.0",
"@vue/test-utils": "1.0.0-beta.29",
"babel-eslint": "^10.0.3",
"core-js": "^3.4.3",
"eslint": "^5.16.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-vue": "^5.0.0",
"jest-serializer-vue": "^2.0.2",
"less": "^3.10.3",
"less-loader": "^5.0.0",
"lint-staged": "^10.0.4",
"prettier": "^1.19.1",
"prettier-eslint-cli": "^5.0.0",
"vue": "^2.6.10",
"vue-template-compiler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {
"no-console": "warn"
},
"parserOptions": {
"parser": "babel-eslint"
},
"overrides": [
{
"files": [
"**/__tests__/*.{j,t}s?(x)",
"**/tests/unit/**/*.spec.{j,t}s?(x)"
],
"env": {
"jest": true
}
}
"lint-staged": {
"**/*.{js,json,md,vue}": [
"prettier --write"
]
},
"browserslist": [
@@ -73,5 +55,6 @@
"repository": {
"type": "git",
"url": "git+https://github.com/ParadeTo/vue-tree-list.git"
}
},
"packageManager": "yarn@3.2.0"
}

15
prettier.config.js Normal file
View File

@@ -0,0 +1,15 @@
module.exports = {
printWidth: 100,
tabWidth: 2,
useTabs: false,
semi: false,
singleQuote: true,
jsxSingleQuote: true,
bracketSpacing: true,
jsxBracketSameLine: false,
rangeStart: 0,
rangeEnd: Infinity,
requirePragma: false,
insertPragma: false,
htmlWhitespaceSensitivity: 'css'
}

136
readme.md
View File

@@ -1,12 +1,29 @@
[![Actions Status](https://github.com/ParadeTo/vue-tree-list/workflows/Test/badge.svg)](https://github.com/ParadeTo/vue-tree-list/actions)
# vue-tree-list
A vue component for tree structure. Support adding treenode/leafnode, editing node's name and dragging.
![vue-tree-demo.gif](https://raw.githubusercontent.com/ParadeTo/vue-tree-list/master/img/demo.gif)
[Live Demo](http://paradeto.com/vue-tree-list/)
# install
Install the plugin then you can use the component globally.
```js
import Vue from 'vue'
import VueTreeList from 'vue-tree-list'
Vue.use(VueTreeList)
```
Or just register locally like the example below.
# use
``npm install vue-tree-list``
`npm install vue-tree-list`
```html
<template>
@@ -20,7 +37,13 @@ A vue component for tree structure. Support adding treenode/leafnode, editing no
:model="data"
default-tree-node-name="new node"
default-leaf-node-name="new leaf"
v-bind:default-expanded="false">
v-bind:default-expanded="false"
>
<template v-slot:leafNameDisplay="slotProps">
<span>
{{ slotProps.model.name }} <span class="muted">#{{ slotProps.model.id }}</span>
</span>
</template>
<span class="icon" slot="addTreeNodeIcon">📂</span>
<span class="icon" slot="addLeafNodeIcon"></span>
<span class="icon" slot="editNodeIcon">📃</span>
@@ -122,8 +145,7 @@ A vue component for tree structure. Support adding treenode/leafnode, editing no
}
vm.newTree = _dfs(vm.data)
},
}
}
}
</script>
@@ -148,62 +170,90 @@ A vue component for tree structure. Support adding treenode/leafnode, editing no
cursor: pointer;
}
}
</style>
.muted {
color: gray;
font-size: 80%;
}
</style>
```
# props
## props of vue-tree-list
| name | type | default | description |
|:-----:|:-------:|:------------:|:----:|
model | TreeNode | - | You can use `const head = new Tree([])` to generate a tree with the head of `TreeNode` type
default-tree-node-name | string | New node node | Default name for new treenode
default-leaf-node-name | string | New leaf node | Default name for new leafnode
default-expanded | boolean | true | Tree is expanded or not
## props of vue-tree-list
| name | type | default | description |
| :--------------------: | :------: | :-----------: | :-----------------------------------------------------------------------------------------: |
| model | TreeNode | - | You can use `const head = new Tree([])` to generate a tree with the head of `TreeNode` type |
| default-tree-node-name | string | New node node | Default name for new treenode |
| default-leaf-node-name | string | New leaf node | Default name for new leafnode |
| default-expanded | boolean | true | Tree is expanded or not |
## props of TreeNode
### attributes
| name | type | default | description |
|:-----:|:-------:|:------------:|:----:|
id | string, number | current timestamp | The node's id
isLeaf | boolean | false | The node is leaf or not
dragDisabled | boolean | false | Forbid dragging tree node
addTreeNodeDisabled | boolean | false | Show `addTreeNode` button or not
addLeafNodeDisabled | boolean | false | Show `addLeafNode` button or not
editNodeDisabled | boolean | false | Show `editNode` button or not
delNodeDisabled | boolean | false | Show `delNode` button or not
children | array | null | The children of node
| :-----------------: | :------------: | :---------------: | :------------------------------: |
| id | string, number | current timestamp | The node's id |
| isLeaf | boolean | false | The node is leaf or not |
| dragDisabled | boolean | false | Forbid dragging tree node |
| addTreeNodeDisabled | boolean | false | Show `addTreeNode` button or not |
| addLeafNodeDisabled | boolean | false | Show `addLeafNode` button or not |
| editNodeDisabled | boolean | false | Show `editNode` button or not |
| delNodeDisabled | boolean | false | Show `delNode` button or not |
| children | array | null | The children of node |
### methods
| name | params | description |
|:-----:|:-------:|:----:|
changeName | name | Change node's name
addChildren | children: object, array | Add children to node
remove | - | Remove node from the tree
moveInto | target: TreeNode | Move node into another node
insertBefore | target: TreeNode | Move node before another node
insertAfter | target: TreeNode | Move node after another node
| :----------: | :---------------------: | :---------------------------: |
| changeName | name | Change node's name |
| addChildren | children: object, array | Add children to node |
| remove | - | Remove node from the tree |
| moveInto | target: TreeNode | Move node into another node |
| insertBefore | target: TreeNode | Move node before another node |
| insertAfter | target: TreeNode | Move node after another node |
# events
| name | params | description |
|:-----:|:-------:|:----:|
click | TreeNode | Trigger when clicking a tree node
change-name | {'id', 'oldName', 'newName'} | Trigger after changing a node's name
delete-node | TreeNode | Trigger when clicking `delNode` button. You can call `remove` of `TreeNode` to remove the node.
add-node | TreeNode | Trigger after adding a new node
drop | {node, src, target} | Trigger after dropping a node into another. node: the draggable node, src: the draggable node's parent, target: the node that draggable node will drop into
drop-before | {node, src, target} | Trigger after dropping a node before another. node: the draggable node, src: the draggable node's parent, target: the node that draggable node will drop before
drop-after | {node, src, target} | Trigger after dropping a node after another. node: the draggable node, src: the draggable node's parent, target: the node that draggable node will drop after
| :---------: | :--------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| click | TreeNode | Trigger when clicking a tree node. You can call `toggle` of `TreeNode` to toggle the folder node. |
| change-name | {'id', 'oldName', 'newName'} | Trigger after changing a node's name |
| delete-node | TreeNode | Trigger when clicking `delNode` button. You can call `remove` of `TreeNode` to remove the node. |
| add-node | TreeNode | Trigger after adding a new node |
| drop | {node, src, target} | Trigger after dropping a node into another. node: the draggable node, src: the draggable node's parent, target: the node that draggable node will drop into |
| drop-before | {node, src, target} | Trigger after dropping a node before another. node: the draggable node, src: the draggable node's parent, target: the node that draggable node will drop before |
| drop-after | {node, src, target} | Trigger after dropping a node after another. node: the draggable node, src: the draggable node's parent, target: the node that draggable node will drop after |
# customize operation icons
The component has default icons for `addTreeNodeIcon`, `addLeafNodeIcon`, `editNodeIcon`, `delNodeIcon`, `leafNodeIcon`, `treeNodeIcon` button, but you can also customize them:
The component has default icons for `addTreeNodeIcon`, `addLeafNodeIcon`, `editNodeIcon`, `delNodeIcon`, `leafNodeIcon`, `treeNodeIcon` button, but you can also customize them and can access `model`, `root`, `expanded` as below:
```html
<span class="icon" slot="addTreeNodeIcon">📂</span>
<span class="icon" slot="addLeafNodeIcon"></span>
<span class="icon" slot="editNodeIcon">📃</span>
<span class="icon" slot="delNodeIcon">✂️</span>
<span class="icon" slot="leafNodeIcon">🍃</span>
<span class="icon" slot="treeNodeIcon">🌲</span>
<template v-slot:leafNameDisplay="slotProps">
<span>{{ slotProps.model.name }} #{{ slotProps.model.id }}</span>
</template>
<template v-slot:addTreeNodeIcon="slotProps">
<span class="icon">📂</span>
</template>
<template v-slot:addLeafNodeIcon="slotProps">
<span class="icon"></span>
</template>
<template v-slot:editNodeIcon="slotProps">
<span class="icon">📃</span>
</template>
<template v-slot:delNodeIcon="slotProps">
<span class="icon">✂️</span>
</template>
<template v-slot:leafNodeIcon="slotProps">
<span class="icon">🍃</span>
</template>
<template v-slot:treeNodeIcon="slotProps">
<span class="icon">
{{ (slotProps.model.children && slotProps.model.children.length > 0 && !slotProps.expanded) ?
'🌲' : '' }}</span
>
</template>
```

View File

@@ -1,3 +1,4 @@
import { traverseTree } from './tools'
/**
* Tree data struct
* Created by ayou on 2017/7/20.
@@ -8,9 +9,10 @@
* dragDisabled: decide if it can be dragged
* disabled: desabled all operation
*/
const TreeNode = function (data) {
export class TreeNode {
constructor(data) {
const { id, isLeaf } = data
this.id = (typeof id === 'undefined') ? new Date().valueOf() : id
this.id = typeof id === 'undefined' ? new Date().valueOf() : id
this.parent = null
this.children = null
this.isLeaf = !!isLeaf
@@ -23,11 +25,11 @@ const TreeNode = function (data) {
}
}
TreeNode.prototype.changeName = function (name) {
changeName(name) {
this.name = name
}
TreeNode.prototype.addChildren = function (children) {
addChildren(children) {
if (!this.children) {
this.children = []
}
@@ -48,14 +50,14 @@ TreeNode.prototype.addChildren = function (children) {
}
// remove self
TreeNode.prototype.remove = function () {
remove() {
const parent = this.parent
const index = parent.findChildIndex(this)
parent.children.splice(index, 1)
}
// remove child
TreeNode.prototype._removeChild = function (child) {
_removeChild(child) {
for (var i = 0, len = this.children.length; i < len; i++) {
if (this.children[i] === child) {
this.children.splice(i, 1)
@@ -64,7 +66,7 @@ TreeNode.prototype._removeChild = function (child) {
}
}
TreeNode.prototype.isTargetChild = function (target) {
isTargetChild(target) {
let parent = target.parent
while (parent) {
if (parent === this) {
@@ -75,7 +77,7 @@ TreeNode.prototype.isTargetChild = function (target) {
return false
}
TreeNode.prototype.moveInto = function (target) {
moveInto(target) {
if (this.name === 'root' || this === target) {
return
}
@@ -99,7 +101,7 @@ TreeNode.prototype.moveInto = function (target) {
target.children.unshift(this)
}
TreeNode.prototype.findChildIndex = function (child) {
findChildIndex(child) {
var index
for (let i = 0, len = this.children.length; i < len; i++) {
if (this.children[i] === child) {
@@ -110,12 +112,12 @@ TreeNode.prototype.findChildIndex = function (child) {
return index
}
TreeNode.prototype._beforeInsert = function (target) {
_canInsert(target) {
if (this.name === 'root' || this === target) {
return false
}
// cannot move ancestor to child
// cannot insert ancestor to child
if (this.isTargetChild(target)) {
return false
}
@@ -126,27 +128,33 @@ TreeNode.prototype._beforeInsert = function (target) {
return true
}
TreeNode.prototype.insertBefore = function (target) {
if (!this._beforeInsert(target)) return
insertBefore(target) {
if (!this._canInsert(target)) return
const pos = target.parent.findChildIndex(target)
target.parent.children.splice(pos, 0, this)
}
TreeNode.prototype.insertAfter = function (target) {
if (!this._beforeInsert(target)) return
insertAfter(target) {
if (!this._canInsert(target)) return
const pos = target.parent.findChildIndex(target)
target.parent.children.splice(pos + 1, 0, this)
}
function Tree (data) {
toString() {
return JSON.stringify(traverseTree(this))
}
}
export class Tree {
constructor(data) {
this.root = new TreeNode({ name: 'root', isLeaf: false, id: 0 })
this.initNode(this.root, data)
return this.root
}
Tree.prototype.initNode = function (node, data) {
initNode(node, data) {
for (let i = 0, len = data.length; i < len; i++) {
var _data = data[i]
@@ -157,6 +165,4 @@ Tree.prototype.initNode = function (node, data) {
node.addChildren(child)
}
}
exports.Tree = Tree
exports.TreeNode = TreeNode
}

View File

@@ -1,87 +1,138 @@
<template>
<div class='vtl'>
<div v-if="model.name !== 'root'">
<div class="vtl-border vtl-up" :class="{'vtl-active': isDragEnterUp}"
<div class="vtl">
<div
v-if="model.name !== 'root'"
:id="model.id"
class="vtl-node"
:class="{ 'vtl-leaf-node': model.isLeaf, 'vtl-tree-node': !model.isLeaf }"
>
<div
class="vtl-border vtl-up"
:class="{ 'vtl-active': isDragEnterUp }"
@drop="dropBefore"
@dragenter="dragEnterUp"
@dragover='dragOverUp'
@dragleave="dragLeaveUp"></div>
<div :id='model.id' :class="treeNodeClass"
@dragover="dragOverUp"
@dragleave="dragLeaveUp"
/>
<div
:class="treeNodeClass"
:draggable="!model.dragDisabled"
@dragstart='dragStart'
@dragover='dragOver'
@dragenter='dragEnter'
@dragleave='dragLeave'
@drop='drop'
@dragend='dragEnd'
@mouseover='mouseOver'
@mouseout='mouseOut'
@click.stop='click'>
@dragstart="dragStart"
@dragover="dragOver"
@dragenter="dragEnter"
@dragleave="dragLeave"
@drop="drop"
@dragend="dragEnd"
@mouseover="mouseOver"
@mouseout="mouseOut"
@click.stop="click"
>
<span class="vtl-caret vtl-is-small" v-if="model.children && model.children.length > 0">
<i class="vtl-icon" :class="caretClass" @click.prevent.stop="toggle"></i>
</span>
<span v-if="model.isLeaf">
<slot name="leafNodeIcon">
<slot name="leafNodeIcon" :expanded="expanded" :model="model" :root="rootNode">
<i class="vtl-icon vtl-menu-icon vtl-icon-file"></i>
</slot>
</span>
<span v-else>
<slot name="treeNodeIcon">
<slot name="treeNodeIcon" :expanded="expanded" :model="model" :root="rootNode">
<i class="vtl-icon vtl-menu-icon vtl-icon-folder"></i>
</slot>
</span>
<div class="vtl-node-content" v-if="!editable">
<slot name="leafNameDisplay" :expanded="expanded" :model="model" :root="rootNode">
{{ model.name }}
</slot>
</div>
<input v-else class="vtl-input" type="text" ref="nodeInput" :value="model.name" @input="updateName" @blur="setUnEditable">
<input
v-else
class="vtl-input"
type="text"
ref="nodeInput"
:value="model.name"
@input="updateName"
@blur="setUnEditable"
/>
<div class="vtl-operation" v-show="isHover">
<span title="add tree node" @click.stop.prevent="addChild(false)" v-if="!model.isLeaf && !model.addTreeNodeDisabled">
<slot name="addTreeNodeIcon">
<span
:title="defaultAddTreeNodeTitle"
@click.stop.prevent="addChild(false)"
v-if="!model.isLeaf && !model.addTreeNodeDisabled"
>
<slot name="addTreeNodeIcon" :expanded="expanded" :model="model" :root="rootNode">
<i class="vtl-icon vtl-icon-folder-plus-e"></i>
</slot>
</span>
<span title="add leaf node" @click.stop.prevent="addChild(true)" v-if="!model.isLeaf && !model.addLeafNodeDisabled">
<slot name="addLeafNodeIcon">
<span
:title="defaultAddLeafNodeTitle"
@click.stop.prevent="addChild(true)"
v-if="!model.isLeaf && !model.addLeafNodeDisabled"
>
<slot name="addLeafNodeIcon" :expanded="expanded" :model="model" :root="rootNode">
<i class="vtl-icon vtl-icon-plus"></i>
</slot>
</span>
<span title="edit" @click.stop.prevent="setEditable" v-if="!model.editNodeDisabled">
<slot name="editNodeIcon">
<slot name="editNodeIcon" :expanded="expanded" :model="model" :root="rootNode">
<i class="vtl-icon vtl-icon-edit"></i>
</slot>
</span>
<span title="delete" @click.stop.prevent="delNode" v-if="!model.delNodeDisabled">
<slot name="delNodeIcon">
<slot name="delNodeIcon" :expanded="expanded" :model="model" :root="rootNode">
<i class="vtl-icon vtl-icon-trash"></i>
</slot>
</span>
</div>
</div>
<div v-if="model.children && model.children.length > 0 && expanded"
<div
v-if="model.children && model.children.length > 0 && expanded"
class="vtl-border vtl-bottom"
:class="{ 'vtl-active': isDragEnterBottom }"
@drop="dropAfter"
@dragenter="dragEnterBottom"
@dragover='dragOverBottom'
@dragleave="dragLeaveBottom"></div>
@dragover="dragOverBottom"
@dragleave="dragLeaveBottom"
></div>
</div>
<div :class="{'vtl-tree-margin': model.name !== 'root'}" v-show="model.name === 'root' || expanded" v-if="isFolder">
<item v-for="model in model.children"
<div
:class="{ 'vtl-tree-margin': model.name !== 'root' }"
v-show="model.name === 'root' || expanded"
v-if="isFolder"
>
<item
v-for="model in model.children"
:default-tree-node-name="defaultTreeNodeName"
:default-leaf-node-name="defaultLeafNodeName"
v-bind:default-expanded="defaultExpanded"
:default-expanded="defaultExpanded"
:model="model"
:key='model.id'>
<slot name="addTreeNodeIcon" slot="addTreeNodeIcon" />
<slot name="addLeafNodeIcon" slot="addLeafNodeIcon" />
<slot name="editNodeIcon" slot="editNodeIcon" />
<slot name="delNodeIcon" slot="delNodeIcon" />
<slot name="leafNodeIcon" slot="leafNodeIcon" />
<slot name="treeNodeIcon" slot="treeNodeIcon" />
:key="model.id"
>
<template v-slot:leafNameDisplay="slotProps">
<slot name="leafNameDisplay" v-bind="slotProps" />
</template>
<template v-slot:addTreeNodeIcon="slotProps">
<slot name="addTreeNodeIcon" v-bind="slotProps" />
</template>
<template v-slot:addLeafNodeIcon="slotProps">
<slot name="addLeafNodeIcon" v-bind="slotProps" />
</template>
<template v-slot:editNodeIcon="slotProps">
<slot name="editNodeIcon" v-bind="slotProps" />
</template>
<template v-slot:delNodeIcon="slotProps">
<slot name="delNodeIcon" v-bind="slotProps" />
</template>
<template v-slot:leafNodeIcon="slotProps">
<slot name="leafNodeIcon" v-bind="slotProps" />
</template>
<template v-slot:treeNodeIcon="slotProps">
<slot name="treeNodeIcon" v-bind="slotProps" />
</template>
</item>
</div>
</div>
@@ -94,6 +145,7 @@
let compInOperation = null
export default {
name: 'vue-tree-list',
data: function() {
return {
isHover: false,
@@ -110,11 +162,19 @@
},
defaultLeafNodeName: {
type: String,
default: 'New leaf node'
default: 'Leaf Node'
},
defaultTreeNodeName: {
type: String,
default: 'New tree node'
default: 'Tree Node'
},
defaultAddTreeNodeTitle: {
type: String,
default: 'Add Tree Node'
},
defaultAddLeafNodeTitle: {
type: String,
default: 'Add Leaf Node'
},
defaultExpanded: {
type: Boolean,
@@ -122,8 +182,12 @@
}
},
computed: {
itemIconClass () {
return this.model.isLeaf ? 'vtl-icon-file' : 'vtl-icon-folder'
rootNode() {
var node = this.$parent
while (node._props.model.name !== 'root') {
node = node.$parent
}
return node
},
caretClass() {
@@ -131,21 +195,17 @@
},
isFolder() {
return this.model.children &&
this.model.children.length
return this.model.children && this.model.children.length
},
treeNodeClass() {
const {
model: {
dragDisabled,
disabled
},
model: { dragDisabled, disabled },
isDragEnterNode
} = this
return {
'vtl-tree-node': true,
'vtl-node-main': true,
'vtl-active': isDragEnterNode,
'vtl-drag-disabled': dragDisabled,
'vtl-disabled': disabled
@@ -169,15 +229,18 @@
},
methods: {
updateName(e) {
var oldName = this.model.name;
var oldName = this.model.name
this.model.changeName(e.target.value)
var node = this.getRootNode();
node.$emit('change-name', {'id': this.model.id, 'oldName': oldName, 'newName': e.target.value})
this.rootNode.$emit('change-name', {
id: this.model.id,
oldName: oldName,
newName: e.target.value,
node: this.model
})
},
delNode() {
var node = this.getRootNode()
node.$emit('delete-node', this.model)
this.rootNode.$emit('delete-node', this.model)
},
setEditable() {
@@ -189,8 +252,16 @@
})
},
setUnEditable () {
setUnEditable(e) {
this.editable = false
var oldName = this.model.name
this.model.changeName(e.target.value)
this.rootNode.$emit('change-name', {
id: this.model.id,
oldName: oldName,
newName: e.target.value,
eventType: 'blur'
})
},
toggle() {
@@ -209,8 +280,10 @@
},
click() {
var node = this.getRootNode()
node.$emit('click', this.model);
this.rootNode.$emit('click', {
toggle: this.toggle,
...this.model
})
},
addChild(isLeaf) {
@@ -218,15 +291,14 @@
this.expanded = true
var node = new TreeNode({ name, isLeaf })
this.model.addChildren(node, true)
var root = this.getRootNode();
root.$emit('add-node', node)
this.rootNode.$emit('add-node', node)
},
dragStart(e) {
if (!(this.model.dragDisabled || this.model.disabled)) {
compInOperation = this
// for firefox
e.dataTransfer.setData("data","data");
e.dataTransfer.setData('data', 'data')
e.dataTransfer.effectAllowed = 'move'
return true
}
@@ -241,7 +313,8 @@
},
dragEnter() {
if (!compInOperation) return
if (this.model.isLeaf) return
if (compInOperation.model.id === this.model.id || !compInOperation || this.model.isLeaf)
return
this.isDragEnterNode = true
},
dragLeave() {
@@ -249,11 +322,14 @@
},
drop() {
if (!compInOperation) return
const oldParent = compInOperation.model.parent;
const oldParent = compInOperation.model.parent
compInOperation.model.moveInto(this.model)
this.isDragEnterNode = false
var node = this.getRootNode();
node.$emit('drop', {target: this.model, node: compInOperation.model, src: oldParent})
this.rootNode.$emit('drop', {
target: this.model,
node: compInOperation.model,
src: oldParent
})
},
dragEnterUp() {
@@ -270,11 +346,14 @@
},
dropBefore() {
if (!compInOperation) return
const oldParent = compInOperation.model.parent;
const oldParent = compInOperation.model.parent
compInOperation.model.insertBefore(this.model)
this.isDragEnterUp = false
var node = this.getRootNode();
node.$emit('drop-before', {target: this.model, node: compInOperation.model, src: oldParent})
this.rootNode.$emit('drop-before', {
target: this.model,
node: compInOperation.model,
src: oldParent
})
},
dragEnterBottom() {
@@ -291,18 +370,14 @@
},
dropAfter() {
if (!compInOperation) return
const oldParent = compInOperation.model.parent;
const oldParent = compInOperation.model.parent
compInOperation.model.insertAfter(this.model)
this.isDragEnterBottom = false
var node = this.getRootNode();
node.$emit('drop-after', {target: this.model, node: compInOperation.model, src: oldParent})
},
getRootNode() {
var node = this.$parent
while (node._props.model.name !== 'root') {
node = node.$parent
}
return node;
this.rootNode.$emit('drop-after', {
target: this.model,
node: compInOperation.model,
src: oldParent
})
}
}
}
@@ -345,31 +420,30 @@
}
.vtl-icon-file:before {
content: "\e906";
content: '\e906';
}
.vtl-icon-folder:before {
content: "\e907";
content: '\e907';
}
.vtl-icon-caret-down:before {
content: "\e901";
content: '\e901';
}
.vtl-icon-caret-right:before {
content: "\e900";
content: '\e900';
}
.vtl-icon-edit:before {
content: "\e902";
content: '\e902';
}
.vtl-icon-folder-plus-e:before {
content: "\e903";
content: '\e903';
}
.vtl-icon-plus:before {
content: "\e904";
content: '\e904';
}
.vtl-icon-trash:before {
content: "\e905";
content: '\e905';
}
.vtl-border {
height: 5px;
&.vtl-up {
@@ -385,7 +459,7 @@
}
}
.vtl-tree-node {
.vtl-node-main {
display: flex;
align-items: center;
padding: 5px 0 5px 1rem;
@@ -409,7 +483,6 @@
}
}
.vtl-item {
cursor: pointer;
}

View File

@@ -2,7 +2,12 @@
* Created by ayou on 17/7/21.
*/
import VueTreeList from "./VueTreeList";
import { Tree, TreeNode } from "./Tree";
import VueTreeList from './VueTreeList'
import { Tree, TreeNode } from './Tree'
export { Tree, TreeNode, VueTreeList };
VueTreeList.install = Vue => {
Vue.component(VueTreeList.name, VueTreeList)
}
export default VueTreeList
export { Tree, TreeNode, VueTreeList }

View File

@@ -4,7 +4,7 @@
var handlerCache
exports.addHandler = function (element, type, handler) {
export const addHandler = function(element, type, handler) {
handlerCache = handler
if (element.addEventListener) {
element.addEventListener(type, handler, false)
@@ -15,7 +15,7 @@ exports.addHandler = function (element, type, handler) {
}
}
exports.removeHandler = function (element, type) {
export const removeHandler = function(element, type) {
if (element.removeEventListener) {
element.removeEventListener(type, handlerCache, false)
} else if (element.detachEvent) {
@@ -25,7 +25,21 @@ exports.removeHandler = function (element, type) {
}
}
// exports.fireFocusEvent = function (ele) {
// var event = new FocusEvent()
// ele.dispatch(event)
// }
// depth first search
export const traverseTree = root => {
var newRoot = {}
for (var k in root) {
if (k !== 'children' && k !== 'parent') {
newRoot[k] = root[k]
}
}
if (root.children && root.children.length > 0) {
newRoot.children = []
for (var i = 0, len = root.children.length; i < len; i++) {
newRoot.children.push(traverseTree(root.children[i]))
}
}
return newRoot
}

View File

@@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`VueTreeList renders correctly 1`] = `
exports[`Render render correctly 1`] = `
<div class="vtl">
<!---->
<div class="">
<div class="vtl">
<div>
<div id="1" class="vtl-node vtl-tree-node">
<div class="vtl-border vtl-up"></div>
<div id="1" draggable="false" class="vtl-tree-node vtl-drag-disabled"><span class="vtl-caret vtl-is-small"><i class="vtl-icon vtl-icon-caret-right"></i></span> <span><i class="vtl-icon vtl-menu-icon vtl-icon-folder"></i></span>
<div draggable="false" class="vtl-node-main vtl-drag-disabled"><span class="vtl-caret vtl-is-small"><i class="vtl-icon vtl-icon-caret-right"></i></span> <span><i class="vtl-icon vtl-menu-icon vtl-icon-folder"></i></span>
<div class="vtl-node-content">
Node 1
</div>
@@ -22,9 +22,9 @@ exports[`VueTreeList renders correctly 1`] = `
</div>
<div class="vtl-tree-margin" style="display: none;">
<div class="vtl">
<div>
<div id="2" class="vtl-node vtl-leaf-node">
<div class="vtl-border vtl-up"></div>
<div id="2" draggable="true" class="vtl-tree-node">
<div draggable="true" class="vtl-node-main">
<!----> <span><i class="vtl-icon vtl-menu-icon vtl-icon-file"></i></span>
<div class="vtl-node-content">
Node 1-2
@@ -40,28 +40,28 @@ exports[`VueTreeList renders correctly 1`] = `
</div>
</div>
<div class="vtl">
<div>
<div id="3" class="vtl-node vtl-tree-node">
<div class="vtl-border vtl-up"></div>
<div id="3" draggable="true" class="vtl-tree-node vtl-disabled">
<div draggable="true" class="vtl-node-main vtl-disabled">
<!----> <span><i class="vtl-icon vtl-menu-icon vtl-icon-folder"></i></span>
<div class="vtl-node-content">
Node 2
</div>
<div class="vtl-operation" style="display: none;"><span title="add tree node"><i class="vtl-icon vtl-icon-folder-plus-e"></i></span> <span title="add leaf node"><i class="vtl-icon vtl-icon-plus"></i></span> <span title="edit"><i class="vtl-icon vtl-icon-edit"></i></span> <span title="delete"><i class="vtl-icon vtl-icon-trash"></i></span></div>
<div class="vtl-operation" style="display: none;"><span title="Add Tree Node"><i class="vtl-icon vtl-icon-folder-plus-e"></i></span> <span title="Add Leaf Node"><i class="vtl-icon vtl-icon-plus"></i></span> <span title="edit"><i class="vtl-icon vtl-icon-edit"></i></span> <span title="delete"><i class="vtl-icon vtl-icon-trash"></i></span></div>
</div>
<!---->
</div>
<!---->
</div>
<div class="vtl">
<div>
<div id="4" class="vtl-node vtl-tree-node">
<div class="vtl-border vtl-up"></div>
<div id="4" draggable="true" class="vtl-tree-node">
<div draggable="true" class="vtl-node-main">
<!----> <span><i class="vtl-icon vtl-menu-icon vtl-icon-folder"></i></span>
<div class="vtl-node-content">
Node 3
</div>
<div class="vtl-operation" style="display: none;"><span title="add tree node"><i class="vtl-icon vtl-icon-folder-plus-e"></i></span> <span title="add leaf node"><i class="vtl-icon vtl-icon-plus"></i></span> <span title="edit"><i class="vtl-icon vtl-icon-edit"></i></span> <span title="delete"><i class="vtl-icon vtl-icon-trash"></i></span></div>
<div class="vtl-operation" style="display: none;"><span title="Add Tree Node"><i class="vtl-icon vtl-icon-folder-plus-e"></i></span> <span title="Add Leaf Node"><i class="vtl-icon vtl-icon-plus"></i></span> <span title="edit"><i class="vtl-icon vtl-icon-edit"></i></span> <span title="delete"><i class="vtl-icon vtl-icon-trash"></i></span></div>
</div>
<!---->
</div>

View File

@@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Slot render slot correctly 1`] = `
<div class="vtl">
<!---->
<div class="">
<div class="vtl">
<div id="t1" class="vtl-node vtl-tree-node">
<div class="vtl-border vtl-up"></div>
<div draggable="true" class="vtl-node-main"><span class="vtl-caret vtl-is-small"><i class="vtl-icon vtl-icon-caret-down"></i></span> <span><span class="tree-node-icon icon">❀</span></span>
<div class="vtl-node-content">
Node 1
</div>
<div class="vtl-operation" style="display: none;"><span title="Add Tree Node"><span class="add-tree-node-icon">📂</span></span> <span title="Add Leaf Node"><span class="icon"></span></span> <span title="edit"><span class="icon">📃</span></span> <span title="delete"><span></span></span></div>
</div>
<div class="vtl-border vtl-bottom"></div>
</div>
<div class="vtl-tree-margin">
<div class="vtl">
<div id="t11" class="vtl-node vtl-leaf-node">
<div class="vtl-border vtl-up"></div>
<div draggable="true" class="vtl-node-main">
<!----> <span><span class="icon">🍃</span></span>
<div class="vtl-node-content">
Node 1-1
</div>
<div class="vtl-operation" style="display: none;">
<!---->
<!----> <span title="edit"><span class="icon">📃</span></span> <span title="delete"><span class="del-node-icon">✂️</span></span></div>
</div>
<!---->
</div>
<!---->
</div>
</div>
</div>
<div class="vtl">
<div id="t2" class="vtl-node vtl-tree-node">
<div class="vtl-border vtl-up"></div>
<div draggable="true" class="vtl-node-main">
<!----> <span><span class="tree-node-icon icon">❀</span></span>
<div class="vtl-node-content">
Node 2
</div>
<div class="vtl-operation" style="display: none;"><span title="Add Tree Node"><span class="add-tree-node-icon">📂</span></span> <span title="Add Leaf Node"><span class="icon"></span></span> <span title="edit"><span class="icon">📃</span></span> <span title="delete"><span class="del-node-icon">✂️</span></span></div>
</div>
<!---->
</div>
<!---->
</div>
</div>
</div>
`;

92
tests/unit/drag.spec.js Normal file
View File

@@ -0,0 +1,92 @@
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import { Tree, VueTreeList } from '@/index'
describe('Drag', () => {
let wrapper
beforeEach(() => {
const tree = new Tree([
{
name: 'Node 1',
id: 't1',
pid: 0,
children: [
{
name: 'Node 1-1',
id: 't11',
isLeaf: true,
pid: 't1'
},
{
name: 'Node 1-2',
id: 't12',
pid: 't1'
}
]
},
{
name: 'Node 2',
id: 't2',
pid: 0
},
{
name: 'Node 3',
id: 't3',
pid: 0
}
])
wrapper = mount(VueTreeList, { propsData: { model: new Tree([]) } })
wrapper.setProps({ model: tree })
})
it('drag before', done => {
const $tree2 = wrapper.find('#t2 .vtl-node-main')
const $tree1Up = wrapper.find('#t1 .vtl-up')
$tree2.trigger('dragstart', { dataTransfer: { setData: () => {} } })
$tree1Up.trigger('drop')
Vue.nextTick(() => {
expect(wrapper.find('.vtl-node').attributes('id')).toBe('t2')
done()
})
})
it('drag after', done => {
const $tree3 = wrapper.find('#t3 .vtl-node-main')
const $tree1Bottom = wrapper.find('#t1 .vtl-bottom')
$tree3.trigger('dragstart', { dataTransfer: { setData: () => {} } })
$tree1Bottom.trigger('drop')
Vue.nextTick(() => {
expect(
wrapper
.findAll('.vtl-tree-node')
.at(2)
.attributes('id')
).toBe('t3')
done()
})
})
it('drag into', done => {
const $tree3 = wrapper.find('#t3 .vtl-node-main')
const $tree1 = wrapper.find('#t1 .vtl-node-main')
$tree3.trigger('dragstart', { dataTransfer: { setData: () => {} } })
$tree1.trigger('drop')
Vue.nextTick(() => {
expect(wrapper.find('#t1 + .vtl-tree-margin .vtl-node').attributes('id')).toBe('t3')
done()
})
})
it('cannot drag ancestor into child', done => {
const snapshot = wrapper.html()
const $tree1 = wrapper.find('#t1 .vtl-node-main')
const $tree1Child = wrapper.find('#t12 .vtl-node-main')
$tree1.trigger('dragstart', { dataTransfer: { setData: () => {} } })
$tree1Child.trigger('drop')
Vue.nextTick(() => {
expect(wrapper.html()).toBe(snapshot)
done()
})
})
})

View File

@@ -0,0 +1,86 @@
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import { Tree, VueTreeList } from '@/index'
describe('Operation', () => {
let wrapper
beforeEach(() => {
const tree = new Tree([
{
name: 'Node 1',
id: 't1',
pid: 0,
children: [
{
name: 'Node 1-1',
id: 't11',
isLeaf: true,
pid: 't1'
}
]
},
{
name: 'Node 2',
id: 't2',
pid: 0
}
])
wrapper = mount(VueTreeList, { propsData: { model: new Tree([]) } })
wrapper.setProps({ model: tree })
})
it('delete leaf node', done => {
const $node11Trash = wrapper.find('#t11 [title="delete"]')
$node11Trash.trigger('click')
wrapper.emitted('delete-node')[0][0].remove()
Vue.nextTick(() => {
expect(wrapper.findAll('.vtl-node').length).toBe(2)
done()
})
})
it('delete tree node', done => {
const $node11Trash = wrapper.find('#t1 [title="delete"]')
$node11Trash.trigger('click')
wrapper.emitted('delete-node')[0][0].remove()
Vue.nextTick(() => {
expect(wrapper.findAll('.vtl-node').length).toBe(1)
done()
})
})
it('add leaf node', done => {
const $node1AddLeafNode = wrapper.find('#t1 [title="Add Leaf Node"]')
$node1AddLeafNode.trigger('click')
Vue.nextTick(() => {
expect(wrapper.findAll('.vtl-leaf-node').length).toBe(2)
done()
})
})
it('add tree node', done => {
const $node1AddTreeNode = wrapper.find('#t1 [title="Add Tree Node"]')
$node1AddTreeNode.trigger('click')
Vue.nextTick(() => {
expect(wrapper.findAll('.vtl-tree-node').length).toBe(3)
done()
})
})
it('change node name', done => {
const $node1Edit = wrapper.find('#t1 [title="edit"]')
$node1Edit.trigger('click')
Vue.nextTick(() => {
const $input = wrapper.find('#t1 .vtl-input')
$input.element.value = 'New Node 1'
$input.trigger('input')
var event = new KeyboardEvent('keyup', { keyCode: 13 })
window.dispatchEvent(event)
Vue.nextTick(() => {
expect(wrapper.find('#t1').text()).toBe('New Node 1')
done()
})
})
})
})

View File

@@ -1,11 +1,11 @@
import { mount } from "@vue/test-utils";
import { Tree, VueTreeList } from "@/index";
import { mount } from '@vue/test-utils'
import { Tree, VueTreeList } from '@/index'
describe("VueTreeList", () => {
it("renders correctly", () => {
describe('Render', () => {
it('render correctly', () => {
const tree = new Tree([
{
name: "Node 1",
name: 'Node 1',
id: 1,
pid: 0,
dragDisabled: true,
@@ -15,7 +15,7 @@ describe("VueTreeList", () => {
delNodeDisabled: true,
children: [
{
name: "Node 1-2",
name: 'Node 1-2',
id: 2,
isLeaf: true,
pid: 1
@@ -23,27 +23,27 @@ describe("VueTreeList", () => {
]
},
{
name: "Node 2",
name: 'Node 2',
id: 3,
pid: 0,
disabled: true
},
{
name: "Node 3",
name: 'Node 3',
id: 4,
pid: 0
}
]);
])
const wrapper = mount(VueTreeList, {
propsData: {
model: tree,
defaultTreeNodeName: "new node",
defaultLeafNodeName: "new leaf",
defaultTreeNodeName: 'new node',
defaultLeafNodeName: 'new leaf',
defaultExpanded: false
}
});
})
expect(wrapper).toMatchSnapshot();
});
});
expect(wrapper).toMatchSnapshot()
})
})

91
tests/unit/slot.spec.js Normal file
View File

@@ -0,0 +1,91 @@
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import { Tree, VueTreeList } from '@/index'
describe('Slot', () => {
let wrapper
beforeEach(() => {
const tree = new Tree([
{
name: 'Node 1',
id: 't1',
pid: 0,
children: [
{
name: 'Node 1-1',
id: 't11',
isLeaf: true,
pid: 't1'
}
]
},
{
name: 'Node 2',
id: 't2',
pid: 0
}
])
wrapper = mount(VueTreeList, {
propsData: { model: new Tree([]) },
scopedSlots: {
addTreeNodeIcon() {
return <span class='add-tree-node-icon'>📂</span>
},
addLeafNodeIcon() {
return <span class='icon'></span>
},
editNodeIcon() {
return <span class='icon'>📃</span>
},
delNodeIcon(slotProps) {
return slotProps.model.isLeaf || !slotProps.model.children ? (
<span class='del-node-icon'></span>
) : (
<span />
)
},
leafNodeIcon() {
return <span class='icon'>🍃</span>
},
treeNodeIcon(slotProps) {
return (
<span class='tree-node-icon icon'>
{slotProps.model.children &&
slotProps.model.children.length > 0 &&
!slotProps.expanded
? '🌲'
: '❀'}
</span>
)
}
}
})
wrapper.setProps({ model: tree })
})
it('render slot correctly', () => {
expect(wrapper).toMatchSnapshot()
})
it('toggle tree node show different icon', done => {
const $caretDown = wrapper.find('.vtl-icon-caret-down')
expect(wrapper.find('#t1 .tree-node-icon').text()).toBe('❀')
$caretDown.trigger('click')
Vue.nextTick(() => {
expect(wrapper.exists('.vtl-icon-caret-right')).toBe(true)
expect(wrapper.find('#t1 .tree-node-icon').text()).toBe('🌲')
done()
})
})
it('dont show ✂️ after add child ', done => {
const $addTreeNodeIcon = wrapper.find('#t2 .add-tree-node-icon')
expect(wrapper.find('#t2 .del-node-icon').exists()).toBe(true)
$addTreeNodeIcon.trigger('click')
Vue.nextTick(() => {
expect(wrapper.find('#t2 .del-node-icon').exists()).toBe(false)
done()
})
})
})

5
vue.config.js Normal file
View File

@@ -0,0 +1,5 @@
module.exports = {
css: {
extract: false
}
}

14861
yarn.lock Normal file

File diff suppressed because it is too large Load Diff