tabs.js 41 KB

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