@@ -23,7 +23,7 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
23
23
* Our Nav Bar directive which updates as the controller state changes.
24
24
*/
25
25
. directive ( 'ionNavBar' , [ '$ionicViewService' , '$rootScope' , '$animate' , '$compile' ,
26
- function ( $ionicViewService , $rootScope , $animate , $compile ) {
26
+ function ( $ionicViewService , $rootScope , $animate , $compile ) {
27
27
28
28
return {
29
29
restrict : 'E' ,
@@ -38,36 +38,41 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
38
38
} ,
39
39
controller : function ( ) { } ,
40
40
template :
41
- '<header class="bar bar-header nav-bar {{type}} {{isReverse ? \'reverse\' : \'\'}} ' +
42
- '{{isInvisible ? \'invisible\' : \'\'}} {{animateEnabled ? animation : \'\'}}"> ' +
43
- '<ion-nav-back-button ng-if="backButtonEnabled && ( backType || backLabel || backIcon) " ' +
44
- 'type="backType" label="backLabel" icon="backIcon" class="invisible" ion-async-visible >' +
41
+ '<header class="bar bar-header nav-bar{{navBarClass()}}"> ' +
42
+ '<ion-nav-back-button ng-if="(backType || backLabel || backIcon)" ' +
43
+ 'type=" backType" label=" backLabel" icon=" backIcon" class="hide " ' +
44
+ 'ng- class="{hide: !backButtonEnabled}" >' +
45
45
'</ion-nav-back-button>' +
46
46
'<div class="buttons left-buttons"> ' +
47
47
'<button ng-click="button.tap($event)" ng-repeat="button in leftButtons" ' +
48
48
'class="button no-animation {{button.type}}" ng-bind-html="button.content">' +
49
49
'</button>' +
50
50
'</div>' +
51
51
52
- //ng-repeat makes it easy to add new / remove old and have proper enter/leave anims
53
- '<h1 ng-repeat="title in titles" ng-bind-html="title" class="title invisible" ion-async-visible ion-nav-bar-title></h1>' +
52
+ '<h1 ng-bind-html="title" class="title"></h1>' +
54
53
55
- '<div class="buttons right-buttons" ng-if="rightButtons.length" > ' +
56
- '<button ng-click="button.tap($event)" ng-repeat="button in rightButtons" ' +
57
- 'class="button no-animation {{button.type}}" ng-bind-html="button.content">' +
54
+ '<div class="buttons right-buttons"> ' +
55
+ '<button ng-click="button.tap($event)" ng-repeat="button in rightButtons" ' +
56
+ 'class="button no-animation {{button.type}}" ng-bind-html="button.content">' +
58
57
'</button>' +
59
58
'</div>' +
60
59
'</header>' ,
61
60
compile : function ( tElement , tAttrs ) {
62
61
63
62
return function link ( $scope , $element , $attr ) {
64
- $scope . titles = [ ] ;
65
63
//defaults
66
- $scope . backButtonEnabled = true ;
64
+ $scope . backButtonEnabled = false ;
67
65
$scope . animateEnabled = true ;
68
66
$scope . isReverse = false ;
69
67
$scope . isInvisible = true ;
70
68
69
+ $scope . navBarClass = function ( ) {
70
+ return ( $scope . type ? ' ' + $scope . type : '' ) +
71
+ ( $scope . isReverse ? ' reverse' : '' ) +
72
+ ( $scope . isInvisible ? ' invisible' : '' ) +
73
+ ( ! $scope . animationDisabled && $scope . animation ? ' ' + $scope . animation : '' ) ;
74
+ } ;
75
+
71
76
// Initialize our header bar view which will handle
72
77
// resizing and aligning our title labels
73
78
var hb = new ionic . views . HeaderBar ( {
@@ -100,7 +105,7 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
100
105
$scope . backButtonEnabled = ! ! data ;
101
106
} ) ,
102
107
$scope . $parent . $on ( 'viewState.titleUpdated' , function ( e , data ) {
103
- $scope . titles [ $scope . titles . length - 1 ] = data && data . title || '' ;
108
+ $scope . title = data && data . title || '' ;
104
109
} )
105
110
] ;
106
111
$scope . $on ( '$destroy' , function ( ) {
@@ -109,19 +114,66 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
109
114
} ) ;
110
115
111
116
function updateHeaderData ( data ) {
112
- var newTitle = data && data . title || '' ;
113
117
114
- $scope . isReverse = data . navDirection == 'back' ;
115
-
116
- if ( data . hideBackButton ) {
117
- $scope . backButtonEnabled = false ;
118
+ if ( angular . isDefined ( data . hideBackButton ) ) {
119
+ $scope . backButtonEnabled = ! ! data . hideBackButton ;
118
120
}
119
-
121
+ $scope . isReverse = data . navDirection == 'back' ;
120
122
$scope . animateEnabled = ! ! ( data . navDirection && data . animate !== false ) ;
121
- $scope . titles . length = 0 ;
122
- $scope . titles . push ( newTitle ) ;
123
+
123
124
$scope . leftButtons = data . leftButtons ;
124
125
$scope . rightButtons = data . rightButtons ;
126
+ $scope . oldTitle = $scope . title ;
127
+ $scope . title = data && data . title || '' ;
128
+
129
+ //If no animation, we're done!
130
+ if ( ! $scope . animateEnabled ) {
131
+ hb . align ( ) ;
132
+ return ;
133
+ } else {
134
+ animateTitles ( ) ;
135
+ }
136
+ }
137
+
138
+ function animateTitles ( ) {
139
+ var oldTitleEl , newTitleEl , currentTitles ;
140
+
141
+ //If we have any title right now (or more than one, they could be transitioning on switch),
142
+ //replace the first one with an oldTitle element
143
+ currentTitles = $element [ 0 ] . querySelectorAll ( '.title' ) ;
144
+ if ( currentTitles . length ) {
145
+ oldTitleEl = $compile ( '<h1 ng-bind-html="oldTitle" class="title"></h1>' ) ( $scope ) ;
146
+ angular . element ( currentTitles [ 0 ] ) . replaceWith ( oldTitleEl ) ;
147
+ }
148
+ //Compile new title
149
+ newTitleEl = $compile ( '<h1 class="title invisible" ng-bind-html="title"></h1>' ) ( $scope ) ;
150
+
151
+ //Animate in one frame
152
+ ionic . requestAnimationFrame ( function ( ) {
153
+
154
+ oldTitleEl && $animate . leave ( angular . element ( oldTitleEl ) ) ;
155
+
156
+ var insert = oldTitleEl && angular . element ( oldTitleEl ) || null ;
157
+ $animate . enter ( newTitleEl , $element , insert , function ( ) {
158
+ hb . align ( ) ;
159
+ } ) ;
160
+
161
+ //Cleanup any old titles leftover (besides the one we already did replaceWith on)
162
+ angular . forEach ( currentTitles , function ( el ) {
163
+ if ( el && el . parentNode ) {
164
+ //Use .remove() to cleanup things like .data()
165
+ angular . element ( el ) . remove ( ) ;
166
+ }
167
+ } ) ;
168
+
169
+ //$apply so bindings fire
170
+ $scope . $digest ( ) ;
171
+
172
+ //Stop flicker of new title on ios7
173
+ ionic . requestAnimationFrame ( function ( ) {
174
+ newTitleEl [ 0 ] . classList . remove ( 'invisible' ) ;
175
+ } ) ;
176
+ } ) ;
125
177
}
126
178
} ;
127
179
}
0 commit comments