-
Notifications
You must be signed in to change notification settings - Fork 925
Expand file tree
/
Copy pathwithScriptjs.js
More file actions
185 lines (144 loc) · 5.03 KB
/
Copy pathwithScriptjs.js
File metadata and controls
185 lines (144 loc) · 5.03 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict"
Object.defineProperty(exports, "__esModule", {
value: true,
})
var _objectWithoutProperties2 = require("babel-runtime/helpers/objectWithoutProperties")
var _objectWithoutProperties3 = _interopRequireDefault(
_objectWithoutProperties2
)
var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of")
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf)
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck")
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2)
var _createClass2 = require("babel-runtime/helpers/createClass")
var _createClass3 = _interopRequireDefault(_createClass2)
var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn")
var _possibleConstructorReturn3 = _interopRequireDefault(
_possibleConstructorReturn2
)
var _inherits2 = require("babel-runtime/helpers/inherits")
var _inherits3 = _interopRequireDefault(_inherits2)
var _bind2 = require("lodash/bind")
var _bind3 = _interopRequireDefault(_bind2)
exports.withScriptjs = withScriptjs
var _invariant = require("invariant")
var _invariant2 = _interopRequireDefault(_invariant)
var _canUseDom = require("can-use-dom")
var _canUseDom2 = _interopRequireDefault(_canUseDom)
var _recompose = require("recompose")
var _propTypes = require("prop-types")
var _propTypes2 = _interopRequireDefault(_propTypes)
var _react = require("react")
var _react2 = _interopRequireDefault(_react)
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj }
}
var LOADING_STATE_NONE = "NONE"
var LOADING_STATE_BEGIN = "BEGIN"
var LOADING_STATE_LOADED = "LOADED"
function withScriptjs(BaseComponent) {
var factory = _react2.default.createFactory(BaseComponent)
var Container = (function(_React$PureComponent) {
;(0, _inherits3.default)(Container, _React$PureComponent)
function Container() {
var _ref
var _temp, _this, _ret
;(0, _classCallCheck3.default)(this, Container)
for (
var _len = arguments.length, args = Array(_len), _key = 0;
_key < _len;
_key++
) {
args[_key] = arguments[_key]
}
return (
(_ret = ((_temp = ((_this = (0, _possibleConstructorReturn3.default)(
this,
(_ref =
Container.__proto__ ||
(0, _getPrototypeOf2.default)(Container)).call.apply(
_ref,
[this].concat(args)
)
)),
_this)),
(_this.state = {
loadingState: LOADING_STATE_NONE,
}),
(_this.isUnmounted = false),
(_this.handleLoaded = (0, _bind3.default)(_this.handleLoaded, _this)),
_temp)),
(0, _possibleConstructorReturn3.default)(_this, _ret)
)
}
;(0, _createClass3.default)(Container, [
{
key: "handleLoaded",
value: function handleLoaded() {
if (this.isUnmounted) {
return
}
this.setState({
loadingState: LOADING_STATE_LOADED,
})
},
},
{
key: "componentDidMount",
value: function componentDidMount() {
var _props = this.props,
loadingElement = _props.loadingElement,
googleMapURL = _props.googleMapURL
;(0, _invariant2.default)(
!!loadingElement && !!googleMapURL,
"Required props loadingElement or googleMapURL is missing. You need to provide both of them."
)
var loadingState = this.state.loadingState
if (loadingState !== LOADING_STATE_NONE || !_canUseDom2.default) {
return
}
this.setState({
loadingState: LOADING_STATE_BEGIN,
})
// Don't load scriptjs as a dependency since we do not want this module be used on server side.
// eslint-disable-next-line global-require
var scriptjs = require("scriptjs")
scriptjs(googleMapURL, this.handleLoaded)
},
},
{
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.isUnmounted = true
},
},
{
key: "render",
value: function render() {
var _props2 = this.props,
loadingElement = _props2.loadingElement,
googleMapURL = _props2.googleMapURL,
restProps = (0, _objectWithoutProperties3.default)(_props2, [
"loadingElement",
"googleMapURL",
])
var loadingState = this.state.loadingState
if (loadingState === LOADING_STATE_LOADED) {
return factory(restProps)
} else {
return loadingElement
}
},
},
])
return Container
})(_react2.default.PureComponent)
Container.displayName =
"withScriptjs(" + (0, _recompose.getDisplayName)(BaseComponent) + ")"
Container.propTypes = {
loadingElement: _propTypes2.default.node.isRequired,
googleMapURL: _propTypes2.default.string.isRequired,
}
return Container
}
exports.default = withScriptjs