25ba3ed57bb97541b626532279af2d978ff8904bc0e1d7121846583f8c5536aa94513cec263165d55c2de0c3913725fd6c29eb2f4c6980351eeca5309d43ce 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { defineComponent, getCurrentInstance, ref, computed, watch, watchEffect, provide, reactive, onMounted, h, withDirectives, nextTick } from 'vue';
  2. import { useResizeObserver, unrefElement } from '@vueuse/core';
  3. import { isNil } from 'lodash-unified';
  4. import { ElIcon } from '../../icon/index.mjs';
  5. import { More } from '@element-plus/icons-vue';
  6. import Menu$1 from './utils/menu-bar.mjs';
  7. import ElMenuCollapseTransition from './menu-collapse-transition.mjs';
  8. import SubMenu from './sub-menu.mjs';
  9. import { useMenuCssVar } from './use-menu-css-var.mjs';
  10. import { MENU_INJECTION_KEY, SUB_MENU_INJECTION_KEY } from './tokens.mjs';
  11. import ClickOutside from '../../../directives/click-outside/index.mjs';
  12. import { buildProps, definePropType } from '../../../utils/vue/props/runtime.mjs';
  13. import { mutable } from '../../../utils/typescript.mjs';
  14. import { iconPropType } from '../../../utils/vue/icon.mjs';
  15. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  16. import { flattedChildren } from '../../../utils/vue/vnode.mjs';
  17. import { isString, isArray, isObject } from '@vue/shared';
  18. import { isUndefined } from '../../../utils/types.mjs';
  19. const menuProps = buildProps({
  20. mode: {
  21. type: String,
  22. values: ["horizontal", "vertical"],
  23. default: "vertical"
  24. },
  25. defaultActive: {
  26. type: String,
  27. default: ""
  28. },
  29. defaultOpeneds: {
  30. type: definePropType(Array),
  31. default: () => mutable([])
  32. },
  33. uniqueOpened: Boolean,
  34. router: Boolean,
  35. menuTrigger: {
  36. type: String,
  37. values: ["hover", "click"],
  38. default: "hover"
  39. },
  40. collapse: Boolean,
  41. backgroundColor: String,
  42. textColor: String,
  43. activeTextColor: String,
  44. closeOnClickOutside: Boolean,
  45. collapseTransition: {
  46. type: Boolean,
  47. default: true
  48. },
  49. ellipsis: {
  50. type: Boolean,
  51. default: true
  52. },
  53. popperOffset: {
  54. type: Number,
  55. default: 6
  56. },
  57. ellipsisIcon: {
  58. type: iconPropType,
  59. default: () => More
  60. },
  61. popperEffect: {
  62. type: definePropType(String),
  63. default: "dark"
  64. },
  65. popperClass: String,
  66. showTimeout: {
  67. type: Number,
  68. default: 300
  69. },
  70. hideTimeout: {
  71. type: Number,
  72. default: 300
  73. },
  74. persistent: {
  75. type: Boolean,
  76. default: true
  77. }
  78. });
  79. const checkIndexPath = (indexPath) => isArray(indexPath) && indexPath.every((path) => isString(path));
  80. const menuEmits = {
  81. close: (index, indexPath) => isString(index) && checkIndexPath(indexPath),
  82. open: (index, indexPath) => isString(index) && checkIndexPath(indexPath),
  83. select: (index, indexPath, item, routerResult) => isString(index) && checkIndexPath(indexPath) && isObject(item) && (isUndefined(routerResult) || routerResult instanceof Promise)
  84. };
  85. var Menu = defineComponent({
  86. name: "ElMenu",
  87. props: menuProps,
  88. emits: menuEmits,
  89. setup(props, { emit, slots, expose }) {
  90. const instance = getCurrentInstance();
  91. const router = instance.appContext.config.globalProperties.$router;
  92. const menu = ref();
  93. const subMenu = ref();
  94. const nsMenu = useNamespace("menu");
  95. const nsSubMenu = useNamespace("sub-menu");
  96. let moreItemWidth = 64;
  97. const sliceIndex = ref(-1);
  98. const openedMenus = ref(props.defaultOpeneds && !props.collapse ? props.defaultOpeneds.slice(0) : []);
  99. const activeIndex = ref(props.defaultActive);
  100. const items = ref({});
  101. const subMenus = ref({});
  102. const isMenuPopup = computed(() => props.mode === "horizontal" || props.mode === "vertical" && props.collapse);
  103. const initMenu = () => {
  104. const activeItem = activeIndex.value && items.value[activeIndex.value];
  105. if (!activeItem || props.mode === "horizontal" || props.collapse)
  106. return;
  107. const indexPath = activeItem.indexPath;
  108. indexPath.forEach((index) => {
  109. const subMenu2 = subMenus.value[index];
  110. subMenu2 && openMenu(index, subMenu2.indexPath);
  111. });
  112. };
  113. const openMenu = (index, indexPath) => {
  114. if (openedMenus.value.includes(index))
  115. return;
  116. if (props.uniqueOpened) {
  117. openedMenus.value = openedMenus.value.filter((index2) => indexPath.includes(index2));
  118. }
  119. openedMenus.value.push(index);
  120. emit("open", index, indexPath);
  121. };
  122. const close = (index) => {
  123. const i = openedMenus.value.indexOf(index);
  124. if (i !== -1) {
  125. openedMenus.value.splice(i, 1);
  126. }
  127. };
  128. const closeMenu = (index, indexPath) => {
  129. close(index);
  130. emit("close", index, indexPath);
  131. };
  132. const handleSubMenuClick = ({
  133. index,
  134. indexPath
  135. }) => {
  136. const isOpened = openedMenus.value.includes(index);
  137. isOpened ? closeMenu(index, indexPath) : openMenu(index, indexPath);
  138. };
  139. const handleMenuItemClick = (menuItem) => {
  140. if (props.mode === "horizontal" || props.collapse) {
  141. openedMenus.value = [];
  142. }
  143. const { index, indexPath } = menuItem;
  144. if (isNil(index) || isNil(indexPath))
  145. return;
  146. if (props.router && router) {
  147. const route = menuItem.route || index;
  148. const routerResult = router.push(route).then((res) => {
  149. if (!res)
  150. activeIndex.value = index;
  151. return res;
  152. });
  153. emit("select", index, indexPath, { index, indexPath, route }, routerResult);
  154. } else {
  155. activeIndex.value = index;
  156. emit("select", index, indexPath, { index, indexPath });
  157. }
  158. };
  159. const updateActiveIndex = (val) => {
  160. var _a;
  161. const itemsInData = items.value;
  162. const item = itemsInData[val] || activeIndex.value && itemsInData[activeIndex.value] || itemsInData[props.defaultActive];
  163. activeIndex.value = (_a = item == null ? void 0 : item.index) != null ? _a : val;
  164. };
  165. const calcMenuItemWidth = (menuItem) => {
  166. const computedStyle = getComputedStyle(menuItem);
  167. const marginLeft = Number.parseInt(computedStyle.marginLeft, 10);
  168. const marginRight = Number.parseInt(computedStyle.marginRight, 10);
  169. return menuItem.offsetWidth + marginLeft + marginRight || 0;
  170. };
  171. const calcSliceIndex = () => {
  172. var _a, _b;
  173. if (!menu.value)
  174. return -1;
  175. const items2 = Array.from((_b = (_a = menu.value) == null ? void 0 : _a.childNodes) != null ? _b : []).filter((item) => item.nodeName !== "#comment" && (item.nodeName !== "#text" || item.nodeValue));
  176. const computedMenuStyle = getComputedStyle(menu.value);
  177. const paddingLeft = Number.parseInt(computedMenuStyle.paddingLeft, 10);
  178. const paddingRight = Number.parseInt(computedMenuStyle.paddingRight, 10);
  179. const menuWidth = menu.value.clientWidth - paddingLeft - paddingRight;
  180. let calcWidth = 0;
  181. let sliceIndex2 = 0;
  182. items2.forEach((item, index) => {
  183. calcWidth += calcMenuItemWidth(item);
  184. if (calcWidth <= menuWidth - moreItemWidth) {
  185. sliceIndex2 = index + 1;
  186. }
  187. });
  188. return sliceIndex2 === items2.length ? -1 : sliceIndex2;
  189. };
  190. const getIndexPath = (index) => subMenus.value[index].indexPath;
  191. const debounce = (fn, wait = 33.34) => {
  192. let timer;
  193. return () => {
  194. timer && clearTimeout(timer);
  195. timer = setTimeout(() => {
  196. fn();
  197. }, wait);
  198. };
  199. };
  200. let isFirstTimeRender = true;
  201. const handleResize = () => {
  202. const el = unrefElement(subMenu);
  203. if (el)
  204. moreItemWidth = calcMenuItemWidth(el) || 64;
  205. if (sliceIndex.value === calcSliceIndex())
  206. return;
  207. const callback = () => {
  208. sliceIndex.value = -1;
  209. nextTick(() => {
  210. sliceIndex.value = calcSliceIndex();
  211. });
  212. };
  213. isFirstTimeRender ? callback() : debounce(callback)();
  214. isFirstTimeRender = false;
  215. };
  216. watch(() => props.defaultActive, (currentActive) => {
  217. if (!items.value[currentActive]) {
  218. activeIndex.value = "";
  219. }
  220. updateActiveIndex(currentActive);
  221. });
  222. watch(() => props.collapse, (value) => {
  223. if (value)
  224. openedMenus.value = [];
  225. });
  226. watch(items.value, initMenu);
  227. let resizeStopper;
  228. watchEffect(() => {
  229. if (props.mode === "horizontal" && props.ellipsis)
  230. resizeStopper = useResizeObserver(menu, handleResize).stop;
  231. else
  232. resizeStopper == null ? void 0 : resizeStopper();
  233. });
  234. const mouseInChild = ref(false);
  235. {
  236. const addSubMenu = (item) => {
  237. subMenus.value[item.index] = item;
  238. };
  239. const removeSubMenu = (item) => {
  240. delete subMenus.value[item.index];
  241. };
  242. const addMenuItem = (item) => {
  243. items.value[item.index] = item;
  244. };
  245. const removeMenuItem = (item) => {
  246. delete items.value[item.index];
  247. };
  248. provide(MENU_INJECTION_KEY, reactive({
  249. props,
  250. openedMenus,
  251. items,
  252. subMenus,
  253. activeIndex,
  254. isMenuPopup,
  255. addMenuItem,
  256. removeMenuItem,
  257. addSubMenu,
  258. removeSubMenu,
  259. openMenu,
  260. closeMenu,
  261. handleMenuItemClick,
  262. handleSubMenuClick
  263. }));
  264. provide(`${SUB_MENU_INJECTION_KEY}${instance.uid}`, {
  265. addSubMenu,
  266. removeSubMenu,
  267. mouseInChild,
  268. level: 0
  269. });
  270. }
  271. onMounted(() => {
  272. if (props.mode === "horizontal") {
  273. new Menu$1(instance.vnode.el, nsMenu.namespace.value);
  274. }
  275. });
  276. {
  277. const open = (index) => {
  278. const { indexPath } = subMenus.value[index];
  279. indexPath.forEach((i) => openMenu(i, indexPath));
  280. };
  281. expose({
  282. open,
  283. close,
  284. updateActiveIndex,
  285. handleResize
  286. });
  287. }
  288. const ulStyle = useMenuCssVar(props, 0);
  289. return () => {
  290. var _a, _b;
  291. let slot = (_b = (_a = slots.default) == null ? void 0 : _a.call(slots)) != null ? _b : [];
  292. const vShowMore = [];
  293. if (props.mode === "horizontal" && menu.value) {
  294. const originalSlot = flattedChildren(slot).filter((vnode) => {
  295. return (vnode == null ? void 0 : vnode.shapeFlag) !== 8;
  296. });
  297. const slotDefault = sliceIndex.value === -1 ? originalSlot : originalSlot.slice(0, sliceIndex.value);
  298. const slotMore = sliceIndex.value === -1 ? [] : originalSlot.slice(sliceIndex.value);
  299. if ((slotMore == null ? void 0 : slotMore.length) && props.ellipsis) {
  300. slot = slotDefault;
  301. vShowMore.push(h(SubMenu, {
  302. ref: subMenu,
  303. index: "sub-menu-more",
  304. class: nsSubMenu.e("hide-arrow"),
  305. popperOffset: props.popperOffset
  306. }, {
  307. title: () => h(ElIcon, {
  308. class: nsSubMenu.e("icon-more")
  309. }, {
  310. default: () => h(props.ellipsisIcon)
  311. }),
  312. default: () => slotMore
  313. }));
  314. }
  315. }
  316. const directives = props.closeOnClickOutside ? [
  317. [
  318. ClickOutside,
  319. () => {
  320. if (!openedMenus.value.length)
  321. return;
  322. if (!mouseInChild.value) {
  323. openedMenus.value.forEach((openedMenu) => emit("close", openedMenu, getIndexPath(openedMenu)));
  324. openedMenus.value = [];
  325. }
  326. }
  327. ]
  328. ] : [];
  329. const vMenu = withDirectives(h("ul", {
  330. key: String(props.collapse),
  331. role: "menubar",
  332. ref: menu,
  333. style: ulStyle.value,
  334. class: {
  335. [nsMenu.b()]: true,
  336. [nsMenu.m(props.mode)]: true,
  337. [nsMenu.m("collapse")]: props.collapse
  338. }
  339. }, [...slot, ...vShowMore]), directives);
  340. if (props.collapseTransition && props.mode === "vertical") {
  341. return h(ElMenuCollapseTransition, () => vMenu);
  342. }
  343. return vMenu;
  344. };
  345. }
  346. });
  347. export { Menu as default, menuEmits, menuProps };
  348. //# sourceMappingURL=menu.mjs.map