-
Notifications
You must be signed in to change notification settings - Fork 0
/
locations.js
141 lines (123 loc) · 4.29 KB
/
locations.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
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
class Locations {
constructor(map) {
this.locations = [];
this.info = {};
this.map = map;
this.parentElement;
this.bounds = new google.maps.LatLngBounds();
}
}
Locations.prototype.createLocationEntry = function(location) {
let locationEntry = document.createElement('div');
locationEntry.classList.add('location_entry');
locationEntry.setAttribute('locid', location.id);
locationEntry.draggable = true;
locationEntry.ondragstart = (e, addLocation) => { this.onDragStart(e, true); };
let content = `
<div class=map"location_entry_content">
<div class="location_name">${location.name}</div>
<div>${location.formatted_address}</div>
<div class="location_entry_expand">
`;
if (location.rating) {
content += `<div>Rating: ${location.rating}</div>`;
}
if (location.opening_hours) {
content += `<div>Open now: ${location.opening_hours.open_now}</div>`
}
if (location.price_level) {
content += `<div>Price level: ${location.price_level}</div>`
}
content += `</div></div>
<div class="remove">
<i class="close material-icons md-dark" title="Remove">close</i>
</div>`
locationEntry.innerHTML = content;
locationEntry.onclick = (e, locId, context) => { this.expand(e, location.id, this) }
return locationEntry;
}
Locations.prototype.createSingleMarker = function(location) {
let lat = location.geometry.location.lat();
let lng = location.geometry.location.lng();
let latlng = new google.maps.LatLng(lat, lng);
let marker = new google.maps.Marker({
map: this.map.map,
title: location.name,
position: latlng,
id: location.id,
address: location.formatted_address
});
if (location.icon) {
let icon = {
url: location.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
marker.icon = icon;
}
marker.addListener('click', (e, locId, context) => { this.expand(e, location.id, this) });
this.bounds.extend(latlng);
this.map.map.fitBounds(this.bounds);
this.map.map.panToBounds(this.bounds);
return marker;
}
Locations.prototype.addLocation = function(parentElement, location) {
if (this.parentElement === undefined) {
this.parentElement = parentElement;
}
let locationEntry = this.createLocationEntry(location);
let marker = this.createSingleMarker(location);
this.info[location.id] = this.locations.length;
parentElement.appendChild(locationEntry);
let locationData = { location: location, element: locationEntry, marker: marker };
this.locations.push(locationData);
return locationData;
}
Locations.prototype.onDragStart = function(e, addLocation) {
e.stopPropagation();
if (e.target.getAttribute('locid')) {
let text = `${e.target.getAttribute('locid')},${addLocation}`;
e.dataTransfer.setData("text", text);
} else {
return;
}
}
Locations.prototype.addResults = function(results) {
this.clearLocations();
sidebarHelpers.expand();
results.forEach(location => this.addLocation(locationsDiv, location));
if (!closeButton.classList.contains('close_active')) {
closeButton.classList.add('close_active');
}
}
Locations.prototype.expand = function(e, locId, context) {
if (e.target && e.target.classList.contains('close')) return;
let locationIndex = context.info[locId];
let locationElement = context.locations[locationIndex].element;
let locationEntryExpand = locationElement.querySelector('.location_entry_expand');
let marker = context.locations[locationIndex].marker;
locationEntryExpand.classList.toggle('location_entry_expand_active');
locationElement.classList.toggle('location_selected');
if (locationEntryExpand.classList.contains('location_entry_expand_active')) {
let latlng = new google.maps.LatLngBounds(marker.position);
this.map.resetBounds(latlng);
setScrollTop(this.parentElement, locationElement);
} else {
this.map.resetBounds(this.bounds);
}
}
Locations.prototype.clearLocations = function() {
this.removeMarkers();
this.locations = [];
this.info = {};
this.bounds = new google.maps.LatLngBounds();
this.map.resetBounds();
}
Locations.prototype.removeMarkers = function() {
this.locations.forEach(location => {
location.marker.setMap(null);
})
}