UNPKG

16.1 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory(require("react"), require("react-dom"), require("react-dom/server"));
4 else if(typeof define === 'function' && define.amd)
5 define(["react", "react-dom", "react-dom/server"], factory);
6 else if(typeof exports === 'object')
7 exports["ReactRailsUJS"] = factory(require("react"), require("react-dom"), require("react-dom/server"));
8 else
9 root["ReactRailsUJS"] = factory(root["React"], root["ReactDOM"], root["ReactDOMServer"]);
10})(this, function(__WEBPACK_EXTERNAL_MODULE_3__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_5__) {
11return /******/ (function(modules) { // webpackBootstrap
12/******/ // The module cache
13/******/ var installedModules = {};
14/******/
15/******/ // The require function
16/******/ function __webpack_require__(moduleId) {
17/******/
18/******/ // Check if module is in cache
19/******/ if(installedModules[moduleId]) {
20/******/ return installedModules[moduleId].exports;
21/******/ }
22/******/ // Create a new module (and put it into the cache)
23/******/ var module = installedModules[moduleId] = {
24/******/ i: moduleId,
25/******/ l: false,
26/******/ exports: {}
27/******/ };
28/******/
29/******/ // Execute the module function
30/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31/******/
32/******/ // Flag the module as loaded
33/******/ module.l = true;
34/******/
35/******/ // Return the exports of the module
36/******/ return module.exports;
37/******/ }
38/******/
39/******/
40/******/ // expose the modules object (__webpack_modules__)
41/******/ __webpack_require__.m = modules;
42/******/
43/******/ // expose the module cache
44/******/ __webpack_require__.c = installedModules;
45/******/
46/******/ // identity function for calling harmony imports with the correct context
47/******/ __webpack_require__.i = function(value) { return value; };
48/******/
49/******/ // define getter function for harmony exports
50/******/ __webpack_require__.d = function(exports, name, getter) {
51/******/ if(!__webpack_require__.o(exports, name)) {
52/******/ Object.defineProperty(exports, name, {
53/******/ configurable: false,
54/******/ enumerable: true,
55/******/ get: getter
56/******/ });
57/******/ }
58/******/ };
59/******/
60/******/ // getDefaultExport function for compatibility with non-harmony modules
61/******/ __webpack_require__.n = function(module) {
62/******/ var getter = module && module.__esModule ?
63/******/ function getDefault() { return module['default']; } :
64/******/ function getModuleExports() { return module; };
65/******/ __webpack_require__.d(getter, 'a', getter);
66/******/ return getter;
67/******/ };
68/******/
69/******/ // Object.prototype.hasOwnProperty.call
70/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
71/******/
72/******/ // __webpack_public_path__
73/******/ __webpack_require__.p = "";
74/******/
75/******/ // Load entry module and return exports
76/******/ return __webpack_require__(__webpack_require__.s = 6);
77/******/ })
78/************************************************************************/
79/******/ ([
80/* 0 */
81/***/ (function(module, exports) {
82
83// Assume className is simple and can be found at top-level (window).
84// Fallback to eval to handle cases like 'My.React.ComponentName'.
85// Also, try to gracefully import Babel 6 style default exports
86var topLevel = typeof window === "undefined" ? this : window;
87
88module.exports = function(className) {
89 var constructor;
90 // Try to access the class globally first
91 constructor = topLevel[className];
92
93 // If that didn't work, try eval
94 if (!constructor) {
95 constructor = eval(className);
96 }
97
98 // Lastly, if there is a default attribute try that
99 if (constructor && constructor['default']) {
100 constructor = constructor['default'];
101 }
102
103 return constructor;
104}
105
106
107/***/ }),
108/* 1 */
109/***/ (function(module, exports, __webpack_require__) {
110
111var nativeEvents = __webpack_require__(7)
112var pjaxEvents = __webpack_require__(8)
113var turbolinksEvents = __webpack_require__(9)
114var turbolinksClassicDeprecatedEvents = __webpack_require__(11)
115var turbolinksClassicEvents = __webpack_require__(10)
116
117// see what things are globally available
118// and setup event handlers to those things
119module.exports = function(ujs) {
120 if (ujs.handleEvent) {
121 // We're calling this a second time -- remove previous handlers
122 if (typeof Turbolinks.EVENTS !== "undefined") {
123 turbolinksClassicEvents.teardown(ujs);
124 }
125 turbolinksEvents.teardown(ujs);
126 turbolinksClassicDeprecatedEvents.teardown(ujs);
127 pjaxEvents.teardown(ujs);
128 nativeEvents.teardown(ujs);
129 }
130
131 if (ujs.jQuery) {
132 ujs.handleEvent = function(eventName, callback) {
133 ujs.jQuery(document).on(eventName, callback);
134 };
135 ujs.removeEvent = function(eventName, callback) {
136 ujs.jQuery(document).off(eventName, callback);
137 }
138 } else if ('addEventListener' in window) {
139 ujs.handleEvent = function(eventName, callback) {
140 document.addEventListener(eventName, callback);
141 };
142 ujs.removeEvent = function(eventName, callback) {
143 document.removeEventListener(eventName, callback);
144 };
145 } else {
146 ujs.handleEvent = function(eventName, callback) {
147 window.attachEvent(eventName, callback);
148 };
149 ujs.removeEvent = function(eventName, callback) {
150 window.detachEvent(eventName, callback);
151 };
152 }
153
154 // Detect which kind of events to set up:
155 if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) {
156 if (typeof Turbolinks.EVENTS !== 'undefined') {
157 // Turbolinks.EVENTS is in classic version 2.4.0+
158 turbolinksClassicEvents.setup(ujs)
159 } else if (typeof Turbolinks.controller !== "undefined") {
160 // Turbolinks.controller is in version 5+
161 turbolinksEvents.setup(ujs);
162 } else {
163 turbolinksClassicDeprecatedEvents.setup(ujs);
164 }
165 } else if (typeof $ !== "undefined" && typeof $.pjax === 'function') {
166 pjaxEvents.setup(ujs);
167 } else {
168 nativeEvents.setup(ujs);
169 }
170}
171
172
173/***/ }),
174/* 2 */
175/***/ (function(module, exports, __webpack_require__) {
176
177// Make a function which:
178// - First tries to require the name
179// - Then falls back to global lookup
180var fromGlobal = __webpack_require__(0)
181var fromRequireContext = __webpack_require__(12)
182
183module.exports = function(reqctx) {
184 var fromCtx = fromRequireContext(reqctx)
185 return function(className) {
186 var component;
187 try {
188 // `require` will raise an error if this className isn't found:
189 component = fromCtx(className)
190 } catch (firstErr) {
191 // fallback to global:
192 try {
193 component = fromGlobal(className)
194 } catch (secondErr) {
195 console.error(firstErr)
196 console.error(secondErr)
197 }
198 }
199 return component
200 }
201}
202
203
204/***/ }),
205/* 3 */
206/***/ (function(module, exports) {
207
208module.exports = __WEBPACK_EXTERNAL_MODULE_3__;
209
210/***/ }),
211/* 4 */
212/***/ (function(module, exports) {
213
214module.exports = __WEBPACK_EXTERNAL_MODULE_4__;
215
216/***/ }),
217/* 5 */
218/***/ (function(module, exports) {
219
220module.exports = __WEBPACK_EXTERNAL_MODULE_5__;
221
222/***/ }),
223/* 6 */
224/***/ (function(module, exports, __webpack_require__) {
225
226var React = __webpack_require__(3)
227var ReactDOM = __webpack_require__(4)
228var ReactDOMServer = __webpack_require__(5)
229
230var detectEvents = __webpack_require__(1)
231var constructorFromGlobal = __webpack_require__(0)
232var constructorFromRequireContextWithGlobalFallback = __webpack_require__(2)
233
234var ReactRailsUJS = {
235 // This attribute holds the name of component which should be mounted
236 // example: `data-react-class="MyApp.Items.EditForm"`
237 CLASS_NAME_ATTR: 'data-react-class',
238
239 // This attribute holds JSON stringified props for initializing the component
240 // example: `data-react-props="{\"item\": { \"id\": 1, \"name\": \"My Item\"} }"`
241 PROPS_ATTR: 'data-react-props',
242
243 // If jQuery is detected, save a reference to it for event handlers
244 jQuery: (typeof window !== 'undefined') && (typeof window.jQuery !== 'undefined') && window.jQuery,
245
246 // helper method for the mount and unmount methods to find the
247 // `data-react-class` DOM elements
248 findDOMNodes: function(searchSelector) {
249 var classNameAttr = ReactRailsUJS.CLASS_NAME_ATTR
250 // we will use fully qualified paths as we do not bind the callbacks
251 var selector, parent;
252
253 switch (typeof searchSelector) {
254 case 'undefined':
255 selector = '[' + classNameAttr + ']';
256 parent = document;
257 break;
258 case 'object':
259 selector = '[' + classNameAttr + ']';
260 parent = searchSelector;
261 break;
262 case 'string':
263 selector = searchSelector + '[' + classNameAttr + '], ' +
264 searchSelector + ' [' + classNameAttr + ']';
265 parent = document;
266 break
267 default:
268 break;
269 }
270
271 if (ReactRailsUJS.jQuery) {
272 return ReactRailsUJS.jQuery(selector, parent);
273 } else {
274 return parent.querySelectorAll(selector);
275 }
276 },
277
278 // Get the constructor for a className (returns a React class)
279 // Override this function to lookup classes in a custom way,
280 // the default is ReactRailsUJS.ComponentGlobal
281 getConstructor: constructorFromGlobal,
282
283 // Given a Webpack `require.context`,
284 // try finding components with `require`,
285 // then falling back to global lookup.
286 useContext: function(requireContext) {
287 this.getConstructor = constructorFromRequireContextWithGlobalFallback(requireContext)
288 },
289
290 // Render `componentName` with `props` to a string,
291 // using the specified `renderFunction` from `react-dom/server`.
292 serverRender: function(renderFunction, componentName, props) {
293 var componentClass = this.getConstructor(componentName)
294 var element = React.createElement(componentClass, props)
295 return ReactDOMServer[renderFunction](element)
296 },
297
298 // Within `searchSelector`, find nodes which should have React components
299 // inside them, and mount them with their props.
300 mountComponents: function(searchSelector) {
301 var ujs = ReactRailsUJS
302 var nodes = ujs.findDOMNodes(searchSelector);
303
304 for (var i = 0; i < nodes.length; ++i) {
305 var node = nodes[i];
306 var className = node.getAttribute(ujs.CLASS_NAME_ATTR);
307 var constructor = ujs.getConstructor(className);
308 var propsJson = node.getAttribute(ujs.PROPS_ATTR);
309 var props = propsJson && JSON.parse(propsJson);
310
311 if (!constructor) {
312 var message = "Cannot find component: '" + className + "'"
313 if (console && console.log) {
314 console.log("%c[react-rails] %c" + message + " for element", "font-weight: bold", "", node)
315 }
316 throw new Error(message + ". Make sure your component is available to render.")
317 } else {
318 ReactDOM.render(React.createElement(constructor, props), node);
319 }
320 }
321 },
322
323 // Within `searchSelector`, find nodes which have React components
324 // inside them, and unmount those components.
325 unmountComponents: function(searchSelector) {
326 var nodes = ReactRailsUJS.findDOMNodes(searchSelector);
327
328 for (var i = 0; i < nodes.length; ++i) {
329 var node = nodes[i];
330 ReactDOM.unmountComponentAtNode(node);
331 }
332 },
333
334 // Check the global context for installed libraries
335 // and figure out which library to hook up to (pjax, Turbolinks, jQuery)
336 // This is called on load, but you can call it again if needed
337 // (It will unmount itself)
338 detectEvents: function() {
339 detectEvents(this)
340 },
341}
342
343// These stable references are so that handlers can be added and removed:
344ReactRailsUJS.handleMount = function(e) {
345 var target = undefined;
346 if (e && e.target) {
347 target = e.target;
348 }
349 ReactRailsUJS.mountComponents(target);
350}
351ReactRailsUJS.handleUnmount = function(e) {
352 var target = undefined;
353 if (e && e.target) {
354 target = e.target;
355 }
356 ReactRailsUJS.unmountComponents(target);
357}
358
359
360if (typeof window !== "undefined") {
361 // Only setup events for browser (not server-rendering)
362 ReactRailsUJS.detectEvents()
363}
364
365// It's a bit of a no-no to populate the global namespace,
366// but we really need it!
367// We need access to this object for server rendering, and
368// we can't do a dynamic `require`, so we'll grab it from here:
369self.ReactRailsUJS = ReactRailsUJS
370
371module.exports = ReactRailsUJS
372
373
374/***/ }),
375/* 7 */
376/***/ (function(module, exports) {
377
378module.exports = {
379 // Attach handlers to browser events to mount
380 // (There are no unmount handlers since the page is destroyed on navigation)
381 setup: function(ujs) {
382 if (ujs.jQuery) {
383 // Use jQuery if it's present:
384 ujs.handleEvent("ready", ujs.handleMount);
385 } else if ('addEventListener' in window) {
386 ujs.handleEvent('DOMContentLoaded', ujs.handleMount);
387 } else {
388 // add support to IE8 without jQuery
389 ujs.handleEvent('onload', ujs.handleMount);
390 }
391 },
392
393 teardown: function(ujs) {
394 ujs.removeEvent("ready", ujs.handleMount);
395 ujs.removeEvent('DOMContentLoaded', ujs.handleMount);
396 ujs.removeEvent('onload', ujs.handleMount);
397 }
398}
399
400
401/***/ }),
402/* 8 */
403/***/ (function(module, exports) {
404
405module.exports = {
406 // pjax support
407 setup: function(ujs) {
408 ujs.handleEvent('ready', ujs.handleMount);
409 ujs.handleEvent('pjax:end', ujs.handleMount);
410 ujs.handleEvent('pjax:beforeReplace', ujs.handleUnmount);
411 },
412
413 teardown: function(ujs) {
414 ujs.removeEvent('ready', ujs.handleMount);
415 ujs.removeEvent('pjax:end', ujs.handleMount);
416 ujs.removeEvent('pjax:beforeReplace', ujs.handleUnmount);
417 },
418}
419
420
421/***/ }),
422/* 9 */
423/***/ (function(module, exports) {
424
425module.exports = {
426 // Turbolinks 5+ got rid of named events (?!)
427 setup: function(ujs) {
428 ujs.handleEvent('DOMContentLoaded', ujs.handleMount)
429 ujs.handleEvent('turbolinks:render', ujs.handleMount)
430 ujs.handleEvent('turbolinks:before-render', ujs.handleUnmount)
431 },
432
433 teardown: function(ujs) {
434 ujs.removeEvent('DOMContentLoaded', ujs.handleMount)
435 ujs.removeEvent('turbolinks:render', ujs.handleMount)
436 ujs.removeEvent('turbolinks:before-render', ujs.handleUnmount)
437 },
438}
439
440
441/***/ }),
442/* 10 */
443/***/ (function(module, exports) {
444
445module.exports = {
446 // Attach handlers to Turbolinks-Classic events
447 // for mounting and unmounting components
448 setup: function(ujs) {
449 ujs.handleEvent(Turbolinks.EVENTS.CHANGE, ujs.handleMount);
450 ujs.handleEvent(Turbolinks.EVENTS.BEFORE_UNLOAD, ujs.handleUnmount);
451 },
452 teardown: function(ujs) {
453 ujs.removeEvent(Turbolinks.EVENTS.CHANGE, ujs.handleMount);
454 ujs.removeEvent(Turbolinks.EVENTS.BEFORE_UNLOAD, ujs.handleUnmount);
455 }
456}
457
458
459/***/ }),
460/* 11 */
461/***/ (function(module, exports) {
462
463module.exports = {
464 // Before Turbolinks 2.4.0, Turbolinks didn't
465 // have named events and didn't have a before-unload event.
466 // Also, it didn't work with the Turbolinks cache, see
467 // https://github.com/reactjs/react-rails/issues/87
468 setup: function(ujs) {
469 Turbolinks.pagesCached(0)
470 ujs.handleEvent('page:change', ujs.handleMount);
471 ujs.handleEvent('page:receive', ujs.handleUnmount);
472 },
473 teardown: function(ujs) {
474 ujs.removeEvent('page:change', ujs.handleMount);
475 ujs.removeEvent('page:receive', ujs.handleUnmount);
476 }
477}
478
479
480/***/ }),
481/* 12 */
482/***/ (function(module, exports) {
483
484// Load React components by requiring them from "components/", for example:
485//
486// - "pages/index" -> `require("components/pages/index")`
487// - "pages/show.Header" -> `require("components/pages/show").Header`
488// - "pages/show.Body.Content" -> `require("components/pages/show").Body.Content`
489//
490module.exports = function(reqctx) {
491 return function(className) {
492 var parts = className.split(".")
493 var filename = parts.shift()
494 var keys = parts
495 // Load the module:
496 var component = reqctx("./" + filename)
497 // Then access each key:
498 keys.forEach(function(k) {
499 component = component[k]
500 })
501 // support `export default`
502 if (component.__esModule) {
503 component = component["default"]
504 }
505 return component
506 }
507}
508
509
510/***/ })
511/******/ ]);
512});
\No newline at end of file