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