menu.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "/dist/";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = 69);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ 0:
  90. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  91. "use strict";
  92. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return normalizeComponent; });
  93. /* globals __VUE_SSR_CONTEXT__ */
  94. // IMPORTANT: Do NOT use ES2015 features in this file (except for modules).
  95. // This module is a runtime utility for cleaner component module output and will
  96. // be included in the final webpack user bundle.
  97. function normalizeComponent (
  98. scriptExports,
  99. render,
  100. staticRenderFns,
  101. functionalTemplate,
  102. injectStyles,
  103. scopeId,
  104. moduleIdentifier, /* server only */
  105. shadowMode /* vue-cli only */
  106. ) {
  107. // Vue.extend constructor export interop
  108. var options = typeof scriptExports === 'function'
  109. ? scriptExports.options
  110. : scriptExports
  111. // render functions
  112. if (render) {
  113. options.render = render
  114. options.staticRenderFns = staticRenderFns
  115. options._compiled = true
  116. }
  117. // functional template
  118. if (functionalTemplate) {
  119. options.functional = true
  120. }
  121. // scopedId
  122. if (scopeId) {
  123. options._scopeId = 'data-v-' + scopeId
  124. }
  125. var hook
  126. if (moduleIdentifier) { // server build
  127. hook = function (context) {
  128. // 2.3 injection
  129. context =
  130. context || // cached call
  131. (this.$vnode && this.$vnode.ssrContext) || // stateful
  132. (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext) // functional
  133. // 2.2 with runInNewContext: true
  134. if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
  135. context = __VUE_SSR_CONTEXT__
  136. }
  137. // inject component styles
  138. if (injectStyles) {
  139. injectStyles.call(this, context)
  140. }
  141. // register component module identifier for async chunk inferrence
  142. if (context && context._registeredComponents) {
  143. context._registeredComponents.add(moduleIdentifier)
  144. }
  145. }
  146. // used by ssr in case component is cached and beforeCreate
  147. // never gets called
  148. options._ssrRegister = hook
  149. } else if (injectStyles) {
  150. hook = shadowMode
  151. ? function () { injectStyles.call(this, this.$root.$options.shadowRoot) }
  152. : injectStyles
  153. }
  154. if (hook) {
  155. if (options.functional) {
  156. // for template-only hot-reload because in that case the render fn doesn't
  157. // go through the normalizer
  158. options._injectStyles = hook
  159. // register for functioal component in vue file
  160. var originalRender = options.render
  161. options.render = function renderWithStyleInjection (h, context) {
  162. hook.call(context)
  163. return originalRender(h, context)
  164. }
  165. } else {
  166. // inject component registration as beforeCreate hook
  167. var existing = options.beforeCreate
  168. options.beforeCreate = existing
  169. ? [].concat(existing, hook)
  170. : [hook]
  171. }
  172. }
  173. return {
  174. exports: scriptExports,
  175. options: options
  176. }
  177. }
  178. /***/ }),
  179. /***/ 11:
  180. /***/ (function(module, exports) {
  181. module.exports = require("element-ui/lib/mixins/migrating");
  182. /***/ }),
  183. /***/ 2:
  184. /***/ (function(module, exports) {
  185. module.exports = require("element-ui/lib/utils/dom");
  186. /***/ }),
  187. /***/ 4:
  188. /***/ (function(module, exports) {
  189. module.exports = require("element-ui/lib/mixins/emitter");
  190. /***/ }),
  191. /***/ 69:
  192. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  193. "use strict";
  194. __webpack_require__.r(__webpack_exports__);
  195. // EXTERNAL MODULE: external "element-ui/lib/mixins/emitter"
  196. var emitter_ = __webpack_require__(4);
  197. var emitter_default = /*#__PURE__*/__webpack_require__.n(emitter_);
  198. // EXTERNAL MODULE: external "element-ui/lib/mixins/migrating"
  199. var migrating_ = __webpack_require__(11);
  200. var migrating_default = /*#__PURE__*/__webpack_require__.n(migrating_);
  201. // CONCATENATED MODULE: ./src/utils/aria-utils.js
  202. var aria = aria || {};
  203. aria.Utils = aria.Utils || {};
  204. /**
  205. * @desc Set focus on descendant nodes until the first focusable element is
  206. * found.
  207. * @param element
  208. * DOM node for which to find the first focusable descendant.
  209. * @returns
  210. * true if a focusable element is found and focus is set.
  211. */
  212. aria.Utils.focusFirstDescendant = function (element) {
  213. for (var i = 0; i < element.childNodes.length; i++) {
  214. var child = element.childNodes[i];
  215. if (aria.Utils.attemptFocus(child) || aria.Utils.focusFirstDescendant(child)) {
  216. return true;
  217. }
  218. }
  219. return false;
  220. };
  221. /**
  222. * @desc Find the last descendant node that is focusable.
  223. * @param element
  224. * DOM node for which to find the last focusable descendant.
  225. * @returns
  226. * true if a focusable element is found and focus is set.
  227. */
  228. aria.Utils.focusLastDescendant = function (element) {
  229. for (var i = element.childNodes.length - 1; i >= 0; i--) {
  230. var child = element.childNodes[i];
  231. if (aria.Utils.attemptFocus(child) || aria.Utils.focusLastDescendant(child)) {
  232. return true;
  233. }
  234. }
  235. return false;
  236. };
  237. /**
  238. * @desc Set Attempt to set focus on the current node.
  239. * @param element
  240. * The node to attempt to focus on.
  241. * @returns
  242. * true if element is focused.
  243. */
  244. aria.Utils.attemptFocus = function (element) {
  245. if (!aria.Utils.isFocusable(element)) {
  246. return false;
  247. }
  248. aria.Utils.IgnoreUtilFocusChanges = true;
  249. try {
  250. element.focus();
  251. } catch (e) {}
  252. aria.Utils.IgnoreUtilFocusChanges = false;
  253. return document.activeElement === element;
  254. };
  255. aria.Utils.isFocusable = function (element) {
  256. if (element.tabIndex > 0 || element.tabIndex === 0 && element.getAttribute('tabIndex') !== null) {
  257. return true;
  258. }
  259. if (element.disabled) {
  260. return false;
  261. }
  262. switch (element.nodeName) {
  263. case 'A':
  264. return !!element.href && element.rel !== 'ignore';
  265. case 'INPUT':
  266. return element.type !== 'hidden' && element.type !== 'file';
  267. case 'BUTTON':
  268. case 'SELECT':
  269. case 'TEXTAREA':
  270. return true;
  271. default:
  272. return false;
  273. }
  274. };
  275. /**
  276. * 触发一个事件
  277. * mouseenter, mouseleave, mouseover, keyup, change, click 等
  278. * @param {Element} elm
  279. * @param {String} name
  280. * @param {*} opts
  281. */
  282. aria.Utils.triggerEvent = function (elm, name) {
  283. var eventName = void 0;
  284. if (/^mouse|click/.test(name)) {
  285. eventName = 'MouseEvents';
  286. } else if (/^key/.test(name)) {
  287. eventName = 'KeyboardEvent';
  288. } else {
  289. eventName = 'HTMLEvents';
  290. }
  291. var evt = document.createEvent(eventName);
  292. for (var _len = arguments.length, opts = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  293. opts[_key - 2] = arguments[_key];
  294. }
  295. evt.initEvent.apply(evt, [name].concat(opts));
  296. elm.dispatchEvent ? elm.dispatchEvent(evt) : elm.fireEvent('on' + name, evt);
  297. return elm;
  298. };
  299. aria.Utils.keys = {
  300. tab: 9,
  301. enter: 13,
  302. space: 32,
  303. left: 37,
  304. up: 38,
  305. right: 39,
  306. down: 40,
  307. esc: 27
  308. };
  309. /* harmony default export */ var aria_utils = (aria.Utils);
  310. // CONCATENATED MODULE: ./src/utils/menu/aria-submenu.js
  311. var SubMenu = function SubMenu(parent, domNode) {
  312. this.domNode = domNode;
  313. this.parent = parent;
  314. this.subMenuItems = [];
  315. this.subIndex = 0;
  316. this.init();
  317. };
  318. SubMenu.prototype.init = function () {
  319. this.subMenuItems = this.domNode.querySelectorAll('li');
  320. this.addListeners();
  321. };
  322. SubMenu.prototype.gotoSubIndex = function (idx) {
  323. if (idx === this.subMenuItems.length) {
  324. idx = 0;
  325. } else if (idx < 0) {
  326. idx = this.subMenuItems.length - 1;
  327. }
  328. this.subMenuItems[idx].focus();
  329. this.subIndex = idx;
  330. };
  331. SubMenu.prototype.addListeners = function () {
  332. var _this = this;
  333. var keys = aria_utils.keys;
  334. var parentNode = this.parent.domNode;
  335. Array.prototype.forEach.call(this.subMenuItems, function (el) {
  336. el.addEventListener('keydown', function (event) {
  337. var prevDef = false;
  338. switch (event.keyCode) {
  339. case keys.down:
  340. _this.gotoSubIndex(_this.subIndex + 1);
  341. prevDef = true;
  342. break;
  343. case keys.up:
  344. _this.gotoSubIndex(_this.subIndex - 1);
  345. prevDef = true;
  346. break;
  347. case keys.tab:
  348. aria_utils.triggerEvent(parentNode, 'mouseleave');
  349. break;
  350. case keys.enter:
  351. case keys.space:
  352. prevDef = true;
  353. event.currentTarget.click();
  354. break;
  355. }
  356. if (prevDef) {
  357. event.preventDefault();
  358. event.stopPropagation();
  359. }
  360. return false;
  361. });
  362. });
  363. };
  364. /* harmony default export */ var aria_submenu = (SubMenu);
  365. // CONCATENATED MODULE: ./src/utils/menu/aria-menuitem.js
  366. var MenuItem = function MenuItem(domNode) {
  367. this.domNode = domNode;
  368. this.submenu = null;
  369. this.init();
  370. };
  371. MenuItem.prototype.init = function () {
  372. this.domNode.setAttribute('tabindex', '0');
  373. var menuChild = this.domNode.querySelector('.el-menu');
  374. if (menuChild) {
  375. this.submenu = new aria_submenu(this, menuChild);
  376. }
  377. this.addListeners();
  378. };
  379. MenuItem.prototype.addListeners = function () {
  380. var _this = this;
  381. var keys = aria_utils.keys;
  382. this.domNode.addEventListener('keydown', function (event) {
  383. var prevDef = false;
  384. switch (event.keyCode) {
  385. case keys.down:
  386. aria_utils.triggerEvent(event.currentTarget, 'mouseenter');
  387. _this.submenu && _this.submenu.gotoSubIndex(0);
  388. prevDef = true;
  389. break;
  390. case keys.up:
  391. aria_utils.triggerEvent(event.currentTarget, 'mouseenter');
  392. _this.submenu && _this.submenu.gotoSubIndex(_this.submenu.subMenuItems.length - 1);
  393. prevDef = true;
  394. break;
  395. case keys.tab:
  396. aria_utils.triggerEvent(event.currentTarget, 'mouseleave');
  397. break;
  398. case keys.enter:
  399. case keys.space:
  400. prevDef = true;
  401. event.currentTarget.click();
  402. break;
  403. }
  404. if (prevDef) {
  405. event.preventDefault();
  406. }
  407. });
  408. };
  409. /* harmony default export */ var aria_menuitem = (MenuItem);
  410. // CONCATENATED MODULE: ./src/utils/menu/aria-menubar.js
  411. var Menu = function Menu(domNode) {
  412. this.domNode = domNode;
  413. this.init();
  414. };
  415. Menu.prototype.init = function () {
  416. var menuChildren = this.domNode.childNodes;
  417. [].filter.call(menuChildren, function (child) {
  418. return child.nodeType === 1;
  419. }).forEach(function (child) {
  420. new aria_menuitem(child); // eslint-disable-line
  421. });
  422. };
  423. /* harmony default export */ var aria_menubar = (Menu);
  424. // EXTERNAL MODULE: external "element-ui/lib/utils/dom"
  425. var dom_ = __webpack_require__(2);
  426. // CONCATENATED MODULE: ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./packages/menu/src/menu.vue?vue&type=script&lang=js&
  427. /* harmony default export */ var menuvue_type_script_lang_js_ = ({
  428. name: 'ElMenu',
  429. render: function render(h) {
  430. var component = h(
  431. 'ul',
  432. {
  433. attrs: {
  434. role: 'menubar'
  435. },
  436. key: +this.collapse,
  437. style: { backgroundColor: this.backgroundColor || '' },
  438. 'class': {
  439. 'el-menu--horizontal': this.mode === 'horizontal',
  440. 'el-menu--collapse': this.collapse,
  441. "el-menu": true
  442. }
  443. },
  444. [this.$slots.default]
  445. );
  446. if (this.collapseTransition) {
  447. return h('el-menu-collapse-transition', [component]);
  448. } else {
  449. return component;
  450. }
  451. },
  452. componentName: 'ElMenu',
  453. mixins: [emitter_default.a, migrating_default.a],
  454. provide: function provide() {
  455. return {
  456. rootMenu: this
  457. };
  458. },
  459. components: {
  460. 'el-menu-collapse-transition': {
  461. functional: true,
  462. render: function render(createElement, context) {
  463. var data = {
  464. props: {
  465. mode: 'out-in'
  466. },
  467. on: {
  468. beforeEnter: function beforeEnter(el) {
  469. el.style.opacity = 0.2;
  470. },
  471. enter: function enter(el) {
  472. Object(dom_["addClass"])(el, 'el-opacity-transition');
  473. el.style.opacity = 1;
  474. },
  475. afterEnter: function afterEnter(el) {
  476. Object(dom_["removeClass"])(el, 'el-opacity-transition');
  477. el.style.opacity = '';
  478. },
  479. beforeLeave: function beforeLeave(el) {
  480. if (!el.dataset) el.dataset = {};
  481. if (Object(dom_["hasClass"])(el, 'el-menu--collapse')) {
  482. Object(dom_["removeClass"])(el, 'el-menu--collapse');
  483. el.dataset.oldOverflow = el.style.overflow;
  484. el.dataset.scrollWidth = el.clientWidth;
  485. Object(dom_["addClass"])(el, 'el-menu--collapse');
  486. } else {
  487. Object(dom_["addClass"])(el, 'el-menu--collapse');
  488. el.dataset.oldOverflow = el.style.overflow;
  489. el.dataset.scrollWidth = el.clientWidth;
  490. Object(dom_["removeClass"])(el, 'el-menu--collapse');
  491. }
  492. el.style.width = el.scrollWidth + 'px';
  493. el.style.overflow = 'hidden';
  494. },
  495. leave: function leave(el) {
  496. Object(dom_["addClass"])(el, 'horizontal-collapse-transition');
  497. el.style.width = el.dataset.scrollWidth + 'px';
  498. }
  499. }
  500. };
  501. return createElement('transition', data, context.children);
  502. }
  503. }
  504. },
  505. props: {
  506. mode: {
  507. type: String,
  508. default: 'vertical'
  509. },
  510. defaultActive: {
  511. type: String,
  512. default: ''
  513. },
  514. defaultOpeneds: Array,
  515. uniqueOpened: Boolean,
  516. router: Boolean,
  517. menuTrigger: {
  518. type: String,
  519. default: 'hover'
  520. },
  521. collapse: Boolean,
  522. backgroundColor: String,
  523. textColor: String,
  524. activeTextColor: String,
  525. collapseTransition: {
  526. type: Boolean,
  527. default: true
  528. }
  529. },
  530. data: function data() {
  531. return {
  532. activeIndex: this.defaultActive,
  533. openedMenus: this.defaultOpeneds && !this.collapse ? this.defaultOpeneds.slice(0) : [],
  534. items: {},
  535. submenus: {}
  536. };
  537. },
  538. computed: {
  539. hoverBackground: function hoverBackground() {
  540. return this.backgroundColor ? this.mixColor(this.backgroundColor, 0.2) : '';
  541. },
  542. isMenuPopup: function isMenuPopup() {
  543. return this.mode === 'horizontal' || this.mode === 'vertical' && this.collapse;
  544. }
  545. },
  546. watch: {
  547. defaultActive: function defaultActive(value) {
  548. if (!this.items[value]) {
  549. this.activeIndex = null;
  550. }
  551. this.updateActiveIndex(value);
  552. },
  553. defaultOpeneds: function defaultOpeneds(value) {
  554. if (!this.collapse) {
  555. this.openedMenus = value;
  556. }
  557. },
  558. collapse: function collapse(value) {
  559. if (value) this.openedMenus = [];
  560. this.broadcast('ElSubmenu', 'toggle-collapse', value);
  561. }
  562. },
  563. methods: {
  564. updateActiveIndex: function updateActiveIndex(val) {
  565. var item = this.items[val] || this.items[this.activeIndex] || this.items[this.defaultActive];
  566. if (item) {
  567. this.activeIndex = item.index;
  568. this.initOpenedMenu();
  569. } else {
  570. this.activeIndex = null;
  571. }
  572. },
  573. getMigratingConfig: function getMigratingConfig() {
  574. return {
  575. props: {
  576. 'theme': 'theme is removed.'
  577. }
  578. };
  579. },
  580. getColorChannels: function getColorChannels(color) {
  581. color = color.replace('#', '');
  582. if (/^[0-9a-fA-F]{3}$/.test(color)) {
  583. color = color.split('');
  584. for (var i = 2; i >= 0; i--) {
  585. color.splice(i, 0, color[i]);
  586. }
  587. color = color.join('');
  588. }
  589. if (/^[0-9a-fA-F]{6}$/.test(color)) {
  590. return {
  591. red: parseInt(color.slice(0, 2), 16),
  592. green: parseInt(color.slice(2, 4), 16),
  593. blue: parseInt(color.slice(4, 6), 16)
  594. };
  595. } else {
  596. return {
  597. red: 255,
  598. green: 255,
  599. blue: 255
  600. };
  601. }
  602. },
  603. mixColor: function mixColor(color, percent) {
  604. var _getColorChannels = this.getColorChannels(color),
  605. red = _getColorChannels.red,
  606. green = _getColorChannels.green,
  607. blue = _getColorChannels.blue;
  608. if (percent > 0) {
  609. // shade given color
  610. red *= 1 - percent;
  611. green *= 1 - percent;
  612. blue *= 1 - percent;
  613. } else {
  614. // tint given color
  615. red += (255 - red) * percent;
  616. green += (255 - green) * percent;
  617. blue += (255 - blue) * percent;
  618. }
  619. return 'rgb(' + Math.round(red) + ', ' + Math.round(green) + ', ' + Math.round(blue) + ')';
  620. },
  621. addItem: function addItem(item) {
  622. this.$set(this.items, item.index, item);
  623. },
  624. removeItem: function removeItem(item) {
  625. delete this.items[item.index];
  626. },
  627. addSubmenu: function addSubmenu(item) {
  628. this.$set(this.submenus, item.index, item);
  629. },
  630. removeSubmenu: function removeSubmenu(item) {
  631. delete this.submenus[item.index];
  632. },
  633. openMenu: function openMenu(index, indexPath) {
  634. var openedMenus = this.openedMenus;
  635. if (openedMenus.indexOf(index) !== -1) return;
  636. // 将不在该菜单路径下的其余菜单收起
  637. // collapse all menu that are not under current menu item
  638. if (this.uniqueOpened) {
  639. this.openedMenus = openedMenus.filter(function (index) {
  640. return indexPath.indexOf(index) !== -1;
  641. });
  642. }
  643. this.openedMenus.push(index);
  644. },
  645. closeMenu: function closeMenu(index) {
  646. var i = this.openedMenus.indexOf(index);
  647. if (i !== -1) {
  648. this.openedMenus.splice(i, 1);
  649. }
  650. },
  651. handleSubmenuClick: function handleSubmenuClick(submenu) {
  652. var index = submenu.index,
  653. indexPath = submenu.indexPath;
  654. var isOpened = this.openedMenus.indexOf(index) !== -1;
  655. if (isOpened) {
  656. this.closeMenu(index);
  657. this.$emit('close', index, indexPath);
  658. } else {
  659. this.openMenu(index, indexPath);
  660. this.$emit('open', index, indexPath);
  661. }
  662. },
  663. handleItemClick: function handleItemClick(item) {
  664. var _this = this;
  665. var index = item.index,
  666. indexPath = item.indexPath;
  667. var oldActiveIndex = this.activeIndex;
  668. var hasIndex = item.index !== null;
  669. if (hasIndex) {
  670. this.activeIndex = item.index;
  671. }
  672. this.$emit('select', index, indexPath, item);
  673. if (this.mode === 'horizontal' || this.collapse) {
  674. this.openedMenus = [];
  675. }
  676. if (this.router && hasIndex) {
  677. this.routeToItem(item, function (error) {
  678. _this.activeIndex = oldActiveIndex;
  679. if (error) {
  680. // vue-router 3.1.0+ push/replace cause NavigationDuplicated error
  681. // https://github.com/ElemeFE/element/issues/17044
  682. if (error.name === 'NavigationDuplicated') return;
  683. console.error(error);
  684. }
  685. });
  686. }
  687. },
  688. // 初始化展开菜单
  689. // initialize opened menu
  690. initOpenedMenu: function initOpenedMenu() {
  691. var _this2 = this;
  692. var index = this.activeIndex;
  693. var activeItem = this.items[index];
  694. if (!activeItem || this.mode === 'horizontal' || this.collapse) return;
  695. var indexPath = activeItem.indexPath;
  696. // 展开该菜单项的路径上所有子菜单
  697. // expand all submenus of the menu item
  698. indexPath.forEach(function (index) {
  699. var submenu = _this2.submenus[index];
  700. submenu && _this2.openMenu(index, submenu.indexPath);
  701. });
  702. },
  703. routeToItem: function routeToItem(item, onError) {
  704. var route = item.route || item.index;
  705. try {
  706. this.$router.push(route, function () {}, onError);
  707. } catch (e) {
  708. console.error(e);
  709. }
  710. },
  711. open: function open(index) {
  712. var _this3 = this;
  713. var indexPath = this.submenus[index.toString()].indexPath;
  714. indexPath.forEach(function (i) {
  715. return _this3.openMenu(i, indexPath);
  716. });
  717. },
  718. close: function close(index) {
  719. this.closeMenu(index);
  720. }
  721. },
  722. mounted: function mounted() {
  723. this.initOpenedMenu();
  724. this.$on('item-click', this.handleItemClick);
  725. this.$on('submenu-click', this.handleSubmenuClick);
  726. if (this.mode === 'horizontal') {
  727. new aria_menubar(this.$el); // eslint-disable-line
  728. }
  729. this.$watch('items', this.updateActiveIndex);
  730. }
  731. });
  732. // CONCATENATED MODULE: ./packages/menu/src/menu.vue?vue&type=script&lang=js&
  733. /* harmony default export */ var src_menuvue_type_script_lang_js_ = (menuvue_type_script_lang_js_);
  734. // EXTERNAL MODULE: ./node_modules/vue-loader/lib/runtime/componentNormalizer.js
  735. var componentNormalizer = __webpack_require__(0);
  736. // CONCATENATED MODULE: ./packages/menu/src/menu.vue
  737. var menu_render, staticRenderFns
  738. /* normalize component */
  739. var component = Object(componentNormalizer["a" /* default */])(
  740. src_menuvue_type_script_lang_js_,
  741. menu_render,
  742. staticRenderFns,
  743. false,
  744. null,
  745. null,
  746. null
  747. )
  748. /* hot reload */
  749. if (false) { var api; }
  750. component.options.__file = "packages/menu/src/menu.vue"
  751. /* harmony default export */ var menu = (component.exports);
  752. // CONCATENATED MODULE: ./packages/menu/index.js
  753. /* istanbul ignore next */
  754. menu.install = function (Vue) {
  755. Vue.component(menu.name, menu);
  756. };
  757. /* harmony default export */ var packages_menu = __webpack_exports__["default"] = (menu);
  758. /***/ })
  759. /******/ });