-
-
Notifications
You must be signed in to change notification settings - Fork 631
/
clientStartup.js
162 lines (137 loc) · 4.63 KB
/
clientStartup.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
import ReactDOM from 'react-dom';
import createReactElement from './createReactElement';
import handleError from './handleError';
import isRouterResult from './isRouterResult';
const REACT_ON_RAILS_COMPONENT_CLASS_NAME = 'js-react-on-rails-component';
const REACT_ON_RAILS_STORE_CLASS_NAME = 'js-react-on-rails-store';
function debugTurbolinks(...msg) {
if (!window) {
return;
}
if (ReactOnRails.option('traceTurbolinks')) {
console.log('TURBO:', ...msg);
}
}
function turbolinksInstalled() {
return (typeof Turbolinks !== 'undefined');
}
function forEachComponent(fn, railsContext) {
forEach(fn, REACT_ON_RAILS_COMPONENT_CLASS_NAME, railsContext);
}
function forEachStore(railsContext) {
forEach(initializeStore, REACT_ON_RAILS_STORE_CLASS_NAME, railsContext);
}
function forEach(fn, className, railsContext) {
const els = document.getElementsByClassName(className);
for (let i = 0; i < els.length; i++) {
fn(els[i], railsContext);
}
}
function turbolinksVersion5() {
return (typeof Turbolinks.controller !== 'undefined');
}
function initializeStore(el, railsContext) {
const name = el.getAttribute('data-store-name');
const props = JSON.parse(el.getAttribute('data-props'));
const storeGenerator = ReactOnRails.getStoreGenerator(name);
const store = storeGenerator(props, railsContext);
ReactOnRails.setStore(name, store);
}
/**
* Used for client rendering by ReactOnRails
* @param el
*/
function render(el, railsContext) {
const name = el.getAttribute('data-component-name');
const domNodeId = el.getAttribute('data-dom-id');
const props = JSON.parse(el.getAttribute('data-props'));
const trace = JSON.parse(el.getAttribute('data-trace'));
try {
const domNode = document.getElementById(domNodeId);
if (domNode) {
const reactElementOrRouterResult = createReactElement({
name,
props,
domNodeId,
trace,
railsContext
});
if (isRouterResult(reactElementOrRouterResult)) {
throw new Error(`\
You returned a server side type of react-router error: ${JSON.stringify(reactElementOrRouterResult)}
You should return a React.Component always for the client side entry point.`);
} else {
ReactDOM.render(reactElementOrRouterResult, domNode);
}
}
} catch (e) {
handleError({
e,
name,
serverSide: false,
});
}
}
function parseRailsContext() {
const el = document.getElementById('js-react-on-rails-context');
if (el) {
return JSON.parse(el.getAttribute('data-rails-context'));
} else {
return null;
}
}
export function reactOnRailsPageLoaded() {
debugTurbolinks('reactOnRailsPageLoaded');
const railsContext = parseRailsContext();
forEachStore(railsContext);
forEachComponent(render, railsContext);
}
function unmount(el) {
const domNodeId = el.getAttribute('data-dom-id');
const domNode = document.getElementById(domNodeId);
ReactDOM.unmountComponentAtNode(domNode);
}
function reactOnRailsPageUnloaded() {
debugTurbolinks('reactOnRailsPageUnloaded');
forEachComponent(unmount);
}
export function clientStartup(context) {
const document = context.document;
// Check if server rendering
if (!document) {
return;
}
// Tried with a file local variable, but the install handler gets called twice.
if (context.__REACT_ON_RAILS_EVENT_HANDLERS_RAN_ONCE__) {
return;
}
context.__REACT_ON_RAILS_EVENT_HANDLERS_RAN_ONCE__ = // eslint-disable-line no-param-reassign
true;
debugTurbolinks('Adding DOMContentLoaded event to install event listeners.');
document.addEventListener('DOMContentLoaded', () => {
// Install listeners when running on the client (browser).
// We must do this check for turbolinks AFTER the document is loaded because we load the
// Webpack bundles first.
if (!turbolinksInstalled()) {
debugTurbolinks(
'NOT USING TURBOLINKS: DOMContentLoaded event, calling reactOnRailsPageLoaded'
);
reactOnRailsPageLoaded();
} else {
if (turbolinksVersion5()) {
debugTurbolinks(
'USING TURBOLINKS 5: document added event listeners turbolinks:before-render and ' +
'turbolinks:load.'
);
document.addEventListener('turbolinks:before-render', reactOnRailsPageUnloaded);
document.addEventListener('turbolinks:load', reactOnRailsPageLoaded);
} else {
debugTurbolinks(
'USING TURBOLINKS 2: document added event listeners page:before-unload and ' +
'page:change.');
document.addEventListener('page:before-unload', reactOnRailsPageUnloaded);
document.addEventListener('page:change', reactOnRailsPageLoaded);
}
}
});
}