mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2025-01-12 12:25:16 +08:00
refactor(cascader): utils path change
This commit is contained in:
parent
09d77642f3
commit
a4cc14c9ae
@ -100,7 +100,7 @@ import {
|
|||||||
patchedOptions,
|
patchedOptions,
|
||||||
linkedCascaderOptions,
|
linkedCascaderOptions,
|
||||||
menuOptions
|
menuOptions
|
||||||
} from '../../_utils/data/menuModel'
|
} from '../../_utils/component/cascader'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'NCascader',
|
name: 'NCascader',
|
||||||
|
@ -44,7 +44,7 @@ import zindexable from '../../_mixins/zindexable'
|
|||||||
import {
|
import {
|
||||||
firstOptionId,
|
firstOptionId,
|
||||||
menuModel
|
menuModel
|
||||||
} from '../../_utils/data/menuModel'
|
} from '../../_utils/component/cascader'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'NCascaderMenu',
|
name: 'NCascaderMenu',
|
||||||
|
@ -167,93 +167,6 @@ function patchedOptions (options, patches) {
|
|||||||
return [].concat(options)
|
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) {
|
function linkedCascaderOptions (options, type) {
|
||||||
const cascaderOptions = options
|
const cascaderOptions = options
|
||||||
const path = []
|
const path = []
|
||||||
@ -451,9 +364,6 @@ export {
|
|||||||
firstOptionId,
|
firstOptionId,
|
||||||
rootedOptions,
|
rootedOptions,
|
||||||
patchedOptions,
|
patchedOptions,
|
||||||
dropIsValid,
|
|
||||||
applyDrop,
|
|
||||||
treedOptions,
|
|
||||||
linkedCascaderOptions,
|
linkedCascaderOptions,
|
||||||
menuOptions,
|
menuOptions,
|
||||||
menuModel
|
menuModel
|
Loading…
Reference in New Issue
Block a user