menu.js 11 KB

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