|
1 | | -// Unobtrusive scripting adapter for React |
2 | | -(function(document, window, React) { |
3 | | - var CLASS_NAME_ATTR = 'data-react-class'; |
4 | | - var PROPS_ATTR = 'data-react-props'; |
| 1 | +/*globals React, Turbolinks*/ |
5 | 2 |
|
| 3 | +// Unobtrusive scripting adapter for React |
| 4 | +(function(document, window) { |
6 | 5 | // jQuery is optional. Use it to support legacy browsers. |
7 | | - var $ = (typeof jQuery !== 'undefined') && jQuery; |
8 | | - |
9 | | - var findReactDOMNodes = function() { |
10 | | - var SELECTOR = '[' + CLASS_NAME_ATTR + ']'; |
11 | | - if ($) { |
12 | | - return $(SELECTOR); |
13 | | - } else { |
14 | | - return document.querySelectorAll(SELECTOR); |
15 | | - } |
16 | | - }; |
17 | | - |
18 | | - var mountReactComponents = function() { |
19 | | - var nodes = findReactDOMNodes(); |
20 | | - for (var i = 0; i < nodes.length; ++i) { |
21 | | - var node = nodes[i]; |
22 | | - var className = node.getAttribute(CLASS_NAME_ATTR); |
23 | | - // Assume className is simple and can be found at top-level (window). |
24 | | - // Fallback to eval to handle cases like 'My.React.ComponentName'. |
25 | | - var constructor = window[className] || eval.call(window, className); |
26 | | - var propsJson = node.getAttribute(PROPS_ATTR); |
27 | | - var props = propsJson && JSON.parse(propsJson); |
28 | | - React.render(React.createElement(constructor, props), node); |
29 | | - } |
30 | | - }; |
31 | | - |
32 | | - var unmountReactComponents = function() { |
33 | | - var nodes = findReactDOMNodes(); |
34 | | - for (var i = 0; i < nodes.length; ++i) { |
35 | | - React.unmountComponentAtNode(nodes[i]); |
| 6 | + var $ = (typeof window.jQuery !== 'undefined') && window.jQuery; |
| 7 | + |
| 8 | + // create the namespace |
| 9 | + window.ReactRailsUJS = { |
| 10 | + CLASS_NAME_ATTR: 'data-react-class', |
| 11 | + PROPS_ATTR: 'data-react-props', |
| 12 | + // helper method for the mount and unmount methods to find the |
| 13 | + // `data-react-class` DOM elements |
| 14 | + findDOMNodes: function() { |
| 15 | + // we will use fully qualified paths as we do not bind the callbacks |
| 16 | + var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']'; |
| 17 | + |
| 18 | + if ($) { |
| 19 | + return $(selector); |
| 20 | + } else { |
| 21 | + return document.querySelectorAll(selector); |
| 22 | + } |
| 23 | + }, |
| 24 | + |
| 25 | + mountComponents: function() { |
| 26 | + var nodes = window.ReactRailsUJS.findDOMNodes(); |
| 27 | + |
| 28 | + for (var i = 0; i < nodes.length; ++i) { |
| 29 | + var node = nodes[i]; |
| 30 | + var className = node.getAttribute(window.ReactRailsUJS.CLASS_NAME_ATTR); |
| 31 | + |
| 32 | + // Assume className is simple and can be found at top-level (window). |
| 33 | + // Fallback to eval to handle cases like 'My.React.ComponentName'. |
| 34 | + var constructor = window[className] || eval.call(window, className); |
| 35 | + var propsJson = node.getAttribute(window.ReactRailsUJS.PROPS_ATTR); |
| 36 | + var props = propsJson && JSON.parse(propsJson); |
| 37 | + |
| 38 | + React.render(React.createElement(constructor, props), node); |
| 39 | + } |
| 40 | + }, |
| 41 | + |
| 42 | + unmountComponents: function() { |
| 43 | + var nodes = window.ReactRailsUJS.findDOMNodes(); |
| 44 | + |
| 45 | + for (var i = 0; i < nodes.length; ++i) { |
| 46 | + var node = nodes[i]; |
| 47 | + |
| 48 | + React.unmountComponentAtNode(node); |
| 49 | + // now remove the `data-react-class` wrapper as well |
| 50 | + node.parentElement && node.parentElement.removeChild(node); |
| 51 | + } |
36 | 52 | } |
37 | 53 | }; |
38 | 54 |
|
39 | | - var handleTurbolinksEvents = function() { |
| 55 | + // functions not exposed publicly |
| 56 | + function handleTurbolinksEvents () { |
40 | 57 | var handleEvent; |
| 58 | + |
41 | 59 | if ($) { |
42 | 60 | handleEvent = function(eventName, callback) { |
43 | 61 | $(document).on(eventName, callback); |
44 | | - } |
| 62 | + }; |
| 63 | + |
45 | 64 | } else { |
46 | 65 | handleEvent = function(eventName, callback) { |
47 | 66 | document.addEventListener(eventName, callback); |
48 | | - } |
| 67 | + }; |
49 | 68 | } |
50 | | - handleEvent('page:change', mountReactComponents); |
51 | | - handleEvent('page:receive', unmountReactComponents); |
52 | | - }; |
| 69 | + handleEvent('page:change', window.ReactRailsUJS.mountComponents); |
| 70 | + handleEvent('page:receive', window.ReactRailsUJS.unmountComponents); |
| 71 | + } |
53 | 72 |
|
54 | | - var handleNativeEvents = function() { |
55 | | - if ($) { |
56 | | - $(mountReactComponents); |
57 | | - $(window).unload(unmountReactComponents); |
| 73 | + function handleNativeEvents() { |
| 74 | + if ($) { |
| 75 | + $(window.ReactRailsUJS.mountComponents); |
| 76 | + $(window).unload(window.ReactRailsUJS.unmountComponents); |
| 77 | + |
58 | 78 | } else { |
59 | | - document.addEventListener('DOMContentLoaded', mountReactComponents); |
60 | | - window.addEventListener('unload', unmountReactComponents); |
| 79 | + document.addEventListener('DOMContentLoaded', window.ReactRailsUJS.mountComponents); |
| 80 | + window.addEventListener('unload', window.ReactRailsUJS.unmountComponents); |
61 | 81 | } |
62 | | - }; |
| 82 | + } |
63 | 83 |
|
64 | 84 | if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) { |
65 | 85 | handleTurbolinksEvents(); |
66 | 86 | } else { |
67 | 87 | handleNativeEvents(); |
68 | 88 | } |
69 | | -})(document, window, React); |
| 89 | +})(document, window); |
0 commit comments