2019-01-12 11:33:27 +08:00
|
|
|
import omit from 'omit.js';
|
|
|
|
import debounce from 'lodash/debounce';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { conductExpandParent, convertTreeToEntities } from '../vc-tree/src/util';
|
|
|
|
import Tree, { TreeProps } from './Tree';
|
|
|
|
import { calcRangeKeys, getFullKeyList } from './util';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import { initDefaultProps, getOptionProps } from '../_util/props-util';
|
2019-04-10 13:28:07 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
// export type ExpandAction = false | 'click' | 'doubleClick';
|
|
|
|
|
|
|
|
// export interface DirectoryTreeProps extends TreeProps {
|
|
|
|
// expandAction?: ExpandAction;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export interface DirectoryTreeState {
|
|
|
|
// expandedKeys?: string[];
|
|
|
|
// selectedKeys?: string[];
|
|
|
|
// }
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getIcon(props, h) {
|
|
|
|
const { isLeaf, expanded } = props;
|
2018-09-26 22:57:01 +08:00
|
|
|
if (isLeaf) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return <Icon type="file" />;
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return <Icon type={expanded ? 'folder-open' : 'folder'} />;
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ADirectoryTree',
|
2019-02-01 17:23:00 +08:00
|
|
|
mixins: [BaseMixin],
|
2018-09-26 22:57:01 +08:00
|
|
|
model: {
|
|
|
|
prop: 'checkedKeys',
|
|
|
|
event: 'check',
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
props: initDefaultProps(
|
|
|
|
{ ...TreeProps(), expandAction: PropTypes.oneOf([false, 'click', 'doubleclick']) },
|
|
|
|
{
|
|
|
|
showIcon: true,
|
|
|
|
expandAction: 'click',
|
|
|
|
},
|
|
|
|
),
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
// state: DirectoryTreeState;
|
|
|
|
// onDebounceExpand: (event, node: AntTreeNode) => void;
|
|
|
|
|
|
|
|
// // Shift click usage
|
|
|
|
// lastSelectedKey?: string;
|
|
|
|
// cachedSelectedKeys?: string[];
|
2019-04-10 13:28:07 +08:00
|
|
|
inject: {
|
|
|
|
configProvider: { default: () => ({}) },
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
const { defaultExpandAll, defaultExpandParent, expandedKeys, defaultExpandedKeys } = props;
|
|
|
|
const { keyEntities } = convertTreeToEntities(this.$slots.default);
|
|
|
|
const state = {};
|
2018-09-26 22:57:01 +08:00
|
|
|
// Selected keys
|
2019-01-12 11:33:27 +08:00
|
|
|
state._selectedKeys = props.selectedKeys || props.defaultSelectedKeys || [];
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
// Expanded keys
|
|
|
|
if (defaultExpandAll) {
|
2019-01-12 11:33:27 +08:00
|
|
|
state._expandedKeys = getFullKeyList(this.$slots.default);
|
2018-09-26 22:57:01 +08:00
|
|
|
} else if (defaultExpandParent) {
|
2019-01-12 11:33:27 +08:00
|
|
|
state._expandedKeys = conductExpandParent(expandedKeys || defaultExpandedKeys, keyEntities);
|
2018-09-26 22:57:01 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
state._expandedKeys = expandedKeys || defaultExpandedKeys;
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.onDebounceExpand = debounce(this.expandFolderNode, 200, {
|
|
|
|
leading: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-26 22:57:01 +08:00
|
|
|
return {
|
|
|
|
_selectedKeys: [],
|
|
|
|
_expandedKeys: [],
|
|
|
|
...state,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 11:33:27 +08:00
|
|
|
expandedKeys(val) {
|
|
|
|
this.setState({ _expandedKeys: val });
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedKeys(val) {
|
|
|
|
this.setState({ _selectedKeys: val });
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
onExpand(expandedKeys, info) {
|
|
|
|
this.setUncontrolledState({ _expandedKeys: expandedKeys });
|
2018-09-26 22:57:01 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('expand', expandedKeys, info);
|
2018-09-26 22:57:01 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return undefined;
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
onClick(event, node) {
|
|
|
|
const { expandAction } = this.$props;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
// Expand the tree
|
|
|
|
if (expandAction === 'click') {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.onDebounceExpand(event, node);
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('click', event, node);
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
onDoubleClick(event, node) {
|
|
|
|
const { expandAction } = this.$props;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
// Expand the tree
|
2018-09-28 14:35:26 +08:00
|
|
|
if (expandAction === 'doubleclick') {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.onDebounceExpand(event, node);
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('doubleclick', event, node);
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
onSelect(keys, event) {
|
|
|
|
const { multiple } = this.$props;
|
|
|
|
const children = this.$slots.default || [];
|
|
|
|
const { _expandedKeys: expandedKeys = [] } = this.$data;
|
|
|
|
const { node, nativeEvent } = event;
|
|
|
|
const { eventKey = '' } = node;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const newState = {};
|
2018-09-26 22:57:01 +08:00
|
|
|
// Windows / Mac single pick
|
2019-01-12 11:33:27 +08:00
|
|
|
const ctrlPick = nativeEvent.ctrlKey || nativeEvent.metaKey;
|
|
|
|
const shiftPick = nativeEvent.shiftKey;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
// Generate new selected keys
|
2019-01-12 11:33:27 +08:00
|
|
|
let newSelectedKeys;
|
2018-09-26 22:57:01 +08:00
|
|
|
if (multiple && ctrlPick) {
|
2019-01-12 11:33:27 +08:00
|
|
|
// Control click
|
|
|
|
newSelectedKeys = keys;
|
|
|
|
this.lastSelectedKey = eventKey;
|
|
|
|
this.cachedSelectedKeys = newSelectedKeys;
|
2018-09-26 22:57:01 +08:00
|
|
|
} else if (multiple && shiftPick) {
|
2019-01-12 11:33:27 +08:00
|
|
|
// Shift click
|
|
|
|
newSelectedKeys = Array.from(
|
|
|
|
new Set([
|
|
|
|
...(this.cachedSelectedKeys || []),
|
|
|
|
...calcRangeKeys(children, expandedKeys, eventKey, this.lastSelectedKey),
|
|
|
|
]),
|
|
|
|
);
|
2018-09-26 22:57:01 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
// Single click
|
|
|
|
newSelectedKeys = [eventKey];
|
|
|
|
this.lastSelectedKey = eventKey;
|
|
|
|
this.cachedSelectedKeys = newSelectedKeys;
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
newState._selectedKeys = newSelectedKeys;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('update:selectedKeys', newSelectedKeys);
|
|
|
|
this.$emit('select', newSelectedKeys, event);
|
2018-09-26 22:57:01 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setUncontrolledState(newState);
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
expandFolderNode(event, node) {
|
|
|
|
const { isLeaf } = node;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
|
|
|
if (isLeaf || event.shiftKey || event.metaKey || event.ctrlKey) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 20:33:37 +08:00
|
|
|
if (this.$refs.tree.$refs.tree) {
|
|
|
|
// Get internal vc-tree
|
2019-01-12 11:33:27 +08:00
|
|
|
const internalTree = this.$refs.tree.$refs.tree;
|
2018-09-26 22:57:01 +08:00
|
|
|
|
2018-12-11 20:33:37 +08:00
|
|
|
// Call internal rc-tree expand function
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12567
|
2019-01-12 11:33:27 +08:00
|
|
|
internalTree.onNodeExpand(event, node);
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
setUncontrolledState(state) {
|
|
|
|
const newState = omit(state, Object.keys(getOptionProps(this)).map(p => `_${p}`));
|
2018-09-26 22:57:01 +08:00
|
|
|
if (Object.keys(newState).length) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState(newState);
|
2018-09-26 22:57:01 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2019-04-10 13:28:07 +08:00
|
|
|
const { prefixCls: customizePrefixCls, ...props } = getOptionProps(this);
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
|
|
|
|
const prefixCls = getPrefixCls('tree', customizePrefixCls);
|
2019-01-12 11:33:27 +08:00
|
|
|
const { _expandedKeys: expandedKeys, _selectedKeys: selectedKeys } = this.$data;
|
2018-09-26 22:57:01 +08:00
|
|
|
const treeProps = {
|
|
|
|
props: {
|
|
|
|
icon: getIcon,
|
|
|
|
...props,
|
|
|
|
prefixCls,
|
|
|
|
expandedKeys,
|
|
|
|
selectedKeys,
|
|
|
|
},
|
2018-12-11 20:33:37 +08:00
|
|
|
ref: 'tree',
|
2018-09-26 22:57:01 +08:00
|
|
|
class: `${prefixCls}-directory`,
|
2018-09-28 14:35:26 +08:00
|
|
|
on: {
|
2018-12-26 18:07:50 +08:00
|
|
|
...omit(this.$listeners, ['update:selectedKeys']),
|
2018-09-28 14:35:26 +08:00
|
|
|
select: this.onSelect,
|
|
|
|
click: this.onClick,
|
|
|
|
doubleclick: this.onDoubleClick,
|
|
|
|
expand: this.onExpand,
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
return <Tree {...treeProps}>{this.$slots.default}</Tree>;
|
2018-09-26 22:57:01 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|