Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 52b32f1

Browse files
committed
modified tabs to work with browser history navigation - Issue 673
1 parent aa7a63e commit 52b32f1

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

app/directives/tc-tabs/tc-tabs.directive.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,49 @@ import angular from 'angular'
1111
bindToController: true,
1212
template: require('./tc-tabs')(),
1313
scope: {},
14-
controller: ['$log', function($log, $scope, $element) {
14+
controller: ['$log', '$location', '$scope', function($log, $location, $scope, $element) {
1515
$log = $log.getInstance('TcTabSetController')
16+
var tabCtrl = this;
1617
this.tabs = []
1718
this.addTab = function addTab(tab) {
1819
this.tabs.push(tab)
19-
if (this.tabs.length === 1) {
20-
tab.active = true
20+
if (!angular.isDefined($location.search().tab) && this.tabs.length === 1) {
21+
tab.active = true;
22+
} else if ($location.search().tab === tab.heading) {
23+
tab.active = true;
2124
}
2225
}
2326

2427
this.select = function(selectedTab) {
28+
var select = false;
2529
angular.forEach(this.tabs, function(tab) {
26-
if (tab.active && tab !== selectedTab) {
27-
tab.active = false
30+
if (tab.active && tab.heading !== selectedTab.heading) {
31+
tab.active = false;
32+
} else if (tab.heading === selectedTab.heading) {
33+
tab.active = true;
34+
select = true;
2835
}
29-
})
36+
});
37+
if (select === false && this.tabs.length > 0) {
38+
this.tabs[0].active = true;
39+
}
40+
}
3041

31-
selectedTab.active = true
42+
this.setTab = function(tab) {
43+
if ($location.search().tab !== tab.heading) {
44+
$location.search('tab', tab.heading);
45+
}
3246
}
47+
48+
$scope.$on( "$locationChangeSuccess", function() {
49+
if (angular.isDefined($location.search().tab)) {
50+
var tab = $location.search().tab;
51+
tabCtrl.select({'heading' : tab});
52+
} else if (tabCtrl.tabs.length > 0) {
53+
var tab = tabCtrl.tabs[0].heading;
54+
tabCtrl.select({'heading' : tab});
55+
}
56+
});
3357
}],
3458
controllerAs: 'tabSet'
3559
}

app/directives/tc-tabs/tc-tabs.jade

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.tab-panel
2-
2+
33
ul.tab-set
44
li.tab-pill(ng-repeat="tab in tabSet.tabs")
5-
a(ng-click="tabSet.select(tab)", ng-class="{'selected': tab.active}")
5+
a(ng-click="tabSet.setTab(tab)", ng-class="{'selected': tab.active}")
66
.header(ng-class="{'selected': tab.active}") {{tab.heading}}
77

88
.tab-view(ng-transclude)

0 commit comments

Comments
 (0)