@@ -11,25 +11,53 @@ import angular from 'angular'
11
11
bindToController : true ,
12
12
template : require ( './tc-tabs' ) ( ) ,
13
13
scope : { } ,
14
- controller : [ '$log' , function ( $log , $scope , $element ) {
14
+ controller : [ '$log' , '$location' , '$scope' , function ( $log , $location , $scope , $element ) {
15
15
$log = $log . getInstance ( 'TcTabSetController' )
16
16
this . tabs = [ ]
17
+
17
18
this . addTab = function addTab ( tab ) {
18
19
this . tabs . push ( tab )
19
- if ( this . tabs . length === 1 ) {
20
+
21
+ if ( ! angular . isDefined ( $location . search ( ) . tab ) && this . tabs . length === 1 ) {
22
+ tab . active = true
23
+ } else if ( $location . search ( ) . tab === tab . heading ) {
20
24
tab . active = true
21
25
}
22
26
}
23
27
24
28
this . select = function ( selectedTab ) {
29
+ var select = false
30
+
25
31
angular . forEach ( this . tabs , function ( tab ) {
26
- if ( tab . active && tab !== selectedTab ) {
32
+ if ( tab . active && tab . heading !== selectedTab . heading ) {
27
33
tab . active = false
34
+ } else if ( tab . heading === selectedTab . heading ) {
35
+ tab . active = true
36
+ select = true
28
37
}
29
38
} )
30
39
31
- selectedTab . active = true
40
+ if ( select === false && this . tabs . length > 0 ) {
41
+ this . tabs [ 0 ] . active = true
42
+ }
43
+ }
44
+
45
+ this . setTab = function ( tab ) {
46
+ if ( $location . search ( ) . tab !== tab . heading ) {
47
+ $location . search ( 'tab' , tab . heading )
48
+ }
32
49
}
50
+
51
+ $scope . $on ( '$locationChangeSuccess' , ( ) => {
52
+ var tab
53
+ if ( angular . isDefined ( $location . search ( ) . tab ) ) {
54
+ tab = $location . search ( ) . tab
55
+ this . select ( { 'heading' : tab } )
56
+ } else if ( this . tabs . length > 0 ) {
57
+ tab = this . tabs [ 0 ] . heading
58
+ this . select ( { 'heading' : tab } )
59
+ }
60
+ } )
33
61
} ] ,
34
62
controllerAs : 'tabSet'
35
63
}
0 commit comments