-
Notifications
You must be signed in to change notification settings - Fork 44
/
dom.js
57 lines (51 loc) · 1.58 KB
/
dom.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
'use strict';
var React = require('react');
var ReactDOM = require('react-dom');
var ReactRouterDOM = require('react-router-dom');
var hopsConfig = require('hops-config');
var Context = require('./lib/common').Context;
exports.Context = exports.createContext = Context.extend({
initialize: function (options) {
this.mountpoint = options.mountpoint || '#main';
},
bootstrap: function () {
return Promise.resolve();
},
enhanceElement: function (reactElement) {
return React.createElement(
ReactRouterDOM.BrowserRouter,
{ basename: hopsConfig.basePath },
reactElement
);
},
prepareRender: function (enhancedElement) {
return Promise.resolve();
},
getMountpoint: function () {
return document.querySelector(this.mountpoint);
}
});
exports.render = function (reactElement, context) {
if (!(context instanceof exports.Context)) {
context = new exports.Context(context);
}
return function () {
var mountpoint = context.getMountpoint();
if (mountpoint.hasAttribute('data-hopsroot')) {
ReactDOM.unmountComponentAtNode(mountpoint);
} else {
mountpoint.setAttribute('data-hopsroot', '');
}
return context.bootstrap().then(function () {
var enhancedElement = context.enhanceElement(reactElement);
return context.prepareRender(enhancedElement).then(function () {
if (ReactDOM.hydrate) {
ReactDOM.hydrate(enhancedElement, mountpoint);
} else {
ReactDOM.render(enhancedElement, mountpoint);
}
});
});
};
};
Object.assign(exports, require('./lib/components'));