-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflow-router-transitions.js
130 lines (116 loc) · 3.81 KB
/
flow-router-transitions.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
/* global Momentum: true */
/* global Transitioner: true */
// Additional function to support Transitioner.TransitionOrder
function CreateTransitions(Transitions, LastPage, Page) {
// Create forward and reverse transition based on args
// console.log('From: ' + LastPage + ' to ' + Page);
var ForwardPageKey = LastPage + '->' + Page;
Transitions[ForwardPageKey] = 'right-to-left';
var ReversePageKey = Page + '->' + LastPage;
Transitions[ReversePageKey] = 'left-to-right';
return Transitions;
};
Transitioner = {
transitions: [],
current: '',
updateRoutesTransitions: function () {
var self = this;
// no reactive computation anymore here
// this function gets called once the route changes (flow router triggred)
// and once mometum asks for a transition type
var route = FlowRouter.getRouteName();
// set transition
var from = self.current,
to = route;
if (from === to) {
return;
}
// set new current route
self.current = route;
if (self.transitions.hasOwnProperty(from + '->' + to)) {
self.transition = self.transitions[from + '->' + to];
} else {
self.transition = self.transitions['default'];
}
},
getRouteForName: function (name) {
return _.findWhere(FlowRouter._routes, {
name: name
});
},
getRouteForPath: function (path) {
return _.findWhere(FlowRouter._routes, {
path: path
});
},
setTransitions: function (transitions) {
var self = this;
self.transitions = transitions;
},
TransitionOrder: function (Pages) {
// Init Vars
var Transitions = {};
var LastPage = '';
var NumPages = Pages.length;
var PageIndex = 0;
// Loop through array of named routes
while (PageIndex < Pages.length) {
var LastIndex = 0;
while (LastIndex < PageIndex) {
Transitions = CreateTransitions(Transitions, Pages[LastIndex], Pages[PageIndex]);
LastIndex++;
}
PageIndex++;
}
// Set fallback transition
Transitions['default'] = 'fade';
// Then resume normal process
Transitioner.setTransitions(Transitions);
}
};
// update transitions once flow router triggers route change
Tracker.autorun(function () {
Transitioner.updateRoutesTransitions();
});
Momentum.registerPlugin('flow-router', function (options) {
check(options.options, Match.Optional(Function));
var getPluginOptions = function () {
// trigger transition update manually
Transitioner.updateRoutesTransitions();
var type = Transitioner.transition;
if (_.isUndefined(type)) {
type = 'none'; // XXX: what should this be?
}
return type;
};
var getPlugin = function (node) {
var pluginOptions = getPluginOptions(node);
if (_.isString(pluginOptions)) {
pluginOptions = {
with: pluginOptions
};
}
var plugin = Momentum.plugins[pluginOptions.with];
if (!plugin) {
return console.error("Can't find momentum plugin '" +
pluginOptions
.with +
"'");
}
pluginOptions = _.extend(_.omit(options, 'options'),
pluginOptions);
return plugin(_.omit(pluginOptions, 'with'));
};
return {
insertElement: function (node, next, done) {
getPlugin(node).insertElement(node, next, done);
},
moveElement: function (node, next, done) {
getPlugin(node).moveElement(node, next, done);
},
removeElement: function (node, done) {
getPlugin(node).removeElement(node, done);
},
// force: true
};
});