1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
|
8 |
|
9 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
10 |
|
11 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
12 |
|
13 | exports.default = enhanceWithRadium;
|
14 |
|
15 | var _react = require('react');
|
16 |
|
17 | var _propTypes = require('prop-types');
|
18 |
|
19 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
20 |
|
21 | var _styleKeeper = require('./style-keeper');
|
22 |
|
23 | var _styleKeeper2 = _interopRequireDefault(_styleKeeper);
|
24 |
|
25 | var _resolveStyles2 = require('./resolve-styles');
|
26 |
|
27 | var _resolveStyles3 = _interopRequireDefault(_resolveStyles2);
|
28 |
|
29 | var _getRadiumStyleState = require('./get-radium-style-state');
|
30 |
|
31 | var _getRadiumStyleState2 = _interopRequireDefault(_getRadiumStyleState);
|
32 |
|
33 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
34 |
|
35 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
36 |
|
37 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
38 |
|
39 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
40 |
|
41 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
42 |
|
43 | var KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES = ['arguments', 'callee', 'caller', 'length', 'name', 'prototype', 'type'];
|
44 |
|
45 | var RADIUM_PROTO = void 0;
|
46 | var RADIUM_METHODS = void 0;
|
47 |
|
48 | function copyProperties(source, target) {
|
49 | Object.getOwnPropertyNames(source).forEach(function (key) {
|
50 | if (KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES.indexOf(key) < 0 && !target.hasOwnProperty(key)) {
|
51 | var descriptor = Object.getOwnPropertyDescriptor(source, key);
|
52 | Object.defineProperty(target, key, descriptor);
|
53 | }
|
54 | });
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function isStateless(component) {
|
62 | var proto = component.prototype || {};
|
63 |
|
64 | return !component.isReactComponent && !proto.isReactComponent && !component.render && !proto.render;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | function isNativeClass(component) {
|
70 | return typeof component === 'function' && /^\s*class\s+/.test(component.toString());
|
71 | }
|
72 |
|
73 | function enhanceWithRadium(configOrComposedComponent) {
|
74 | var _class, _temp;
|
75 |
|
76 | var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
77 |
|
78 | if (typeof configOrComposedComponent !== 'function') {
|
79 | var newConfig = _extends({}, config, configOrComposedComponent);
|
80 | return function (configOrComponent) {
|
81 | return enhanceWithRadium(configOrComponent, newConfig);
|
82 | };
|
83 | }
|
84 |
|
85 | var component = configOrComposedComponent;
|
86 | var _ComposedComponent = component;
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | if (isNativeClass(_ComposedComponent)) {
|
92 | _ComposedComponent = function (OrigComponent) {
|
93 | function NewComponent() {
|
94 |
|
95 | var obj = Reflect.construct(OrigComponent, arguments, this.constructor);
|
96 |
|
97 | return obj;
|
98 | }
|
99 |
|
100 |
|
101 | Reflect.setPrototypeOf(NewComponent.prototype, OrigComponent.prototype);
|
102 |
|
103 | Reflect.setPrototypeOf(NewComponent, OrigComponent);
|
104 |
|
105 | return NewComponent;
|
106 | }(_ComposedComponent);
|
107 | }
|
108 |
|
109 |
|
110 | if (isStateless(_ComposedComponent)) {
|
111 | _ComposedComponent = function (_Component) {
|
112 | _inherits(ComposedComponent, _Component);
|
113 |
|
114 | function ComposedComponent() {
|
115 | _classCallCheck(this, ComposedComponent);
|
116 |
|
117 | return _possibleConstructorReturn(this, (ComposedComponent.__proto__ || Object.getPrototypeOf(ComposedComponent)).apply(this, arguments));
|
118 | }
|
119 |
|
120 | _createClass(ComposedComponent, [{
|
121 | key: 'render',
|
122 | value: function render() {
|
123 | return component(this.props, this.context);
|
124 | }
|
125 | }]);
|
126 |
|
127 | return ComposedComponent;
|
128 | }(_react.Component);
|
129 |
|
130 | _ComposedComponent.displayName = component.displayName || component.name;
|
131 | }
|
132 |
|
133 |
|
134 | if (_ComposedComponent === component) {
|
135 | _ComposedComponent = function (_ComposedComponent2) {
|
136 | _inherits(ComposedComponent, _ComposedComponent2);
|
137 |
|
138 | function ComposedComponent() {
|
139 | _classCallCheck(this, ComposedComponent);
|
140 |
|
141 | return _possibleConstructorReturn(this, (ComposedComponent.__proto__ || Object.getPrototypeOf(ComposedComponent)).apply(this, arguments));
|
142 | }
|
143 |
|
144 | return ComposedComponent;
|
145 | }(_ComposedComponent);
|
146 | }
|
147 |
|
148 | var RadiumEnhancer = (_temp = _class = function (_ComposedComponent3) {
|
149 | _inherits(RadiumEnhancer, _ComposedComponent3);
|
150 |
|
151 | function RadiumEnhancer() {
|
152 | _classCallCheck(this, RadiumEnhancer);
|
153 |
|
154 | var _this3 = _possibleConstructorReturn(this, (RadiumEnhancer.__proto__ || Object.getPrototypeOf(RadiumEnhancer)).apply(this, arguments));
|
155 |
|
156 | _this3.state = _this3.state || {};
|
157 | _this3.state._radiumStyleState = {};
|
158 | _this3._radiumIsMounted = true;
|
159 |
|
160 | var self = _this3;
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | RADIUM_METHODS.forEach(function (name) {
|
167 | var thisDesc = Object.getOwnPropertyDescriptor(self, name);
|
168 | var thisMethod = (thisDesc || {}).value;
|
169 |
|
170 |
|
171 | if (!thisMethod) {
|
172 | return;
|
173 | }
|
174 |
|
175 | var radiumDesc = Object.getOwnPropertyDescriptor(RADIUM_PROTO, name);
|
176 | var radiumProtoMethod = radiumDesc.value;
|
177 | var superProtoMethod = _ComposedComponent.prototype[name];
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | if (!superProtoMethod && thisMethod !== radiumProtoMethod) {
|
184 |
|
185 | Object.defineProperty(_ComposedComponent.prototype, name, thisDesc);
|
186 |
|
187 |
|
188 |
|
189 | delete self[name];
|
190 | }
|
191 | });
|
192 | return _this3;
|
193 | }
|
194 |
|
195 | _createClass(RadiumEnhancer, [{
|
196 | key: 'componentWillUnmount',
|
197 | value: function componentWillUnmount() {
|
198 | if (_get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentWillUnmount', this)) {
|
199 | _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentWillUnmount', this).call(this);
|
200 | }
|
201 |
|
202 | this._radiumIsMounted = false;
|
203 |
|
204 | if (this._radiumMouseUpListener) {
|
205 | this._radiumMouseUpListener.remove();
|
206 | }
|
207 |
|
208 | if (this._radiumMediaQueryListenersByQuery) {
|
209 | Object.keys(this._radiumMediaQueryListenersByQuery).forEach(function (query) {
|
210 | this._radiumMediaQueryListenersByQuery[query].remove();
|
211 | }, this);
|
212 | }
|
213 | }
|
214 | }, {
|
215 | key: 'getChildContext',
|
216 | value: function getChildContext() {
|
217 | var superChildContext = _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'getChildContext', this) ? _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'getChildContext', this).call(this) : {};
|
218 |
|
219 | if (!this.props.radiumConfig) {
|
220 | return superChildContext;
|
221 | }
|
222 |
|
223 | var newContext = _extends({}, superChildContext);
|
224 |
|
225 | if (this.props.radiumConfig) {
|
226 | newContext._radiumConfig = this.props.radiumConfig;
|
227 | }
|
228 |
|
229 | return newContext;
|
230 | }
|
231 | }, {
|
232 | key: 'render',
|
233 | value: function render() {
|
234 | var renderedElement = _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'render', this).call(this);
|
235 | var currentConfig = this.props.radiumConfig || this.context._radiumConfig || config;
|
236 |
|
237 | if (config && currentConfig !== config) {
|
238 | currentConfig = _extends({}, config, currentConfig);
|
239 | }
|
240 |
|
241 | var _resolveStyles = (0, _resolveStyles3.default)(this, renderedElement, currentConfig),
|
242 | extraStateKeyMap = _resolveStyles.extraStateKeyMap,
|
243 | element = _resolveStyles.element;
|
244 |
|
245 | this._extraRadiumStateKeys = Object.keys(extraStateKeyMap);
|
246 |
|
247 | return element;
|
248 | }
|
249 |
|
250 |
|
251 |
|
252 | }, {
|
253 | key: 'componentDidUpdate',
|
254 | value: function componentDidUpdate(prevProps, prevState) {
|
255 | if (_get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentDidUpdate', this)) {
|
256 | _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentDidUpdate', this).call(this, prevProps, prevState);
|
257 | }
|
258 |
|
259 | if (this._extraRadiumStateKeys.length > 0) {
|
260 | var trimmedRadiumState = this._extraRadiumStateKeys.reduce(function (state, key) {
|
261 | var extraStateKey = state[key],
|
262 | remainingState = _objectWithoutProperties(state, [key]);
|
263 |
|
264 | return remainingState;
|
265 | }, (0, _getRadiumStyleState2.default)(this));
|
266 |
|
267 | this._lastRadiumState = trimmedRadiumState;
|
268 | this.setState({ _radiumStyleState: trimmedRadiumState });
|
269 | }
|
270 | }
|
271 |
|
272 |
|
273 | }]);
|
274 |
|
275 | return RadiumEnhancer;
|
276 | }(_ComposedComponent), _class._isRadiumEnhanced = true, _temp);
|
277 |
|
278 |
|
279 |
|
280 | RADIUM_PROTO = RadiumEnhancer.prototype;
|
281 | RADIUM_METHODS = Object.getOwnPropertyNames(RADIUM_PROTO).filter(function (n) {
|
282 | return n !== 'constructor' && typeof RADIUM_PROTO[n] === 'function';
|
283 | });
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | copyProperties(component, RadiumEnhancer);
|
290 |
|
291 | if (process.env.NODE_ENV !== 'production') {
|
292 |
|
293 |
|
294 |
|
295 | copyProperties(_ComposedComponent.prototype, RadiumEnhancer.prototype);
|
296 | }
|
297 |
|
298 | if (RadiumEnhancer.propTypes && RadiumEnhancer.propTypes.style) {
|
299 | RadiumEnhancer.propTypes = _extends({}, RadiumEnhancer.propTypes, {
|
300 | style: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.object])
|
301 | });
|
302 | }
|
303 |
|
304 | RadiumEnhancer.displayName = component.displayName || component.name || 'Component';
|
305 |
|
306 | RadiumEnhancer.contextTypes = _extends({}, RadiumEnhancer.contextTypes, {
|
307 | _radiumConfig: _propTypes2.default.object,
|
308 | _radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
|
309 | });
|
310 |
|
311 | RadiumEnhancer.childContextTypes = _extends({}, RadiumEnhancer.childContextTypes, {
|
312 | _radiumConfig: _propTypes2.default.object,
|
313 | _radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
|
314 | });
|
315 |
|
316 | return RadiumEnhancer;
|
317 | } |
\ | No newline at end of file |