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.

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