From 5bc3f232f1850d80ec3dbf9bb7fbcf93b173f8fc Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Wed, 6 May 2015 14:38:56 -0700 Subject: [PATCH] fix(tabs): reordering tabs should now work --- src/components/tabs/js/tabDirective.js | 9 +++++++++ src/components/tabs/js/tabsController.js | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/components/tabs/js/tabDirective.js b/src/components/tabs/js/tabDirective.js index f9df4400692..e10a5146333 100644 --- a/src/components/tabs/js/tabDirective.js +++ b/src/components/tabs/js/tabDirective.js @@ -86,6 +86,15 @@ function MdTab () { scope.$watch('active', function (active) { if (active) ctrl.select(data.getIndex()); }); scope.$watch('disabled', function () { ctrl.refreshIndex(); }); + scope.$watch( + function () { + var tabs = element.parent()[0].getElementsByTagName('md-tab'); + return Array.prototype.indexOf.call(tabs, element[0]); + }, + function (newIndex) { + data.index = newIndex; + ctrl.updateTabOrder(); + }); scope.$on('$destroy', function () { ctrl.removeTab(data); }); function getLabel () { diff --git a/src/components/tabs/js/tabsController.js b/src/components/tabs/js/tabsController.js index bf1436be995..90cf0f67ccf 100644 --- a/src/components/tabs/js/tabsController.js +++ b/src/components/tabs/js/tabsController.js @@ -17,7 +17,7 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md ctrl.offsetLeft = 0; ctrl.hasContent = false; ctrl.hasFocus = false; - ctrl.lastClick = false; + ctrl.lastClick = true; ctrl.redirectFocus = redirectFocus; ctrl.attachRipple = attachRipple; @@ -36,6 +36,7 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md ctrl.refreshIndex = refreshIndex; ctrl.incrementSelectedIndex = incrementSelectedIndex; ctrl.updateInkBarStyles = updateInkBarStyles; + ctrl.updateTabOrder = $mdUtil.debounce(updateTabOrder, 100); init(); @@ -90,6 +91,17 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md ctrl.lastClick = false; } + function updateTabOrder () { + var selectedItem = ctrl.tabs[$scope.selectedIndex], + focusItem = ctrl.tabs[ctrl.focusIndex]; + ctrl.tabs = ctrl.tabs.sort(function (a, b) { + return a.index - b.index; + }); + $scope.selectedIndex = ctrl.tabs.indexOf(selectedItem); + ctrl.focusIndex = ctrl.tabs.indexOf(focusItem); + $timeout(updateInkBarStyles, 0, false); + } + function incrementSelectedIndex (inc, focus) { var newIndex, index = focus ? ctrl.focusIndex : $scope.selectedIndex;