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
+
99
+ return ( 0 , _ScriptCache . ScriptCache ) ( {
100
+ google : ( 0 , _GoogleApi2 . default ) ( {
101
+ apiKey : apiKey ,
102
+ language : language ,
103
+ libraries : libraries ,
104
+ version : version ,
105
+ url : url ,
106
+ client : client
107
+ } )
108
+ } ) ;
109
+ } ;
110
+
111
+ var DefaultLoadingContainer = function DefaultLoadingContainer ( props ) {
112
+ return _react2 . default . createElement (
113
+ 'div' ,
114
+ null ,
115
+ 'Loading...'
116
+ ) ;
117
+ } ;
118
+
119
+ var wrapper = exports . wrapper = function wrapper ( input ) {
120
+ return function ( WrappedComponent ) {
121
+ var Wrapper = function ( _React$Component ) {
122
+ _inherits ( Wrapper , _React$Component ) ;
123
+
124
+ function Wrapper ( props , context ) {
125
+ _classCallCheck ( this , Wrapper ) ;
126
+
127
+ // Build options from input
128
+ var _this = _possibleConstructorReturn ( this , ( Wrapper . __proto__ || Object . getPrototypeOf ( Wrapper ) ) . call ( this , props , context ) ) ;
129
+
130
+ var options = typeof input === 'function' ? input ( props ) : input ;
131
+
132
+ // Initialize required Google scripts and other configured options
133
+ _this . initialize ( options ) ;
134
+
135
+ _this . state = {
136
+ loaded : false ,
137
+ map : null ,
138
+ google : null ,
139
+ options : options
140
+ } ;
141
+ return _this ;
142
+ }
143
+
144
+ _createClass ( Wrapper , [ {
145
+ key : 'componentWillReceiveProps' ,
146
+ value : function componentWillReceiveProps ( props ) {
147
+ // Do not update input if it's not dynamic
148
+ if ( typeof input !== 'function' ) {
149
+ return ;
150
+ }
151
+
152
+ // Get options to compare
153
+ var prevOptions = this . state . options ;
154
+ var options = typeof input === 'function' ? input ( props ) : input ;
155
+
156
+ // Ignore when options are not changed
157
+ if ( isSame ( options , prevOptions ) ) {
158
+ return ;
159
+ }
160
+
161
+ // Initialize with new options
162
+ this . initialize ( options ) ;
163
+
164
+ // Save new options in component state,
165
+ // and remove information about previous API handlers
166
+ this . setState ( {
167
+ options : options ,
168
+ loaded : false ,
169
+ google : null
170
+ } ) ;
171
+ }
172
+ } , {
173
+ key : 'initialize' ,
174
+ value : function initialize ( options ) {
175
+ // Avoid race condition: remove previous 'load' listener
176
+ if ( this . unregisterLoadHandler ) {
177
+ this . unregisterLoadHandler ( ) ;
178
+ this . unregisterLoadHandler = null ;
179
+ }
180
+
181
+ // Load cache factory
182
+ var createCache = options . createCache || defaultCreateCache ;
183
+
184
+ // Build script
185
+ this . scriptCache = createCache ( options ) ;
186
+ this . unregisterLoadHandler = this . scriptCache . google . onLoad ( this . onLoad . bind ( this ) ) ;
187
+
188
+ // Store information about loading container
189
+ this . LoadingContainer = options . LoadingContainer || DefaultLoadingContainer ;
190
+ }
191
+ } , {
192
+ key : 'onLoad' ,
193
+ value : function onLoad ( err , tag ) {
194
+ this . _gapi = window . google ;
195
+
196
+ this . setState ( { loaded : true , google : this . _gapi } ) ;
197
+ }
198
+ } , {
199
+ key : 'render' ,
200
+ value : function render ( ) {
201
+ var LoadingContainer = this . LoadingContainer ;
202
+
203
+ if ( ! this . state . loaded ) {
204
+ return _react2 . default . createElement ( LoadingContainer , null ) ;
205
+ }
206
+
207
+ var props = Object . assign ( { } , this . props , {
208
+ loaded : this . state . loaded ,
209
+ google : window . google
210
+ } ) ;
211
+
212
+ return _react2 . default . createElement (
213
+ 'div' ,
214
+ null ,
215
+ _react2 . default . createElement ( WrappedComponent , props ) ,
216
+ _react2 . default . createElement ( 'div' , { ref : 'map' } )
217
+ ) ;
218
+ }
219
+ } ] ) ;
220
+
221
+ return Wrapper ;
222
+ } ( _react2 . default . Component ) ;
223
+
224
+ return Wrapper ;
225
+ } ;
226
+ } ;
227
+
228
+ exports . default = wrapper ;
229
+ } ) ;
0 commit comments