Skip to content

Commit

Permalink
Fixing 'is-active' for equal routes, sibling and ancestor routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpocklin committed Jul 4, 2017
1 parent db413f4 commit 692f35c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "4.1"
- "6.10.3"
before_script:
- npm install -g bower
- bower install
Expand Down
34 changes: 31 additions & 3 deletions src/ui-router-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ angular.module('ui.router.tabs').directive(
}

var currentStateEqualTo = function(tab) {

var isEqual = $state.is(tab.route, tab.params, tab.options);
return isEqual;
};
Expand All @@ -76,11 +75,39 @@ angular.module('ui.router.tabs').directive(
}
};

$scope.is_sibling = function(tab) {
var isSiblingOfCurrentRoute = false;

if (tab && tab.route && tab.route.indexOf('.') > -1) {
var previousRoutePath = $state.current.name.split('.');
var currentRoutePath = tab.route.split('.');
// console.log('old:', previousRoutePath);
// console.log('new:', currentRoutePath);
var isSibling = true;
for (var i = 0; i < currentRoutePath.length - 2; i++) {
if (previousRoutePath[i] !== currentRoutePath[i]) {
isSibling = false;
}
}
isSiblingOfCurrentRoute = isSibling;
}

return isSiblingOfCurrentRoute;
};

/* whether to highlight given route as part of the current state */
$scope.is_active = function(tab) {

var isAncestorOfCurrentRoute = $state.includes(tab.route, tab.params, tab.options);
return isAncestorOfCurrentRoute;
// console.log('existing route:', $state.current.name);
// console.log('new route:', tab);

var isAncestorOfCurrentRoute = !!$state.includes(tab.route, tab.params, tab.options);
var isSiblingOfCurrentRoute = $scope.is_sibling(tab);

// console.log('is ancestor:', isAncestorOfCurrentRoute);
// console.log('is sibling:', isSiblingOfCurrentRoute);
// console.log('is equal:', currentStateEqualTo(tab));
return isAncestorOfCurrentRoute || isSiblingOfCurrentRoute || currentStateEqualTo(tab);
};

$scope.update_tabs = function() {
Expand All @@ -92,6 +119,7 @@ angular.module('ui.router.tabs').directive(
tab.class = tab.class || '';

tab.active = $scope.is_active(tab);

if (tab.active) {
$scope.tabs.active = index;
}
Expand Down
50 changes: 50 additions & 0 deletions src/ui-router-tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,54 @@ describe('Directive : UI Router : Tabs', function() {
expect(get_current_state()).toEqual(previous_state);
expect(spy.notCalled).toBeTruthy();
});

describe('is active', function() {
it('should highlight the active tab for the same route', function() {

var tabIndex = 0;
renderView();

$ngView.find('a').eq(tabIndex).click();

scope.$apply();
spy.reset();

expect(directive_scope.is_active(scope.tabConfiguration[tabIndex])).toBeTruthy();
});

it('should highlight the active tab for sibling routes', function() {

var firstTabIndex = 1;
renderView();

$ngView.find('a').eq(firstTabIndex).click();

scope.$apply();
spy.reset();

var secondTabIndex = 2;

$ngView.find('a').eq(secondTabIndex).click();
scope.$apply();

expect(directive_scope.is_active(scope.tabConfiguration[firstTabIndex])).toBeTruthy();
});
it('should highlight the active tab for common ancestor routes', function() {

var firstTabIndex = 0;
renderView();

$ngView.find('a').eq(firstTabIndex).click();

scope.$apply();
spy.reset();

var secondTabIndex = 2;

$ngView.find('a').eq(secondTabIndex).click();
scope.$apply();

expect(directive_scope.is_active(scope.tabConfiguration[firstTabIndex])).toBeTruthy();
});
});
});

0 comments on commit 692f35c

Please sign in to comment.