|
79 | 79 | }
|
80 | 80 |
|
81 | 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 | + |
82 | 90 | var defaultCreateCache = function defaultCreateCache(options) {
|
83 | 91 | options = options || {};
|
84 | 92 | var apiKey = options.apiKey;
|
|
106 | 114 | );
|
107 | 115 | };
|
108 | 116 |
|
109 |
| - var wrapper = exports.wrapper = function wrapper(options) { |
| 117 | + var wrapper = exports.wrapper = function wrapper(input) { |
110 | 118 | return function (WrappedComponent) {
|
111 |
| - var createCache = options.createCache || defaultCreateCache; |
112 |
| - |
113 | 119 | var Wrapper = function (_React$Component) {
|
114 | 120 | _inherits(Wrapper, _React$Component);
|
115 | 121 |
|
116 | 122 | function Wrapper(props, context) {
|
117 | 123 | _classCallCheck(this, Wrapper);
|
118 | 124 |
|
| 125 | + // Build options from input |
119 | 126 | var _this = _possibleConstructorReturn(this, (Wrapper.__proto__ || Object.getPrototypeOf(Wrapper)).call(this, props, context));
|
120 | 127 |
|
121 |
| - _this.scriptCache = createCache(options); |
122 |
| - _this.scriptCache.google.onLoad(_this.onLoad.bind(_this)); |
123 |
| - _this.LoadingContainer = options.LoadingContainer || DefaultLoadingContainer; |
| 128 | + var options = typeof input === 'function' ? input(props) : input; |
| 129 | + |
| 130 | + // Initialize required Google scripts and other configured options |
| 131 | + _this.initialize(options); |
124 | 132 |
|
125 | 133 | _this.state = {
|
126 | 134 | loaded: false,
|
127 | 135 | map: null,
|
128 |
| - google: null |
| 136 | + google: null, |
| 137 | + options: options |
129 | 138 | };
|
130 | 139 | return _this;
|
131 | 140 | }
|
132 | 141 |
|
133 | 142 | _createClass(Wrapper, [{
|
| 143 | + key: 'componentWillReceiveProps', |
| 144 | + value: function componentWillReceiveProps(props) { |
| 145 | + // Do not update input if it's not dynamic |
| 146 | + if (typeof input !== 'function') { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // Get options to compare |
| 151 | + var prevOptions = this.state.options; |
| 152 | + var options = typeof input === 'function' ? input(props) : input; |
| 153 | + |
| 154 | + // Ignore when options are not changed |
| 155 | + if (isSame(options, prevOptions)) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Initialize with new options |
| 160 | + this.initialize(options); |
| 161 | + |
| 162 | + // Save new options in component state |
| 163 | + this.setState({ options: options }); |
| 164 | + } |
| 165 | + }, { |
| 166 | + key: 'initialize', |
| 167 | + value: function initialize(options) { |
| 168 | + // Load cache factory |
| 169 | + var createCache = options.createCache || defaultCreateCache; |
| 170 | + |
| 171 | + // Build script |
| 172 | + this.scriptCache = createCache(options); |
| 173 | + this.scriptCache.google.onLoad(this.onLoad.bind(this)); |
| 174 | + |
| 175 | + // Store information about loading container |
| 176 | + this.LoadingContainer = options.LoadingContainer || DefaultLoadingContainer; |
| 177 | + |
| 178 | + // Remove information about previous API handlers |
| 179 | + this.setState({ loaded: false, google: null }); |
| 180 | + } |
| 181 | + }, { |
134 | 182 | key: 'onLoad',
|
135 | 183 | value: function onLoad(err, tag) {
|
136 | 184 | this._gapi = window.google;
|
|
0 commit comments