2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-11-23 06:21:18 +00:00
|
|
|
import { default as SubPopupMenu } from './SubPopupMenu';
|
2019-01-12 03:33:27 +00:00
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-11-23 06:21:18 +00:00
|
|
|
import hasProp, { getOptionProps, getComponent } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import commonPropsType from './commonPropsType';
|
2020-11-23 06:21:18 +00:00
|
|
|
import {
|
|
|
|
computed,
|
|
|
|
defineComponent,
|
|
|
|
getCurrentInstance,
|
|
|
|
provide,
|
|
|
|
reactive,
|
|
|
|
ref,
|
|
|
|
toRaw,
|
|
|
|
watch,
|
|
|
|
} from 'vue';
|
2017-12-12 05:49:02 +00:00
|
|
|
|
|
|
|
const Menu = {
|
|
|
|
name: 'Menu',
|
2020-06-16 14:55:02 +00:00
|
|
|
inheritAttrs: false,
|
2017-12-12 05:49:02 +00:00
|
|
|
props: {
|
2018-01-04 11:06:54 +00:00
|
|
|
...commonPropsType,
|
2020-10-23 06:29:39 +00:00
|
|
|
onClick: PropTypes.func,
|
2020-10-10 10:16:28 +00:00
|
|
|
selectable: PropTypes.looseBool.def(true),
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-11-23 06:21:18 +00:00
|
|
|
setup(props) {
|
|
|
|
const menuChildrenInfo = reactive({});
|
|
|
|
const selectedKeys = ref(props.selectedKeys || props.defaultSelectedKeys || []);
|
|
|
|
const openKeys = ref(props.openKeys || props.defaultOpenKeys || []);
|
|
|
|
// computed(() => {
|
|
|
|
// return props.openKeys || props.defaultOpenKeys || [];
|
|
|
|
// });
|
|
|
|
watch(
|
|
|
|
() => props.selectedKeys,
|
|
|
|
() => {
|
|
|
|
selectedKeys.value = props.selectedKeys;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
watch(
|
|
|
|
() => props.openKeys,
|
|
|
|
() => {
|
|
|
|
openKeys.value = props.openKeys;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const activeKey = reactive({
|
|
|
|
'0-menu-': props.activeKey,
|
|
|
|
});
|
|
|
|
const defaultActiveFirst = reactive({});
|
|
|
|
const addChildrenInfo = (key, info) => {
|
|
|
|
menuChildrenInfo[key] = info;
|
|
|
|
};
|
|
|
|
const removeChildrenInfo = key => {
|
|
|
|
delete menuChildrenInfo[key];
|
|
|
|
};
|
|
|
|
const getActiveKey = key => {
|
|
|
|
return key;
|
|
|
|
}; // TODO
|
|
|
|
const selectedParentUniKeys = ref([]);
|
|
|
|
watch(menuChildrenInfo, () => {
|
|
|
|
const keys = Object.values(menuChildrenInfo)
|
|
|
|
.filter(info => info.isSelected)
|
|
|
|
.reduce((allKeys, { parentUniKeys = [] }) => {
|
|
|
|
return [...allKeys, ...toRaw(parentUniKeys)];
|
|
|
|
}, []);
|
|
|
|
selectedParentUniKeys.value = keys || [];
|
|
|
|
});
|
|
|
|
const store = reactive({
|
2018-09-05 13:28:54 +00:00
|
|
|
selectedKeys,
|
|
|
|
openKeys,
|
2020-11-23 06:21:18 +00:00
|
|
|
activeKey,
|
|
|
|
defaultActiveFirst,
|
|
|
|
menuChildrenInfo,
|
|
|
|
selectedParentUniKeys,
|
|
|
|
addChildrenInfo,
|
|
|
|
removeChildrenInfo,
|
|
|
|
getActiveKey,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-11-23 06:21:18 +00:00
|
|
|
const ins = getCurrentInstance();
|
|
|
|
const getEl = () => {
|
|
|
|
return ins.vnode.el;
|
|
|
|
};
|
|
|
|
provide('menuStore', store);
|
|
|
|
provide(
|
|
|
|
'parentMenu',
|
|
|
|
reactive({
|
|
|
|
isRootMenu: computed(() => props.isRootMenu),
|
|
|
|
getPopupContainer: computed(() => props.getPopupContainer),
|
|
|
|
getEl,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
store,
|
|
|
|
};
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2020-10-24 14:38:25 +00:00
|
|
|
handleSelect(selectInfo) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const props = this.$props;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.selectable) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// root menu
|
2020-11-23 06:21:18 +00:00
|
|
|
let selectedKeys = this.store.selectedKeys;
|
2019-01-12 03:33:27 +00:00
|
|
|
const selectedKey = selectInfo.key;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.multiple) {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys = selectedKeys.concat([selectedKey]);
|
2017-12-12 05:49:02 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys = [selectedKey];
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-02 11:05:02 +00:00
|
|
|
if (!hasProp(this, 'selectedKeys')) {
|
2020-11-23 06:21:18 +00:00
|
|
|
this.store.selectedKeys = selectedKeys;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('select', {
|
2017-12-12 05:49:02 +00:00
|
|
|
...selectInfo,
|
2020-03-18 06:52:02 +00:00
|
|
|
selectedKeys,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-10-24 14:38:25 +00:00
|
|
|
handleClick(e) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('click', e);
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
// onKeyDown needs to be exposed as a instance method
|
|
|
|
// e.g., in rc-select, we need to navigate menu item while
|
|
|
|
// current active item is rc-select input box rather than the menu itself
|
2019-01-12 03:33:27 +00:00
|
|
|
onKeyDown(e, callback) {
|
2020-07-15 10:22:32 +00:00
|
|
|
this.innerMenu.getWrappedInstance().onKeyDown(e, callback);
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
onOpenChange(event) {
|
2020-11-23 06:21:18 +00:00
|
|
|
const openKeys = this.store.openKeys.concat();
|
2019-01-12 03:33:27 +00:00
|
|
|
let changed = false;
|
|
|
|
const processSingle = e => {
|
|
|
|
let oneChanged = false;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (e.open) {
|
2019-01-12 03:33:27 +00:00
|
|
|
oneChanged = openKeys.indexOf(e.key) === -1;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (oneChanged) {
|
2019-01-12 03:33:27 +00:00
|
|
|
openKeys.push(e.key);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
const index = openKeys.indexOf(e.key);
|
|
|
|
oneChanged = index !== -1;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (oneChanged) {
|
2019-01-12 03:33:27 +00:00
|
|
|
openKeys.splice(index, 1);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
changed = changed || oneChanged;
|
|
|
|
};
|
2018-01-17 08:12:53 +00:00
|
|
|
if (Array.isArray(event)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
// batch change call
|
|
|
|
event.forEach(processSingle);
|
2017-12-12 05:49:02 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
processSingle(event);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
if (changed) {
|
2018-01-02 11:05:02 +00:00
|
|
|
if (!hasProp(this, 'openKeys')) {
|
2020-11-23 06:21:18 +00:00
|
|
|
this.store.openKeys = openKeys;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.__emit('openChange', openKeys);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-10-24 14:38:25 +00:00
|
|
|
handleDeselect(selectInfo) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const props = this.$props;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (props.selectable) {
|
2020-11-23 06:21:18 +00:00
|
|
|
const selectedKeys = this.store.selectedKeys.concat();
|
2019-01-12 03:33:27 +00:00
|
|
|
const selectedKey = selectInfo.key;
|
|
|
|
const index = selectedKeys.indexOf(selectedKey);
|
2017-12-12 05:49:02 +00:00
|
|
|
if (index !== -1) {
|
2019-01-12 03:33:27 +00:00
|
|
|
selectedKeys.splice(index, 1);
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-02 11:05:02 +00:00
|
|
|
if (!hasProp(this, 'selectedKeys')) {
|
2020-11-23 06:21:18 +00:00
|
|
|
this.store.selectedKeys = selectedKeys;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2018-01-12 08:10:41 +00:00
|
|
|
this.__emit('deselect', {
|
2017-12-12 05:49:02 +00:00
|
|
|
...selectInfo,
|
2020-03-18 06:52:02 +00:00
|
|
|
selectedKeys,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
getOpenTransitionName() {
|
|
|
|
const props = this.$props;
|
|
|
|
let transitionName = props.openTransitionName;
|
|
|
|
const animationName = props.openAnimation;
|
2017-12-12 05:49:02 +00:00
|
|
|
if (!transitionName && typeof animationName === 'string') {
|
2019-01-12 03:33:27 +00:00
|
|
|
transitionName = `${props.prefixCls}-open-${animationName}`;
|
2017-12-12 05:49:02 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return transitionName;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2020-07-15 10:22:32 +00:00
|
|
|
saveInnerMenu(ref) {
|
|
|
|
this.innerMenu = ref;
|
|
|
|
},
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-16 14:55:02 +00:00
|
|
|
const props = { ...getOptionProps(this), ...this.$attrs };
|
2020-06-29 10:43:43 +00:00
|
|
|
props.class = props.class
|
|
|
|
? `${props.class} ${props.prefixCls}-root`
|
|
|
|
: `${props.prefixCls}-root`;
|
2018-09-05 13:28:54 +00:00
|
|
|
const subPopupMenuProps = {
|
2020-06-16 14:55:02 +00:00
|
|
|
...props,
|
|
|
|
itemIcon: getComponent(this, 'itemIcon', props),
|
|
|
|
expandIcon: getComponent(this, 'expandIcon', props),
|
|
|
|
overflowedIndicator: getComponent(this, 'overflowedIndicator', props) || <span>···</span>,
|
|
|
|
openTransitionName: this.getOpenTransitionName(),
|
2020-10-24 14:38:25 +00:00
|
|
|
onClick: this.handleClick,
|
2020-06-16 14:55:02 +00:00
|
|
|
onOpenChange: this.onOpenChange,
|
2020-10-24 14:38:25 +00:00
|
|
|
onDeselect: this.handleDeselect,
|
|
|
|
onSelect: this.handleSelect,
|
2020-07-15 10:22:32 +00:00
|
|
|
ref: this.saveInnerMenu,
|
2020-11-23 06:21:18 +00:00
|
|
|
store: this.store,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-11-23 06:21:18 +00:00
|
|
|
return <SubPopupMenu {...subPopupMenuProps} v-slots={this.$slots} />;
|
2017-12-12 05:49:02 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-27 05:27:36 +00:00
|
|
|
|
2020-10-12 05:27:16 +00:00
|
|
|
export default defineComponent(Menu);
|