-
Notifications
You must be signed in to change notification settings - Fork 62
/
g-map.js
127 lines (106 loc) · 3.27 KB
/
g-map.js
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
import Ember from 'ember';
import layout from '../templates/components/g-map';
/* global google */
const { isEmpty, isPresent, computed, observer, run, inject } = Ember;
export default Ember.Component.extend({
layout: layout,
classNames: ['g-map'],
bannedOptions: Ember.A(['center', 'zoom']),
places: inject.service(),
init() {
this._super();
this.set('markers', Ember.A());
if (isEmpty(this.get('options'))) {
this.set('options', {});
}
},
permittedOptions: computed('options', function() {
const { options, bannedOptions } = this.getProperties(['options', 'bannedOptions']);
const permittedOptions = {};
for (let option in options) {
if (options.hasOwnProperty(option) && !bannedOptions.contains(option)) {
permittedOptions[option] = options[option];
}
}
return permittedOptions;
}),
didInsertElement() {
this._super();
if (isEmpty(this.get('map'))) {
const canvas = this.$().find('.g-map-canvas').get(0);
const options = this.get('permittedOptions');
const map = new google.maps.Map(canvas, options);
this.set('map', map);
this.get('places').start(map);
}
this.setZoom();
this.setCenter();
if (this.get('shouldFit')) {
this.fitToMarkers();
}
},
permittedOptionsChanged: observer('permittedOptions', function() {
run.once(this, 'setOptions');
}),
setOptions() {
const map = this.get('map');
const options = this.get('permittedOptions');
if (isPresent(map)) {
map.setOptions(options);
}
},
zoomChanged: observer('zoom', function() {
run.once(this, 'setZoom');
}),
setZoom() {
const map = this.get('map');
const zoom = this.get('zoom');
if (isPresent(map)) {
map.setZoom(zoom);
}
},
coordsChanged: observer('lat', 'lng', function() {
run.once(this, 'setCenter');
}),
setCenter() {
const map = this.get('map');
const lat = this.get('lat');
const lng = this.get('lng');
if (isPresent(map) && isPresent(lat) && isPresent(lng)) {
const center = new google.maps.LatLng(lat, lng);
map.setCenter(center);
}
},
registerMarker(marker) {
this.get('markers').addObject(marker);
},
unregisterMarker(marker) {
this.get('markers').removeObject(marker);
},
shouldFit: computed('markersFitMode', function() {
return Ember.A(['init', 'live']).contains(this.get('markersFitMode'));
}),
markersChanged: observer('markers.@each.lat', 'markers.@each.lng', function() {
if (this.get('markersFitMode') === 'live') {
run.once(this, 'fitToMarkers');
}
}),
fitToMarkers() {
const markers = this.get('markers').filter((marker) => {
return isPresent(marker.get('lat')) && isPresent(marker.get('lng'));
});
if (markers.length > 0) {
const map = this.get('map');
const bounds = new google.maps.LatLngBounds();
const points = markers.map((marker) => {
return new google.maps.LatLng(marker.get('lat'), marker.get('lng'));
});
points.forEach((point) => bounds.extend(point));
map.fitBounds(bounds);
}
},
groupMarkerClicked(marker, group) {
let markers = this.get('markers').without(marker).filterBy('group', group);
markers.forEach((marker) => marker.closeInfowindow());
}
});