menuBar.js 16 KB

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