-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarionette.csstransition-region.js
73 lines (61 loc) · 2.8 KB
/
marionette.csstransition-region.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
/*
* Greetings, adventurer!
*
* Are you looking to support animated regions in your app? That's awesome! However,
* I don't suggest using this code. It's mostly untested code that I wrote really, really quickly
* for the sole purpose of making the three examples work.
*
* Feel free to use this code for inspiration, but please – don't use it unless you
* test it more thoroughly!
*
*/
(function() {
Marionette.CSSTransitionRegion = Marionette.Region.extend({
// avoid property collision
transitionRegion: {},
detachedClassName: 'detached',
// force preventDestroy: true if transitionOut
// see [#1785](https://github.com/marionettejs/backbone.marionette/issues/1785)
show: function(view, options) {
if (view.transitionOut) {
options = {} || options;
options.preventDestroy = true;
}
Marionette.Region.prototype.show.call(this, view, options);
},
onBeforeSwap: function(view) {
if (this.currentView && this.currentView.transitionOut) {
// copy currentView properties before it get destroyed
this.transitionRegion.transitionOut = this.currentView.transitionOut;
var clonedNode = this.transitionRegion.clonedNode = this.currentView.el.cloneNode(true);
// put the clone in front region
clonedNode.classList.add(this.detachedClassName);
this.el.parentNode.appendChild(clonedNode);
// call empty manually see [#1785](https://github.com/marionettejs/backbone.marionette/issues/1785)
this.empty();
}
},
onSwap: function(view) {
// if someone knows why I have to wrap this in a setTimeout for Firefox let me know
setTimeout(function() {
if (this.transitionRegion.transitionOut && this.transitionRegion.clonedNode) {
this.transitionRegion.clonedNode.classList.add(this.transitionRegion.transitionOut);
// TODO: prefix event name?
this.transitionRegion.clonedNode.addEventListener('transitionend', function(event) {
// hurra for node.remove & node.removeNode differences =_=
event.target.parentNode.removeChild(event.target);
}, false);
this.transitionRegion = {};
}
}.bind(this), 50);
},
onShow: function(view) {
// if someone knows why I have to wrap this in a setTimeout for Firefox let me know
setTimeout(function() {
if (view.transitionIn) {
view.el.classList.add(view.transitionIn);
}
}.bind(this), 50);
}
});
})();