menu.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <script type="text/jsx">
  2. import emitter from 'element-ui/src/mixins/emitter';
  3. import Migrating from 'element-ui/src/mixins/migrating';
  4. import Menubar from 'element-ui/src/utils/menu/aria-menubar';
  5. import { addClass, removeClass, hasClass } from 'element-ui/src/utils/dom';
  6. export default {
  7. name: 'ElMenu',
  8. render (h) {
  9. const component = (
  10. <ul
  11. role="menubar"
  12. key={ +this.collapse }
  13. style={{ backgroundColor: this.backgroundColor || '' }}
  14. class={{
  15. 'el-menu--horizontal': this.mode === 'horizontal',
  16. 'el-menu--collapse': this.collapse,
  17. "el-menu": true
  18. }}
  19. >
  20. { this.$slots.default }
  21. </ul>
  22. );
  23. if (this.collapseTransition) {
  24. return (
  25. <el-menu-collapse-transition>
  26. { component }
  27. </el-menu-collapse-transition>
  28. );
  29. } else {
  30. return component;
  31. }
  32. },
  33. componentName: 'ElMenu',
  34. mixins: [emitter, Migrating],
  35. provide() {
  36. return {
  37. rootMenu: this
  38. };
  39. },
  40. components: {
  41. 'el-menu-collapse-transition': {
  42. functional: true,
  43. render(createElement, context) {
  44. const data = {
  45. props: {
  46. mode: 'out-in'
  47. },
  48. on: {
  49. beforeEnter(el) {
  50. el.style.opacity = 0.2;
  51. },
  52. enter(el) {
  53. addClass(el, 'el-opacity-transition');
  54. el.style.opacity = 1;
  55. },
  56. afterEnter(el) {
  57. removeClass(el, 'el-opacity-transition');
  58. el.style.opacity = '';
  59. },
  60. beforeLeave(el) {
  61. if (!el.dataset) el.dataset = {};
  62. if (hasClass(el, 'el-menu--collapse')) {
  63. removeClass(el, 'el-menu--collapse');
  64. el.dataset.oldOverflow = el.style.overflow;
  65. el.dataset.scrollWidth = el.clientWidth;
  66. addClass(el, 'el-menu--collapse');
  67. } else {
  68. addClass(el, 'el-menu--collapse');
  69. el.dataset.oldOverflow = el.style.overflow;
  70. el.dataset.scrollWidth = el.clientWidth;
  71. removeClass(el, 'el-menu--collapse');
  72. }
  73. el.style.width = el.scrollWidth + 'px';
  74. el.style.overflow = 'hidden';
  75. },
  76. leave(el) {
  77. addClass(el, 'horizontal-collapse-transition');
  78. el.style.width = el.dataset.scrollWidth + 'px';
  79. }
  80. }
  81. };
  82. return createElement('transition', data, context.children);
  83. }
  84. }
  85. },
  86. props: {
  87. mode: {
  88. type: String,
  89. default: 'vertical'
  90. },
  91. defaultActive: {
  92. type: String,
  93. default: ''
  94. },
  95. defaultOpeneds: Array,
  96. uniqueOpened: Boolean,
  97. router: Boolean,
  98. menuTrigger: {
  99. type: String,
  100. default: 'hover'
  101. },
  102. collapse: Boolean,
  103. backgroundColor: String,
  104. textColor: String,
  105. activeTextColor: String,
  106. collapseTransition: {
  107. type: Boolean,
  108. default: true
  109. }
  110. },
  111. data() {
  112. return {
  113. activeIndex: this.defaultActive,
  114. openedMenus: (this.defaultOpeneds && !this.collapse) ? this.defaultOpeneds.slice(0) : [],
  115. items: {},
  116. submenus: {}
  117. };
  118. },
  119. computed: {
  120. hoverBackground() {
  121. return this.backgroundColor ? this.mixColor(this.backgroundColor, 0.2) : '';
  122. },
  123. isMenuPopup() {
  124. return this.mode === 'horizontal' || (this.mode === 'vertical' && this.collapse);
  125. }
  126. },
  127. watch: {
  128. defaultActive(value){
  129. if(!this.items[value]){
  130. this.activeIndex = null
  131. }
  132. this.updateActiveIndex(value)
  133. },
  134. defaultOpeneds(value) {
  135. if (!this.collapse) {
  136. this.openedMenus = value;
  137. }
  138. },
  139. collapse(value) {
  140. if (value) this.openedMenus = [];
  141. this.broadcast('ElSubmenu', 'toggle-collapse', value);
  142. }
  143. },
  144. methods: {
  145. updateActiveIndex(val) {
  146. const item = this.items[val] || this.items[this.activeIndex] || this.items[this.defaultActive];
  147. if (item) {
  148. this.activeIndex = item.index;
  149. this.initOpenedMenu();
  150. } else {
  151. this.activeIndex = null;
  152. }
  153. },
  154. getMigratingConfig() {
  155. return {
  156. props: {
  157. 'theme': 'theme is removed.'
  158. }
  159. };
  160. },
  161. getColorChannels(color) {
  162. color = color.replace('#', '');
  163. if (/^[0-9a-fA-F]{3}$/.test(color)) {
  164. color = color.split('');
  165. for (let i = 2; i >= 0; i--) {
  166. color.splice(i, 0, color[i]);
  167. }
  168. color = color.join('');
  169. }
  170. if (/^[0-9a-fA-F]{6}$/.test(color)) {
  171. return {
  172. red: parseInt(color.slice(0, 2), 16),
  173. green: parseInt(color.slice(2, 4), 16),
  174. blue: parseInt(color.slice(4, 6), 16)
  175. };
  176. } else {
  177. return {
  178. red: 255,
  179. green: 255,
  180. blue: 255
  181. };
  182. }
  183. },
  184. mixColor(color, percent) {
  185. let { red, green, blue } = this.getColorChannels(color);
  186. if (percent > 0) { // shade given color
  187. red *= 1 - percent;
  188. green *= 1 - percent;
  189. blue *= 1 - percent;
  190. } else { // tint given color
  191. red += (255 - red) * percent;
  192. green += (255 - green) * percent;
  193. blue += (255 - blue) * percent;
  194. }
  195. return `rgb(${ Math.round(red) }, ${ Math.round(green) }, ${ Math.round(blue) })`;
  196. },
  197. addItem(item) {
  198. this.$set(this.items, item.index, item);
  199. },
  200. removeItem(item) {
  201. delete this.items[item.index];
  202. },
  203. addSubmenu(item) {
  204. this.$set(this.submenus, item.index, item);
  205. },
  206. removeSubmenu(item) {
  207. delete this.submenus[item.index];
  208. },
  209. openMenu(index, indexPath) {
  210. let openedMenus = this.openedMenus;
  211. if (openedMenus.indexOf(index) !== -1) return;
  212. // 将不在该菜单路径下的其余菜单收起
  213. // collapse all menu that are not under current menu item
  214. if (this.uniqueOpened) {
  215. this.openedMenus = openedMenus.filter(index => {
  216. return indexPath.indexOf(index) !== -1;
  217. });
  218. }
  219. this.openedMenus.push(index);
  220. },
  221. closeMenu(index) {
  222. const i = this.openedMenus.indexOf(index);
  223. if (i !== -1) {
  224. this.openedMenus.splice(i, 1);
  225. }
  226. },
  227. handleSubmenuClick(submenu) {
  228. const { index, indexPath } = submenu;
  229. let isOpened = this.openedMenus.indexOf(index) !== -1;
  230. if (isOpened) {
  231. this.closeMenu(index);
  232. this.$emit('close', index, indexPath);
  233. } else {
  234. this.openMenu(index, indexPath);
  235. this.$emit('open', index, indexPath);
  236. }
  237. },
  238. handleItemClick(item) {
  239. const { index, indexPath } = item;
  240. const oldActiveIndex = this.activeIndex;
  241. const hasIndex = item.index !== null;
  242. if (hasIndex) {
  243. this.activeIndex = item.index;
  244. }
  245. this.$emit('select', index, indexPath, item);
  246. if (this.mode === 'horizontal' || this.collapse) {
  247. this.openedMenus = [];
  248. }
  249. if (this.router && hasIndex) {
  250. this.routeToItem(item, (error) => {
  251. this.activeIndex = oldActiveIndex;
  252. if (error) {
  253. // vue-router 3.1.0+ push/replace cause NavigationDuplicated error
  254. // https://github.com/ElemeFE/element/issues/17044
  255. if (error.name === 'NavigationDuplicated') return
  256. console.error(error)
  257. }
  258. });
  259. }
  260. },
  261. // 初始化展开菜单
  262. // initialize opened menu
  263. initOpenedMenu() {
  264. const index = this.activeIndex;
  265. const activeItem = this.items[index];
  266. if (!activeItem || this.mode === 'horizontal' || this.collapse) return;
  267. let indexPath = activeItem.indexPath;
  268. // 展开该菜单项的路径上所有子菜单
  269. // expand all submenus of the menu item
  270. indexPath.forEach(index => {
  271. let submenu = this.submenus[index];
  272. submenu && this.openMenu(index, submenu.indexPath);
  273. });
  274. },
  275. routeToItem(item, onError) {
  276. let route = item.route || item.index;
  277. try {
  278. this.$router.push(route, () => {}, onError);
  279. } catch (e) {
  280. console.error(e);
  281. }
  282. },
  283. open(index) {
  284. const { indexPath } = this.submenus[index.toString()];
  285. indexPath.forEach(i => this.openMenu(i, indexPath));
  286. },
  287. close(index) {
  288. this.closeMenu(index);
  289. }
  290. },
  291. mounted() {
  292. this.initOpenedMenu();
  293. this.$on('item-click', this.handleItemClick);
  294. this.$on('submenu-click', this.handleSubmenuClick);
  295. if (this.mode === 'horizontal') {
  296. new Menubar(this.$el); // eslint-disable-line
  297. }
  298. this.$watch('items', this.updateActiveIndex);
  299. }
  300. };
  301. </script>