Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80537 views
1
require('../shared/init');
2
import './init';
3
4
import React from 'react';
5
import Router from 'react-router';
6
import FluxComponent from 'flummox/component';
7
import Flux from '../shared/Flux';
8
import routes from '../shared/routes';
9
import performRouteHandlerStaticMethod from '../shared/utils/performRouteHandlerStaticMethod';
10
import url from 'url';
11
12
// Initialize flux
13
const flux = new Flux();
14
15
const router = Router.create({
16
routes: routes,
17
location: Router.HistoryLocation
18
});
19
20
// Render app
21
router.run(async (Handler, state) => {
22
const routeHandlerInfo = { state, flux };
23
24
await performRouteHandlerStaticMethod(state.routes, 'routerWillRun', routeHandlerInfo);
25
26
React.render(
27
<FluxComponent flux={flux}>
28
<Handler {...state} />
29
</FluxComponent>,
30
document.getElementById('app')
31
);
32
});
33
34
// Intercept local route changes
35
document.onclick = event => {
36
const { toElement: target } = event;
37
38
if (!target) return;
39
40
if (target.tagName !== 'A') return;
41
42
const href = target.getAttribute('href');
43
44
if (!href) return;
45
46
const resolvedHref = url.resolve(window.location.href, href);
47
const { host, path } = url.parse(resolvedHref);
48
49
if (host === window.location.host) {
50
event.preventDefault();
51
router.transitionTo(path);
52
}
53
};
54
55