-
Notifications
You must be signed in to change notification settings - Fork 6
/
LayerMixin.js
116 lines (96 loc) · 2.67 KB
/
LayerMixin.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
"use strict";
var React = require('react');
var CustomPropTypes = require('./modules/CustomPropTypes');
var Container = require('./modules/Container');
var documentBodyContainer = require('./modules/documentBodyContainer');
var ReactLayer = require('./modules/ReactLayer');
function createContainer(container) {
if (container.nodeType === 1) {
return new Container(container);
}
// Handle extracting node from a React component
// NOTE: this is deprecated behaviour and will be removed in 1.0
if (container.getDOMNode) {
return new Container(container.getDOMNode());
}
return container;
}
var LayerMixin = {
propTypes: {
container: CustomPropTypes.mountable.isRequired
},
getDefaultProps: function() {
return {
container: documentBodyContainer
};
},
componentWillMount: function() {
if (typeof this.renderLayer !== 'function') {
throw new Error(
'createClass(...): Class specification of ' +
(this.constructor.displayName || 'ReactCompositeComponent') + ' ' +
'using LayerMixin must implement a `renderLayer` method.'
);
}
},
componentDidMount: function() {
this.__container = createContainer(this.props.container);
this._updateLayer();
},
componentDidUpdate: function(prevProps) {
if (this.props.container !== prevProps.container) {
this._destroyLayer();
this.__container = createContainer(this.props.container);
}
this._updateLayer();
},
componentWillUnmount: function() {
this._destroyLayer();
},
/**
* Render layer
*
* To be implemented within your `React.createClass({})`
*/
// renderLayer() { return <MyLayer />; },
/**
* Update layer
*
* @private
*/
_updateLayer: function() {
var layer = this.renderLayer();
if (layer === null) {
// No layer so just remove any existing components if they exist.
this._destroyLayer();
return;
}
if (!React.isValidComponent(layer)) {
throw new Error(
(this.constructor.displayName || 'ReactCompositeComponent') +
'.renderLayer(): A valid ReactComponent must be returned. You may have ' +
'returned undefined, an array or some other invalid object.'
);
}
if (!this.__layer) {
this.__layer = new ReactLayer();
this.__container.addLayer(this.__layer);
}
this.__layer.render(layer);
},
/**
* Destroy layer
*
* Unmount component and remove container element from the DOM.
*
* @private
*/
_destroyLayer: function() {
if (!this.__layer) {
return;
}
this.__container.removeLayer(this.__layer);
this.__layer = null;
}
};
module.exports = LayerMixin;