-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.js
94 lines (77 loc) · 2.72 KB
/
renderer.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
var React = require('react');
var Router = require('react-router');
var registerElement, ReactElements;
if (typeof(Duktape) === 'object') {
ReactElements = __ReactElements;
registerElement = __registerElement;
function consoleShim() {
print(Array.prototype.join.call(arguments, ' '));
}
global.console = {};
['debug', 'log', 'info', 'warn', 'warning', 'error'].forEach(function(i) {
global.console[i] = consoleShim;
});
} else {
ReactElements = {};
registerElement = function(elementName, element) {
ReactElements[elementName] = element;
};
}
var renderPath, renderElement;
var __routes;
var registerRoutes = function(routes) {
__routes = routes;
};
if (typeof(Duktape) === 'object') {
renderPath = function(path) {
var markup;
Router.run(__routes, path, function(Handler) {
markup = React.renderToString(<Handler />);
});
return markup;
};
renderElement = function(elementName, props) {
var re = /data-reactid="([^"]*)"/;
var markup = React.renderToString(
React.createElement(ReactElements[elementName], props)
);
var id = re.exec(markup)[1];
var propsAsString = null;
if (props) {
propsAsString = '"' + encodeURIComponent(JSON.stringify(props)) + '"';
}
// Wrapper must live outside of React or checksums mismatch
var scriptTag = (
'<script type="text/javascript">' +
'window.__reactElements = window.__reactElements || []; ' +
'window.__reactElements.push(["' + id + '", "' + elementName + '", JSON.parse(decodeURIComponent(' + propsAsString + '))]);' +
'</script>'
);
return ('<div>' + markup + '</div>' + scriptTag);
};
// Also make these functions available for server-side rendering
global.renderPath = renderPath;
global.renderElement = renderElement;
} else {
renderPath = function(path) {
Router.run(__routes, Router.HistoryLocation, function(Handler) {
React.render(<Handler />, document);
});
};
renderElement = function(element, props, ele) {
React.render(React.createElement(ReactElements[element], props), ele);
};
document.addEventListener('DOMContentLoaded', function() {
window.__reactElements.forEach(function(eleData) {
var ele = document.querySelector('[data-reactid="' + eleData[0] + '"]');
renderElement(eleData[1], eleData[2], ele.parentNode);
});
});
}
module.exports = {
registerElement: registerElement,
ReactElements: ReactElements,
renderPath: renderPath,
renderElement: renderElement,
registerRoutes: registerRoutes
};