-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
107 lines (98 loc) · 2.29 KB
/
router.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
var actions, init, reducer, riot, router, start, triggerRoute, types;
riot = require("riot");
router = require("riot-route");
types = {
route: "ROUTE"
};
actions = {
route: function(route) {
return {
type: types.route,
route: route
};
}
};
reducer = function(state, action) {
if (state == null) {
state = "home";
}
if (action.type === types.route) {
if (action.route.length < 1) {
return "home";
} else {
return action.route;
}
} else {
return state;
}
};
triggerRoute = function(tag) {
var key, results;
if (typeof tag.trigger === "function") {
tag.trigger("route");
}
results = [];
for (key in tag.tags) {
results.push(triggerRoute(tag.tags[key]));
}
return results;
};
init = function(bootstrap) {
var currentPage, currentRoute, redux;
redux = require("./redux");
riot.mixin({
init: function() {
var route;
route = "";
this.on("mount", function() {
var state;
state = redux.store.getState();
if (state.route === this.root.localName) {
triggerRoute(this);
}
return route = state.route;
});
return this.on("update", function() {
var routed, state;
state = redux.store.getState();
routed = state.route === this.root.localName && state.route !== route;
route = state.route;
if (routed) {
return triggerRoute(this);
}
});
}
});
router(function(route) {
return redux.store.dispatch(actions.route(route));
});
currentPage = null;
currentRoute = null;
redux.store.subscribe(function() {
var main, route;
main = document.getElementById("layoutMain");
if ((main != null) && ((bootstrap == null) || bootstrap(redux.store, redux.actions))) {
bootstrap = null;
route = redux.store.getState().route;
if (currentRoute !== route) {
currentRoute = route;
if (currentPage != null) {
if (typeof currentPage.unmount === "function") {
currentPage.unmount(true);
}
}
return currentPage = riot.mount(main, route);
}
}
});
return router.start();
};
start = function() {
return router.exec();
};
module.exports = {
actions: actions,
reducer: reducer,
init: init,
start: start
};