From a4cc14c9ae7a5c4addb2e754aacb481951bfb86d Mon Sep 17 00:00:00 2001 From: 07akioni <07akioni2@gmail.com> Date: Tue, 25 Feb 2020 14:30:17 +0800 Subject: [PATCH] refactor(cascader): utils path change --- src/Cascader/src/Cascader.vue | 2 +- src/Cascader/src/CascaderMenu.vue | 2 +- .../menuModel.js => component/cascader.js} | 90 ------------------- 3 files changed, 2 insertions(+), 92 deletions(-) rename src/_utils/{data/menuModel.js => component/cascader.js} (78%) diff --git a/src/Cascader/src/Cascader.vue b/src/Cascader/src/Cascader.vue index 287213b74..eb34ae45f 100644 --- a/src/Cascader/src/Cascader.vue +++ b/src/Cascader/src/Cascader.vue @@ -100,7 +100,7 @@ import { patchedOptions, linkedCascaderOptions, menuOptions -} from '../../_utils/data/menuModel' +} from '../../_utils/component/cascader' export default { name: 'NCascader', diff --git a/src/Cascader/src/CascaderMenu.vue b/src/Cascader/src/CascaderMenu.vue index 56e4022ba..ef525f416 100644 --- a/src/Cascader/src/CascaderMenu.vue +++ b/src/Cascader/src/CascaderMenu.vue @@ -44,7 +44,7 @@ import zindexable from '../../_mixins/zindexable' import { firstOptionId, menuModel -} from '../../_utils/data/menuModel' +} from '../../_utils/component/cascader' export default { name: 'NCascaderMenu', diff --git a/src/_utils/data/menuModel.js b/src/_utils/component/cascader.js similarity index 78% rename from src/_utils/data/menuModel.js rename to src/_utils/component/cascader.js index 106b95ef3..dee6fe8cd 100644 --- a/src/_utils/data/menuModel.js +++ b/src/_utils/component/cascader.js @@ -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