forked from reactjs/react-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (132 loc) · 5.37 KB
/
index.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
var React = require("react")
var ReactDOM = require("react-dom")
var ReactDOMServer = require("react-dom/server")
var detectEvents = require("./src/events/detect")
var constructorFromGlobal = require("./src/getConstructor/fromGlobal")
var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback")
var ReactRailsUJS = {
// This attribute holds the name of component which should be mounted
// example: `data-react-class="MyApp.Items.EditForm"`
CLASS_NAME_ATTR: 'data-react-class',
// This attribute holds JSON stringified props for initializing the component
// example: `data-react-props="{\"item\": { \"id\": 1, \"name\": \"My Item\"} }"`
PROPS_ATTR: 'data-react-props',
// This attribute holds which method to use between: ReactDOM.hydrate, ReactDOM.render
RENDER_ATTR: 'data-hydrate',
// If jQuery is detected, save a reference to it for event handlers
jQuery: (typeof window !== 'undefined') && (typeof window.jQuery !== 'undefined') && window.jQuery,
// helper method for the mount and unmount methods to find the
// `data-react-class` DOM elements
findDOMNodes: function(searchSelector) {
var classNameAttr = ReactRailsUJS.CLASS_NAME_ATTR
// we will use fully qualified paths as we do not bind the callbacks
var selector, parent;
switch (typeof searchSelector) {
case 'undefined':
selector = '[' + classNameAttr + ']';
parent = document;
break;
case 'object':
selector = '[' + classNameAttr + ']';
parent = searchSelector;
break;
case 'string':
selector = searchSelector + '[' + classNameAttr + '], ' +
searchSelector + ' [' + classNameAttr + ']';
parent = document;
break
default:
break;
}
if (ReactRailsUJS.jQuery) {
return ReactRailsUJS.jQuery(selector, parent);
} else {
return parent.querySelectorAll(selector);
}
},
// Get the constructor for a className (returns a React class)
// Override this function to lookup classes in a custom way,
// the default is ReactRailsUJS.ComponentGlobal
getConstructor: constructorFromGlobal,
// Given a Webpack `require.context`,
// try finding components with `require`,
// then falling back to global lookup.
useContext: function(requireContext) {
this.getConstructor = constructorFromRequireContextWithGlobalFallback(requireContext)
},
// Render `componentName` with `props` to a string,
// using the specified `renderFunction` from `react-dom/server`.
serverRender: function(renderFunction, componentName, props) {
var componentClass = this.getConstructor(componentName)
var element = React.createElement(componentClass, props)
return ReactDOMServer[renderFunction](element)
},
// Within `searchSelector`, find nodes which should have React components
// inside them, and mount them with their props.
mountComponents: function(searchSelector) {
var ujs = ReactRailsUJS
var nodes = ujs.findDOMNodes(searchSelector);
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i];
var className = node.getAttribute(ujs.CLASS_NAME_ATTR);
var constructor = ujs.getConstructor(className);
var propsJson = node.getAttribute(ujs.PROPS_ATTR);
var props = propsJson && JSON.parse(propsJson);
var hydrate = node.getAttribute(ujs.RENDER_ATTR);
if (!constructor) {
var message = "Cannot find component: '" + className + "'"
if (console && console.log) {
console.log("%c[react-rails] %c" + message + " for element", "font-weight: bold", "", node)
}
throw new Error(message + ". Make sure your component is available to render.")
} else {
if (hydrate && typeof ReactDOM.hydrate === "function") {
ReactDOM.hydrate(React.createElement(constructor, props), node);
} else {
ReactDOM.render(React.createElement(constructor, props), node);
}
}
}
},
// Within `searchSelector`, find nodes which have React components
// inside them, and unmount those components.
unmountComponents: function(searchSelector) {
var nodes = ReactRailsUJS.findDOMNodes(searchSelector);
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i];
ReactDOM.unmountComponentAtNode(node);
}
},
// Check the global context for installed libraries
// and figure out which library to hook up to (pjax, Turbolinks, jQuery)
// This is called on load, but you can call it again if needed
// (It will unmount itself)
detectEvents: function() {
detectEvents(this)
},
}
// These stable references are so that handlers can be added and removed:
ReactRailsUJS.handleMount = function(e) {
var target = undefined;
if (e && e.target) {
target = e.target;
}
ReactRailsUJS.mountComponents(target);
}
ReactRailsUJS.handleUnmount = function(e) {
var target = undefined;
if (e && e.target) {
target = e.target;
}
ReactRailsUJS.unmountComponents(target);
}
if (typeof window !== "undefined") {
// Only setup events for browser (not server-rendering)
ReactRailsUJS.detectEvents()
}
// It's a bit of a no-no to populate the global namespace,
// but we really need it!
// We need access to this object for server rendering, and
// we can't do a dynamic `require`, so we'll grab it from here:
self.ReactRailsUJS = ReactRailsUJS
module.exports = ReactRailsUJS