@@ -11,25 +11,50 @@ 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
+ var tabCtrl = this
16
17
this . tabs = [ ]
17
18
this . addTab = function addTab ( tab ) {
18
19
this . tabs . push ( tab )
19
- if ( this . tabs . length === 1 ) {
20
+ if ( ! angular . isDefined ( $location . search ( ) . tab ) && this . tabs . length === 1 ) {
21
+ tab . active = true
22
+ } else if ( $location . search ( ) . tab === tab . heading ) {
20
23
tab . active = true
21
24
}
22
25
}
23
26
24
27
this . select = function ( selectedTab ) {
28
+ var select = false
25
29
angular . forEach ( this . tabs , function ( tab ) {
26
- if ( tab . active && tab !== selectedTab ) {
30
+ if ( tab . active && tab . heading !== selectedTab . heading ) {
27
31
tab . active = false
32
+ } else if ( tab . heading === selectedTab . heading ) {
33
+ tab . active = true
34
+ select = true
28
35
}
29
36
} )
37
+ if ( select === false && this . tabs . length > 0 ) {
38
+ this . tabs [ 0 ] . active = true
39
+ }
40
+ }
30
41
31
- selectedTab . active = true
42
+ this . setTab = function ( tab ) {
43
+ if ( $location . search ( ) . tab !== tab . heading ) {
44
+ $location . search ( 'tab' , tab . heading )
45
+ }
32
46
}
47
+
48
+ $scope . $on ( '$locationChangeSuccess' , function ( ) {
49
+ var tab
50
+ if ( angular . isDefined ( $location . search ( ) . tab ) ) {
51
+ tab = $location . search ( ) . tab
52
+ tabCtrl . select ( { 'heading' : tab } )
53
+ } else if ( tabCtrl . tabs . length > 0 ) {
54
+ tab = tabCtrl . tabs [ 0 ] . heading
55
+ tabCtrl . select ( { 'heading' : tab } )
56
+ }
57
+ } )
33
58
} ] ,
34
59
controllerAs : 'tabSet'
35
60
}
0 commit comments