refactor(cascader): utils path change

This commit is contained in:
07akioni 2020-02-25 14:30:17 +08:00
parent 09d77642f3
commit a4cc14c9ae
3 changed files with 2 additions and 92 deletions

View File

@ -100,7 +100,7 @@ import {
patchedOptions,
linkedCascaderOptions,
menuOptions
} from '../../_utils/data/menuModel'
} from '../../_utils/component/cascader'
export default {
name: 'NCascader',

View File

@ -44,7 +44,7 @@ import zindexable from '../../_mixins/zindexable'
import {
firstOptionId,
menuModel
} from '../../_utils/data/menuModel'
} from '../../_utils/component/cascader'
export default {
name: 'NCascaderMenu',

View File

@ -167,93 +167,6 @@ function patchedOptions (options, patches) {
return [].concat(options)
}
function dropIsValid ([sourceNode, targetNode, type]) {
if (sourceNode.key === targetNode.key) return false
if (type === 'append') {
if (targetNode.key === sourceNode.parent.key) return false
let parent = targetNode.parent
while (!parent.isRoot) {
if (parent.key === sourceNode.key) {
return false
}
parent = parent.parent
}
} else if (type === 'insertBefore' || type === 'insertAfter') {
let parent = targetNode.parent
while (!parent.isRoot) {
if (parent.key === sourceNode.key) {
return false
}
parent = parent.parent
}
} else return false
return true
}
function treedOptions (options) {
const decoratedOptions = rootedOptions(options)
function traverse (root, parent = null) {
root.parent = parent
if (Array.isArray(root.children)) {
root.children.forEach(child => traverse(child, root))
}
}
traverse(decoratedOptions[0])
return decoratedOptions
}
function applyDrop ([sourceNode, targetNode, type]) {
if (type === 'append') {
const parent = sourceNode.parent
const index = parent.children.findIndex(child => child.key === sourceNode.key)
if (~index) {
parent.children.splice(index, 1)
if (!parent.children.length) {
parent.children = null
parent.isLeaf = true
}
} else {
throw new Error('[naive-ui/n-tree]: switch error')
}
if (Array.isArray(targetNode.children)) {
if (type === 'append') {
targetNode.children.push(sourceNode)
} else {
targetNode.children.unshift(sourceNode)
}
} else {
targetNode.isLeaf = false
targetNode.children = [sourceNode]
}
sourceNode.parent = targetNode
} else if (type === 'insertBefore' || type === 'insertAfter') {
let parent = sourceNode.parent
const sourceIndex = parent.children.findIndex(child => child.key === sourceNode.key)
if (~sourceIndex) {
parent.children.splice(sourceIndex, 1)
if (!parent.children.length) {
parent.children = null
parent.isLeaf = true
}
} else {
throw new Error('[naive-ui/n-tree]: switch error')
}
parent = targetNode.parent
let targetIndex = parent.children.findIndex(child => child.key === targetNode.key)
if (type === 'insertAfter') targetIndex += 1
if (~targetIndex) {
parent.children.splice(targetIndex, 0, sourceNode)
if (!parent.children.length) {
parent.children = null
parent.isLeaf = true
}
} else {
throw new Error('[naive-ui/n-tree]: switch error')
}
sourceNode.parent = targetNode.parent
}
}
function linkedCascaderOptions (options, type) {
const cascaderOptions = options
const path = []
@ -451,9 +364,6 @@ export {
firstOptionId,
rootedOptions,
patchedOptions,
dropIsValid,
applyDrop,
treedOptions,
linkedCascaderOptions,
menuOptions,
menuModel