Skip to content

Commit

Permalink
feat(ionNavBar): make back button animated
Browse files Browse the repository at this point in the history
Closes #1030
  • Loading branch information
ajoslin committed Apr 3, 2014
1 parent f2d52ea commit 9725793
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
14 changes: 9 additions & 5 deletions js/ext/angular/src/directive/ionicNavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,15 @@ function($ionicViewService, $rootScope, $animate, $compile) {
* }
* ```
*/
.directive('ionNavBackButton', ['$ionicNgClick', function($ionicNgClick) {
.directive('ionNavBackButton', [
'$ionicNgClick',
'$animate',
function($ionicNgClick, $animate) {
return {
restrict: 'E',
require: '^ionNavBar',
compile: function(tElement, tAttrs) {
tElement.addClass('button back-button');
tElement.addClass('button back-button ng-hide');
return function($scope, $element, $attr, navBarCtrl) {
if (!$attr.ngClick) {
$scope.$navBack = navBarCtrl.back;
Expand All @@ -396,9 +399,10 @@ function($ionicViewService, $rootScope, $animate, $compile) {

//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);
});
$scope.$watch('!!(backButtonShown && hasBackButton)', ionic.animationFrameThrottle(function(show) {
if (show) $animate.removeClass($element, 'ng-hide');
else $animate.addClass($element, 'ng-hide');
}));
};
}
};
Expand Down
15 changes: 8 additions & 7 deletions js/ext/angular/test/directive/ionicNavBackButton.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe('ionNavBackButton directive', function() {
beforeEach(module('ionic', function($compileProvider) {
$compileProvider.directive('needsScroll', function() {
return {
//Test if the buttons are 'children of ionScroll' when compiled
//Test if the buttons are 'children of ionScroll' when compiled
require: '^$ionicScroll',
link: function(scope, element, attrs, ctrl) {
element.data('scrollCtrl', ctrl);
Expand All @@ -24,7 +24,7 @@ describe('ionNavBackButton directive', function() {
return el;
}

it('should compile buttons with same scope & access the same data on compile', inject(function($compile, $rootScope) {
it('ionNavButtons should compile buttons with same scope & access the same data on compile', inject(function($compile, $rootScope) {
var el = $compile('<div>' +
'<ion-nav-bar></ion-nav-bar>' +
'<ion-view>' +
Expand Down Expand Up @@ -66,16 +66,17 @@ describe('ionNavBackButton directive', function() {
});

it('should hide based on backButtonShown && hasBackButton', function() {
ionic.animationFrameThrottle = function(cb) { return cb; };
var el = setup();
expect(el.hasClass('hide')).toBe(true);
expect(el.hasClass('ng-hide')).toBe(true);
el.scope().$apply('backButtonShown = true; hasBackButton = true');
expect(el.hasClass('hide')).toBe(false);
expect(el.hasClass('ng-hide')).toBe(false);
el.scope().$apply('backButtonShown = false; hasBackButton = true');
expect(el.hasClass('hide')).toBe(true);
expect(el.hasClass('ng-hide')).toBe(true);
el.scope().$apply('backButtonShown = true; hasBackButton = false');
expect(el.hasClass('hide')).toBe(true);
expect(el.hasClass('ng-hide')).toBe(true);
el.scope().$apply('backButtonShown = true; hasBackButton = true');
expect(el.hasClass('hide')).toBe(false);
expect(el.hasClass('ng-hide')).toBe(false);
});

it('should transclude content', function() {
Expand Down
4 changes: 3 additions & 1 deletion js/ext/angular/test/service/ionicActivator.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
describe('Ionic Element Activator', function() {

window.setTimeout = ionic.requestAnimationFrame = function(cb) { cb(); };
beforeEach(function() {
window.setTimeout = ionic.requestAnimationFrame = function(cb) { cb(); };
});

it('Should activate an <a>', function() {
var e = { target: document.createElement('a') };
Expand Down
16 changes: 16 additions & 0 deletions scss/_animations.scss
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,27 @@ $ios7-transition-duration: 250ms;
}



/**
* Some component specific animations
*/
$nav-title-slide-ios7-delay: 250ms;
.nav-title-slide-ios7 {
.button.back-button {
@include transition(all $nav-title-slide-ios7-delay);
@include transition-timing-function($ios7-timing-function);

@include translate3d(0%, 0, 0);
opacity: 1;
&.ng-hide {
opacity: 0;
@include translate3d(30%, 0, 0);
}
&.ng-hide-add,
&.ng-hide-remove {
display: block !important;
}
}
> .ng-enter, &.ng-enter,
> .ng-leave, &.ng-leave {
@include transition(all $nav-title-slide-ios7-delay);
Expand Down

3 comments on commit 9725793

@hollingberry
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but not quite right. Back button shouldn't animate when changing tabs.

See http://codepen.io/hollingberry/pen/afcIk.

@ajoslin
Copy link
Contributor Author

@ajoslin ajoslin commented on 9725793 Apr 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @hollingberry! Fixed via 6b5083b

@hollingberry
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Please sign in to comment.