You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

614 lines
19 KiB

7 years ago
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.3
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @ngdoc module
  11. * @name material.components.menuBar
  12. */
  13. angular.module('material.components.menuBar', [
  14. 'material.core',
  15. 'material.components.icon',
  16. 'material.components.menu'
  17. ]);
  18. MenuBarController['$inject'] = ["$scope", "$rootScope", "$element", "$attrs", "$mdConstant", "$document", "$mdUtil", "$timeout"];
  19. angular
  20. .module('material.components.menuBar')
  21. .controller('MenuBarController', MenuBarController);
  22. var BOUND_MENU_METHODS = ['handleKeyDown', 'handleMenuHover', 'scheduleOpenHoveredMenu', 'cancelScheduledOpen'];
  23. /**
  24. * ngInject
  25. */
  26. function MenuBarController($scope, $rootScope, $element, $attrs, $mdConstant, $document, $mdUtil, $timeout) {
  27. this.$element = $element;
  28. this.$attrs = $attrs;
  29. this.$mdConstant = $mdConstant;
  30. this.$mdUtil = $mdUtil;
  31. this.$document = $document;
  32. this.$scope = $scope;
  33. this.$rootScope = $rootScope;
  34. this.$timeout = $timeout;
  35. var self = this;
  36. angular.forEach(BOUND_MENU_METHODS, function(methodName) {
  37. self[methodName] = angular.bind(self, self[methodName]);
  38. });
  39. }
  40. MenuBarController.prototype.init = function() {
  41. var $element = this.$element;
  42. var $mdUtil = this.$mdUtil;
  43. var $scope = this.$scope;
  44. var self = this;
  45. var deregisterFns = [];
  46. $element.on('keydown', this.handleKeyDown);
  47. this.parentToolbar = $mdUtil.getClosest($element, 'MD-TOOLBAR');
  48. deregisterFns.push(this.$rootScope.$on('$mdMenuOpen', function(event, el) {
  49. if (self.getMenus().indexOf(el[0]) != -1) {
  50. $element[0].classList.add('md-open');
  51. el[0].classList.add('md-open');
  52. self.currentlyOpenMenu = el.controller('mdMenu');
  53. self.currentlyOpenMenu.registerContainerProxy(self.handleKeyDown);
  54. self.enableOpenOnHover();
  55. }
  56. }));
  57. deregisterFns.push(this.$rootScope.$on('$mdMenuClose', function(event, el, opts) {
  58. var rootMenus = self.getMenus();
  59. if (rootMenus.indexOf(el[0]) != -1) {
  60. $element[0].classList.remove('md-open');
  61. el[0].classList.remove('md-open');
  62. }
  63. if ($element[0].contains(el[0])) {
  64. var parentMenu = el[0];
  65. while (parentMenu && rootMenus.indexOf(parentMenu) == -1) {
  66. parentMenu = $mdUtil.getClosest(parentMenu, 'MD-MENU', true);
  67. }
  68. if (parentMenu) {
  69. if (!opts.skipFocus) parentMenu.querySelector('button:not([disabled])').focus();
  70. self.currentlyOpenMenu = undefined;
  71. self.disableOpenOnHover();
  72. self.setKeyboardMode(true);
  73. }
  74. }
  75. }));
  76. $scope.$on('$destroy', function() {
  77. self.disableOpenOnHover();
  78. while (deregisterFns.length) {
  79. deregisterFns.shift()();
  80. }
  81. });
  82. this.setKeyboardMode(true);
  83. };
  84. MenuBarController.prototype.setKeyboardMode = function(enabled) {
  85. if (enabled) this.$element[0].classList.add('md-keyboard-mode');
  86. else this.$element[0].classList.remove('md-keyboard-mode');
  87. };
  88. MenuBarController.prototype.enableOpenOnHover = function() {
  89. if (this.openOnHoverEnabled) return;
  90. var self = this;
  91. self.openOnHoverEnabled = true;
  92. if (self.parentToolbar) {
  93. self.parentToolbar.classList.add('md-has-open-menu');
  94. // Needs to be on the next tick so it doesn't close immediately.
  95. self.$mdUtil.nextTick(function() {
  96. angular.element(self.parentToolbar).on('click', self.handleParentClick);
  97. }, false);
  98. }
  99. angular
  100. .element(self.getMenus())
  101. .on('mouseenter', self.handleMenuHover);
  102. };
  103. MenuBarController.prototype.handleMenuHover = function(e) {
  104. this.setKeyboardMode(false);
  105. if (this.openOnHoverEnabled) {
  106. this.scheduleOpenHoveredMenu(e);
  107. }
  108. };
  109. MenuBarController.prototype.disableOpenOnHover = function() {
  110. if (!this.openOnHoverEnabled) return;
  111. this.openOnHoverEnabled = false;
  112. if (this.parentToolbar) {
  113. this.parentToolbar.classList.remove('md-has-open-menu');
  114. angular.element(this.parentToolbar).off('click', this.handleParentClick);
  115. }
  116. angular
  117. .element(this.getMenus())
  118. .off('mouseenter', this.handleMenuHover);
  119. };
  120. MenuBarController.prototype.scheduleOpenHoveredMenu = function(e) {
  121. var menuEl = angular.element(e.currentTarget);
  122. var menuCtrl = menuEl.controller('mdMenu');
  123. this.setKeyboardMode(false);
  124. this.scheduleOpenMenu(menuCtrl);
  125. };
  126. MenuBarController.prototype.scheduleOpenMenu = function(menuCtrl) {
  127. var self = this;
  128. var $timeout = this.$timeout;
  129. if (menuCtrl != self.currentlyOpenMenu) {
  130. $timeout.cancel(self.pendingMenuOpen);
  131. self.pendingMenuOpen = $timeout(function() {
  132. self.pendingMenuOpen = undefined;
  133. if (self.currentlyOpenMenu) {
  134. self.currentlyOpenMenu.close(true, { closeAll: true });
  135. }
  136. menuCtrl.open();
  137. }, 200, false);
  138. }
  139. };
  140. MenuBarController.prototype.handleKeyDown = function(e) {
  141. var keyCodes = this.$mdConstant.KEY_CODE;
  142. var currentMenu = this.currentlyOpenMenu;
  143. var wasOpen = currentMenu && currentMenu.isOpen;
  144. this.setKeyboardMode(true);
  145. var handled, newMenu, newMenuCtrl;
  146. switch (e.keyCode) {
  147. case keyCodes.DOWN_ARROW:
  148. if (currentMenu) {
  149. currentMenu.focusMenuContainer();
  150. } else {
  151. this.openFocusedMenu();
  152. }
  153. handled = true;
  154. break;
  155. case keyCodes.UP_ARROW:
  156. currentMenu && currentMenu.close();
  157. handled = true;
  158. break;
  159. case keyCodes.LEFT_ARROW:
  160. newMenu = this.focusMenu(-1);
  161. if (wasOpen) {
  162. newMenuCtrl = angular.element(newMenu).controller('mdMenu');
  163. this.scheduleOpenMenu(newMenuCtrl);
  164. }
  165. handled = true;
  166. break;
  167. case keyCodes.RIGHT_ARROW:
  168. newMenu = this.focusMenu(+1);
  169. if (wasOpen) {
  170. newMenuCtrl = angular.element(newMenu).controller('mdMenu');
  171. this.scheduleOpenMenu(newMenuCtrl);
  172. }
  173. handled = true;
  174. break;
  175. }
  176. if (handled) {
  177. e && e.preventDefault && e.preventDefault();
  178. e && e.stopImmediatePropagation && e.stopImmediatePropagation();
  179. }
  180. };
  181. MenuBarController.prototype.focusMenu = function(direction) {
  182. var menus = this.getMenus();
  183. var focusedIndex = this.getFocusedMenuIndex();
  184. if (focusedIndex == -1) { focusedIndex = this.getOpenMenuIndex(); }
  185. var changed = false;
  186. if (focusedIndex == -1) { focusedIndex = 0; changed = true; }
  187. else if (
  188. direction < 0 && focusedIndex > 0 ||
  189. direction > 0 && focusedIndex < menus.length - direction
  190. ) {
  191. focusedIndex += direction;
  192. changed = true;
  193. }
  194. if (changed) {
  195. menus[focusedIndex].querySelector('button').focus();
  196. return menus[focusedIndex];
  197. }
  198. };
  199. MenuBarController.prototype.openFocusedMenu = function() {
  200. var menu = this.getFocusedMenu();
  201. menu && angular.element(menu).controller('mdMenu').open();
  202. };
  203. MenuBarController.prototype.getMenus = function() {
  204. var $element = this.$element;
  205. return this.$mdUtil.nodesToArray($element[0].children)
  206. .filter(function(el) { return el.nodeName == 'MD-MENU'; });
  207. };
  208. MenuBarController.prototype.getFocusedMenu = function() {
  209. return this.getMenus()[this.getFocusedMenuIndex()];
  210. };
  211. MenuBarController.prototype.getFocusedMenuIndex = function() {
  212. var $mdUtil = this.$mdUtil;
  213. var focusedEl = $mdUtil.getClosest(
  214. this.$document[0].activeElement,
  215. 'MD-MENU'
  216. );
  217. if (!focusedEl) return -1;
  218. var focusedIndex = this.getMenus().indexOf(focusedEl);
  219. return focusedIndex;
  220. };
  221. MenuBarController.prototype.getOpenMenuIndex = function() {
  222. var menus = this.getMenus();
  223. for (var i = 0; i < menus.length; ++i) {
  224. if (menus[i].classList.contains('md-open')) return i;
  225. }
  226. return -1;
  227. };
  228. MenuBarController.prototype.handleParentClick = function(event) {
  229. var openMenu = this.querySelector('md-menu.md-open');
  230. if (openMenu && !openMenu.contains(event.target)) {
  231. angular.element(openMenu).controller('mdMenu').close(true, {
  232. closeAll: true
  233. });
  234. }
  235. };
  236. /**
  237. * @ngdoc directive
  238. * @name mdMenuBar
  239. * @module material.components.menuBar
  240. * @restrict E
  241. * @description
  242. *
  243. * Menu bars are containers that hold multiple menus. They change the behavior and appearence
  244. * of the `md-menu` directive to behave similar to an operating system provided menu.
  245. *
  246. * @usage
  247. * <hljs lang="html">
  248. * <md-menu-bar>
  249. * <md-menu>
  250. * <button ng-click="$mdMenu.open()">
  251. * File
  252. * </button>
  253. * <md-menu-content>
  254. * <md-menu-item>
  255. * <md-button ng-click="ctrl.sampleAction('share', $event)">
  256. * Share...
  257. * </md-button>
  258. * </md-menu-item>
  259. * <md-menu-divider></md-menu-divider>
  260. * <md-menu-item>
  261. * <md-menu-item>
  262. * <md-menu>
  263. * <md-button ng-click="$mdMenu.open()">New</md-button>
  264. * <md-menu-content>
  265. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Document', $event)">Document</md-button></md-menu-item>
  266. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Spreadsheet', $event)">Spreadsheet</md-button></md-menu-item>
  267. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Presentation', $event)">Presentation</md-button></md-menu-item>
  268. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Form', $event)">Form</md-button></md-menu-item>
  269. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Drawing', $event)">Drawing</md-button></md-menu-item>
  270. * </md-menu-content>
  271. * </md-menu>
  272. * </md-menu-item>
  273. * </md-menu-content>
  274. * </md-menu>
  275. * </md-menu-bar>
  276. * </hljs>
  277. *
  278. * ## Menu Bar Controls
  279. *
  280. * You may place `md-menu-items` that function as controls within menu bars.
  281. * There are two modes that are exposed via the `type` attribute of the `md-menu-item`.
  282. * `type="checkbox"` will function as a boolean control for the `ng-model` attribute of the
  283. * `md-menu-item`. `type="radio"` will function like a radio button, setting the `ngModel`
  284. * to the `string` value of the `value` attribute. If you need non-string values, you can use
  285. * `ng-value` to provide an expression (this is similar to how angular's native `input[type=radio]` works.
  286. *
  287. * <hljs lang="html">
  288. * <md-menu-bar>
  289. * <md-menu>
  290. * <button ng-click="$mdMenu.open()">
  291. * Sample Menu
  292. * </button>
  293. * <md-menu-content>
  294. * <md-menu-item type="checkbox" ng-model="settings.allowChanges">Allow changes</md-menu-item>
  295. * <md-menu-divider></md-menu-divider>
  296. * <md-menu-item type="radio" ng-model="settings.mode" ng-value="1">Mode 1</md-menu-item>
  297. * <md-menu-item type="radio" ng-model="settings.mode" ng-value="1">Mode 2</md-menu-item>
  298. * <md-menu-item type="radio" ng-model="settings.mode" ng-value="1">Mode 3</md-menu-item>
  299. * </md-menu-content>
  300. * </md-menu>
  301. * </md-menu-bar>
  302. * </hljs>
  303. *
  304. *
  305. * ### Nesting Menus
  306. *
  307. * Menus may be nested within menu bars. This is commonly called cascading menus.
  308. * To nest a menu place the nested menu inside the content of the `md-menu-item`.
  309. * <hljs lang="html">
  310. * <md-menu-item>
  311. * <md-menu>
  312. * <button ng-click="$mdMenu.open()">New</md-button>
  313. * <md-menu-content>
  314. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Document', $event)">Document</md-button></md-menu-item>
  315. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Spreadsheet', $event)">Spreadsheet</md-button></md-menu-item>
  316. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Presentation', $event)">Presentation</md-button></md-menu-item>
  317. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Form', $event)">Form</md-button></md-menu-item>
  318. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Drawing', $event)">Drawing</md-button></md-menu-item>
  319. * </md-menu-content>
  320. * </md-menu>
  321. * </md-menu-item>
  322. * </hljs>
  323. *
  324. */
  325. MenuBarDirective['$inject'] = ["$mdUtil", "$mdTheming"];
  326. angular
  327. .module('material.components.menuBar')
  328. .directive('mdMenuBar', MenuBarDirective);
  329. /* ngInject */
  330. function MenuBarDirective($mdUtil, $mdTheming) {
  331. return {
  332. restrict: 'E',
  333. require: 'mdMenuBar',
  334. controller: 'MenuBarController',
  335. compile: function compile(templateEl, templateAttrs) {
  336. if (!templateAttrs.ariaRole) {
  337. templateEl[0].setAttribute('role', 'menubar');
  338. }
  339. angular.forEach(templateEl[0].children, function(menuEl) {
  340. if (menuEl.nodeName == 'MD-MENU') {
  341. if (!menuEl.hasAttribute('md-position-mode')) {
  342. menuEl.setAttribute('md-position-mode', 'left bottom');
  343. // Since we're in the compile function and actual `md-buttons` are not compiled yet,
  344. // we need to query for possible `md-buttons` as well.
  345. menuEl.querySelector('button, a, md-button').setAttribute('role', 'menuitem');
  346. }
  347. var contentEls = $mdUtil.nodesToArray(menuEl.querySelectorAll('md-menu-content'));
  348. angular.forEach(contentEls, function(contentEl) {
  349. contentEl.classList.add('md-menu-bar-menu');
  350. contentEl.classList.add('md-dense');
  351. if (!contentEl.hasAttribute('width')) {
  352. contentEl.setAttribute('width', 5);
  353. }
  354. });
  355. }
  356. });
  357. // Mark the child menu items that they're inside a menu bar. This is necessary,
  358. // because mnMenuItem has special behaviour during compilation, depending on
  359. // whether it is inside a mdMenuBar. We can usually figure this out via the DOM,
  360. // however if a directive that uses documentFragment is applied to the child (e.g. ngRepeat),
  361. // the element won't have a parent and won't compile properly.
  362. templateEl.find('md-menu-item').addClass('md-in-menu-bar');
  363. return function postLink(scope, el, attr, ctrl) {
  364. el.addClass('_md'); // private md component indicator for styling
  365. $mdTheming(scope, el);
  366. ctrl.init();
  367. };
  368. }
  369. };
  370. }
  371. angular
  372. .module('material.components.menuBar')
  373. .directive('mdMenuDivider', MenuDividerDirective);
  374. function MenuDividerDirective() {
  375. return {
  376. restrict: 'E',
  377. compile: function(templateEl, templateAttrs) {
  378. if (!templateAttrs.role) {
  379. templateEl[0].setAttribute('role', 'separator');
  380. }
  381. }
  382. };
  383. }
  384. MenuItemController['$inject'] = ["$scope", "$element", "$attrs"];
  385. angular
  386. .module('material.components.menuBar')
  387. .controller('MenuItemController', MenuItemController);
  388. /**
  389. * ngInject
  390. */
  391. function MenuItemController($scope, $element, $attrs) {
  392. this.$element = $element;
  393. this.$attrs = $attrs;
  394. this.$scope = $scope;
  395. }
  396. MenuItemController.prototype.init = function(ngModel) {
  397. var $element = this.$element;
  398. var $attrs = this.$attrs;
  399. this.ngModel = ngModel;
  400. if ($attrs.type == 'checkbox' || $attrs.type == 'radio') {
  401. this.mode = $attrs.type;
  402. this.iconEl = $element[0].children[0];
  403. this.buttonEl = $element[0].children[1];
  404. if (ngModel) {
  405. // Clear ngAria set attributes
  406. this.initClickListeners();
  407. }
  408. }
  409. };
  410. // ngAria auto sets attributes on a menu-item with a ngModel.
  411. // We don't want this because our content (buttons) get the focus
  412. // and set their own aria attributes appropritately. Having both
  413. // breaks NVDA / JAWS. This undeoes ngAria's attrs.
  414. MenuItemController.prototype.clearNgAria = function() {
  415. var el = this.$element[0];
  416. var clearAttrs = ['role', 'tabindex', 'aria-invalid', 'aria-checked'];
  417. angular.forEach(clearAttrs, function(attr) {
  418. el.removeAttribute(attr);
  419. });
  420. };
  421. MenuItemController.prototype.initClickListeners = function() {
  422. var self = this;
  423. var ngModel = this.ngModel;
  424. var $scope = this.$scope;
  425. var $attrs = this.$attrs;
  426. var $element = this.$element;
  427. var mode = this.mode;
  428. this.handleClick = angular.bind(this, this.handleClick);
  429. var icon = this.iconEl;
  430. var button = angular.element(this.buttonEl);
  431. var handleClick = this.handleClick;
  432. $attrs.$observe('disabled', setDisabled);
  433. setDisabled($attrs.disabled);
  434. ngModel.$render = function render() {
  435. self.clearNgAria();
  436. if (isSelected()) {
  437. icon.style.display = '';
  438. button.attr('aria-checked', 'true');
  439. } else {
  440. icon.style.display = 'none';
  441. button.attr('aria-checked', 'false');
  442. }
  443. };
  444. $scope.$$postDigest(ngModel.$render);
  445. function isSelected() {
  446. if (mode == 'radio') {
  447. var val = $attrs.ngValue ? $scope.$eval($attrs.ngValue) : $attrs.value;
  448. return ngModel.$modelValue == val;
  449. } else {
  450. return ngModel.$modelValue;
  451. }
  452. }
  453. function setDisabled(disabled) {
  454. if (disabled) {
  455. button.off('click', handleClick);
  456. } else {
  457. button.on('click', handleClick);
  458. }
  459. }
  460. };
  461. MenuItemController.prototype.handleClick = function(e) {
  462. var mode = this.mode;
  463. var ngModel = this.ngModel;
  464. var $attrs = this.$attrs;
  465. var newVal;
  466. if (mode == 'checkbox') {
  467. newVal = !ngModel.$modelValue;
  468. } else if (mode == 'radio') {
  469. newVal = $attrs.ngValue ? this.$scope.$eval($attrs.ngValue) : $attrs.value;
  470. }
  471. ngModel.$setViewValue(newVal);
  472. ngModel.$render();
  473. };
  474. MenuItemDirective['$inject'] = ["$mdUtil", "$mdConstant", "$$mdSvgRegistry"];
  475. angular
  476. .module('material.components.menuBar')
  477. .directive('mdMenuItem', MenuItemDirective);
  478. /* ngInject */
  479. function MenuItemDirective($mdUtil, $mdConstant, $$mdSvgRegistry) {
  480. return {
  481. controller: 'MenuItemController',
  482. require: ['mdMenuItem', '?ngModel'],
  483. priority: $mdConstant.BEFORE_NG_ARIA,
  484. compile: function(templateEl, templateAttrs) {
  485. var type = templateAttrs.type;
  486. var inMenuBarClass = 'md-in-menu-bar';
  487. // Note: This allows us to show the `check` icon for the md-menu-bar items.
  488. // The `md-in-menu-bar` class is set by the mdMenuBar directive.
  489. if ((type == 'checkbox' || type == 'radio') && templateEl.hasClass(inMenuBarClass)) {
  490. var text = templateEl[0].textContent;
  491. var buttonEl = angular.element('<md-button type="button"></md-button>');
  492. var iconTemplate = '<md-icon md-svg-src="' + $$mdSvgRegistry.mdChecked + '"></md-icon>';
  493. buttonEl.html(text);
  494. buttonEl.attr('tabindex', '0');
  495. templateEl.html('');
  496. templateEl.append(angular.element(iconTemplate));
  497. templateEl.append(buttonEl);
  498. templateEl.addClass('md-indent').removeClass(inMenuBarClass);
  499. setDefault('role', type == 'checkbox' ? 'menuitemcheckbox' : 'menuitemradio', buttonEl);
  500. moveAttrToButton('ng-disabled');
  501. } else {
  502. setDefault('role', 'menuitem', templateEl[0].querySelector('md-button, button, a'));
  503. }
  504. return function(scope, el, attrs, ctrls) {
  505. var ctrl = ctrls[0];
  506. var ngModel = ctrls[1];
  507. ctrl.init(ngModel);
  508. };
  509. function setDefault(attr, val, el) {
  510. el = el || templateEl;
  511. if (el instanceof angular.element) {
  512. el = el[0];
  513. }
  514. if (!el.hasAttribute(attr)) {
  515. el.setAttribute(attr, val);
  516. }
  517. }
  518. function moveAttrToButton(attribute) {
  519. var attributes = $mdUtil.prefixer(attribute);
  520. angular.forEach(attributes, function(attr) {
  521. if (templateEl[0].hasAttribute(attr)) {
  522. var val = templateEl[0].getAttribute(attr);
  523. buttonEl[0].setAttribute(attr, val);
  524. templateEl[0].removeAttribute(attr);
  525. }
  526. });
  527. }
  528. }
  529. };
  530. }
  531. })(window, window.angular);