This repository has been archived by the owner on Nov 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
google-api-mock.coffee
487 lines (423 loc) · 12.8 KB
/
google-api-mock.coffee
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
###global _:true, angular:true, google:true ###
do ->
capitalize = (s) ->
return s[0].toUpperCase() + s.slice(1)
class MapObject
getMap: =>
@map
setMap: (m) =>
@map = m
setOptions: (o) =>
@opts = o
class DraggableObject extends MapObject
setDraggable: (bool) =>
@draggable = bool
getDraggable: =>
@draggable
class VisibleObject extends MapObject
setVisible: (bool) =>
@visible = bool
getVisible: =>
@visible
class PositionObject extends MapObject
setPosition:(position) =>
@position
getPosition: =>
@position
class MockInfoWindow
constructor: ->
@_isOpen = false
open: (map, anchor) =>
@_isOpen = true
return
close: =>
@_isOpen = false
return
isOpen: (val = undefined) ->
unless val?
return @_isOpen
else
@_isOpen = val
setContent: (content) =>
@content = content
getContent: =>
@content
getLatLng = ->
class LatLng
constructor: (@y,@x, nowrap) ->
lat: =>
@y
lng: =>
@x
getMarker = ->
class Marker extends MapObject
_.extend @::, PositionObject::, DraggableObject::, VisibleObject::
@instances = 0
@resetInstances = =>
@instances = 0
@creationSubscribe = (obj, cb) ->
window.google.maps.event.addListener(obj, 'creation', cb)
@creationUnSubscribe = (listener) ->
window.google.maps.event.removeListener listener
constructor: (opts) ->
super()
if opts?
['draggable', 'editable',
'map','visible', 'position'].forEach (o) =>
@[o] = opts[o]
Marker.instances += 1
if window?.google?.maps?.event?
window.google.maps.event.fireAllListeners 'creation', @
setOptions: (o) =>
super(o)
if o?.position?
@position = o.position
setAnimation:(obj) =>
@animation = obj
getAnimation: =>
@animation
setIcon: (icon) =>
@icon
getIcon: =>
@icon
setClickable: (bool) =>
@clickable = bool
getClickable: =>
@clickable
setZIndex:(z) =>
@zIndex = z
getZIndex: =>
@zIndex
setTitle: (str) =>
@title = str
getTitle: =>
@title
setOpacity: (num) =>
@opacity = num
getOpacity: =>
@opacity
getCircle = ->
class Circle extends MapObject
_.extend @::, DraggableObject::, VisibleObject::
@instances = 0
@resetInstances = =>
@instances = 0
@creationSubscribe = (obj, cb) ->
window.google.maps.event.addListener(obj, 'creation', cb)
@creationUnSubscribe = (listener) ->
window.google.maps.event.removeListener listener
constructor: (opts) ->
super()
@props = ['draggable', 'editable', 'map','visible', 'radius', 'center']
@setOptions opts
#getters
@props.forEach (p) =>
@["get#{capitalize p}"] = =>
@[p]
#setters
@props.forEach (p) =>
@["set#{capitalize p}"] = (val) =>
@[p] = val
if p == "radius" or p == "center"
window.google.maps.event.fireAllListeners "#{p}_changed", @
Circle.instances += 1
@instance = Circle.instances
if window?.google?.maps?.event?
window.google.maps.event.fireAllListeners 'creation', @
setOptions: (o) =>
super(o)
_.extend @, o
getMap = ->
Map = (opts) -> return
Map::center =
lat: -> 0
lng: -> 0
Map::controls = {
TOP_CENTER: [],
TOP_LEFT: [],
TOP_RIGHT: [],
LEFT_TOP: [],
RIGHT_TOP: [],
LEFT_CENTER: [],
RIGHT_CENTER: [],
LEFT_BOTTOM: [],
RIGHT_BOTTOM: [],
BOTTOM_CENTER: [],
BOTTOM_LEFT: [],
BOTTOM_RIGHT: []
}
Map::overlayMapTypes = new window.google.maps.MVCArray()
Map::getControls = -> return @controls
Map::setOpts = -> return
Map::setOptions = -> return
Map::setZoom = -> return
Map::setCenter = -> return
Map::getCoords = -> return {latitude: 47, longitude: -27} unless Map.getCoords?
Map::getBounds = ->
unless Map.getBounds?
getNorthEast: ->
google.maps.LatLng(47,27)
getSouthWest: ->
google.maps.LatLng(89,100)
return Map
getMarkerWithLabel: ->
class MarkerWithLabel extends getMarker()
@instances = 0
@resetInstances = =>
@instances = 0
constructor: (opts) ->
if opts?
['draggable', 'editable', 'map','path', 'visible'].forEach (o) =>
@[o] = opts[o]
@drawn = false
MarkerWithLabel.instances += 1
setAnchor: (anchor) =>
@anchor = @anchor
getAnchor: =>
@anchor
setMandatoryStyles: (obj) =>
@mandatoryStyles = obj
getMandatoryStyles: =>
@mandatoryStyles
setStyles:(obj) =>
@styles = obj
getStyles: =>
@styles
setContent:(content) =>
@content = content
getContent: =>
@content
draw: =>
@drawn = true
onRemove: ->
onAdd: ->
getPolyline = ->
class Polyline extends DraggableObject
@instances = 0
@resetInstances = =>
@instances = 0
constructor: (opts) ->
if opts?
['draggable', 'editable', 'map','path', 'visible'].forEach (o) =>
@[o] = opts[o]
Polyline.instances += 1
getEditable: =>
@editable
getPath: =>
@path
setEditable: (bool) =>
@editable = bool
setPath: (array) =>
@path = array
getMVCArray = ->
class MVCArray extends Array
@instances = 0
@resetInstances = =>
@instances = 0
constructor: ->
MVCArray.instances += 1
super()
clear: ->
@length = 0
getArray: =>
@
getAt:(i) =>
@[i]
getLength: =>
@length
insertAt:(i, elem) =>
@splice(i, 0, elem)
removeAt:(i) ->
@splice(i,1)
setAt:(i, elem) ->
@[i] = elem
class GoogleApiMock
constructor: ->
@mocks = [
@mockAPI
@mockLatLng
@mockLatLngBounds
@mockControlPosition
@mockAnimation
@mockMapTypeId
@mockOverlayView
@mockOverlayView
@mockEvent
@mockInfoWindow
@mockMarker
@mockCircle
@mockMVCArray
@mockPoint
@mockPolygon
@mockPolyline
@mockMap
@mockPlaces
@mockSearchBox
]
@initAll = -> @mocks.forEach (fn) -> fn?()
mockAPI: ->
window.google = {}
window.google.maps = {}
# To make debugging easier, mock everything with exceptions
unmocked = (api) -> () -> throw new String('Unmocked API ' + api)
window.google.maps.Marker = unmocked('Marker')
window.google.maps.event =
clearListeners: unmocked('event.clearListeners')
addListener: unmocked('event.addListener')
removeListener: unmocked('event.removeListener')
window.google.maps.OverlayView = unmocked('OverlayView')
window.google.maps.InfoWindow = unmocked('InfoWindow')
window.google.maps.LatLng = unmocked('LatLng')
window.google.maps.MVCArray = unmocked('MVCArray')
window.google.maps.Point = unmocked('Point')
window.google.maps.LatLngBounds = unmocked('LatLngBounds')
window.google.maps.Polyline = unmocked('Polyline')
mockPlaces: ->
window.google.maps.places = {}
mockSearchBox: (SearchBox = () -> return) ->
window.google.maps.places.SearchBox = SearchBox
mockLatLng: (yours) ->
window.google.maps.LatLng = unless yours then getLatLng() else yours
mockLatLngBounds: (LatLngBounds = () -> return) ->
if not (LatLngBounds.extend?)
LatLngBounds.prototype.extend = () -> return
window.google.maps.LatLngBounds = LatLngBounds
mockMap: =>
@mockMapTypeId()
@mockLatLng()
@mockOverlayView()
@mockEvent()
@mockMVCArray()
Map = getMap()
window.google.maps.Map = Map
mockControlPosition: ->
ControlPosition =
TOP_CENTER: 'TOP_CENTER',
TOP_LEFT: 'TOP_LEFT',
TOP_RIGHT: 'TOP_RIGHT',
LEFT_TOP: 'LEFT_TOP',
RIGHT_TOP: 'RIGHT_TOP',
LEFT_CENTER: 'LEFT_CENTER',
RIGHT_CENTER: 'RIGHT_CENTER',
LEFT_BOTTOM: 'LEFT_BOTTOM',
RIGHT_BOTTOM: 'RIGHT_BOTTOM',
BOTTOM_CENTER: 'BOTTOM_CENTER',
BOTTOM_LEFT: 'BOTTOM_LEFT',
BOTTOM_RIGHT: 'BOTTOM_RIGHT'
window.google.maps.ControlPosition = ControlPosition
mockAnimation: (Animation = {BOUNCE: 'bounce'}) ->
window.google.maps.Animation = Animation
mockMapTypeId: (MapTypeId = {ROADMAP: 'roadmap'}) ->
window.google.maps.MapTypeId = MapTypeId
mockOverlayView: (OverlayView = class OverlayView
setMap: () ->) ->
window.google.maps.OverlayView = OverlayView
mockEvent: (event = {}) ->
listeners = []
#mocking google maps event listener
if not event.addListener
event.addListener = (thing, eventName, callBack) ->
found = _.find listeners, (obj) ->
obj.obj == thing
unless found?
toPush = {}
toPush.obj = thing
toPush.events = {}
toPush.events[eventName] = [callBack]
listeners.push toPush
else
if !found.events[eventName]
found.events[eventName] = [callBack]
else
found.events[eventName].push callBack
event.addListenerOnce = (thing, eventName, callBack) ->
callBack() #forcing immediate return for idle so async api kicks off
event.addListener(thing, eventName, callBack)
if not event.clearListeners
event.clearListeners = () ->
listeners.length = 0
if not event.removeListener
event.removeListener = (item) ->
index = listeners.indexOf(item)
if index != -1
listeners.splice(index)
unless event.fireListener
event.fireListener = (thing, eventName) ->
found = _.find listeners, (obj) ->
obj.obj == thing
if found? and found?.events[eventName]?
found.events[eventName].forEach (cb) -> cb(found.obj)
unless event.normalizedEvents
event.normalizedEvents = ->
ret = _ listeners.map (obj) ->
_.keys(obj.events)
.chain()
.flatten()
.uniq()
.value()
ret
unless event.fireAllListeners
event.fireAllListeners = (eventName, state) ->
listeners.forEach (obj) ->
if obj.events[eventName]?
obj.events[eventName].forEach (cb) -> cb(state)
window.google.maps.event = event
return listeners
mockInfoWindow: (InfoWindow = MockInfoWindow) ->
window.google.maps.InfoWindow = InfoWindow
mockMarker: (Marker = getMarker()) ->
window.google.maps.Marker = Marker
mockMVCArray: (impl = getMVCArray()) ->
window.google.maps.MVCArray = impl
mockCircle: (Circle = getCircle()) ->
window.google.maps.Circle = Circle
mockPoint: (Point = (x, y) -> return {x: x, y: y}) ->
window.google.maps.Point = Point
mockPolyline: (impl = getPolyline()) ->
return window.google.maps.Polyline = impl
mockPolygon: (polygon) ->
return window.google.maps.Polygon = polygon if polygon?
#https://developers.google.com/maps/documentation/javascript/reference#Polygon
#https://developers.google.com/maps/documentation/javascript/reference#PolygonOptions
window.google.maps.Polygon = (options) ->
@getDraggable = ->
options.draggable
@getEditable = ->
options.editable
@getMap = ->
options.map
@getPath = ->
_.first(options.paths)
@getPaths = ->
options.paths
@getVisible = ->
options.visible
@setOptions = (opts) ->
options = opts
@setDraggable = (boolean) ->
options.draggable = boolean
@setEditable = (boolean) ->
options.editable = boolean
@setMap = (map) ->
options.map = map
@setPath = (path) ->
if options.paths? and options.paths.length > 0
options.paths[0] = path
else
options.paths = []
options.paths.push path
@setPaths = (paths) ->
options.paths = paths
@setVisible = (boolean) ->
options.visible = boolean
@
getMarker: getMarker
getMap: getMap
getPolyline: getPolyline
getMVCArray: getMVCArray
getLatLng: getLatLng
(new GoogleApiMock()).initAll()
angular.module('uiGmapgoogle-maps.mocks', ['uiGmapgoogle-maps'])
.factory('GoogleApiMock', ->
GoogleApiMock
)