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.

1398 lines
47 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.tabs
  12. * @description
  13. *
  14. * Tabs, created with the `<md-tabs>` directive provide *tabbed* navigation with different styles.
  15. * The Tabs component consists of clickable tabs that are aligned horizontally side-by-side.
  16. *
  17. * Features include support for:
  18. *
  19. * - static or dynamic tabs,
  20. * - responsive designs,
  21. * - accessibility support (ARIA),
  22. * - tab pagination,
  23. * - external or internal tab content,
  24. * - focus indicators and arrow-key navigations,
  25. * - programmatic lookup and access to tab controllers, and
  26. * - dynamic transitions through different tab contents.
  27. *
  28. */
  29. /*
  30. * @see js folder for tabs implementation
  31. */
  32. angular.module('material.components.tabs', [
  33. 'material.core',
  34. 'material.components.icon'
  35. ]);
  36. /**
  37. * @ngdoc directive
  38. * @name mdTab
  39. * @module material.components.tabs
  40. *
  41. * @restrict E
  42. *
  43. * @description
  44. * Use the `<md-tab>` a nested directive used within `<md-tabs>` to specify a tab with a **label** and optional *view content*.
  45. *
  46. * If the `label` attribute is not specified, then an optional `<md-tab-label>` tag can be used to specify more
  47. * complex tab header markup. If neither the **label** nor the **md-tab-label** are specified, then the nested
  48. * markup of the `<md-tab>` is used as the tab header markup.
  49. *
  50. * Please note that if you use `<md-tab-label>`, your content **MUST** be wrapped in the `<md-tab-body>` tag. This
  51. * is to define a clear separation between the tab content and the tab label.
  52. *
  53. * This container is used by the TabsController to show/hide the active tab's content view. This synchronization is
  54. * automatically managed by the internal TabsController whenever the tab selection changes. Selection changes can
  55. * be initiated via data binding changes, programmatic invocation, or user gestures.
  56. *
  57. * @param {string=} label Optional attribute to specify a simple string as the tab label
  58. * @param {boolean=} ng-disabled If present and expression evaluates to truthy, disabled tab selection.
  59. * @param {expression=} md-on-deselect Expression to be evaluated after the tab has been de-selected.
  60. * @param {expression=} md-on-select Expression to be evaluated after the tab has been selected.
  61. * @param {boolean=} md-active When true, sets the active tab. Note: There can only be one active tab at a time.
  62. *
  63. *
  64. * @usage
  65. *
  66. * <hljs lang="html">
  67. * <md-tab label="" ng-disabled md-on-select="" md-on-deselect="" >
  68. * <h3>My Tab content</h3>
  69. * </md-tab>
  70. *
  71. * <md-tab >
  72. * <md-tab-label>
  73. * <h3>My Tab content</h3>
  74. * </md-tab-label>
  75. * <md-tab-body>
  76. * <p>
  77. * Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
  78. * totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
  79. * dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit,
  80. * sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.
  81. * </p>
  82. * </md-tab-body>
  83. * </md-tab>
  84. * </hljs>
  85. *
  86. */
  87. angular
  88. .module('material.components.tabs')
  89. .directive('mdTab', MdTab);
  90. function MdTab () {
  91. return {
  92. require: '^?mdTabs',
  93. terminal: true,
  94. compile: function (element, attr) {
  95. var label = firstChild(element, 'md-tab-label'),
  96. body = firstChild(element, 'md-tab-body');
  97. if (label.length === 0) {
  98. label = angular.element('<md-tab-label></md-tab-label>');
  99. if (attr.label) label.text(attr.label);
  100. else label.append(element.contents());
  101. if (body.length === 0) {
  102. var contents = element.contents().detach();
  103. body = angular.element('<md-tab-body></md-tab-body>');
  104. body.append(contents);
  105. }
  106. }
  107. element.append(label);
  108. if (body.html()) element.append(body);
  109. return postLink;
  110. },
  111. scope: {
  112. active: '=?mdActive',
  113. disabled: '=?ngDisabled',
  114. select: '&?mdOnSelect',
  115. deselect: '&?mdOnDeselect'
  116. }
  117. };
  118. function postLink (scope, element, attr, ctrl) {
  119. if (!ctrl) return;
  120. var index = ctrl.getTabElementIndex(element),
  121. body = firstChild(element, 'md-tab-body').remove(),
  122. label = firstChild(element, 'md-tab-label').remove(),
  123. data = ctrl.insertTab({
  124. scope: scope,
  125. parent: scope.$parent,
  126. index: index,
  127. element: element,
  128. template: body.html(),
  129. label: label.html()
  130. }, index);
  131. scope.select = scope.select || angular.noop;
  132. scope.deselect = scope.deselect || angular.noop;
  133. scope.$watch('active', function (active) { if (active) ctrl.select(data.getIndex(), true); });
  134. scope.$watch('disabled', function () { ctrl.refreshIndex(); });
  135. scope.$watch(
  136. function () {
  137. return ctrl.getTabElementIndex(element);
  138. },
  139. function (newIndex) {
  140. data.index = newIndex;
  141. ctrl.updateTabOrder();
  142. }
  143. );
  144. scope.$on('$destroy', function () { ctrl.removeTab(data); });
  145. }
  146. function firstChild (element, tagName) {
  147. var children = element[0].children;
  148. for (var i = 0, len = children.length; i < len; i++) {
  149. var child = children[i];
  150. if (child.tagName === tagName.toUpperCase()) return angular.element(child);
  151. }
  152. return angular.element();
  153. }
  154. }
  155. angular
  156. .module('material.components.tabs')
  157. .directive('mdTabItem', MdTabItem);
  158. function MdTabItem () {
  159. return {
  160. require: '^?mdTabs',
  161. link: function link (scope, element, attr, ctrl) {
  162. if (!ctrl) return;
  163. ctrl.attachRipple(scope, element);
  164. }
  165. };
  166. }
  167. angular
  168. .module('material.components.tabs')
  169. .directive('mdTabLabel', MdTabLabel);
  170. function MdTabLabel () {
  171. return { terminal: true };
  172. }
  173. MdTabScroll['$inject'] = ["$parse"];angular.module('material.components.tabs')
  174. .directive('mdTabScroll', MdTabScroll);
  175. function MdTabScroll ($parse) {
  176. return {
  177. restrict: 'A',
  178. compile: function ($element, attr) {
  179. var fn = $parse(attr.mdTabScroll, null, true);
  180. return function ngEventHandler (scope, element) {
  181. element.on('mousewheel', function (event) {
  182. scope.$apply(function () { fn(scope, { $event: event }); });
  183. });
  184. };
  185. }
  186. };
  187. }
  188. MdTabsController['$inject'] = ["$scope", "$element", "$window", "$mdConstant", "$mdTabInkRipple", "$mdUtil", "$animateCss", "$attrs", "$compile", "$mdTheming", "$mdInteraction"];angular
  189. .module('material.components.tabs')
  190. .controller('MdTabsController', MdTabsController);
  191. /**
  192. * ngInject
  193. */
  194. function MdTabsController ($scope, $element, $window, $mdConstant, $mdTabInkRipple, $mdUtil,
  195. $animateCss, $attrs, $compile, $mdTheming, $mdInteraction) {
  196. // define private properties
  197. var ctrl = this,
  198. locked = false,
  199. elements = getElements(),
  200. queue = [],
  201. destroyed = false,
  202. loaded = false;
  203. // Define public methods
  204. ctrl.$onInit = $onInit;
  205. ctrl.updatePagination = $mdUtil.debounce(updatePagination, 100);
  206. ctrl.redirectFocus = redirectFocus;
  207. ctrl.attachRipple = attachRipple;
  208. ctrl.insertTab = insertTab;
  209. ctrl.removeTab = removeTab;
  210. ctrl.select = select;
  211. ctrl.scroll = scroll;
  212. ctrl.nextPage = nextPage;
  213. ctrl.previousPage = previousPage;
  214. ctrl.keydown = keydown;
  215. ctrl.canPageForward = canPageForward;
  216. ctrl.canPageBack = canPageBack;
  217. ctrl.refreshIndex = refreshIndex;
  218. ctrl.incrementIndex = incrementIndex;
  219. ctrl.getTabElementIndex = getTabElementIndex;
  220. ctrl.updateInkBarStyles = $mdUtil.debounce(updateInkBarStyles, 100);
  221. ctrl.updateTabOrder = $mdUtil.debounce(updateTabOrder, 100);
  222. ctrl.getFocusedTabId = getFocusedTabId;
  223. // For Angular 1.4 and older, where there are no lifecycle hooks but bindings are pre-assigned,
  224. // manually call the $onInit hook.
  225. if (angular.version.major === 1 && angular.version.minor <= 4) {
  226. this.$onInit();
  227. }
  228. /**
  229. * Angular Lifecycle hook for newer Angular versions.
  230. * Bindings are not guaranteed to have been assigned in the controller, but they are in the $onInit hook.
  231. */
  232. function $onInit() {
  233. // Define one-way bindings
  234. defineOneWayBinding('stretchTabs', handleStretchTabs);
  235. // Define public properties with change handlers
  236. defineProperty('focusIndex', handleFocusIndexChange, ctrl.selectedIndex || 0);
  237. defineProperty('offsetLeft', handleOffsetChange, 0);
  238. defineProperty('hasContent', handleHasContent, false);
  239. defineProperty('maxTabWidth', handleMaxTabWidth, getMaxTabWidth());
  240. defineProperty('shouldPaginate', handleShouldPaginate, false);
  241. // Define boolean attributes
  242. defineBooleanAttribute('noInkBar', handleInkBar);
  243. defineBooleanAttribute('dynamicHeight', handleDynamicHeight);
  244. defineBooleanAttribute('noPagination');
  245. defineBooleanAttribute('swipeContent');
  246. defineBooleanAttribute('noDisconnect');
  247. defineBooleanAttribute('autoselect');
  248. defineBooleanAttribute('noSelectClick');
  249. defineBooleanAttribute('centerTabs', handleCenterTabs, false);
  250. defineBooleanAttribute('enableDisconnect');
  251. // Define public properties
  252. ctrl.scope = $scope;
  253. ctrl.parent = $scope.$parent;
  254. ctrl.tabs = [];
  255. ctrl.lastSelectedIndex = null;
  256. ctrl.hasFocus = false;
  257. ctrl.styleTabItemFocus = false;
  258. ctrl.shouldCenterTabs = shouldCenterTabs();
  259. ctrl.tabContentPrefix = 'tab-content-';
  260. // Setup the tabs controller after all bindings are available.
  261. setupTabsController();
  262. }
  263. /**
  264. * Perform setup for the controller, setup events and watcher(s)
  265. */
  266. function setupTabsController () {
  267. ctrl.selectedIndex = ctrl.selectedIndex || 0;
  268. compileTemplate();
  269. configureWatchers();
  270. bindEvents();
  271. $mdTheming($element);
  272. $mdUtil.nextTick(function () {
  273. // Note that the element references need to be updated, because certain "browsers"
  274. // (IE/Edge) lose them and start throwing "Invalid calling object" errors, when we
  275. // compile the element contents down in `compileElement`.
  276. elements = getElements();
  277. updateHeightFromContent();
  278. adjustOffset();
  279. updateInkBarStyles();
  280. ctrl.tabs[ ctrl.selectedIndex ] && ctrl.tabs[ ctrl.selectedIndex ].scope.select();
  281. loaded = true;
  282. updatePagination();
  283. });
  284. }
  285. /**
  286. * Compiles the template provided by the user. This is passed as an attribute from the tabs
  287. * directive's template function.
  288. */
  289. function compileTemplate () {
  290. var template = $attrs.$mdTabsTemplate,
  291. element = angular.element($element[0].querySelector('md-tab-data'));
  292. element.html(template);
  293. $compile(element.contents())(ctrl.parent);
  294. delete $attrs.$mdTabsTemplate;
  295. }
  296. /**
  297. * Binds events used by the tabs component.
  298. */
  299. function bindEvents () {
  300. angular.element($window).on('resize', handleWindowResize);
  301. $scope.$on('$destroy', cleanup);
  302. }
  303. /**
  304. * Configure watcher(s) used by Tabs
  305. */
  306. function configureWatchers () {
  307. $scope.$watch('$mdTabsCtrl.selectedIndex', handleSelectedIndexChange);
  308. }
  309. /**
  310. * Creates a one-way binding manually rather than relying on Angular's isolated scope
  311. * @param key
  312. * @param handler
  313. */
  314. function defineOneWayBinding (key, handler) {
  315. var attr = $attrs.$normalize('md-' + key);
  316. if (handler) defineProperty(key, handler);
  317. $attrs.$observe(attr, function (newValue) { ctrl[ key ] = newValue; });
  318. }
  319. /**
  320. * Defines boolean attributes with default value set to true. (ie. md-stretch-tabs with no value
  321. * will be treated as being truthy)
  322. * @param key
  323. * @param handler
  324. */
  325. function defineBooleanAttribute (key, handler) {
  326. var attr = $attrs.$normalize('md-' + key);
  327. if (handler) defineProperty(key, handler);
  328. if ($attrs.hasOwnProperty(attr)) updateValue($attrs[attr]);
  329. $attrs.$observe(attr, updateValue);
  330. function updateValue (newValue) {
  331. ctrl[ key ] = newValue !== 'false';
  332. }
  333. }
  334. /**
  335. * Remove any events defined by this controller
  336. */
  337. function cleanup () {
  338. destroyed = true;
  339. angular.element($window).off('resize', handleWindowResize);
  340. }
  341. // Change handlers
  342. /**
  343. * Toggles stretch tabs class and updates inkbar when tab stretching changes
  344. * @param stretchTabs
  345. */
  346. function handleStretchTabs (stretchTabs) {
  347. var elements = getElements();
  348. angular.element(elements.wrapper).toggleClass('md-stretch-tabs', shouldStretchTabs());
  349. updateInkBarStyles();
  350. }
  351. function handleCenterTabs (newValue) {
  352. ctrl.shouldCenterTabs = shouldCenterTabs();
  353. }
  354. function handleMaxTabWidth (newWidth, oldWidth) {
  355. if (newWidth !== oldWidth) {
  356. var elements = getElements();
  357. angular.forEach(elements.tabs, function(tab) {
  358. tab.style.maxWidth = newWidth + 'px';
  359. });
  360. $mdUtil.nextTick(ctrl.updateInkBarStyles);
  361. }
  362. }
  363. function handleShouldPaginate (newValue, oldValue) {
  364. if (newValue !== oldValue) {
  365. ctrl.maxTabWidth = getMaxTabWidth();
  366. ctrl.shouldCenterTabs = shouldCenterTabs();
  367. $mdUtil.nextTick(function () {
  368. ctrl.maxTabWidth = getMaxTabWidth();
  369. adjustOffset(ctrl.selectedIndex);
  370. });
  371. }
  372. }
  373. /**
  374. * Add/remove the `md-no-tab-content` class depending on `ctrl.hasContent`
  375. * @param hasContent
  376. */
  377. function handleHasContent (hasContent) {
  378. $element[ hasContent ? 'removeClass' : 'addClass' ]('md-no-tab-content');
  379. }
  380. /**
  381. * Apply ctrl.offsetLeft to the paging element when it changes
  382. * @param left
  383. */
  384. function handleOffsetChange (left) {
  385. var elements = getElements();
  386. var newValue = ctrl.shouldCenterTabs ? '' : '-' + left + 'px';
  387. angular.element(elements.paging).css($mdConstant.CSS.TRANSFORM, 'translate3d(' + newValue + ', 0, 0)');
  388. $scope.$broadcast('$mdTabsPaginationChanged');
  389. }
  390. /**
  391. * Update the UI whenever `ctrl.focusIndex` is updated
  392. * @param newIndex
  393. * @param oldIndex
  394. */
  395. function handleFocusIndexChange (newIndex, oldIndex) {
  396. if (newIndex === oldIndex) return;
  397. if (!getElements().tabs[ newIndex ]) return;
  398. adjustOffset();
  399. redirectFocus();
  400. }
  401. /**
  402. * Update the UI whenever the selected index changes. Calls user-defined select/deselect methods.
  403. * @param newValue
  404. * @param oldValue
  405. */
  406. function handleSelectedIndexChange (newValue, oldValue) {
  407. if (newValue === oldValue) return;
  408. ctrl.selectedIndex = getNearestSafeIndex(newValue);
  409. ctrl.lastSelectedIndex = oldValue;
  410. ctrl.updateInkBarStyles();
  411. updateHeightFromContent();
  412. adjustOffset(newValue);
  413. $scope.$broadcast('$mdTabsChanged');
  414. ctrl.tabs[ oldValue ] && ctrl.tabs[ oldValue ].scope.deselect();
  415. ctrl.tabs[ newValue ] && ctrl.tabs[ newValue ].scope.select();
  416. }
  417. function getTabElementIndex(tabEl){
  418. var tabs = $element[0].getElementsByTagName('md-tab');
  419. return Array.prototype.indexOf.call(tabs, tabEl[0]);
  420. }
  421. /**
  422. * Queues up a call to `handleWindowResize` when a resize occurs while the tabs component is
  423. * hidden.
  424. */
  425. function handleResizeWhenVisible () {
  426. // if there is already a watcher waiting for resize, do nothing
  427. if (handleResizeWhenVisible.watcher) return;
  428. // otherwise, we will abuse the $watch function to check for visible
  429. handleResizeWhenVisible.watcher = $scope.$watch(function () {
  430. // since we are checking for DOM size, we use $mdUtil.nextTick() to wait for after the DOM updates
  431. $mdUtil.nextTick(function () {
  432. // if the watcher has already run (ie. multiple digests in one cycle), do nothing
  433. if (!handleResizeWhenVisible.watcher) return;
  434. if ($element.prop('offsetParent')) {
  435. handleResizeWhenVisible.watcher();
  436. handleResizeWhenVisible.watcher = null;
  437. handleWindowResize();
  438. }
  439. }, false);
  440. });
  441. }
  442. // Event handlers / actions
  443. /**
  444. * Handle user keyboard interactions
  445. * @param event
  446. */
  447. function keydown (event) {
  448. switch (event.keyCode) {
  449. case $mdConstant.KEY_CODE.LEFT_ARROW:
  450. event.preventDefault();
  451. incrementIndex(-1, true);
  452. break;
  453. case $mdConstant.KEY_CODE.RIGHT_ARROW:
  454. event.preventDefault();
  455. incrementIndex(1, true);
  456. break;
  457. case $mdConstant.KEY_CODE.SPACE:
  458. case $mdConstant.KEY_CODE.ENTER:
  459. event.preventDefault();
  460. if (!locked) select(ctrl.focusIndex);
  461. break;
  462. }
  463. }
  464. /**
  465. * Update the selected index. Triggers a click event on the original `md-tab` element in order
  466. * to fire user-added click events if canSkipClick or `md-no-select-click` are false.
  467. * @param index
  468. * @param canSkipClick Optionally allow not firing the click event if `md-no-select-click` is also true.
  469. */
  470. function select (index, canSkipClick) {
  471. if (!locked) ctrl.focusIndex = ctrl.selectedIndex = index;
  472. // skip the click event if noSelectClick is enabled
  473. if (canSkipClick && ctrl.noSelectClick) return;
  474. // nextTick is required to prevent errors in user-defined click events
  475. $mdUtil.nextTick(function () {
  476. ctrl.tabs[ index ].element.triggerHandler('click');
  477. }, false);
  478. }
  479. /**
  480. * When pagination is on, this makes sure the selected index is in view.
  481. * @param event
  482. */
  483. function scroll (event) {
  484. if (!ctrl.shouldPaginate) return;
  485. event.preventDefault();
  486. ctrl.offsetLeft = fixOffset(ctrl.offsetLeft - event.wheelDelta);
  487. }
  488. /**
  489. * Slides the tabs over approximately one page forward.
  490. */
  491. function nextPage () {
  492. var elements = getElements();
  493. var viewportWidth = elements.canvas.clientWidth,
  494. totalWidth = viewportWidth + ctrl.offsetLeft,
  495. i, tab;
  496. for (i = 0; i < elements.tabs.length; i++) {
  497. tab = elements.tabs[ i ];
  498. if (tab.offsetLeft + tab.offsetWidth > totalWidth) break;
  499. }
  500. if (viewportWidth > tab.offsetWidth) {
  501. //Canvas width *greater* than tab width: usual positioning
  502. ctrl.offsetLeft = fixOffset(tab.offsetLeft);
  503. } else {
  504. /**
  505. * Canvas width *smaller* than tab width: positioning at the *end* of current tab to let
  506. * pagination "for loop" to proceed correctly on next tab when nextPage() is called again
  507. */
  508. ctrl.offsetLeft = fixOffset(tab.offsetLeft + (tab.offsetWidth - viewportWidth + 1));
  509. }
  510. }
  511. /**
  512. * Slides the tabs over approximately one page backward.
  513. */
  514. function previousPage () {
  515. var i, tab, elements = getElements();
  516. for (i = 0; i < elements.tabs.length; i++) {
  517. tab = elements.tabs[ i ];
  518. if (tab.offsetLeft + tab.offsetWidth >= ctrl.offsetLeft) break;
  519. }
  520. if (elements.canvas.clientWidth > tab.offsetWidth) {
  521. //Canvas width *greater* than tab width: usual positioning
  522. ctrl.offsetLeft = fixOffset(tab.offsetLeft + tab.offsetWidth - elements.canvas.clientWidth);
  523. } else {
  524. /**
  525. * Canvas width *smaller* than tab width: positioning at the *beginning* of current tab to let
  526. * pagination "for loop" to break correctly on previous tab when previousPage() is called again
  527. */
  528. ctrl.offsetLeft = fixOffset(tab.offsetLeft);
  529. }
  530. }
  531. /**
  532. * Update size calculations when the window is resized.
  533. */
  534. function handleWindowResize () {
  535. ctrl.lastSelectedIndex = ctrl.selectedIndex;
  536. ctrl.offsetLeft = fixOffset(ctrl.offsetLeft);
  537. $mdUtil.nextTick(function () {
  538. ctrl.updateInkBarStyles();
  539. updatePagination();
  540. });
  541. }
  542. function handleInkBar (hide) {
  543. angular.element(getElements().inkBar).toggleClass('ng-hide', hide);
  544. }
  545. /**
  546. * Toggle dynamic height class when value changes
  547. * @param value
  548. */
  549. function handleDynamicHeight (value) {
  550. $element.toggleClass('md-dynamic-height', value);
  551. }
  552. /**
  553. * Remove a tab from the data and select the nearest valid tab.
  554. * @param tabData
  555. */
  556. function removeTab (tabData) {
  557. if (destroyed) return;
  558. var selectedIndex = ctrl.selectedIndex,
  559. tab = ctrl.tabs.splice(tabData.getIndex(), 1)[ 0 ];
  560. refreshIndex();
  561. // when removing a tab, if the selected index did not change, we have to manually trigger the
  562. // tab select/deselect events
  563. if (ctrl.selectedIndex === selectedIndex) {
  564. tab.scope.deselect();
  565. ctrl.tabs[ ctrl.selectedIndex ] && ctrl.tabs[ ctrl.selectedIndex ].scope.select();
  566. }
  567. $mdUtil.nextTick(function () {
  568. updatePagination();
  569. ctrl.offsetLeft = fixOffset(ctrl.offsetLeft);
  570. });
  571. }
  572. /**
  573. * Create an entry in the tabs array for a new tab at the specified index.
  574. * @param tabData
  575. * @param index
  576. * @returns {*}
  577. */
  578. function insertTab (tabData, index) {
  579. var hasLoaded = loaded;
  580. var proto = {
  581. getIndex: function () { return ctrl.tabs.indexOf(tab); },
  582. isActive: function () { return this.getIndex() === ctrl.selectedIndex; },
  583. isLeft: function () { return this.getIndex() < ctrl.selectedIndex; },
  584. isRight: function () { return this.getIndex() > ctrl.selectedIndex; },
  585. shouldRender: function () { return !ctrl.noDisconnect || this.isActive(); },
  586. hasFocus: function () {
  587. return ctrl.styleTabItemFocus
  588. && ctrl.hasFocus && this.getIndex() === ctrl.focusIndex;
  589. },
  590. id: $mdUtil.nextUid(),
  591. hasContent: !!(tabData.template && tabData.template.trim())
  592. },
  593. tab = angular.extend(proto, tabData);
  594. if (angular.isDefined(index)) {
  595. ctrl.tabs.splice(index, 0, tab);
  596. } else {
  597. ctrl.tabs.push(tab);
  598. }
  599. processQueue();
  600. updateHasContent();
  601. $mdUtil.nextTick(function () {
  602. updatePagination();
  603. setAriaControls(tab);
  604. // if autoselect is enabled, select the newly added tab
  605. if (hasLoaded && ctrl.autoselect) $mdUtil.nextTick(function () {
  606. $mdUtil.nextTick(function () { select(ctrl.tabs.indexOf(tab)); });
  607. });
  608. });
  609. return tab;
  610. }
  611. // Getter methods
  612. /**
  613. * Gathers references to all of the DOM elements used by this controller.
  614. * @returns {{}}
  615. */
  616. function getElements () {
  617. var elements = {};
  618. var node = $element[0];
  619. // gather tab bar elements
  620. elements.wrapper = node.querySelector('md-tabs-wrapper');
  621. elements.canvas = elements.wrapper.querySelector('md-tabs-canvas');
  622. elements.paging = elements.canvas.querySelector('md-pagination-wrapper');
  623. elements.inkBar = elements.paging.querySelector('md-ink-bar');
  624. elements.contents = node.querySelectorAll('md-tabs-content-wrapper > md-tab-content');
  625. elements.tabs = elements.paging.querySelectorAll('md-tab-item');
  626. elements.dummies = elements.canvas.querySelectorAll('md-dummy-tab');
  627. return elements;
  628. }
  629. /**
  630. * Determines whether or not the left pagination arrow should be enabled.
  631. * @returns {boolean}
  632. */
  633. function canPageBack () {
  634. return ctrl.offsetLeft > 0;
  635. }
  636. /**
  637. * Determines whether or not the right pagination arrow should be enabled.
  638. * @returns {*|boolean}
  639. */
  640. function canPageForward () {
  641. var elements = getElements();
  642. var lastTab = elements.tabs[ elements.tabs.length - 1 ];
  643. return lastTab && lastTab.offsetLeft + lastTab.offsetWidth > elements.canvas.clientWidth +
  644. ctrl.offsetLeft;
  645. }
  646. /**
  647. * Returns currently focused tab item's element ID
  648. */
  649. function getFocusedTabId() {
  650. var focusedTab = ctrl.tabs[ctrl.focusIndex];
  651. if (!focusedTab || !focusedTab.id) {
  652. return null;
  653. }
  654. return 'tab-item-' + focusedTab.id;
  655. }
  656. /**
  657. * Determines if the UI should stretch the tabs to fill the available space.
  658. * @returns {*}
  659. */
  660. function shouldStretchTabs () {
  661. switch (ctrl.stretchTabs) {
  662. case 'always':
  663. return true;
  664. case 'never':
  665. return false;
  666. default:
  667. return !ctrl.shouldPaginate
  668. && $window.matchMedia('(max-width: 600px)').matches;
  669. }
  670. }
  671. /**
  672. * Determines if the tabs should appear centered.
  673. * @returns {string|boolean}
  674. */
  675. function shouldCenterTabs () {
  676. return ctrl.centerTabs && !ctrl.shouldPaginate;
  677. }
  678. /**
  679. * Determines if pagination is necessary to display the tabs within the available space.
  680. * @returns {boolean}
  681. */
  682. function shouldPaginate () {
  683. if (ctrl.noPagination || !loaded) return false;
  684. var canvasWidth = $element.prop('clientWidth');
  685. angular.forEach(getElements().dummies, function (tab) {
  686. canvasWidth -= tab.offsetWidth;
  687. });
  688. return canvasWidth < 0;
  689. }
  690. /**
  691. * Finds the nearest tab index that is available. This is primarily used for when the active
  692. * tab is removed.
  693. * @param newIndex
  694. * @returns {*}
  695. */
  696. function getNearestSafeIndex (newIndex) {
  697. if (newIndex === -1) return -1;
  698. var maxOffset = Math.max(ctrl.tabs.length - newIndex, newIndex),
  699. i, tab;
  700. for (i = 0; i <= maxOffset; i++) {
  701. tab = ctrl.tabs[ newIndex + i ];
  702. if (tab && (tab.scope.disabled !== true)) return tab.getIndex();
  703. tab = ctrl.tabs[ newIndex - i ];
  704. if (tab && (tab.scope.disabled !== true)) return tab.getIndex();
  705. }
  706. return newIndex;
  707. }
  708. // Utility methods
  709. /**
  710. * Defines a property using a getter and setter in order to trigger a change handler without
  711. * using `$watch` to observe changes.
  712. * @param key
  713. * @param handler
  714. * @param value
  715. */
  716. function defineProperty (key, handler, value) {
  717. Object.defineProperty(ctrl, key, {
  718. get: function () { return value; },
  719. set: function (newValue) {
  720. var oldValue = value;
  721. value = newValue;
  722. handler && handler(newValue, oldValue);
  723. }
  724. });
  725. }
  726. /**
  727. * Updates whether or not pagination should be displayed.
  728. */
  729. function updatePagination () {
  730. ctrl.maxTabWidth = getMaxTabWidth();
  731. ctrl.shouldPaginate = shouldPaginate();
  732. }
  733. /**
  734. * Calculates the width of the pagination wrapper by summing the widths of the dummy tabs.
  735. * @returns {number}
  736. */
  737. function calcPagingWidth () {
  738. return calcTabsWidth(getElements().dummies);
  739. }
  740. function calcTabsWidth(tabs) {
  741. var width = 0;
  742. angular.forEach(tabs, function (tab) {
  743. //-- Uses the larger value between `getBoundingClientRect().width` and `offsetWidth`. This
  744. // prevents `offsetWidth` value from being rounded down and causing wrapping issues, but
  745. // also handles scenarios where `getBoundingClientRect()` is inaccurate (ie. tabs inside
  746. // of a dialog)
  747. width += Math.max(tab.offsetWidth, tab.getBoundingClientRect().width);
  748. });
  749. return Math.ceil(width);
  750. }
  751. function getMaxTabWidth () {
  752. return $element.prop('clientWidth');
  753. }
  754. /**
  755. * Re-orders the tabs and updates the selected and focus indexes to their new positions.
  756. * This is triggered by `tabDirective.js` when the user's tabs have been re-ordered.
  757. */
  758. function updateTabOrder () {
  759. var selectedItem = ctrl.tabs[ ctrl.selectedIndex ],
  760. focusItem = ctrl.tabs[ ctrl.focusIndex ];
  761. ctrl.tabs = ctrl.tabs.sort(function (a, b) {
  762. return a.index - b.index;
  763. });
  764. ctrl.selectedIndex = ctrl.tabs.indexOf(selectedItem);
  765. ctrl.focusIndex = ctrl.tabs.indexOf(focusItem);
  766. }
  767. /**
  768. * This moves the selected or focus index left or right. This is used by the keydown handler.
  769. * @param inc
  770. */
  771. function incrementIndex (inc, focus) {
  772. var newIndex,
  773. key = focus ? 'focusIndex' : 'selectedIndex',
  774. index = ctrl[ key ];
  775. for (newIndex = index + inc;
  776. ctrl.tabs[ newIndex ] && ctrl.tabs[ newIndex ].scope.disabled;
  777. newIndex += inc) {}
  778. if (ctrl.tabs[ newIndex ]) {
  779. ctrl[ key ] = newIndex;
  780. }
  781. }
  782. /**
  783. * This is used to forward focus to dummy elements. This method is necessary to avoid animation
  784. * issues when attempting to focus an item that is out of view.
  785. */
  786. function redirectFocus () {
  787. ctrl.styleTabItemFocus = ($mdInteraction.getLastInteractionType() === 'keyboard');
  788. getElements().dummies[ ctrl.focusIndex ].focus();
  789. }
  790. /**
  791. * Forces the pagination to move the focused tab into view.
  792. */
  793. function adjustOffset (index) {
  794. var elements = getElements();
  795. if (!angular.isNumber(index)) index = ctrl.focusIndex;
  796. if (!elements.tabs[ index ]) return;
  797. if (ctrl.shouldCenterTabs) return;
  798. var tab = elements.tabs[ index ],
  799. left = tab.offsetLeft,
  800. right = tab.offsetWidth + left;
  801. ctrl.offsetLeft = Math.max(ctrl.offsetLeft, fixOffset(right - elements.canvas.clientWidth + 32 * 2));
  802. ctrl.offsetLeft = Math.min(ctrl.offsetLeft, fixOffset(left));
  803. }
  804. /**
  805. * Iterates through all queued functions and clears the queue. This is used for functions that
  806. * are called before the UI is ready, such as size calculations.
  807. */
  808. function processQueue () {
  809. queue.forEach(function (func) { $mdUtil.nextTick(func); });
  810. queue = [];
  811. }
  812. /**
  813. * Determines if the tab content area is needed.
  814. */
  815. function updateHasContent () {
  816. var hasContent = false;
  817. for (var i = 0; i < ctrl.tabs.length; i++) {
  818. if (ctrl.tabs[i].hasContent) {
  819. hasContent = true;
  820. break;
  821. }
  822. }
  823. ctrl.hasContent = hasContent;
  824. }
  825. /**
  826. * Moves the indexes to their nearest valid values.
  827. */
  828. function refreshIndex () {
  829. ctrl.selectedIndex = getNearestSafeIndex(ctrl.selectedIndex);
  830. ctrl.focusIndex = getNearestSafeIndex(ctrl.focusIndex);
  831. }
  832. /**
  833. * Calculates the content height of the current tab.
  834. * @returns {*}
  835. */
  836. function updateHeightFromContent () {
  837. if (!ctrl.dynamicHeight) return $element.css('height', '');
  838. if (!ctrl.tabs.length) return queue.push(updateHeightFromContent);
  839. var elements = getElements();
  840. var tabContent = elements.contents[ ctrl.selectedIndex ],
  841. contentHeight = tabContent ? tabContent.offsetHeight : 0,
  842. tabsHeight = elements.wrapper.offsetHeight,
  843. newHeight = contentHeight + tabsHeight,
  844. currentHeight = $element.prop('clientHeight');
  845. if (currentHeight === newHeight) return;
  846. // Adjusts calculations for when the buttons are bottom-aligned since this relies on absolute
  847. // positioning. This should probably be cleaned up if a cleaner solution is possible.
  848. if ($element.attr('md-align-tabs') === 'bottom') {
  849. currentHeight -= tabsHeight;
  850. newHeight -= tabsHeight;
  851. // Need to include bottom border in these calculations
  852. if ($element.attr('md-border-bottom') !== undefined) ++currentHeight;
  853. }
  854. // Lock during animation so the user can't change tabs
  855. locked = true;
  856. var fromHeight = { height: currentHeight + 'px' },
  857. toHeight = { height: newHeight + 'px' };
  858. // Set the height to the current, specific pixel height to fix a bug on iOS where the height
  859. // first animates to 0, then back to the proper height causing a visual glitch
  860. $element.css(fromHeight);
  861. // Animate the height from the old to the new
  862. $animateCss($element, {
  863. from: fromHeight,
  864. to: toHeight,
  865. easing: 'cubic-bezier(0.35, 0, 0.25, 1)',
  866. duration: 0.5
  867. }).start().done(function () {
  868. // Then (to fix the same iOS issue as above), disable transitions and remove the specific
  869. // pixel height so the height can size with browser width/content changes, etc.
  870. $element.css({
  871. transition: 'none',
  872. height: ''
  873. });
  874. // In the next tick, re-allow transitions (if we do it all at once, $element.css is "smart"
  875. // enough to batch it for us instead of doing it immediately, which undoes the original
  876. // transition: none)
  877. $mdUtil.nextTick(function() {
  878. $element.css('transition', '');
  879. });
  880. // And unlock so tab changes can occur
  881. locked = false;
  882. });
  883. }
  884. /**
  885. * Repositions the ink bar to the selected tab.
  886. * @returns {*}
  887. */
  888. function updateInkBarStyles () {
  889. var elements = getElements();
  890. if (!elements.tabs[ ctrl.selectedIndex ]) {
  891. angular.element(elements.inkBar).css({ left: 'auto', right: 'auto' });
  892. return;
  893. }
  894. if (!ctrl.tabs.length) return queue.push(ctrl.updateInkBarStyles);
  895. // if the element is not visible, we will not be able to calculate sizes until it is
  896. // we should treat that as a resize event rather than just updating the ink bar
  897. if (!$element.prop('offsetParent')) return handleResizeWhenVisible();
  898. var index = ctrl.selectedIndex,
  899. totalWidth = elements.paging.offsetWidth,
  900. tab = elements.tabs[ index ],
  901. left = tab.offsetLeft,
  902. right = totalWidth - left - tab.offsetWidth;
  903. if (ctrl.shouldCenterTabs) {
  904. // We need to use the same calculate process as in the pagination wrapper, to avoid rounding deviations.
  905. var tabWidth = calcTabsWidth(elements.tabs);
  906. if (totalWidth > tabWidth) {
  907. $mdUtil.nextTick(updateInkBarStyles, false);
  908. }
  909. }
  910. updateInkBarClassName();
  911. angular.element(elements.inkBar).css({ left: left + 'px', right: right + 'px' });
  912. }
  913. /**
  914. * Adds left/right classes so that the ink bar will animate properly.
  915. */
  916. function updateInkBarClassName () {
  917. var elements = getElements();
  918. var newIndex = ctrl.selectedIndex,
  919. oldIndex = ctrl.lastSelectedIndex,
  920. ink = angular.element(elements.inkBar);
  921. if (!angular.isNumber(oldIndex)) return;
  922. ink
  923. .toggleClass('md-left', newIndex < oldIndex)
  924. .toggleClass('md-right', newIndex > oldIndex);
  925. }
  926. /**
  927. * Takes an offset value and makes sure that it is within the min/max allowed values.
  928. * @param value
  929. * @returns {*}
  930. */
  931. function fixOffset (value) {
  932. var elements = getElements();
  933. if (!elements.tabs.length || !ctrl.shouldPaginate) return 0;
  934. var lastTab = elements.tabs[ elements.tabs.length - 1 ],
  935. totalWidth = lastTab.offsetLeft + lastTab.offsetWidth;
  936. value = Math.max(0, value);
  937. value = Math.min(totalWidth - elements.canvas.clientWidth, value);
  938. return value;
  939. }
  940. /**
  941. * Attaches a ripple to the tab item element.
  942. * @param scope
  943. * @param element
  944. */
  945. function attachRipple (scope, element) {
  946. var elements = getElements();
  947. var options = { colorElement: angular.element(elements.inkBar) };
  948. $mdTabInkRipple.attach(scope, element, options);
  949. }
  950. /**
  951. * Sets the `aria-controls` attribute to the elements that
  952. * correspond to the passed-in tab.
  953. * @param tab
  954. */
  955. function setAriaControls (tab) {
  956. if (tab.hasContent) {
  957. var nodes = $element[0].querySelectorAll('[md-tab-id="' + tab.id + '"]');
  958. angular.element(nodes).attr('aria-controls', ctrl.tabContentPrefix + tab.id);
  959. }
  960. }
  961. }
  962. /**
  963. * @ngdoc directive
  964. * @name mdTabs
  965. * @module material.components.tabs
  966. *
  967. * @restrict E
  968. *
  969. * @description
  970. * The `<md-tabs>` directive serves as the container for 1..n `<md-tab>` child directives to
  971. * produces a Tabs components. In turn, the nested `<md-tab>` directive is used to specify a tab
  972. * label for the **header button** and a [optional] tab view content that will be associated with
  973. * each tab button.
  974. *
  975. * Below is the markup for its simplest usage:
  976. *
  977. * <hljs lang="html">
  978. * <md-tabs>
  979. * <md-tab label="Tab #1"></md-tab>
  980. * <md-tab label="Tab #2"></md-tab>
  981. * <md-tab label="Tab #3"></md-tab>
  982. * </md-tabs>
  983. * </hljs>
  984. *
  985. * Tabs supports three (3) usage scenarios:
  986. *
  987. * 1. Tabs (buttons only)
  988. * 2. Tabs with internal view content
  989. * 3. Tabs with external view content
  990. *
  991. * **Tab-only** support is useful when tab buttons are used for custom navigation regardless of any
  992. * other components, content, or views.
  993. *
  994. * <i><b>Note:</b> If you are using the Tabs component for page-level navigation, please take a look
  995. * at the <a ng-href="./api/directive/mdNavBar">NavBar component</a> instead as it can handle this
  996. * case a bit more natively.</i>
  997. *
  998. * **Tabs with internal views** are the traditional usages where each tab has associated view
  999. * content and the view switching is managed internally by the Tabs component.
  1000. *
  1001. * **Tabs with external view content** is often useful when content associated with each tab is
  1002. * independently managed and data-binding notifications announce tab selection changes.
  1003. *
  1004. * Additional features also include:
  1005. *
  1006. * * Content can include any markup.
  1007. * * If a tab is disabled while active/selected, then the next tab will be auto-selected.
  1008. *
  1009. * ### Explanation of tab stretching
  1010. *
  1011. * Initially, tabs will have an inherent size. This size will either be defined by how much space is needed to accommodate their text or set by the user through CSS. Calculations will be based on this size.
  1012. *
  1013. * On mobile devices, tabs will be expanded to fill the available horizontal space. When this happens, all tabs will become the same size.
  1014. *
  1015. * On desktops, by default, stretching will never occur.
  1016. *
  1017. * This default behavior can be overridden through the `md-stretch-tabs` attribute. Here is a table showing when stretching will occur:
  1018. *
  1019. * `md-stretch-tabs` | mobile | desktop
  1020. * ------------------|-----------|--------
  1021. * `auto` | stretched | ---
  1022. * `always` | stretched | stretched
  1023. * `never` | --- | ---
  1024. *
  1025. * @param {integer=} md-selected Index of the active/selected tab
  1026. * @param {boolean=} md-no-ink If present, disables ink ripple effects.
  1027. * @param {boolean=} md-no-ink-bar If present, disables the selection ink bar.
  1028. * @param {string=} md-align-tabs Attribute to indicate position of tab buttons: `bottom` or `top`; default is `top`
  1029. * @param {string=} md-stretch-tabs Attribute to indicate whether or not to stretch tabs: `auto`, `always`, or `never`; default is `auto`
  1030. * @param {boolean=} md-dynamic-height When enabled, the tab wrapper will resize based on the contents of the selected tab
  1031. * @param {boolean=} md-border-bottom If present, shows a solid `1px` border between the tabs and their content
  1032. * @param {boolean=} md-center-tabs When enabled, tabs will be centered provided there is no need for pagination
  1033. * @param {boolean=} md-no-pagination When enabled, pagination will remain off
  1034. * @param {boolean=} md-swipe-content When enabled, swipe gestures will be enabled for the content area to jump between tabs
  1035. * @param {boolean=} md-enable-disconnect When enabled, scopes will be disconnected for tabs that are not being displayed. This provides a performance boost, but may also cause unexpected issues and is not recommended for most users.
  1036. * @param {boolean=} md-autoselect When present, any tabs added after the initial load will be automatically selected
  1037. * @param {boolean=} md-no-select-click When enabled, click events will not be fired when selecting tabs
  1038. *
  1039. * @usage
  1040. * <hljs lang="html">
  1041. * <md-tabs md-selected="selectedIndex" >
  1042. * <img ng-src="img/angular.png" class="centered">
  1043. * <md-tab
  1044. * ng-repeat="tab in tabs | orderBy:predicate:reversed"
  1045. * md-on-select="onTabSelected(tab)"
  1046. * md-on-deselect="announceDeselected(tab)"
  1047. * ng-disabled="tab.disabled">
  1048. * <md-tab-label>
  1049. * {{tab.title}}
  1050. * <img src="img/removeTab.png" ng-click="removeTab(tab)" class="delete">
  1051. * </md-tab-label>
  1052. * <md-tab-body>
  1053. * {{tab.content}}
  1054. * </md-tab-body>
  1055. * </md-tab>
  1056. * </md-tabs>
  1057. * </hljs>
  1058. *
  1059. */
  1060. MdTabs['$inject'] = ["$$mdSvgRegistry"];
  1061. angular
  1062. .module('material.components.tabs')
  1063. .directive('mdTabs', MdTabs);
  1064. function MdTabs ($$mdSvgRegistry) {
  1065. return {
  1066. scope: {
  1067. selectedIndex: '=?mdSelected'
  1068. },
  1069. template: function (element, attr) {
  1070. attr.$mdTabsTemplate = element.html();
  1071. return '' +
  1072. '<md-tabs-wrapper> ' +
  1073. '<md-tab-data></md-tab-data> ' +
  1074. '<md-prev-button ' +
  1075. 'tabindex="-1" ' +
  1076. 'role="button" ' +
  1077. 'aria-label="Previous Page" ' +
  1078. 'aria-disabled="{{!$mdTabsCtrl.canPageBack()}}" ' +
  1079. 'ng-class="{ \'md-disabled\': !$mdTabsCtrl.canPageBack() }" ' +
  1080. 'ng-if="$mdTabsCtrl.shouldPaginate" ' +
  1081. 'ng-click="$mdTabsCtrl.previousPage()"> ' +
  1082. '<md-icon md-svg-src="'+ $$mdSvgRegistry.mdTabsArrow +'"></md-icon> ' +
  1083. '</md-prev-button> ' +
  1084. '<md-next-button ' +
  1085. 'tabindex="-1" ' +
  1086. 'role="button" ' +
  1087. 'aria-label="Next Page" ' +
  1088. 'aria-disabled="{{!$mdTabsCtrl.canPageForward()}}" ' +
  1089. 'ng-class="{ \'md-disabled\': !$mdTabsCtrl.canPageForward() }" ' +
  1090. 'ng-if="$mdTabsCtrl.shouldPaginate" ' +
  1091. 'ng-click="$mdTabsCtrl.nextPage()"> ' +
  1092. '<md-icon md-svg-src="'+ $$mdSvgRegistry.mdTabsArrow +'"></md-icon> ' +
  1093. '</md-next-button> ' +
  1094. '<md-tabs-canvas ' +
  1095. 'tabindex="{{ $mdTabsCtrl.hasFocus ? -1 : 0 }}" ' +
  1096. 'aria-activedescendant="{{$mdTabsCtrl.getFocusedTabId()}}" ' +
  1097. 'ng-focus="$mdTabsCtrl.redirectFocus()" ' +
  1098. 'ng-class="{ ' +
  1099. '\'md-paginated\': $mdTabsCtrl.shouldPaginate, ' +
  1100. '\'md-center-tabs\': $mdTabsCtrl.shouldCenterTabs ' +
  1101. '}" ' +
  1102. 'ng-keydown="$mdTabsCtrl.keydown($event)" ' +
  1103. 'role="tablist"> ' +
  1104. '<md-pagination-wrapper ' +
  1105. 'ng-class="{ \'md-center-tabs\': $mdTabsCtrl.shouldCenterTabs }" ' +
  1106. 'md-tab-scroll="$mdTabsCtrl.scroll($event)"> ' +
  1107. '<md-tab-item ' +
  1108. 'tabindex="-1" ' +
  1109. 'class="md-tab" ' +
  1110. 'ng-repeat="tab in $mdTabsCtrl.tabs" ' +
  1111. 'role="tab" ' +
  1112. 'md-tab-id="{{::tab.id}}"' +
  1113. 'aria-selected="{{tab.isActive()}}" ' +
  1114. 'aria-disabled="{{tab.scope.disabled || \'false\'}}" ' +
  1115. 'ng-click="$mdTabsCtrl.select(tab.getIndex())" ' +
  1116. 'ng-class="{ ' +
  1117. '\'md-active\': tab.isActive(), ' +
  1118. '\'md-focused\': tab.hasFocus(), ' +
  1119. '\'md-disabled\': tab.scope.disabled ' +
  1120. '}" ' +
  1121. 'ng-disabled="tab.scope.disabled" ' +
  1122. 'md-swipe-left="$mdTabsCtrl.nextPage()" ' +
  1123. 'md-swipe-right="$mdTabsCtrl.previousPage()" ' +
  1124. 'md-tabs-template="::tab.label" ' +
  1125. 'md-scope="::tab.parent"></md-tab-item> ' +
  1126. '<md-ink-bar></md-ink-bar> ' +
  1127. '</md-pagination-wrapper> ' +
  1128. '<md-tabs-dummy-wrapper class="md-visually-hidden md-dummy-wrapper"> ' +
  1129. '<md-dummy-tab ' +
  1130. 'class="md-tab" ' +
  1131. 'tabindex="-1" ' +
  1132. 'id="tab-item-{{::tab.id}}" ' +
  1133. 'md-tab-id="{{::tab.id}}"' +
  1134. 'aria-selected="{{tab.isActive()}}" ' +
  1135. 'aria-disabled="{{tab.scope.disabled || \'false\'}}" ' +
  1136. 'ng-focus="$mdTabsCtrl.hasFocus = true" ' +
  1137. 'ng-blur="$mdTabsCtrl.hasFocus = false" ' +
  1138. 'ng-repeat="tab in $mdTabsCtrl.tabs" ' +
  1139. 'md-tabs-template="::tab.label" ' +
  1140. 'md-scope="::tab.parent"></md-dummy-tab> ' +
  1141. '</md-tabs-dummy-wrapper> ' +
  1142. '</md-tabs-canvas> ' +
  1143. '</md-tabs-wrapper> ' +
  1144. '<md-tabs-content-wrapper ng-show="$mdTabsCtrl.hasContent && $mdTabsCtrl.selectedIndex >= 0" class="_md"> ' +
  1145. '<md-tab-content ' +
  1146. 'id="{{:: $mdTabsCtrl.tabContentPrefix + tab.id}}" ' +
  1147. 'class="_md" ' +
  1148. 'role="tabpanel" ' +
  1149. 'aria-labelledby="tab-item-{{::tab.id}}" ' +
  1150. 'md-swipe-left="$mdTabsCtrl.swipeContent && $mdTabsCtrl.incrementIndex(1)" ' +
  1151. 'md-swipe-right="$mdTabsCtrl.swipeContent && $mdTabsCtrl.incrementIndex(-1)" ' +
  1152. 'ng-if="tab.hasContent" ' +
  1153. 'ng-repeat="(index, tab) in $mdTabsCtrl.tabs" ' +
  1154. 'ng-class="{ ' +
  1155. '\'md-no-transition\': $mdTabsCtrl.lastSelectedIndex == null, ' +
  1156. '\'md-active\': tab.isActive(), ' +
  1157. '\'md-left\': tab.isLeft(), ' +
  1158. '\'md-right\': tab.isRight(), ' +
  1159. '\'md-no-scroll\': $mdTabsCtrl.dynamicHeight ' +
  1160. '}"> ' +
  1161. '<div ' +
  1162. 'md-tabs-template="::tab.template" ' +
  1163. 'md-connected-if="tab.isActive()" ' +
  1164. 'md-scope="::tab.parent" ' +
  1165. 'ng-if="$mdTabsCtrl.enableDisconnect || tab.shouldRender()"></div> ' +
  1166. '</md-tab-content> ' +
  1167. '</md-tabs-content-wrapper>';
  1168. },
  1169. controller: 'MdTabsController',
  1170. controllerAs: '$mdTabsCtrl',
  1171. bindToController: true
  1172. };
  1173. }
  1174. MdTabsDummyWrapper['$inject'] = ["$mdUtil", "$window"];angular
  1175. .module('material.components.tabs')
  1176. .directive('mdTabsDummyWrapper', MdTabsDummyWrapper);
  1177. /**
  1178. * @private
  1179. *
  1180. * @param $mdUtil
  1181. * @param $window
  1182. * @returns {{require: string, link: link}}
  1183. * @constructor
  1184. *
  1185. * ngInject
  1186. */
  1187. function MdTabsDummyWrapper ($mdUtil, $window) {
  1188. return {
  1189. require: '^?mdTabs',
  1190. link: function link (scope, element, attr, ctrl) {
  1191. if (!ctrl) return;
  1192. var observer;
  1193. var disconnect;
  1194. var mutationCallback = function() {
  1195. ctrl.updatePagination();
  1196. ctrl.updateInkBarStyles();
  1197. };
  1198. if('MutationObserver' in $window) {
  1199. var config = {
  1200. childList: true,
  1201. subtree: true,
  1202. // Per https://bugzilla.mozilla.org/show_bug.cgi?id=1138368, browsers will not fire
  1203. // the childList mutation, once a <span> element's innerText changes.
  1204. // The characterData of the <span> element will change.
  1205. characterData: true
  1206. };
  1207. observer = new MutationObserver(mutationCallback);
  1208. observer.observe(element[0], config);
  1209. disconnect = observer.disconnect.bind(observer);
  1210. } else {
  1211. var debounced = $mdUtil.debounce(mutationCallback, 15, null, false);
  1212. element.on('DOMSubtreeModified', debounced);
  1213. disconnect = element.off.bind(element, 'DOMSubtreeModified', debounced);
  1214. }
  1215. // Disconnect the observer
  1216. scope.$on('$destroy', function() {
  1217. disconnect();
  1218. });
  1219. }
  1220. };
  1221. }
  1222. MdTabsTemplate['$inject'] = ["$compile", "$mdUtil"];angular
  1223. .module('material.components.tabs')
  1224. .directive('mdTabsTemplate', MdTabsTemplate);
  1225. function MdTabsTemplate ($compile, $mdUtil) {
  1226. return {
  1227. restrict: 'A',
  1228. link: link,
  1229. scope: {
  1230. template: '=mdTabsTemplate',
  1231. connected: '=?mdConnectedIf',
  1232. compileScope: '=mdScope'
  1233. },
  1234. require: '^?mdTabs'
  1235. };
  1236. function link (scope, element, attr, ctrl) {
  1237. if (!ctrl) return;
  1238. var compileScope = ctrl.enableDisconnect ? scope.compileScope.$new() : scope.compileScope;
  1239. element.html(scope.template);
  1240. $compile(element.contents())(compileScope);
  1241. return $mdUtil.nextTick(handleScope);
  1242. function handleScope () {
  1243. scope.$watch('connected', function (value) { value === false ? disconnect() : reconnect(); });
  1244. scope.$on('$destroy', reconnect);
  1245. }
  1246. function disconnect () {
  1247. if (ctrl.enableDisconnect) $mdUtil.disconnectScope(compileScope);
  1248. }
  1249. function reconnect () {
  1250. if (ctrl.enableDisconnect) $mdUtil.reconnectScope(compileScope);
  1251. }
  1252. }
  1253. }
  1254. })(window, window.angular);