This repository was archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransition_adapter.js
More file actions
107 lines (83 loc) · 4.31 KB
/
transition_adapter.js
File metadata and controls
107 lines (83 loc) · 4.31 KB
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
(function() {
define([
'backbone.marionette'
],
function(Marionette){
Backbone.Marionette.Transition = {
initialize: function() {
var transEndEventNames = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd',
'msTransition' : 'MSTransitionEnd',
'transition' : 'transitionend'
};
this.transEndEventName = transEndEventNames[ Modernizr.prefixed('transition') ];
},
/*
** transitionEl is the function used to transition two elements on the page.
** @param $oldEl jQuery OBJECT - the original jQuery element being transitioned out
** @param $newEl jQuery OBJECT - the new jQuery element being transitioned in
** @param options Optional OBJECT - object consisten of several transition options
** @param options keys - the keys acceptable in the options object | type, direction, transitionEndCb
*/
transitionEl: function($oldEl, $newEl, options) {
var self = this,
deferred = $.Deferred();
if (!self._isInitialized) {
self.initialize();
self._isInitialized = true;
}
if (typeof options == 'undefined') options = {};
_.defaults(options, {
type: 'slide',
direction: 'left',
transitionEndCb: null
});
// clean up the old listeners, just to ensure we only have 1 active.
$oldEl.off(self.transEndEventName);
// Move the new view to an off-screen position using transformation matrix
var translationClass;
if (options.direction === 'up' || options.direction === 'down') {
deferred.resolve();
} else {
deferred.resolve();
}
deferred.then(function() {
// The $newEl is coming "in", so add the "in" class to it, and handle "in" via css
$newEl.addClass('in');
// Determine the type of transition and build the css transformation.
if(options.type === 'slide') {
translationClass = 'slide';
translationClass = translationClass + ' ' + options.direction;
}
else if(options.type === 'fade') {
translationClass = 'fade';
}
// Add the new view to the dom
$oldEl.append($newEl);
// Turn on the css animations to enable the transition. We do this here,
// before the tranision instead of after the transition is complete
// because it causes less of a visual 'snap' as the pattern moves.
// The old view is on its' way out, so add "out" class along with transitionClass
$oldEl.addClass('animated' + ' ' + translationClass + ' ' + 'out');
// after transition, clean up by removing the old view, then
// re-position everything back to a zero-point. There might be a problem
// relying on the transitionEnd event because there are cases where it
// does not fire.
$oldEl.on(self.transEndEventName, function () {
$oldEl.off(self.transEndEventName);
// clean up new view and place everything back
// to a sane starting position, ready for next transition.
$oldEl.removeClass('animated' + ' ' + translationClass + ' ' + 'out');
$newEl.removeClass('in');
if (_.isFunction(options.transitionEndCb)) {
options.transitionEndCb.call(self);
}
});
});
}
};
return Backbone.Marionette.Transition;
});
}).call(this);