/
main.js
310 lines (283 loc) · 8.98 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { FlowRouter as Router } from "meteor/kadira:flow-router-ssr";
import { BlazeLayout } from "meteor/kadira:blaze-layout";
import { Reaction, Logger } from "/client/api";
import { Packages, Shops } from "/lib/collections";
import { MetaData } from "/lib/api/router/metadata";
import { Session } from "meteor/session";
import { Meteor } from "meteor/meteor";
import { Tracker } from "meteor/tracker";
// init flow-router
//
/* eslint no-loop-func: 0 */
// client should wait on subs
Router.wait();
/**
* checkRouterPermissions
* check if user has route permissions
* @param {Object} context - route context
* @param {redirect} null object
* @return {Object} return context
*/
function checkRouterPermissions(context) {
const routeName = context.route.name;
if (Reaction.hasPermission(routeName, Meteor.userId())) {
if (context.unauthorized === true) {
delete context.unauthorized;
return context;
}
return context;
}
// determine if this is a valid route or a 404
const routeExists = _.find(Router._routes, function (route) {
return route.path === context.path;
});
// if route exists (otherwise this will return 404)
// return unauthorized flag on context
if (routeExists) {
context.unauthorized = true;
}
return context;
}
// initialize title and meta data and check permissions
Router.triggers.enter([checkRouterPermissions, MetaData.init]);
/**
* getRouteName
* assemble route name to be standard
* prefix/package name + registry name or route
* @param {String} packageName [package name]
* @param {Object} registryItem [registry object]
* @return {String} [route name]
*/
function getRegistryRouteName(packageName, registryItem) {
let routeName;
if (packageName && registryItem) {
if (registryItem.name) {
routeName = registryItem.name;
} else if (registryItem.template) {
routeName = `${packageName}/${registryItem.template}`;
} else {
routeName = packageName;
}
// dont include params in the name
routeName = routeName.split(":")[0];
return routeName;
}
return null;
}
/**
* selectLayout
* @param {Object} layout - element of shops.layout array
* @param {Object} setLayout - layout
* @param {Object} setWorkflow - workflow
* @returns {Object} layout - return object of template definitions for Blaze Layout
*/
function selectLayout(layout, setLayout, setWorkflow) {
const currentLayout = setLayout || Session.get("DEFAULT_LAYOUT") || "coreLayout";
const currentWorkflow = setWorkflow || Session.get("DEFAULT_WORKFLOW") || "coreWorkflow";
if (layout.layout === currentLayout && layout.workflow === currentWorkflow && layout.enabled === true) {
return layout;
}
return null;
}
/**
* ReactionLayout
* sets and returns reaction layout structure
* @param {Object} options - this router context
* @param {String} options.layout - string of shop.layout.layout (defaults to coreLayout)
* @param {String} options.workflow - string of shop.layout.workflow (defaults to coreLayout)
* @returns {Object} layout - return object of template definitions for Blaze Layout
*/
export function ReactionLayout(options = {}) {
const layout = options.layout || Session.get("DEFAULT_LAYOUT") || "coreLayout";
const workflow = options.workflow || Session.get("DEFAULT_WORKFLOW") || "coreWorkflow";
if (!options.layout) {
options.layout = "coreLayout";
}
if (!options.workflow) {
options.workflow = "coreWorkflow";
}
// check if router has denied permissions
// see: checkRouterPermissions
let unauthorized = {};
if (Router.current().unauthorized) {
unauthorized.template = "unauthorized";
}
// autorun router rendering
Tracker.autorun(function () {
if (Reaction.Subscriptions.Shops.ready()) {
const shop = Shops.findOne(Reaction.getShopId());
if (shop) {
const newLayout = shop.layout.find((x) => selectLayout(x, layout, workflow));
// oops this layout wasn't found. render notFound
if (!newLayout) {
BlazeLayout.render("notFound");
} else {
const layoutToRender = Object.assign({}, newLayout.structure, options, unauthorized);
BlazeLayout.render(layout, layoutToRender);
}
}
}
});
return options;
}
// default not found route
Router.notFound = {
action() {
ReactionLayout({
template: "notFound"
});
}
};
/**
* initPackageRoutes
* registers route and template when registry item has
* registryItem.route && registryItem.template
* @param {String} userId - userId
* @returns {undefined} returns undefined
*/
Router.initPackageRoutes = () => {
const pkgs = Packages.find().fetch();
const prefix = Reaction.getSlug(Reaction.getShopName()); // todo add shopId
// prefixing isnt necessary if we only have one shop
// but we need to bypass the current
// subscription to determine this.
const shopSub = Meteor.subscribe("shopsCount");
if (shopSub.ready()) {
// using tmeasday:publish-counts
const shopCount = Counts.get("shops-count");
// initialize index
// define default routing groups
const shop = Router.group({
name: "shop"
});
//
// index / home route
// to overide layout, ie: home page templates
// set DEFAULT_LAYOUT, in config.js
//
shop.route("/", {
name: "index",
action() {
ReactionLayout(Session.get("INDEX_OPTIONS") || {});
}
});
// get package registry route configurations
for (let pkg of pkgs) {
const newRoutes = [];
// pkg registry
if (pkg.registry) {
const registry = Array.from(pkg.registry);
for (let registryItem of registry) {
// registryItems
if (registryItem.route) {
const {
route,
template,
layout,
workflow,
triggersEnter,
triggersExit
} = registryItem;
// get registry route name
const routeName = getRegistryRouteName(pkg.name, registryItem);
// layout option structure
const options = {
template: template,
workflow: workflow,
layout: layout
};
// define new route
// we could allow the options to be passed in the registry if we need to be more flexible
const newRouteConfig = {
route: route,
options: {
name: routeName,
template: options.template,
layout: options.layout,
triggersEnter: triggersEnter,
triggersExit: triggersExit,
action: () => {
ReactionLayout(options);
}
}
};
// push new routes
newRoutes.push(newRouteConfig);
} // end registryItems
} // end package.registry
//
// add group and routes to routing table
//
const uniqRoutes = new Set(newRoutes);
for (let route of uniqRoutes) {
// allow overriding of prefix in route definitions
// define an "absolute" url by excluding "/"
if (route.route.substring(0, 1) !== "/") {
route.route = "/" + route.route;
shop.newGroup = Router.group({
prefix: ""
});
} else if (shopCount <= 1) {
shop.newGroup = Router.group({
prefix: ""
});
} else {
shop.newGroup = Router.group({
prefix: "/" + prefix
});
}
// todo: look for a cheap way to validate and prevent duplicate additions
shop.newGroup.route(route.route, route.options);
}
}
} // end package loop
//
// initialize the router
//
try {
Router.initialize();
} catch (e) {
Logger.error(e);
Router.reload();
}
}
};
/**
* pathFor
* @summary get current router path
* @param {String} path - path to fetch
* @param {Object} options - url params
* @return {String} returns current router path
*/
Router.pathFor = (path, options = {}) => {
let params = options.hash || {};
let query = params.query ? Router._qs.parse(params.query) : {};
// prevent undefined param error
for (let i in params) {
if (params[i] === null || params[i] === undefined) {
params[i] = "/";
}
}
return Router.path(path, params, query);
};
/**
* isActive
* @summary general helper to return "active" when on current path
* @example {{active "name"}}
* @param {String} routeName - route name as defined in registry
* @return {String} return "active" or null
*/
Router.isActiveClassName = (routeName) => {
Router.watchPathChange();
const group = Router.current().route.group;
let prefix;
if (group && group.prefix) {
prefix = Router.current().route.group.prefix;
} else {
prefix = "";
}
const path = Router.current().route.path;
const routeDef = path.replace(prefix + "/", "");
return routeDef === routeName ? "active" : "";
};
export default Router;