@@ -11,25 +11,49 @@ 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
- 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 ;
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 ) {
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 ;
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
+ 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
+ } ) ;
33
57
} ] ,
34
58
controllerAs : 'tabSet'
35
59
}
0 commit comments