Skip to content

Commit

Permalink
Add demo: Vuex Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin committed May 11, 2018
1 parent 72b831d commit b5c8703
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": ["plugin:vue-libs/recommended", "plugin:jest/recommended"],
"rules": {
"no-cond-assign": 0
"no-cond-assign": 0,
"eqeqeq": 0
},
"plugins": ["jest"]
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ A Vue tree component that allows you to present hierarchically organized data in
* multi selection
* keyboard navigation
* filtering
* sorting
* integration with Vuex

## Installation
**Npm:**
Expand Down
5 changes: 4 additions & 1 deletion demo/pages/vuex.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@

updateTree(state, newData) {
state.tree = newData
},
tt(state, aaa) {
state.bb = aaa;
}
},
actions: {
Expand All @@ -84,7 +87,7 @@
store: {
store: Store,
key: 'tree',
mutations: []
mutations: ['initTree', 'updateTree']
},
checkbox: true
}
Expand Down
4 changes: 4 additions & 0 deletions docs/hexo/source/_data/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
href: "#Inline-Editing"
title: Inline Editing

- id: Integration with Vuex
href: "#Integration-with-Vuex"
title: Integration with Vuex


- title: Examples
href: "#Examples"
Expand Down
65 changes: 65 additions & 0 deletions docs/hexo/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,71 @@ There are two ways to set an async data:
In progress...
### Integration with Vuex
The library is allow to pass `store` options. Example:
```javascript
const Store = new Vuex.Store({
state: {
treeStore: []
},
mutations: {
// ---
},
actions: {
// ---
}
})
// -------
new Vue({
el: '#app',
store: Store,
data: () => ({
options: {
store: {
store: Store,
key: 'treeStore',
mutations: [] // this property is not required
},
checkbox: true
}
})
})
```
Every changes in store will redraw the tree. You can pass `mutations` to the options to check whether to update a tree. Ex:
```javascript
const Store = new Vuex.Store({
state: {
treeStore: []
},
mutations: {
initTree(state, treeData) {
state.tree.push(...treeData)
},
updateTree(state, newData) {
state.tree = newData
}
}
})
// tree options:
{
store: {
store: Store,
key: 'treeStore',
mutations: ['initTree', 'updateTree]
}
}
```
## Examples
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "liquor-tree",
"description": "A Vue.js tree component.",
"version": "0.2.25",
"version": "0.2.26",
"author": "Kostiantyn <phlyze@gmail.com>",
"library": "LiquorTree",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class Tree {
}

_sort (source, compareFn, deep) {
if (false !== deep) {
if (deep !== false) {
this.recurseDown(source, node => {
if (node.hasChildren()) {
sort(node.children, compareFn)
Expand Down
19 changes: 11 additions & 8 deletions src/mixins/TreeMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,33 @@ export default {
return data
}

const applyState = (state) => {
const data = state[key]

updateTree(
syncStates(data, this.model)
)
}

// actions must be an array
let { mutations } = store

if (mutations && !mutations.length) {
mutations = null
}

updateTree(Store.state[key] || [])

Store.subscribe((action, state) => {
if (mutations && !mutations.includes(action.type)) {
return
}

const data = state[key]

updateTree(
syncStates(data, this.model)
)
applyState(state)
})

updateTree(Store.state[key] || [])
},

recurseDown(fn) {
recurseDown (fn) {
this.tree.recurseDown(fn)
},

Expand Down
25 changes: 12 additions & 13 deletions src/utils/sort.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@

function orderAsc(node0, node1) {
function orderAsc (node0, node1) {
if (node0.text < node1.text) {
return -1;
return -1
}

if (node0.text > node1.text) {
return 1;
return 1
}

return 0;
return 0
}

function orderDesc(node0, node1) {
function orderDesc (node0, node1) {
if (node0.text < node1.text) {
return 1;
return 1
}

if (node0.text > node1.text) {
return -1;
return -1
}

return 0;
return 0
}


function getCompareFunction(order) {
switch(order.toLowerCase()) {
function getCompareFunction (order) {
switch (order.toLowerCase()) {
case 'asc': return orderAsc
case 'desc': return orderDesc
}
}

export default (source, compareFunction) => {
if (typeof compareFunction == 'string') {
if (typeof compareFunction === 'string') {
compareFunction = getCompareFunction(compareFunction)
}

if (Array.isArray(source) && typeof compareFunction == 'function') {
if (Array.isArray(source) && typeof compareFunction === 'function') {
source.sort(compareFunction)
}
}

0 comments on commit b5c8703

Please sign in to comment.