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