-
Notifications
You must be signed in to change notification settings - Fork 7
/
transitionerPanes.js
192 lines (151 loc) · 5.68 KB
/
transitionerPanes.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
ClientRouter.prototype.layout = 'transitionerPanes';
Transitioner = {
// by default, listen to the singleton router, override to not (e.g. tests)
router: Router,
_transitionEvents: 'webkitTransitionEnd.transitioner oTransitionEnd.transitioner transitionEnd.transitioner msTransitionEnd.transitioner transitionend.transitioner',
lastTransitionClasses: null,
transitioning: false,
leftIsNext: true,
_nextTransitionType: null,
// we are transitioning from -> to.
// what template should we use?
// return false to not transition at all (directly render into currentPage)
_defaultTransitionType: function(from, to) {
// rendering the initial page? no transition
if (_.isUndefined(from)) return false;
// just the data context changed? no transition
if (from.context.path === to.context.path &&
from.partial.template === to.partial.template)
return false;
// did we set a next transition?
if (this._nextTransitionType !== null)
return this._nextTransitionType;
// a standard transition
return 'normal';
},
attach: function(template) {
var self = this, oldRender, newRender, type;
var dependency = new Deps.Dependency;
self.container = template.find('.transitioner-panes');
self.leftPane = template.find('.left-pane');
self.rightPane = template.find('.right-pane');
// call this after each transition is setup, ready for the next time
var done = function() {
// save for next time
oldRender = newRender;
self._nextTransitionType = null;
}
// the first auto run talks to the router and .change()s the dependency
// when ever a transition is necessary. In the process it stores
// oldRender, newRender and type.
Deps.autorun(function() {
newRender = {
partial: self.router._partials.get(),
// XXX: shouldn't need to do this, the router is doing something funny
context: Deps.nonreactive(function() { return self.router.current(); })
};
type = self._defaultTransitionType(oldRender, newRender);
if (self.transitionType)
type = self.transitionType(oldRender, newRender, type)
// console.log('new render', oldRender, newRender, type)
// if type is false, we are explicitly _NOT_ transitioning
// so do nothing.
if (type === false)
return done();
// ok, we have a transition type, so we re-run the rendering computation
dependency.changed();
});
Deps.autorun(function() {
dependency.depend();
// console.log(oldRender && oldRender.context.path,
// oldRender && oldRender.partial.template,
// newRender.context.path,
// newRender.partial.template,
// type);
// if type is false, this must be the first time
if (type === false) {
// console.log('setting up transition', oldRender, newRender)
self.makeCurrentPage(self.leftPane);
self.leftIsNext = false;
self.renderToPane(self.currentPage);
} else {
// console.log('transitioning', self.leftIsNext, oldRender, newRender)
self.transitionStart(type, oldRender, newRender);
}
return done();
});
},
setNextTransitionType: function(type) {
this._nextTransitionType = type;
},
back: function() {
this.setNextTransitionType('back');
Location.back()
},
transitionStart: function(type, from, to) {
var self = this;
// kill exisiting transition
self.transitioning && self.transitionEnd();
self.transitioning = true;
self.renderToNextPane(self.leftIsNext ? self.leftPane : self.rightPane);
self.leftIsNext = ! self.leftIsNext;
var classes = type;
if (_.isString(from.partial.template))
classes = classes + ' from-' + from.partial.template;
if (_.isString(to.partial.template))
classes = classes + ' to-' + to.partial.template;
$(self.container).addClass(self.lastTransitionClasses = classes)
self.container.offsetWidth; // force a redraw
$(self.container).addClass('transitioning')
.on(self._transitionEvents, function(e) {
if (! $(e.target).is(self.container))
return;
self.transitionEnd();
});
},
transitionEnd: function() {
var self = this;
// console.log('transitionEnd', self.currentPage, self.nextPage);
// switch classes around
if (self.currentPage) {
$(self.currentPage).removeClass('current-page');
self.clearPane(self.currentPage);
}
self.makeCurrentPage(self.nextPage);
// finish.
$(self.container).removeClass('transitioning ' + self.lastTransitionClasses)
.off(self._transitionEvents);
self.transitioning = false;
},
clearPane: function(pane) {
// console.log('clearing pane', pane)
Spark.finalize(pane);
pane.innerHTML = '';
},
renderToPane: function(pane) {
// console.log('rendering to pane', pane)
var self = this;
pane.appendChild(Meteor.render(function() {
// render the current page to the current pane
return self.router._partials.get().render();
}));
},
renderToNextPane: function(pane) {
this.renderToPane(pane);
this.makeNextPage(pane);
},
makeCurrentPage: function(pane) {
this.currentPage = pane;
$(pane).removeClass('next-page').addClass('current-page');
},
makeNextPage: function(pane) {
this.nextPage = pane;
$(pane).removeClass('current-page').addClass('next-page');
}
}
Template.transitionerPanes.rendered = function() {
if (! this.attached) {
this.attached = true;
Transitioner.attach(this);
}
}