forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionicNavBar.js
479 lines (439 loc) · 13.9 KB
/
ionicNavBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
angular.module('ionic.ui.navBar', ['ionic.service.view', 'ngSanitize'])
/**
* @ngdoc service
* @name $ionicNavBarDelegate
* @module ionic
* @description
* Delegate for controlling the {@link ionic.directive:ionNavBar} directive.
*
* @usage
*
* ```html
* <body ng-controller="MyCtrl">
* <ion-nav-bar>
* <button ng-click="setNavTitle('banana')">
* Set title to banana!
* </button>
* </ion-nav-bar>
* </body>
* ```
* ```js
* function MyCtrl($scope, $ionicNavBarDelegate) {
* $scope.setNavTitle = function(title) {
* $ionicNavBarDelegate.setTitle(title);
* }
* }
* ```
*/
.service('$ionicNavBarDelegate', delegateService([
/**
* @ngdoc method
* @name $ionicNavBarDelegate#back
* @description Goes back in the view history.
* @param {DOMEvent=} event The event object (eg from a tap event)
*/
'back',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#align
* @description Aligns the title with the buttons in a given direction.
* @param {string=} direction The direction to the align the title text towards.
* Available: 'left', 'right', 'center'. Default: 'center'.
*/
'align',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#showBackButton
* @description
* Set whether the {@link ionic.directive:ionNavBackButton} should be shown
* (if it exists).
* @param {boolean} show Whether to show the back button.
*/
'showBackButton',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#showBar
* @description
* Set whether the {@link ionic.directive:ionNavBar} should be shown.
* @param {boolean} show Whether to show the bar.
*/
'showBar',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#setTitle
* @description
* Set the title for the {@link ionic.directive:ionNavBar}.
* @param {string} title The new title to show.
*/
'setTitle',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#changeTitle
* @description
* Change the title, transitioning the new title in and the old one out in a given direction.
* @param {string} title The new title to show.
* @param {string} direction The direction to transition the new title in.
* Available: 'forward', 'back'.
*/
'changeTitle',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#getTitle
* @returns {string} The current title of the navbar.
*/
'getTitle',
/**
* @ngdoc method
* @name $ionicNavBarDelegate#getPreviousTitle
* @returns {string} The previous title of the navbar.
*/
'getPreviousTitle'
/**
* @ngdoc method
* @name $ionicNavBarDelegate#$getByHandle
* @param {string} handle
* @returns `delegateInstance` A delegate instance that controls only the
* navBars with delegate-handle matching the given handle.
*
* Example: `$ionicNavBarDelegate.$getByHandle('myHandle').setTitle('newTitle')`
*/
]))
.controller('$ionicNavBar', [
'$scope',
'$element',
'$attrs',
'$ionicViewService',
'$animate',
'$compile',
'$ionicNavBarDelegate',
function($scope, $element, $attrs, $ionicViewService, $animate, $compile, $ionicNavBarDelegate) {
//Let the parent know about our controller too so that children of
//sibling content elements can know about us
$element.parent().data('$ionNavBarController', this);
var deregisterInstance = $ionicNavBarDelegate._registerInstance(this, $attrs.delegateHandle);
$scope.$on('$destroy', deregisterInstance);
var self = this;
this.leftButtonsElement = angular.element(
$element[0].querySelector('.buttons.left-buttons')
);
this.rightButtonsElement = angular.element(
$element[0].querySelector('.buttons.right-buttons')
);
this.back = function(e) {
var backView = $ionicViewService.getBackView();
backView && backView.go();
e && (e.alreadyHandled = true);
return false;
};
this.align = function(direction) {
this._headerBarView.align(direction);
};
this.showBackButton = function(show) {
$scope.backButtonShown = !!show;
};
this.showBar = function(show) {
$scope.isInvisible = !show;
};
this.setTitle = function(title) {
$scope.oldTitle = $scope.title;
$scope.title = title || '';
};
this.changeTitle = function(title, direction) {
if ($scope.title === title) {
return false;
}
this.setTitle(title);
$scope.isReverse = direction == 'back';
$scope.shouldAnimate = !!direction;
if (!$scope.shouldAnimate) {
//We're done!
this._headerBarView.align();
} else {
this._animateTitles();
}
return true;
};
this.getTitle = function() {
return $scope.title || '';
};
this.getPreviousTitle = function() {
return $scope.oldTitle || '';
};
/**
* Exposed for testing
*/
this._animateTitles = function() {
var oldTitleEl, newTitleEl, currentTitles;
//If we have any title right now
//(or more than one, they could be transitioning on switch),
//replace the first one with an oldTitle element
currentTitles = $element[0].querySelectorAll('.title');
if (currentTitles.length) {
oldTitleEl = $compile('<h1 class="title" ng-bind-html="oldTitle"></h1>')($scope);
angular.element(currentTitles[0]).replaceWith(oldTitleEl);
}
//Compile new title
newTitleEl = $compile('<h1 class="title invisible" ng-bind-html="title"></h1>')($scope);
//Animate in on next frame
ionic.requestAnimationFrame(function() {
oldTitleEl && $animate.leave(angular.element(oldTitleEl));
var insert = oldTitleEl && angular.element(oldTitleEl) || null;
$animate.enter(newTitleEl, $element, insert, function() {
self._headerBarView.align();
});
//Cleanup any old titles leftover (besides the one we already did replaceWith on)
angular.forEach(currentTitles, function(el) {
if (el && el.parentNode) {
//Use .remove() to cleanup things like .data()
angular.element(el).remove();
}
});
//$apply so bindings fire
$scope.$digest();
//Stop flicker of new title on ios7
ionic.requestAnimationFrame(function() {
newTitleEl[0].classList.remove('invisible');
});
});
};
}])
/**
* @ngdoc directive
* @name ionNavBar
* @module ionic
* @delegate ionic.service:$ionicNavBarDelegate
* @restrict E
*
* @description
* If we have an {@link ionic.directive:ionNavView} directive, we can also create an
* `<ion-nav-bar>`, which will create a topbar that updates as the application state changes.
*
* We can add a back button by putting an {@link ionic.directive:ionNavBackButton} inside.
*
* We can add buttons depending on the currently visible view using
* {@link ionic.directive:ionNavButtons}.
*
* Assign an [animation class](/docs/components#animations) to the element to
* enable animated changing of titles (recommended: 'slide-left-right' or 'nav-title-slide-ios7')
*
* @usage
*
* ```html
* <body ng-app="starter">
* <!-- The nav bar that will be updated as we navigate -->
* <ion-nav-bar class="bar-positive nav-title-slide-ios7">
* </ion-nav-bar>
*
* <!-- where the initial view template will be rendered -->
* <ion-nav-view></ion-nav-view>
* </body>
* ```
*
* @param {string=} delegate-handle The handle used to identify this navBar
* with {@link ionic.service:$ionicNavBarDelegate}.
* @param align-title {string=} Where to align the title of the navbar.
* Available: 'left', 'right', 'center'. Defaults to 'center'.
*/
.directive('ionNavBar', ['$ionicViewService', '$rootScope', '$animate', '$compile',
function($ionicViewService, $rootScope, $animate, $compile) {
return {
restrict: 'E',
controller: '$ionicNavBar',
scope: true,
compile: function(tElement, tAttrs) {
//We cannot transclude here because it breaks element.data() inheritance on compile
tElement
.addClass('bar bar-header nav-bar')
.append(
'<div class="buttons left-buttons"> ' +
'</div>' +
'<h1 ng-bind-html="title" class="title"></h1>' +
'<div class="buttons right-buttons"> ' +
'</div>'
);
return { pre: prelink };
function prelink($scope, $element, $attr, navBarCtrl) {
navBarCtrl._headerBarView = new ionic.views.HeaderBar({
el: $element[0],
alignTitle: $attr.alignTitle || 'center'
});
//defaults
$scope.backButtonShown = false;
$scope.shouldAnimate = true;
$scope.isReverse = false;
$scope.isInvisible = true;
$scope.$parent.$hasHeader = true;
$scope.$on('$destroy', function() {
$scope.$parent.$hasHeader = false;
});
$scope.$watch(function() {
return ($scope.isReverse ? ' reverse' : '') +
($scope.isInvisible ? ' invisible' : '') +
(!$scope.shouldAnimate ? ' no-animation' : '');
}, function(className, oldClassName) {
$element.removeClass(oldClassName);
$element.addClass(className);
});
}
}
};
}])
/**
* @ngdoc directive
* @name ionNavBackButton
* @module ionic
* @restrict E
* @parent ionNavBar
* @description
* Creates a back button inside an {@link ionic.directive:ionNavBar}.
*
* Will show up when the user is able to go back in the current navigation stack.
*
* By default, will go back when clicked. If you wish for more advanced behavior, see the
* examples below.
*
* @usage
*
* With default click action:
*
* ```html
* <ion-nav-bar>
* <ion-nav-back-button class="button-icon">
* <i class="ion-arrow-left-c"></i> Back!
* </ion-nav-back-button>
* </ion-nav-bar>
* ```
*
* With custom click action, using {@link ionic.service:$ionicNavBarDelegate}:
*
* ```html
* <ion-nav-bar ng-controller="MyCtrl">
* <ion-nav-back-button class="button-icon"
* ng-click="canGoBack && goBack()">
* <i class="ion-arrow-left-c"></i> Back
* </ion-nav-back-button>
* </ion-nav-bar>
* ```
* ```js
* function MyCtrl($scope, $ionicNavBarDelegate) {
* $scope.goBack = function() {
* $ionicNavBarDelegate.back();
* };
* }
* ```
*
* Displaying the previous title on the back button, again using
* {@link ionic.service:$ionicNavBarDelegate}.
*
* ```html
* <ion-nav-bar ng-controller="MyCtrl">
* <ion-nav-back-button class="button button-icon ion-arrow-left-c">
* {% raw %}{{getPreviousTitle() || 'Back'}}{% endraw %}
* </ion-nav-back-button>
* </ion-nav-bar>
* ```
* ```js
* function MyCtrl($scope, $ionicNavBarDelegate) {
* $scope.getPreviousTitle = function() {
* return $ionicNavBarDelegate.getPreviousTitle();
* };
* }
* ```
*/
.directive('ionNavBackButton', ['$ionicNgClick', function($ionicNgClick) {
return {
restrict: 'E',
require: '^ionNavBar',
compile: function(tElement, tAttrs) {
tElement.addClass('button back-button');
return function($scope, $element, $attr, navBarCtrl) {
if (!$attr.ngClick) {
$scope.$navBack = navBarCtrl.back;
$ionicNgClick($scope, $element, '$navBack($event)');
}
//If the current viewstate does not allow a back button,
//always hide it.
var deregisterListener = $scope.$parent.$on(
'$viewHistory.historyChange',
function(e, data) {
$scope.hasBackButton = !!data.showBack;
}
);
$scope.$on('$destroy', deregisterListener);
//Make sure both that a backButton is allowed in the first place,
//and that it is shown by the current view.
$scope.$watch('!!(backButtonShown && hasBackButton)', function(show) {
$element.toggleClass('hide', !show);
});
};
}
};
}])
/**
* @ngdoc directive
* @name ionNavButtons
* @module ionic
* @restrict E
* @parent ionNavView
*
* @description
* Use ionNavButtons to set the buttons on your {@link ionic.directive:ionNavBar}
* from within an {@link ionic.directive:ionView}.
*
* Any buttons you declare will be placed onto the navbar's corresponding side,
* and then destroyed when the user leaves their parent view.
*
* @usage
* ```html
* <ion-nav-bar>
* </ion-nav-bar>
* <ion-nav-view>
* <ion-view>
* <ion-nav-buttons side="left">
* <button class="button" ng-click="doSomething()">
* I'm a button on the left of the navbar!
* </button>
* </ion-nav-buttons>
* <ion-content>
* Some super content here!
* </ion-content>
* </ion-view>
* </ion-nav-view>
* ```
*
* @param {string} side The side to place the buttons on in the parent
* {@link ionic.directive:ionNavBar}. Available: 'left' or 'right'.
*/
.directive('ionNavButtons', ['$compile', '$animate', function($compile, $animate) {
return {
require: '^ionNavBar',
restrict: 'E',
compile: function($element, $attrs) {
var content = $element.contents().remove();
return function($scope, $element, $attrs, navBarCtrl) {
var navElement = $attrs.side === 'right' ?
navBarCtrl.rightButtonsElement :
navBarCtrl.leftButtonsElement;
//Put all of our inside buttons into their own div,
//so we can remove them all when this element dies -
//even if the buttons have changed through an ng-repeat or the like,
//we just remove their div parent and they are gone.
var buttons = angular.element('<div>').append(content);
//Compile buttons inside content so they have access to everything
//something inside content does (eg parent ionicScroll)
$element.append(buttons);
$compile(buttons)($scope);
//Append buttons to navbar
$animate.enter(buttons, navElement);
//When our ion-nav-buttons container is destroyed,
//destroy everything in the navbar
$scope.$on('$destroy', function() {
$animate.leave(buttons);
});
// The original element is just a completely empty <ion-nav-buttons> element.
// make it invisible just to be sure it doesn't change any layout
$element.css('display', 'none');
};
}
};
}]);