xdesign-vue-next
Version:
XDesign Component for vue-next
384 lines (380 loc) • 16.5 kB
JavaScript
/**
* xdesign v1.0.6
* (c) 2023 xdesign
* @license MIT
*/
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, inject, computed, ref, watch, toRefs, provide, reactive, onMounted, getCurrentInstance, createVNode, mergeProps, Transition, withDirectives, vShow, nextTick } from 'vue';
import props from './submenu-props.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import FakeArrow from '../common-components/fake-arrow.js';
import useRipple from '../hooks/useRipple.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { Popup } from '../popup/index.js';
import isFunction from 'lodash/isFunction';
import useCollapseAnimation from '../hooks/useCollapseAnimation.js';
import 'lodash/isEmpty';
import 'lodash/isString';
import 'lodash/isObject';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../hooks/useKeepAnimation.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/en_US.js';
import '../_chunks/dep-3a1cce9f.js';
import 'lodash/isArray';
import '../utils/set-style.js';
import '../popup/popup.js';
import '@babel/runtime/helpers/slicedToArray';
import '@popperjs/core';
import 'lodash/debounce';
import '../hooks/useVModel.js';
import '../utils/dom.js';
import '../utils/easing.js';
import '../_common/js/utils/set-style.js';
import '../popup/container.js';
import '../popup/props.js';
import '../hooks/useResizeObserver.js';
import '../hooks/tnode.js';
import '../utils/withInstall.js';
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Submenu = defineComponent({
name: "XSubmenu",
props: props,
setup: function setup(props2, ctx) {
var classPrefix = usePrefixClass();
var menu = inject("TdMenu");
var theme = menu.theme,
activeValues = menu.activeValues,
expandValues = menu.expandValues,
isHead = menu.isHead,
open = menu.open;
var submenu = inject("TdSubmenu", {});
var setSubPopup = submenu.setSubPopup,
closeParentPopup = submenu.closeParentPopup;
var mode = computed(function () {
return ctx.attrs.expandType || menu.mode.value;
});
var menuItems = ref([]);
var isActive = computed(function () {
return activeValues.value.indexOf(props2.value) > -1;
});
var popupVisible = ref(false);
var isCursorInPopup = ref(false);
var rippleColor = computed(function () {
return theme.value === "light" ? "#E7E7E7" : "#383838";
});
var isOpen = computed(function () {
var _expandValues$value;
if (mode.value === "popup") {
return popupVisible.value;
}
return expandValues ? (_expandValues$value = expandValues.value) === null || _expandValues$value === void 0 ? void 0 : _expandValues$value.includes(props2.value) : false;
});
var isNested = ref(false);
var popupWrapperRef = ref();
var subPopupRef = ref();
var submenuRef = ref();
var transitionClass = usePrefixClass("slide-down");
useRipple(submenuRef, rippleColor);
var classes = computed(function () {
var _ref;
return ["".concat(classPrefix.value, "-submenu"), (_ref = {}, _defineProperty(_ref, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), _defineProperty(_ref, "".concat(classPrefix.value, "-is-opened"), isOpen.value), _ref)];
});
var overlayInnerClassName = computed(function () {
var _props2$popupProps;
return ["".concat(classPrefix.value, "-menu__popup"), "".concat(classPrefix.value, "-is-").concat(isHead ? "horizontal" : "vertical"), _defineProperty({}, "".concat(classPrefix.value, "-is-opened"), popupVisible.value), (_props2$popupProps = props2.popupProps) === null || _props2$popupProps === void 0 ? void 0 : _props2$popupProps.overlayInnerClassName];
});
var overlayClassName = computed(function () {
var _props2$popupProps2;
return ["".concat(classPrefix.value, "-menu--").concat(theme.value), isHead && "".concat(classPrefix.value, "-is-head-menu"), _defineProperty({}, "".concat(classPrefix.value, "-menu-is-nested"), isNested.value), (_props2$popupProps2 = props2.popupProps) === null || _props2$popupProps2 === void 0 ? void 0 : _props2$popupProps2.overlayClassName];
});
var submenuClass = computed(function () {
var _ref4;
return ["".concat(classPrefix.value, "-menu__item"), "".concat(classPrefix.value, "-menu__item-spacer"), "".concat(classPrefix.value, "-menu__item-spacer--").concat(isHead && !isNested.value ? "bottom" : "right"), (_ref4 = {}, _defineProperty(_ref4, "".concat(classPrefix.value, "-is-disabled"), props2.disabled), _defineProperty(_ref4, "".concat(classPrefix.value, "-is-opened"), isOpen.value), _defineProperty(_ref4, "".concat(classPrefix.value, "-is-active"), isActive.value), _ref4)];
});
var subClass = computed(function () {
return ["".concat(classPrefix.value, "-menu__sub"), _defineProperty({}, "".concat(classPrefix.value, "-is-opened"), isOpen.value)];
});
var arrowClass = computed(function () {
return [_defineProperty({}, "".concat(classPrefix.value, "-fake-arrow--active"), isOpen.value)];
});
var passSubPopupRefToParent = function passSubPopupRefToParent(val) {
if (isFunction(setSubPopup)) {
setSubPopup(val);
}
};
var handleMouseEnter = function handleMouseEnter() {
if (props2.disabled) return;
setTimeout(function () {
if (!popupVisible.value) {
open(props2.value);
nextTick().then(function () {
passSubPopupRefToParent(popupWrapperRef.value);
});
}
popupVisible.value = true;
}, 0);
};
var targetInPopup = function targetInPopup(el) {
return el === null || el === void 0 ? void 0 : el.classList.contains("".concat(classPrefix.value, "-menu__popup"));
};
var loopInPopup = function loopInPopup(el) {
if (!el) return false;
return targetInPopup(el) || loopInPopup(el.parentElement);
};
var handleMouseLeave = function handleMouseLeave(e) {
setTimeout(function () {
var inPopup = targetInPopup(e.relatedTarget);
if (isCursorInPopup.value || inPopup) return;
popupVisible.value = false;
}, 0);
};
var handleMouseLeavePopup = function handleMouseLeavePopup(e) {
var toElement = e.toElement,
relatedTarget = e.relatedTarget;
var target = toElement || relatedTarget;
if (target === subPopupRef.value) return;
var isSubmenu = function isSubmenu(el) {
return el === submenuRef.value;
};
while (target !== null && target !== document && !isSubmenu(target)) {
target = target.parentNode;
}
isCursorInPopup.value = false;
if (!isSubmenu(target)) {
popupVisible.value = false;
}
closeParentPopup === null || closeParentPopup === void 0 ? void 0 : closeParentPopup(e);
};
var handleEnterPopup = function handleEnterPopup() {
isCursorInPopup.value = true;
};
var handleSubmenuItemClick = function handleSubmenuItemClick() {
if (props2.disabled) return;
open(props2.value);
};
watch(popupVisible, function (visible) {
menu.open(props2.value, visible ? "add" : "remove");
});
var _toRefs = toRefs(props2),
value = _toRefs.value;
provide("TdSubmenu", reactive({
value: value,
addMenuItem: function addMenuItem(item) {
menuItems.value.push(item);
if (submenu) {
submenu.addMenuItem(item);
}
},
setSubPopup: function setSubPopup(ref2) {
subPopupRef.value = ref2;
},
closeParentPopup: function closeParentPopup(e) {
var related = e.relatedTarget;
if (loopInPopup(related)) return;
handleMouseLeavePopup(e);
}
}));
watch(popupWrapperRef, function () {
passSubPopupRefToParent(popupWrapperRef.value);
});
onMounted(function () {
var _menu$vMenu;
menu === null || menu === void 0 || (_menu$vMenu = menu.vMenu) === null || _menu$vMenu === void 0 ? void 0 : _menu$vMenu.add({
value: props2.value,
parent: submenu === null || submenu === void 0 ? void 0 : submenu.value,
vnode: ctx.slots["default"]
});
var instance = getCurrentInstance();
var node = instance.parent;
while (node && !/^t(head)?menu/i.test((_node = node) === null || _node === void 0 ? void 0 : _node.type.name)) {
var _node, _node2, _node3;
if (/submenu/i.test((_node2 = node) === null || _node2 === void 0 ? void 0 : _node2.type.name)) {
isNested.value = true;
break;
}
node = (_node3 = node) === null || _node3 === void 0 ? void 0 : _node3.parent;
}
});
return {
classPrefix: classPrefix,
menuItems: menuItems,
mode: mode,
theme: theme,
isHead: isHead,
isNested: isNested,
classes: classes,
subClass: subClass,
isOpen: isOpen,
transitionClass: transitionClass,
arrowClass: arrowClass,
overlayInnerClassName: overlayInnerClassName,
overlayClassName: overlayClassName,
submenuClass: submenuClass,
submenuRef: submenuRef,
popupWrapperRef: popupWrapperRef,
popupVisible: popupVisible,
isCursorInPopup: isCursorInPopup,
handleEnterPopup: handleEnterPopup,
handleMouseEnter: handleMouseEnter,
handleMouseLeave: handleMouseLeave,
handleMouseLeavePopup: handleMouseLeavePopup,
handleSubmenuItemClick: handleSubmenuItemClick
};
},
methods: {
renderPopup: function renderPopup(triggerElement) {
var _this$popupProps,
_this = this;
var placement = "right-top";
if (!this.isNested && this.isHead) {
placement = "bottom-left";
}
var popupWrapper = createVNode("div", {
"ref": "popupWrapperRef",
"class": ["".concat(this.classPrefix, "-menu__spacer"), "".concat(this.classPrefix, "-menu__spacer--").concat(!this.isNested && this.isHead ? "top" : "left")],
"onMouseenter": this.handleEnterPopup,
"onMouseleave": this.handleMouseLeavePopup
}, [createVNode("ul", {
"class": "".concat(this.classPrefix, "-menu__popup-wrapper")
}, [renderContent(this, "default", "content")])]);
var slots = {
content: function content() {
return popupWrapper;
}
};
var realPopup = createVNode(Popup, mergeProps((_this$popupProps = this.popupProps) !== null && _this$popupProps !== void 0 ? _this$popupProps : {}, {
"overlayInnerClassName": _toConsumableArray(this.overlayInnerClassName),
"overlayClassName": _toConsumableArray(this.overlayClassName),
"visible": this.popupVisible,
"placement": placement
}), _objectSpread({
"default": function _default() {
return [createVNode("div", {
"ref": "submenuRef",
"class": _this.submenuClass
}, [triggerElement])];
}
}, slots));
return realPopup;
},
renderHeadSubmenu: function renderHeadSubmenu() {
var icon = renderTNodeJSX(this, "icon");
var normalSubmenu = [createVNode("div", {
"ref": "submenuRef",
"class": this.submenuClass,
"onClick": this.handleSubmenuItemClick
}, [icon, createVNode("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title", {
silent: true
})])]), createVNode("ul", {
"style": "opacity: 0; width: 0; height: 0; overflow: hidden"
}, [renderContent(this, "default", "content")])];
var needRotate = this.mode === "popup" && this.isNested;
var triggerElement = [icon, createVNode("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title", {
silent: true
})]), createVNode(FakeArrow, {
"overlayClassName": /menu/i.test(this.$parent.$options.name) ? this.arrowClass : null,
"overlayStyle": {
transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
}
}, null)];
return this.mode === "normal" ? normalSubmenu : this.renderPopup(triggerElement);
},
renderSubmenu: function renderSubmenu() {
var _this2 = this;
var hasContent = this.$slots.content || this.$slots["default"];
var icon = renderTNodeJSX(this, "icon");
var child = renderContent(this, "default", "content");
var _getCurrentInstance = getCurrentInstance(),
parent = _getCurrentInstance.parent;
var paddingLeft = 44;
while (parent && parent.type.name !== "XMenu") {
if (parent.type.name === "XSubmenu") {
paddingLeft += 16;
}
parent = parent.parent;
}
var _useCollapseAnimation = useCollapseAnimation(),
beforeEnter = _useCollapseAnimation.beforeEnter,
enter = _useCollapseAnimation.enter,
afterEnter = _useCollapseAnimation.afterEnter,
beforeLeave = _useCollapseAnimation.beforeLeave,
leave = _useCollapseAnimation.leave,
afterLeave = _useCollapseAnimation.afterLeave;
var needRotate = this.mode === "popup" && this.isNested;
var normalSubmenu = [createVNode("div", {
"ref": "submenuRef",
"class": this.submenuClass,
"onClick": this.handleSubmenuItemClick
}, [icon, createVNode("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title", {
silent: true
})]), hasContent && createVNode(FakeArrow, {
"overlayClassName": this.arrowClass,
"overlayStyle": {
transform: "rotate(".concat(needRotate ? -90 : 0, "deg)")
}
}, null)]), createVNode(Transition, {
"name": this.transitionClass,
"onBeforeEnter": beforeEnter,
"onEnter": enter,
"onAfterEnter": afterEnter,
"onBeforeLeave": beforeLeave,
"onLeave": leave,
"onAfterLeave": afterLeave
}, {
"default": function _default() {
return [withDirectives(createVNode("ul", {
"class": _this2.subClass,
"style": {
"--padding-left": "".concat(paddingLeft, "px")
}
}, [child]), [[vShow, _this2.isOpen]])];
}
})];
var triggerElement = [icon, createVNode("span", {
"class": ["".concat(this.classPrefix, "-menu__content")]
}, [renderTNodeJSX(this, "title", {
silent: true
})]), createVNode(FakeArrow, {
"overlayClassName": /menu/i.test(this.$parent.$options.name) ? this.arrowClass : null,
"overlayStyle": {
transform: "rotate(".concat(needRotate ? -90 : 0, "deg)"),
"margin-left": "auto"
}
}, null)];
return this.mode === "normal" ? normalSubmenu : this.renderPopup(triggerElement);
}
},
render: function render() {
var child = null;
var events = {};
if (this.mode === "popup") {
events = {
onMouseenter: this.handleMouseEnter,
onMouseleave: this.handleMouseLeave
};
}
if (Object.keys(this.$slots).length > 0) {
child = this.isHead ? this.renderHeadSubmenu() : this.renderSubmenu();
}
return createVNode("li", mergeProps({
"class": this.classes
}, events), [child]);
}
});
export { _Submenu as default };
//# sourceMappingURL=submenu.js.map