|
| 1 | +(function (global, factory) { |
| 2 | + if (typeof define === "function" && define.amd) { |
| 3 | + define(['exports', 'react', 'react-dom', './lib/ScriptCache', './lib/GoogleApi'], factory); |
| 4 | + } else if (typeof exports !== "undefined") { |
| 5 | + factory(exports, require('react'), require('react-dom'), require('./lib/ScriptCache'), require('./lib/GoogleApi')); |
| 6 | + } else { |
| 7 | + var mod = { |
| 8 | + exports: {} |
| 9 | + }; |
| 10 | + factory(mod.exports, global.react, global.reactDom, global.ScriptCache, global.GoogleApi); |
| 11 | + global.GoogleApiComponent = mod.exports; |
| 12 | + } |
| 13 | +})(this, function (exports, _react, _reactDom, _ScriptCache, _GoogleApi) { |
| 14 | + 'use strict'; |
| 15 | + |
| 16 | + Object.defineProperty(exports, "__esModule", { |
| 17 | + value: true |
| 18 | + }); |
| 19 | + exports.wrapper = undefined; |
| 20 | + |
| 21 | + var _react2 = _interopRequireDefault(_react); |
| 22 | + |
| 23 | + var _reactDom2 = _interopRequireDefault(_reactDom); |
| 24 | + |
| 25 | + var _GoogleApi2 = _interopRequireDefault(_GoogleApi); |
| 26 | + |
| 27 | + function _interopRequireDefault(obj) { |
| 28 | + return obj && obj.__esModule ? obj : { |
| 29 | + default: obj |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + function _classCallCheck(instance, Constructor) { |
| 34 | + if (!(instance instanceof Constructor)) { |
| 35 | + throw new TypeError("Cannot call a class as a function"); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + var _createClass = function () { |
| 40 | + function defineProperties(target, props) { |
| 41 | + for (var i = 0; i < props.length; i++) { |
| 42 | + var descriptor = props[i]; |
| 43 | + descriptor.enumerable = descriptor.enumerable || false; |
| 44 | + descriptor.configurable = true; |
| 45 | + if ("value" in descriptor) descriptor.writable = true; |
| 46 | + Object.defineProperty(target, descriptor.key, descriptor); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return function (Constructor, protoProps, staticProps) { |
| 51 | + if (protoProps) defineProperties(Constructor.prototype, protoProps); |
| 52 | + if (staticProps) defineProperties(Constructor, staticProps); |
| 53 | + return Constructor; |
| 54 | + }; |
| 55 | + }(); |
| 56 | + |
| 57 | + function _possibleConstructorReturn(self, call) { |
| 58 | + if (!self) { |
| 59 | + throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); |
| 60 | + } |
| 61 | + |
| 62 | + return call && (typeof call === "object" || typeof call === "function") ? call : self; |
| 63 | + } |
| 64 | + |
| 65 | + function _inherits(subClass, superClass) { |
| 66 | + if (typeof superClass !== "function" && superClass !== null) { |
| 67 | + throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); |
| 68 | + } |
| 69 | + |
| 70 | + subClass.prototype = Object.create(superClass && superClass.prototype, { |
| 71 | + constructor: { |
| 72 | + value: subClass, |
| 73 | + enumerable: false, |
| 74 | + writable: true, |
| 75 | + configurable: true |
| 76 | + } |
| 77 | + }); |
| 78 | + if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; |
| 79 | + } |
| 80 | + |
| 81 | + var defaultMapConfig = {}; |
| 82 | + |
| 83 | + var serialize = function serialize(obj) { |
| 84 | + return JSON.stringify(obj); |
| 85 | + }; |
| 86 | + var isSame = function isSame(obj1, obj2) { |
| 87 | + return obj1 === obj2 || serialize(obj1) === serialize(obj2); |
| 88 | + }; |
| 89 | + |
| 90 | + var defaultCreateCache = function defaultCreateCache(options) { |
| 91 | + options = options || {}; |
| 92 | + var apiKey = options.apiKey; |
| 93 | + var libraries = options.libraries || ['places']; |
| 94 | + var version = options.version || '3'; |
| 95 | + var language = options.language || 'en'; |
| 96 | + var url = options.url; |
| 97 | + var client = options.client; |
| 98 | + var region = options.region; |
| 99 | + |
| 100 | + return (0, _ScriptCache.ScriptCache)({ |
| 101 | + google: (0, _GoogleApi2.default)({ |
| 102 | + apiKey: apiKey, |
| 103 | + language: language, |
| 104 | + libraries: libraries, |
| 105 | + version: version, |
| 106 | + url: url, |
| 107 | + client: client, |
| 108 | + region: region |
| 109 | + }) |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + var DefaultLoadingContainer = function DefaultLoadingContainer(props) { |
| 114 | + return _react2.default.createElement( |
| 115 | + 'div', |
| 116 | + null, |
| 117 | + 'Loading...' |
| 118 | + ); |
| 119 | + }; |
| 120 | + |
| 121 | + var wrapper = exports.wrapper = function wrapper(input) { |
| 122 | + return function (WrappedComponent) { |
| 123 | + var Wrapper = function (_React$Component) { |
| 124 | + _inherits(Wrapper, _React$Component); |
| 125 | + |
| 126 | + function Wrapper(props, context) { |
| 127 | + _classCallCheck(this, Wrapper); |
| 128 | + |
| 129 | + // Build options from input |
| 130 | + var _this = _possibleConstructorReturn(this, (Wrapper.__proto__ || Object.getPrototypeOf(Wrapper)).call(this, props, context)); |
| 131 | + |
| 132 | + var options = typeof input === 'function' ? input(props) : input; |
| 133 | + |
| 134 | + // Initialize required Google scripts and other configured options |
| 135 | + _this.initialize(options); |
| 136 | + |
| 137 | + _this.state = { |
| 138 | + loaded: false, |
| 139 | + map: null, |
| 140 | + google: null, |
| 141 | + options: options |
| 142 | + }; |
| 143 | + |
| 144 | + _this.mapRef = _react2.default.createRef(); |
| 145 | + return _this; |
| 146 | + } |
| 147 | + |
| 148 | + _createClass(Wrapper, [{ |
| 149 | + key: 'UNSAFE_componentWillReceiveProps', |
| 150 | + value: function UNSAFE_componentWillReceiveProps(props) { |
| 151 | + // Do not update input if it's not dynamic |
| 152 | + if (typeof input !== 'function') { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // Get options to compare |
| 157 | + var prevOptions = this.state.options; |
| 158 | + var options = typeof input === 'function' ? input(props) : input; |
| 159 | + |
| 160 | + // Ignore when options are not changed |
| 161 | + if (isSame(options, prevOptions)) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // Initialize with new options |
| 166 | + this.initialize(options); |
| 167 | + |
| 168 | + // Save new options in component state, |
| 169 | + // and remove information about previous API handlers |
| 170 | + this.setState({ |
| 171 | + options: options, |
| 172 | + loaded: false, |
| 173 | + google: null |
| 174 | + }); |
| 175 | + } |
| 176 | + }, { |
| 177 | + key: 'componentWillUnmount', |
| 178 | + value: function componentWillUnmount() { |
| 179 | + if (this.unregisterLoadHandler) { |
| 180 | + this.unregisterLoadHandler(); |
| 181 | + } |
| 182 | + } |
| 183 | + }, { |
| 184 | + key: 'initialize', |
| 185 | + value: function initialize(options) { |
| 186 | + // Avoid race condition: remove previous 'load' listener |
| 187 | + if (this.unregisterLoadHandler) { |
| 188 | + this.unregisterLoadHandler(); |
| 189 | + this.unregisterLoadHandler = null; |
| 190 | + } |
| 191 | + |
| 192 | + // Load cache factory |
| 193 | + var createCache = options.createCache || defaultCreateCache; |
| 194 | + |
| 195 | + // Build script |
| 196 | + this.scriptCache = createCache(options); |
| 197 | + this.unregisterLoadHandler = this.scriptCache.google.onLoad(this.onLoad.bind(this)); |
| 198 | + |
| 199 | + // Store information about loading container |
| 200 | + this.LoadingContainer = options.LoadingContainer || DefaultLoadingContainer; |
| 201 | + } |
| 202 | + }, { |
| 203 | + key: 'onLoad', |
| 204 | + value: function onLoad(err, tag) { |
| 205 | + this._gapi = window.google; |
| 206 | + |
| 207 | + this.setState({ loaded: true, google: this._gapi }); |
| 208 | + } |
| 209 | + }, { |
| 210 | + key: 'render', |
| 211 | + value: function render() { |
| 212 | + var LoadingContainer = this.LoadingContainer; |
| 213 | + |
| 214 | + if (!this.state.loaded) { |
| 215 | + return _react2.default.createElement(LoadingContainer, null); |
| 216 | + } |
| 217 | + |
| 218 | + var props = Object.assign({}, this.props, { |
| 219 | + loaded: this.state.loaded, |
| 220 | + google: window.google |
| 221 | + }); |
| 222 | + |
| 223 | + return _react2.default.createElement( |
| 224 | + 'div', |
| 225 | + null, |
| 226 | + _react2.default.createElement(WrappedComponent, props), |
| 227 | + _react2.default.createElement('div', { ref: this.mapRef }) |
| 228 | + ); |
| 229 | + } |
| 230 | + }]); |
| 231 | + |
| 232 | + return Wrapper; |
| 233 | + }(_react2.default.Component); |
| 234 | + |
| 235 | + return Wrapper; |
| 236 | + }; |
| 237 | + }; |
| 238 | + |
| 239 | + exports.default = wrapper; |
| 240 | +}); |
0 commit comments