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.

618 lines
19 KiB

  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.1
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @ngdoc module
  11. * @name material.components.menu-bar
  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();
  232. }
  233. };
  234. /**
  235. * @ngdoc directive
  236. * @name mdMenuBar
  237. * @module material.components.menu-bar
  238. * @restrict E
  239. * @description
  240. *
  241. * Menu bars are containers that hold multiple menus. They change the behavior and appearence
  242. * of the `md-menu` directive to behave similar to an operating system provided menu.
  243. *
  244. * @usage
  245. * <hljs lang="html">
  246. * <md-menu-bar>
  247. * <md-menu>
  248. * <button ng-click="$mdOpenMenu()">
  249. * File
  250. * </button>
  251. * <md-menu-content>
  252. * <md-menu-item>
  253. * <md-button ng-click="ctrl.sampleAction('share', $event)">
  254. * Share...
  255. * </md-button>
  256. * </md-menu-item>
  257. * <md-menu-divider></md-menu-divider>
  258. * <md-menu-item>
  259. * <md-menu-item>
  260. * <md-menu>
  261. * <md-button ng-click="$mdOpenMenu()">New</md-button>
  262. * <md-menu-content>
  263. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Document', $event)">Document</md-button></md-menu-item>
  264. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Spreadsheet', $event)">Spreadsheet</md-button></md-menu-item>
  265. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Presentation', $event)">Presentation</md-button></md-menu-item>
  266. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Form', $event)">Form</md-button></md-menu-item>
  267. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Drawing', $event)">Drawing</md-button></md-menu-item>
  268. * </md-menu-content>
  269. * </md-menu>
  270. * </md-menu-item>
  271. * </md-menu-content>
  272. * </md-menu>
  273. * </md-menu-bar>
  274. * </hljs>
  275. *
  276. * ## Menu Bar Controls
  277. *
  278. * You may place `md-menu-items` that function as controls within menu bars.
  279. * There are two modes that are exposed via the `type` attribute of the `md-menu-item`.
  280. * `type="checkbox"` will function as a boolean control for the `ng-model` attribute of the
  281. * `md-menu-item`. `type="radio"` will function like a radio button, setting the `ngModel`
  282. * to the `string` value of the `value` attribute. If you need non-string values, you can use
  283. * `ng-value` to provide an expression (this is similar to how angular's native `input[type=radio]` works.
  284. *
  285. * <hljs lang="html">
  286. * <md-menu-bar>
  287. * <md-menu>
  288. * <button ng-click="$mdOpenMenu()">
  289. * Sample Menu
  290. * </button>
  291. * <md-menu-content>
  292. * <md-menu-item type="checkbox" ng-model="settings.allowChanges">Allow changes</md-menu-item>
  293. * <md-menu-divider></md-menu-divider>
  294. * <md-menu-item type="radio" ng-model="settings.mode" ng-value="1">Mode 1</md-menu-item>
  295. * <md-menu-item type="radio" ng-model="settings.mode" ng-value="1">Mode 2</md-menu-item>
  296. * <md-menu-item type="radio" ng-model="settings.mode" ng-value="1">Mode 3</md-menu-item>
  297. * </md-menu-content>
  298. * </md-menu>
  299. * </md-menu-bar>
  300. * </hljs>
  301. *
  302. *
  303. * ### Nesting Menus
  304. *
  305. * Menus may be nested within menu bars. This is commonly called cascading menus.
  306. * To nest a menu place the nested menu inside the content of the `md-menu-item`.
  307. * <hljs lang="html">
  308. * <md-menu-item>
  309. * <md-menu>
  310. * <button ng-click="$mdOpenMenu()">New</md-button>
  311. * <md-menu-content>
  312. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Document', $event)">Document</md-button></md-menu-item>
  313. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Spreadsheet', $event)">Spreadsheet</md-button></md-menu-item>
  314. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Presentation', $event)">Presentation</md-button></md-menu-item>
  315. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Form', $event)">Form</md-button></md-menu-item>
  316. * <md-menu-item><md-button ng-click="ctrl.sampleAction('New Drawing', $event)">Drawing</md-button></md-menu-item>
  317. * </md-menu-content>
  318. * </md-menu>
  319. * </md-menu-item>
  320. * </hljs>
  321. *
  322. */
  323. MenuBarDirective.$inject = ["$mdUtil", "$mdTheming"];
  324. angular
  325. .module('material.components.menuBar')
  326. .directive('mdMenuBar', MenuBarDirective);
  327. /* ngInject */
  328. function MenuBarDirective($mdUtil, $mdTheming) {
  329. return {
  330. restrict: 'E',
  331. require: 'mdMenuBar',
  332. controller: 'MenuBarController',
  333. compile: function compile(templateEl, templateAttrs) {
  334. if (!templateAttrs.ariaRole) {
  335. templateEl[0].setAttribute('role', 'menubar');
  336. }
  337. angular.forEach(templateEl[0].children, function(menuEl) {
  338. if (menuEl.nodeName == 'MD-MENU') {
  339. if (!menuEl.hasAttribute('md-position-mode')) {
  340. menuEl.setAttribute('md-position-mode', 'left bottom');
  341. // Since we're in the compile function and actual `md-buttons` are not compiled yet,
  342. // we need to query for possible `md-buttons` as well.
  343. menuEl.querySelector('button, a, md-button').setAttribute('role', 'menuitem');
  344. }
  345. var contentEls = $mdUtil.nodesToArray(menuEl.querySelectorAll('md-menu-content'));
  346. angular.forEach(contentEls, function(contentEl) {
  347. contentEl.classList.add('md-menu-bar-menu');
  348. contentEl.classList.add('md-dense');
  349. if (!contentEl.hasAttribute('width')) {
  350. contentEl.setAttribute('width', 5);
  351. }
  352. });
  353. }
  354. });
  355. // Mark the child menu items that they're inside a menu bar. This is necessary,
  356. // because mnMenuItem has special behaviour during compilation, depending on
  357. // whether it is inside a mdMenuBar. We can usually figure this out via the DOM,
  358. // however if a directive that uses documentFragment is applied to the child (e.g. ngRepeat),
  359. // the element won't have a parent and won't compile properly.
  360. templateEl.find('md-menu-item').addClass('md-in-menu-bar');
  361. return function postLink(scope, el, attr, ctrl) {
  362. el.addClass('_md'); // private md component indicator for styling
  363. $mdTheming(scope, el);
  364. ctrl.init();
  365. };
  366. }
  367. };
  368. }
  369. angular
  370. .module('material.components.menuBar')
  371. .directive('mdMenuDivider', MenuDividerDirective);
  372. function MenuDividerDirective() {
  373. return {
  374. restrict: 'E',
  375. compile: function(templateEl, templateAttrs) {
  376. if (!templateAttrs.role) {
  377. templateEl[0].setAttribute('role', 'separator');
  378. }
  379. }
  380. };
  381. }
  382. MenuItemController.$inject = ["$scope", "$element", "$attrs"];
  383. angular
  384. .module('material.components.menuBar')
  385. .controller('MenuItemController', MenuItemController);
  386. /**
  387. * ngInject
  388. */
  389. function MenuItemController($scope, $element, $attrs) {
  390. this.$element = $element;
  391. this.$attrs = $attrs;
  392. this.$scope = $scope;
  393. }
  394. MenuItemController.prototype.init = function(ngModel) {
  395. var $element = this.$element;
  396. var $attrs = this.$attrs;
  397. this.ngModel = ngModel;
  398. if ($attrs.type == 'checkbox' || $attrs.type == 'radio') {
  399. this.mode = $attrs.type;
  400. this.iconEl = $element[0].children[0];
  401. this.buttonEl = $element[0].children[1];
  402. if (ngModel) {
  403. // Clear ngAria set attributes
  404. this.initClickListeners();
  405. }
  406. }
  407. };
  408. // ngAria auto sets attributes on a menu-item with a ngModel.
  409. // We don't want this because our content (buttons) get the focus
  410. // and set their own aria attributes appropritately. Having both
  411. // breaks NVDA / JAWS. This undeoes ngAria's attrs.
  412. MenuItemController.prototype.clearNgAria = function() {
  413. var el = this.$element[0];
  414. var clearAttrs = ['role', 'tabindex', 'aria-invalid', 'aria-checked'];
  415. angular.forEach(clearAttrs, function(attr) {
  416. el.removeAttribute(attr);
  417. });
  418. };
  419. MenuItemController.prototype.initClickListeners = function() {
  420. var self = this;
  421. var ngModel = this.ngModel;
  422. var $scope = this.$scope;
  423. var $attrs = this.$attrs;
  424. var $element = this.$element;
  425. var mode = this.mode;
  426. this.handleClick = angular.bind(this, this.handleClick);
  427. var icon = this.iconEl;
  428. var button = angular.element(this.buttonEl);
  429. var handleClick = this.handleClick;
  430. $attrs.$observe('disabled', setDisabled);
  431. setDisabled($attrs.disabled);
  432. ngModel.$render = function render() {
  433. self.clearNgAria();
  434. if (isSelected()) {
  435. icon.style.display = '';
  436. button.attr('aria-checked', 'true');
  437. } else {
  438. icon.style.display = 'none';
  439. button.attr('aria-checked', 'false');
  440. }
  441. };
  442. $scope.$$postDigest(ngModel.$render);
  443. function isSelected() {
  444. if (mode == 'radio') {
  445. var val = $attrs.ngValue ? $scope.$eval($attrs.ngValue) : $attrs.value;
  446. return ngModel.$modelValue == val;
  447. } else {
  448. return ngModel.$modelValue;
  449. }
  450. }
  451. function setDisabled(disabled) {
  452. if (disabled) {
  453. button.off('click', handleClick);
  454. } else {
  455. button.on('click', handleClick);
  456. }
  457. }
  458. };
  459. MenuItemController.prototype.handleClick = function(e) {
  460. var mode = this.mode;
  461. var ngModel = this.ngModel;
  462. var $attrs = this.$attrs;
  463. var newVal;
  464. if (mode == 'checkbox') {
  465. newVal = !ngModel.$modelValue;
  466. } else if (mode == 'radio') {
  467. newVal = $attrs.ngValue ? this.$scope.$eval($attrs.ngValue) : $attrs.value;
  468. }
  469. ngModel.$setViewValue(newVal);
  470. ngModel.$render();
  471. };
  472. MenuItemDirective.$inject = ["$mdUtil", "$$mdSvgRegistry"];
  473. angular
  474. .module('material.components.menuBar')
  475. .directive('mdMenuItem', MenuItemDirective);
  476. /* ngInject */
  477. function MenuItemDirective($mdUtil, $$mdSvgRegistry) {
  478. return {
  479. controller: 'MenuItemController',
  480. require: ['mdMenuItem', '?ngModel'],
  481. priority: 210, // ensure that our post link runs after ngAria
  482. compile: function(templateEl, templateAttrs) {
  483. var type = templateAttrs.type;
  484. var inMenuBarClass = 'md-in-menu-bar';
  485. // Note: This allows us to show the `check` icon for the md-menu-bar items.
  486. // The `md-in-menu-bar` class is set by the mdMenuBar directive.
  487. if ((type == 'checkbox' || type == 'radio') && templateEl.hasClass(inMenuBarClass)) {
  488. var text = templateEl[0].textContent;
  489. var buttonEl = angular.element('<md-button type="button"></md-button>');
  490. var iconTemplate = '<md-icon md-svg-src="' + $$mdSvgRegistry.mdChecked + '"></md-icon>';
  491. buttonEl.html(text);
  492. buttonEl.attr('tabindex', '0');
  493. templateEl.html('');
  494. templateEl.append(angular.element(iconTemplate));
  495. templateEl.append(buttonEl);
  496. templateEl.addClass('md-indent').removeClass(inMenuBarClass);
  497. setDefault('role', type == 'checkbox' ? 'menuitemcheckbox' : 'menuitemradio', buttonEl);
  498. moveAttrToButton('ng-disabled');
  499. } else {
  500. setDefault('role', 'menuitem', templateEl[0].querySelector('md-button, button, a'));
  501. }
  502. return function(scope, el, attrs, ctrls) {
  503. var ctrl = ctrls[0];
  504. var ngModel = ctrls[1];
  505. ctrl.init(ngModel);
  506. };
  507. function setDefault(attr, val, el) {
  508. el = el || templateEl;
  509. if (el instanceof angular.element) {
  510. el = el[0];
  511. }
  512. if (!el.hasAttribute(attr)) {
  513. el.setAttribute(attr, val);
  514. }
  515. }
  516. function moveAttrToButton(attribute) {
  517. var attributes = $mdUtil.prefixer(attribute);
  518. angular.forEach(attributes, function(attr) {
  519. if (templateEl[0].hasAttribute(attr)) {
  520. var val = templateEl[0].getAttribute(attr);
  521. buttonEl[0].setAttribute(attr, val);
  522. templateEl[0].removeAttribute(attr);
  523. }
  524. });
  525. }
  526. }
  527. };
  528. }
  529. })(window, window.angular);