ant-design-vue/components/vc-tree/src/util.js

423 lines
10 KiB
JavaScript
Raw Normal View History

2018-03-23 13:30:20 +00:00
/* eslint no-loop-func: 0*/
import warning from 'warning'
2018-09-26 14:57:01 +00:00
import omit from 'omit.js'
import TreeNode from './TreeNode'
2018-03-24 09:12:22 +00:00
import { getSlotOptions, getOptionProps } from '../../_util/props-util'
2018-04-11 13:25:16 +00:00
const DRAG_SIDE_RANGE = 0.25
const DRAG_MIN_GAP = 2
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
let onlyTreeNodeWarned = false
export function warnOnlyTreeNode () {
if (onlyTreeNodeWarned) return
onlyTreeNodeWarned = true
warning(false, 'Tree only accept TreeNode as children.')
}
2018-03-23 13:30:20 +00:00
export function arrDel (list, value) {
const clone = list.slice()
const index = clone.indexOf(value)
if (index >= 0) {
clone.splice(index, 1)
}
return clone
}
export function arrAdd (list, value) {
const clone = list.slice()
if (clone.indexOf(value) === -1) {
clone.push(value)
}
return clone
}
export function posToArr (pos) {
return pos.split('-')
}
export function getPosition (level, index) {
return `${level}-${index}`
}
2018-09-26 14:57:01 +00:00
export function isTreeNode (node) {
return getSlotOptions(node).isTreeNode
}
2018-03-24 09:12:22 +00:00
export function getNodeChildren (children = []) {
2018-09-26 14:57:01 +00:00
return children.filter(isTreeNode)
2018-03-23 13:30:20 +00:00
}
export function isCheckDisabled (node) {
2018-03-24 09:12:22 +00:00
const { disabled, disableCheckbox } = getOptionProps(node) || {}
2018-03-23 13:30:20 +00:00
return !!(disabled || disableCheckbox)
}
2018-09-26 14:57:01 +00:00
export function traverseTreeNodes (treeNodes, callback) {
2018-03-23 13:30:20 +00:00
function processNode (node, index, parent) {
2018-03-24 09:12:22 +00:00
const children = node ? node.componentOptions.children : treeNodes
2018-03-23 13:30:20 +00:00
const pos = node ? getPosition(parent.pos, index) : 0
// Filter children
const childList = getNodeChildren(children)
// Process node if is not root
if (node) {
const data = {
node,
index,
pos,
key: node.key || pos,
parentPos: parent.node ? parent.pos : null,
}
2018-09-26 14:57:01 +00:00
callback(data)
2018-03-23 13:30:20 +00:00
}
// Process children node
2018-03-24 09:12:22 +00:00
childList.forEach((subNode, subIndex) => {
2018-03-23 13:30:20 +00:00
processNode(subNode, subIndex, { node, pos })
})
}
processNode(null)
}
update to antd3.8.3 (#159) * refactor: align * feat: update align to 2.4.3 * feat: update trigger 2.5.4 * feat: update tooltip 3.7.2 * fix: align * feat: update vc-calendar to 9.6.2 * feat: update vc-checkbox to 2.1.5 * feat: update vc-dialog to 7.1.8 * feat: update vc-from to 2.2.1 * feat: update vc-notification to 3.1.1 * test: update snapshots * feat: update vc-tree to 1.12.6 * feat: update vc-table to 6.2.8 * feat: update vc-upload to 2.5.1 * feat: update vc-input-number to 4.0.12 * feat: update vc-tabs to 9.2.6 * refactor: vc-menu * refactor: update vc-menu to 7.0.5 * style: remove unused * feat: update pagination to 1.16.5 * feat: add vc-progress 2.2.5 tag * feat: add vc-rate 2.4.0 tag * feat: update vc-slider to 8.6.1 * fix: tooltip error * style: delete conosle * feat: update vc-steps to 3.1.1 * add vc-switch tag 1.6.0 * feat: update upload to 2.5.1 * fix: update vc-menu * fix: update store * fix: add ref dir * fix: trigger mock shouldComponentUpdate * fix: update vc-select * revert: trigger lazyrenderbox * fix: update vc-select * fix: update vc-select * fix: update vc-select * fix: update vc-menu * fix: update vc-slick ref * update style to 3.8.2 * test: update snapshots * update vc-select * update util & affix * feat: add drawer * fix: support title add slot mode * test: update affix test * update alert * update anchor * update snapshots * fix: doc and vc-drawer * update select & auto-complete * update back-top & grid * feractor: avatar * test: add drawer test * update badge * update button * update card * update divider * feat: update vc-tabs to 9.3.6 and tabs * add afterEnter callback * update form * fix: update drawer * test: update snapshots * update modal & notification * test: update snapshots * update message * update locale-provider * update dropdown * update layout popconfirm popover * update time-picker * update menu * update date-picker * docs: update input docs * update input * update snapshots * update table * update test snapshots * feat: update progress * update checkbox * feat: update spin * update radio * docs: slider steps timeline * update list * update transfer * update collapse * update cascader * update upload
2018-09-05 13:28:54 +00:00
/**
* Use `rc-util` `toArray` to get the children list which keeps the key.
* And return single node if children is only one(This can avoid `key` missing check).
*/
export function mapChildren (children = [], func) {
const list = children.map(func)
if (list.length === 1) {
return list[0]
}
return list
}
2018-03-23 13:30:20 +00:00
export function getDragNodesKeys (treeNodes, node) {
2018-03-24 09:12:22 +00:00
const { eventKey, pos } = getOptionProps(node)
2018-03-23 13:30:20 +00:00
const dragNodesKeys = []
2018-09-26 14:57:01 +00:00
traverseTreeNodes(treeNodes, ({ key }) => {
dragNodesKeys.push(key)
2018-03-23 13:30:20 +00:00
})
dragNodesKeys.push(eventKey || pos)
return dragNodesKeys
}
export function calcDropPosition (event, treeNode) {
2018-04-11 13:25:16 +00:00
const { clientY } = event
2018-04-12 14:04:55 +00:00
const { top, bottom, height } = treeNode.$refs.selectHandle.getBoundingClientRect()
2018-04-11 13:25:16 +00:00
const des = Math.max(height * DRAG_SIDE_RANGE, DRAG_MIN_GAP)
if (clientY <= top + des) {
2018-03-23 13:30:20 +00:00
return -1
2018-04-11 13:25:16 +00:00
} else if (clientY >= bottom - des) {
return 1
2018-03-23 13:30:20 +00:00
}
return 0
}
/**
2018-09-26 14:57:01 +00:00
* Return selectedKeys according with multiple prop
* @param selectedKeys
2018-03-23 13:30:20 +00:00
* @param props
* @returns [string]
*/
2018-09-26 14:57:01 +00:00
export function calcSelectedKeys (selectedKeys, props) {
if (!selectedKeys) { return undefined }
const { multiple } = props
if (multiple) {
return selectedKeys.slice()
2018-03-23 13:30:20 +00:00
}
2018-09-26 14:57:01 +00:00
if (selectedKeys.length) {
return [selectedKeys[0]]
}
return selectedKeys
}
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
/**
* Since React internal will convert key to string,
* we need do this to avoid `checkStrictly` use number match
*/
function keyListToString (keyList) {
if (!keyList) return keyList
return keyList.map(key => String(key))
}
const internalProcessProps = (props = {}) => {
return {
props: omit(props, ['on', 'key', 'class', 'className', 'style']),
on: props.on || {},
class: props.class || props.className,
style: props.style,
key: props.key,
}
}
export function convertDataToTree (h, treeData, processer) {
if (!treeData) return []
const { processProps = internalProcessProps } = processer || {}
const list = Array.isArray(treeData) ? treeData : [treeData]
return list.map(({ children, ...props }) => {
const childrenNodes = convertDataToTree(h, children, processer)
return (
<TreeNode {...processProps(props) }>
{childrenNodes}
</TreeNode>
)
2018-03-23 13:30:20 +00:00
})
2018-09-26 14:57:01 +00:00
}
// TODO: ========================= NEW LOGIC =========================
/**
* Calculate treeNodes entities. `processTreeEntity` is used for `rc-tree-select`
* @param treeNodes
* @param processTreeEntity User can customize the entity
*/
export function convertTreeToEntities (treeNodes, { initWrapper, processEntity, onProcessFinished } = {}) {
const posEntities = {}
const keyEntities = {}
let wrapper = {
posEntities,
keyEntities,
}
if (initWrapper) {
wrapper = initWrapper(wrapper) || wrapper
}
traverseTreeNodes(treeNodes, (item) => {
const { node, index, pos, key, parentPos } = item
const entity = { node, index, key, pos }
posEntities[pos] = entity
keyEntities[key] = entity
// Fill children
entity.parent = posEntities[parentPos]
if (entity.parent) {
entity.parent.children = entity.parent.children || []
entity.parent.children.push(entity)
}
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
if (processEntity) {
processEntity(entity, wrapper)
2018-03-23 13:30:20 +00:00
}
})
2018-09-26 14:57:01 +00:00
if (onProcessFinished) {
onProcessFinished(wrapper)
}
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
return wrapper
2018-03-23 13:30:20 +00:00
}
/**
2018-09-26 14:57:01 +00:00
* Parse `checkedKeys` to { checkedKeys, halfCheckedKeys } style
2018-03-23 13:30:20 +00:00
*/
2018-09-26 14:57:01 +00:00
export function parseCheckedKeys (keys) {
if (!keys) {
return null
2018-03-23 13:30:20 +00:00
}
2018-09-26 14:57:01 +00:00
// Convert keys to object format
let keyProps
if (Array.isArray(keys)) {
// [Legacy] Follow the api doc
keyProps = {
checkedKeys: keys,
halfCheckedKeys: undefined,
}
} else if (typeof keys === 'object') {
keyProps = {
checkedKeys: keys.checked || undefined,
halfCheckedKeys: keys.halfChecked || undefined,
}
} else {
warning(false, '`checkedKeys` is not an array or an object')
return null
2018-03-23 13:30:20 +00:00
}
2018-09-26 14:57:01 +00:00
keyProps.checkedKeys = keyListToString(keyProps.checkedKeys)
keyProps.halfCheckedKeys = keyListToString(keyProps.halfCheckedKeys)
return keyProps
2018-03-23 13:30:20 +00:00
}
/**
2018-09-26 14:57:01 +00:00
* Conduct check state by the keyList. It will conduct up & from the provided key.
* If the conduct path reach the disabled or already checked / unchecked node will stop conduct.
* @param keyList list of keys
* @param isCheck is check the node or not
* @param keyEntities parsed by `convertTreeToEntities` function in Tree
* @param checkStatus Can pass current checked status for process (usually for uncheck operation)
* @returns {{checkedKeys: [], halfCheckedKeys: []}}
2018-03-23 13:30:20 +00:00
*/
2018-09-26 14:57:01 +00:00
export function conductCheck (keyList, isCheck, keyEntities, checkStatus = {}) {
const checkedKeys = {}
const halfCheckedKeys = {}; // Record the key has some child checked (include child half checked)
(checkStatus.checkedKeys || []).forEach((key) => {
checkedKeys[key] = true
});
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
(checkStatus.halfCheckedKeys || []).forEach((key) => {
halfCheckedKeys[key] = true
})
2018-03-23 13:30:20 +00:00
// Conduct up
2018-09-26 14:57:01 +00:00
function conductUp (key) {
if (checkedKeys[key] === isCheck) return
const entity = keyEntities[key]
if (!entity) return
const { children, parent, node } = entity
2018-03-23 13:30:20 +00:00
if (isCheckDisabled(node)) return
2018-09-26 14:57:01 +00:00
// Check child node checked status
let everyChildChecked = true
let someChildChecked = false; // Child checked or half checked
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
(children || [])
.filter(child => !isCheckDisabled(child.node))
.forEach(({ key: childKey }) => {
const childChecked = checkedKeys[childKey]
const childHalfChecked = halfCheckedKeys[childKey]
if (childChecked || childHalfChecked) someChildChecked = true
if (!childChecked) everyChildChecked = false
})
// Update checked status
if (isCheck) {
checkedKeys[key] = everyChildChecked
2018-03-23 13:30:20 +00:00
} else {
2018-09-26 14:57:01 +00:00
checkedKeys[key] = false
2018-03-23 13:30:20 +00:00
}
2018-09-26 14:57:01 +00:00
halfCheckedKeys[key] = someChildChecked
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
if (parent) {
conductUp(parent.key)
2018-03-23 13:30:20 +00:00
}
}
// Conduct down
function conductDown (key) {
2018-09-26 14:57:01 +00:00
if (checkedKeys[key] === isCheck) return
const entity = keyEntities[key]
if (!entity) return
const { children, node } = entity
2018-03-23 13:30:20 +00:00
if (isCheckDisabled(node)) return
2018-09-26 14:57:01 +00:00
checkedKeys[key] = isCheck;
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
(children || []).forEach((child) => {
conductDown(child.key)
2018-03-23 13:30:20 +00:00
})
}
function conduct (key) {
2018-09-26 14:57:01 +00:00
const entity = keyEntities[key]
if (!entity) {
2018-03-23 13:30:20 +00:00
warning(false, `'${key}' does not exist in the tree.`)
return
}
2018-09-26 14:57:01 +00:00
const { children, parent, node } = entity
checkedKeys[key] = isCheck
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
if (isCheckDisabled(node)) return;
2018-04-11 13:25:16 +00:00
2018-03-23 13:30:20 +00:00
// Conduct down
2018-09-26 14:57:01 +00:00
(children || [])
.filter(child => !isCheckDisabled(child.node))
.forEach((child) => {
conductDown(child.key)
2018-03-23 13:30:20 +00:00
})
// Conduct up
2018-09-26 14:57:01 +00:00
if (parent) {
conductUp(parent.key)
2018-03-23 13:30:20 +00:00
}
}
2018-09-26 14:57:01 +00:00
(keyList || []).forEach((key) => {
2018-03-23 13:30:20 +00:00
conduct(key)
})
2018-09-26 14:57:01 +00:00
const checkedKeyList = []
const halfCheckedKeyList = []
// Fill checked list
Object.keys(checkedKeys).forEach((key) => {
if (checkedKeys[key]) {
checkedKeyList.push(key)
}
})
// Fill half checked list
Object.keys(halfCheckedKeys).forEach((key) => {
if (!checkedKeys[key] && halfCheckedKeys[key]) {
halfCheckedKeyList.push(key)
}
})
2018-03-23 13:30:20 +00:00
return {
2018-09-26 14:57:01 +00:00
checkedKeys: checkedKeyList,
halfCheckedKeys: halfCheckedKeyList,
2018-03-23 13:30:20 +00:00
}
}
/**
2018-09-26 14:57:01 +00:00
* If user use `autoExpandParent` we should get the list of parent node
* @param keyList
* @param keyEntities
2018-03-23 13:30:20 +00:00
*/
2018-09-26 14:57:01 +00:00
export function conductExpandParent (keyList, keyEntities) {
const expandedKeys = {}
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
function conductUp (key) {
if (expandedKeys[key]) return
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
const entity = keyEntities[key]
if (!entity) return
expandedKeys[key] = true
const { parent, node } = entity
if (isCheckDisabled(node)) return
if (parent) {
conductUp(parent.key)
2018-03-23 13:30:20 +00:00
}
}
2018-09-26 14:57:01 +00:00
(keyList || []).forEach((key) => {
conductUp(key)
})
2018-07-11 09:51:20 +00:00
2018-09-26 14:57:01 +00:00
return Object.keys(expandedKeys)
}
2018-03-23 13:30:20 +00:00
2018-09-26 14:57:01 +00:00
/**
* Returns only the data- and aria- key/value pairs
* @param {object} props
*/
export function getDataAndAria (props) {
return Object.keys(props).reduce((prev, key) => {
if ((key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-')) {
prev[key] = props[key]
}
return prev
}, {})
2018-03-23 13:30:20 +00:00
}