Skip to content

Commit 8354d42

Browse files
committed
feat(ionNavAnimation): <a href="#/page" ion-nav-animation="slide-in-up">
1 parent 4c996c7 commit 8354d42

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
angular.module('ionic.ui.navAnimation', [])
2+
.directive('ionNavAnimation', function() {
3+
return {
4+
restrict: 'A',
5+
require: '^?ionNavView',
6+
link: function($scope, $element, $attrs, navViewCtrl) {
7+
if (!navViewCtrl) {
8+
return;
9+
}
10+
ionic.on('tap', function() {
11+
navViewCtrl.setNextAnimation($attrs.ionNavAnimation);
12+
}, $element[0]);
13+
}
14+
};
15+
});

js/ext/angular/src/directive/ionicViewState.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
295295
};
296296
}])
297297

298-
299298
.directive('ionNavView', ['$ionicViewService', '$state', '$compile', '$controller', '$animate',
300299
function( $ionicViewService, $state, $compile, $controller, $animate) {
301300
// IONIC's fork of Angular UI Router, v0.2.7
@@ -307,9 +306,13 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
307306
terminal: true,
308307
priority: 2000,
309308
transclude: true,
310-
controller: function() {}, //noop controller so this can be required
309+
controller: ['$scope', function($scope) {
310+
this.setNextAnimation = function(anim) {
311+
$scope.$nextAnimation = anim;
312+
};
313+
}],
311314
compile: function (element, attr, transclude) {
312-
return function(scope, element, attr) {
315+
return function(scope, element, attr, navViewCtrl) {
313316
var viewScope, viewLocals,
314317
name = attr[directive.name] || attr.name || '',
315318
onloadExp = attr.onload || '',
@@ -351,7 +354,6 @@ angular.module('ionic.ui.viewState', ['ionic.service.view', 'ionic.service.gestu
351354
if (locals === viewLocals) return; // nothing to do
352355
var renderer = $ionicViewService.getRenderer(element, attr, scope);
353356

354-
355357
// Destroy previous view scope
356358
if (viewScope) {
357359
viewScope.$destroy();

js/ext/angular/src/service/ionicView.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,26 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
384384
var doAnimation;
385385

386386
// climb up the DOM and see which animation classname to use, if any
387-
var animationClass = null;
388-
var el = navViewElement[0];
389-
while(!animationClass && el) {
390-
animationClass = el.getAttribute('animation');
391-
el = el.parentElement;
387+
var animationClass = angular.isDefined(navViewScope.$nextAnimation) ?
388+
navViewScope.$nextAnimation :
389+
getParentAnimationClass(navViewElement[0]);
390+
391+
navViewScope.$nextAnimation = undefined;
392+
393+
function getParentAnimationClass(el) {
394+
var className = '';
395+
while(!className && el) {
396+
className = el.getAttribute('animation');
397+
el = el.parentElement;
398+
}
399+
return className;
392400
}
393-
el = null;
394401

395402
function setAnimationClass() {
396403
// add the animation CSS class we're gonna use to transition between views
397-
navViewElement[0].classList.add(animationClass);
404+
if (animationClass) {
405+
navViewElement[0].classList.add(animationClass);
406+
}
398407

399408
if(registerData.navDirection === 'back') {
400409
// animate like we're moving backward
@@ -421,6 +430,9 @@ angular.module('ionic.service.view', ['ui.router', 'ionic.service.platform'])
421430

422431
$animate.enter(element, navViewElement, null, function() {
423432
document.body.classList.remove('disable-pointer-events');
433+
if (animationClass) {
434+
navViewElement[0].classList.remove(animationClass);
435+
}
424436
});
425437
return;
426438
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
describe('ionNavAnimation directive', function() {
2+
beforeEach(module('ionic.ui.navAnimation'));
3+
4+
var navViewCtrl;
5+
function setup(anim, noNavViewCtrl) {
6+
if (noNavViewCtrl) {
7+
navViewCtrl = null;
8+
} else {
9+
navViewCtrl = {
10+
setNextAnimation: jasmine.createSpy('setNextAnimation')
11+
};
12+
}
13+
var element = angular.element(
14+
'<div ion-nav-animation="'+(anim||'')+'"></div>'
15+
);
16+
element.data('$ionNavViewController', navViewCtrl);
17+
inject(function($compile, $rootScope) {
18+
$compile(element)($rootScope.$new());
19+
});
20+
21+
return element;
22+
}
23+
24+
it('should not listen for tap if no navViewCtrl', function() {
25+
spyOn(ionic, 'on');
26+
setup('', true);
27+
expect(ionic.on).not.toHaveBeenCalled();
28+
});
29+
30+
it('should listen for tap', function() {
31+
spyOn(ionic, 'on');
32+
var el = setup('');
33+
expect(ionic.on).toHaveBeenCalledWith('tap', jasmine.any(Function), el[0]);
34+
});
35+
36+
it('should call navViewCtrl.setNextAnimation on tap', function() {
37+
var el = setup('foobar');
38+
ionic.trigger('tap', { target: el[0] });
39+
expect(navViewCtrl.setNextAnimation).toHaveBeenCalledWith('foobar');
40+
});
41+
});

js/ext/angular/test/viewState.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ <h3>Information</h3>
140140
<ion-view title="'Auto List'">
141141
<ion-content has-header="true" has-tabs="true">
142142
<ion-list>
143-
<ion-item ng-repeat="auto in autos" ng-href="#/tabs/autos/{{ $index }}">
143+
<ion-item ng-repeat="auto in autos" ng-href="#/tabs/autos/{{ $index }}" ion-nav-animation="{{$index === 0 ? 'slide-in-up' : 'slide-left-right'}}">
144144
{{ auto.year }} {{ auto.make }} {{ auto.model }}
145145
</ion-item>
146146
</ion-list>

0 commit comments

Comments
 (0)