Skip to content

Commit e606878

Browse files
committed
refactor(transitions): created Transition class
Created interfaces for NavOptions, AnimationOptions and TransitionOptions Created Transition class and move transition ts files to their own folder.
1 parent 7c10c4d commit e606878

File tree

10 files changed

+265
-177
lines changed

10 files changed

+265
-177
lines changed

ionic/animations/animation.ts

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {ViewController} from '../components/nav/view-controller';
21
import {CSS, rafFrames, raf, transitionEnd} from '../util/dom';
3-
import {assign} from '../util/util';
2+
import {assign, isDefined} from '../util/util';
43

54

65
/**
@@ -25,12 +24,14 @@ export class Animation {
2524
private _fOnceFns: Array<Function>;
2625
private _wChg: boolean = false;
2726
private _rv: boolean;
27+
private _unregTrans: Function;
28+
private _tmr;
2829

2930
public isPlaying: boolean;
3031
public hasTween: boolean;
3132
public meta;
3233

33-
constructor(ele?, opts={}) {
34+
constructor(ele?, opts: AnimationOptions = {}) {
3435
this._reset();
3536
this.element(ele);
3637

@@ -54,6 +55,8 @@ export class Animation {
5455
this._fFns = [];
5556
this._fOnceFns = [];
5657

58+
this._clearAsync();
59+
5760
this.isPlaying = this.hasTween = this._rv = false;
5861
this._el = this._easing = this._dur = null;
5962
}
@@ -106,11 +109,11 @@ export class Animation {
106109
return this;
107110
}
108111

109-
from(prop: string, val: string): Animation {
112+
from(prop: string, val): Animation {
110113
return this._addProp('from', prop, val);
111114
}
112115

113-
to(prop: string, val: string): Animation {
116+
to(prop: string, val): Animation {
114117
return this._addProp('to', prop, val);
115118
}
116119

@@ -196,12 +199,12 @@ export class Animation {
196199
}
197200
}
198201

199-
play() {
202+
play(opts: PlayOptions = {}) {
200203
var self = this;
204+
var i;
205+
var duration = isDefined(opts.duration) ? opts.duration : self._dur;
201206

202-
var i, fallbackTimerId, deregTransEnd;
203-
204-
console.debug('Animation, play, duration', self._dur, 'easing', self._easing);
207+
console.debug('Animation, play, duration', duration, 'easing', self._easing);
205208

206209
// always default that an animation does not tween
207210
// a tween requires that an Animation class has an element
@@ -226,7 +229,10 @@ export class Animation {
226229
// will recursively stage all child elements
227230
self._before();
228231

229-
if (self._dur > 30) {
232+
// ensure all past transition end events have been cleared
233+
this._clearAsync();
234+
235+
if (duration > 30) {
230236
// this animation has a duration, so it should animate
231237
// place all the elements with their FROM properties
232238

@@ -235,17 +241,17 @@ export class Animation {
235241

236242
self._willChange(true);
237243

238-
// set the TRANSITION END event
244+
// set the async TRANSITION END event
239245
// and run onFinishes when the transition ends
240-
self._asyncEnd(self._dur);
246+
self._asyncEnd(duration);
241247

242248
// begin each animation when everything is rendered in their place
243249
// and the transition duration/easing is ready to go
244250
rafFrames(self._opts.renderDelay / 16, function() {
245251
// there's been a moment and the elements are in place
246252

247253
// now set the TRANSITION duration/easing
248-
self._setTrans(self._dur, false);
254+
self._setTrans(duration, false);
249255

250256
// wait a few moments again to wait for the transition
251257
// info to take hold in the DOM
@@ -264,47 +270,73 @@ export class Animation {
264270
// just go straight to the TO properties and call it done
265271
self._progress(1);
266272

267-
// so there was no animation, immediately run the after
273+
// since there was no animation, immediately run the after
268274
self._after();
269275

270-
// so there was no animation, it's done
276+
// since there was no animation, it's done
271277
// fire off all the onFinishes
272278
self._onFinish();
273279
}
274280
}
275281

276-
_asyncEnd(duration: number) {
282+
stop(opts: PlayOptions = {}) {
277283
var self = this;
278-
var deregTransEnd, fallbackTimerId;
284+
var duration = isDefined(opts.duration) ? opts.duration : 0;
285+
var stepValue = isDefined(opts.stepValue) ? opts.stepValue : 1;
279286

280-
// set the TRANSITION END event
281-
deregTransEnd = transitionEnd(self._transEl(), function() {
282-
// transition has completed
283-
console.debug('Animation, transition end');
287+
// ensure all past transition end events have been cleared
288+
this._clearAsync();
284289

285-
// cancel the fallback timer so it doesn't fire also
286-
clearTimeout(fallbackTimerId);
290+
// set the TO properties
291+
self._progress(stepValue);
287292

288-
// set the after styles
293+
if (duration > 30) {
294+
// this animation has a duration, so it should animate
295+
// place all the elements with their TO properties
296+
297+
// now set the TRANSITION duration
298+
self._setTrans(duration, true);
299+
300+
// set the async TRANSITION END event
301+
// and run onFinishes when the transition ends
302+
self._asyncEnd(duration);
303+
304+
} else {
305+
// this animation does not have a duration, so it should not animate
306+
// just go straight to the TO properties and call it done
289307
self._after();
290-
self._willChange(false);
308+
309+
// since there was no animation, it's done
310+
// fire off all the onFinishes
291311
self._onFinish();
292-
});
312+
}
313+
}
293314

294-
// set a fallback timeout if the transition end event never fires
295-
fallbackTimerId = setTimeout(function() {
296-
// fallback timeout fired instead of the transition end
297-
console.debug('Animation, fallback end');
315+
_asyncEnd(duration: number) {
316+
var self = this;
298317

299-
// deregister the transition end event listener
300-
deregTransEnd();
318+
function onTransitionEnd(ev) {
319+
console.debug('Animation async end,', (ev ? 'transitionEnd event' : 'fallback timeout'));
320+
321+
// ensure transition end events and timeouts have been cleared
322+
self._clearAsync();
301323

302324
// set the after styles
303325
self._after();
304326
self._willChange(false);
305327
self._onFinish();
328+
}
329+
330+
// set the TRANSITION END event on one of the transition elements
331+
self._unregTrans = transitionEnd(self._transEl(), onTransitionEnd);
332+
333+
// set a fallback timeout if the transition end event never fires
334+
self._tmr = setTimeout(onTransitionEnd, duration + 300);
335+
}
306336

307-
}, duration + 300);
337+
_clearAsync() {
338+
this._unregTrans && this._unregTrans();
339+
clearTimeout(this._tmr);
308340
}
309341

310342
_progress(stepValue: number) {
@@ -565,7 +597,11 @@ export class Animation {
565597
return this;
566598
}
567599

568-
onFinish(callback: Function, onceTimeCallback: boolean = false) {
600+
onFinish(callback: Function, onceTimeCallback: boolean = false, clearOnFinishCallacks: boolean = false) {
601+
if (clearOnFinishCallacks) {
602+
this._fFns = [];
603+
this._fOnceFns = [];
604+
}
569605
if (onceTimeCallback) {
570606
this._fOnceFns.push(callback);
571607

@@ -625,25 +661,15 @@ export class Animation {
625661
/*
626662
STATIC CLASSES
627663
*/
628-
static create(name: string): Animation {
664+
static create(name: string, opts: AnimationOptions = {}): Animation {
629665
let AnimationClass = AnimationRegistry[name];
630666

631667
if (!AnimationClass) {
632668
// couldn't find an animation by the given name
633669
// fallback to just the base Animation class
634670
AnimationClass = Animation;
635671
}
636-
return new AnimationClass();
637-
}
638-
639-
static createTransition(enteringView: ViewController, leavingView: ViewController, opts: any = {}): Animation {
640-
let TransitionClass = AnimationRegistry[opts.animation];
641-
if (!TransitionClass) {
642-
// didn't find a transition animation, default to ios-transition
643-
TransitionClass = AnimationRegistry['ios-transition'];
644-
}
645-
646-
return new TransitionClass(enteringView, leavingView, opts);
672+
return new AnimationClass(null, opts);
647673
}
648674

649675
static register(name: string, AnimationClass) {
@@ -652,6 +678,16 @@ export class Animation {
652678

653679
}
654680

681+
export interface AnimationOptions {
682+
animation?: string;
683+
renderDelay?: number;
684+
}
685+
686+
export interface PlayOptions {
687+
duration?: number;
688+
stepValue?: number;
689+
}
690+
655691
const doc: any = document;
656692
const TRANSFORMS = [
657693
'translateX', 'translateY', 'translateZ', 'scale', 'scaleX', 'scaleY', 'scaleZ',

ionic/components/action-sheet/action-sheet.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, Renderer, ElementRef} from 'angular2/core';
22
import {NgFor, NgIf} from 'angular2/common';
33

44
import {Animation} from '../../animations/animation';
5+
import {Transition, TransitionOptions} from '../../transitions/transition';
56
import {Config} from '../../config/config';
67
import {Icon} from '../icon/icon';
78
import {isDefined} from '../../util/util';
@@ -272,9 +273,9 @@ class ActionSheetCmp {
272273

273274

274275

275-
class ActionSheetSlideIn extends Animation {
276-
constructor(enteringView, leavingView, opts) {
277-
super(null, opts);
276+
class ActionSheetSlideIn extends Transition {
277+
constructor(enteringView, leavingView, opts: TransitionOptions) {
278+
super(opts);
278279

279280
let ele = enteringView.pageRef().nativeElement;
280281
let backdrop = new Animation(ele.querySelector('.backdrop'));
@@ -286,12 +287,12 @@ class ActionSheetSlideIn extends Animation {
286287
this.easing('cubic-bezier(.36,.66,.04,1)').duration(400).add(backdrop).add(wrapper);
287288
}
288289
}
289-
Animation.register('action-sheet-slide-in', ActionSheetSlideIn);
290+
Transition.register('action-sheet-slide-in', ActionSheetSlideIn);
290291

291292

292-
class ActionSheetSlideOut extends Animation {
293-
constructor(enteringView, leavingView, opts) {
294-
super(null, opts);
293+
class ActionSheetSlideOut extends Transition {
294+
constructor(enteringView: ViewController, leavingView: ViewController, opts: TransitionOptions) {
295+
super(opts);
295296

296297
let ele = leavingView.pageRef().nativeElement;
297298
let backdrop = new Animation(ele.querySelector('.backdrop'));
@@ -303,12 +304,12 @@ class ActionSheetSlideOut extends Animation {
303304
this.easing('cubic-bezier(.36,.66,.04,1)').duration(300).add(backdrop).add(wrapper);
304305
}
305306
}
306-
Animation.register('action-sheet-slide-out', ActionSheetSlideOut);
307+
Transition.register('action-sheet-slide-out', ActionSheetSlideOut);
307308

308309

309-
class ActionSheetMdSlideIn extends Animation {
310-
constructor(enteringView, leavingView, opts) {
311-
super(null, opts);
310+
class ActionSheetMdSlideIn extends Transition {
311+
constructor(enteringView: ViewController, leavingView: ViewController, opts: TransitionOptions) {
312+
super(opts);
312313

313314
let ele = enteringView.pageRef().nativeElement;
314315
let backdrop = new Animation(ele.querySelector('.backdrop'));
@@ -320,12 +321,12 @@ class ActionSheetMdSlideIn extends Animation {
320321
this.easing('cubic-bezier(.36,.66,.04,1)').duration(450).add(backdrop).add(wrapper);
321322
}
322323
}
323-
Animation.register('action-sheet-md-slide-in', ActionSheetMdSlideIn);
324+
Transition.register('action-sheet-md-slide-in', ActionSheetMdSlideIn);
324325

325326

326-
class ActionSheetMdSlideOut extends Animation {
327-
constructor(enteringView, leavingView, opts) {
328-
super(null, opts);
327+
class ActionSheetMdSlideOut extends Transition {
328+
constructor(enteringView: ViewController, leavingView: ViewController, opts: TransitionOptions) {
329+
super(opts);
329330

330331
let ele = leavingView.pageRef().nativeElement;
331332
let backdrop = new Animation(ele.querySelector('.backdrop'));
@@ -337,4 +338,4 @@ class ActionSheetMdSlideOut extends Animation {
337338
this.easing('cubic-bezier(.36,.66,.04,1)').duration(450).add(backdrop).add(wrapper);
338339
}
339340
}
340-
Animation.register('action-sheet-md-slide-out', ActionSheetMdSlideOut);
341+
Transition.register('action-sheet-md-slide-out', ActionSheetMdSlideOut);

0 commit comments

Comments
 (0)