-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
108 lines (94 loc) · 2.86 KB
/
helpers.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
'use strict';
function handleAlert(element, message) {
element.innerHTML = message;
element.classList.add('alert_active');
element.addEventListener('animationend', (e) => {
element.classList.remove('alert_active');
}, false);
}
function formatTime(time) {
if (time < 60) {
return "Less than 1 min";
}
let secondsRemaining = time;
let result = '';
let days = Math.floor(secondsRemaining/(3600 * 24));
if (days > 0) {
result += `${days} ${days === 1 ? 'day ' : 'days '}`;
}
secondsRemaining = secondsRemaining - (days * (3600 * 24));
let hours = Math.floor(secondsRemaining/3600);
if (hours > 0) {
result += `${hours} ${hours === 1 ? 'hr ' : 'hrs '}`;
}
secondsRemaining = secondsRemaining - (hours * 3600);
let mins = Math.floor(secondsRemaining/60);
if (mins > 0) {
result += `${mins} ${mins === 1 ? 'min ' : 'mins '}`;
}
return result;
}
function roundNumber(number, precision) {
let factor = Math.pow(10, precision);
let tempNumber = number * factor;
let roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
}
function clearChildNodes(parentElement) {
while (parentElement.firstChild) {
parentElement.removeChild(parentElement.firstChild);
}
}
function rightSidebarDivHandleClick(e, context) {
if (e.target.classList.contains('add_route')) {
context.addRoute();
}
if (e.target.classList.contains('calc_routes')) {
context.calculateRoutes();
}
if (e.target.classList.contains('transportation')) {
context.updateTransportation(e);
}
e.stopPropagation();
}
function removeResultsButtonHandleClick(e) {
searchForm.reset();
locations.clearLocations();
clearChildNodes(locationsDiv);
if (closeButton.classList.contains('close_active')) {
closeButton.classList.remove('close_active');
}
e.stopPropagation();
}
function escape(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
function handleSearch(e) {
e.preventDefault();
let query = escape(searchText.value);
if (query === "") {
handleAlert(alertSearchDiv, 'Search criteria required');
} else {
loaderHelpers.start();
searchService.initService(query);
}
};
function handlePlaceChange(e) {
let newLocation = map.autocomplete.getPlace();
if (newLocation.id) {
locations.clearLocations();
clearChildNodes(locationsDiv);
map.resetBounds();
sidebarHelpers.expand();
locations.addLocation(locationsDiv, newLocation);
if (!closeButton.classList.contains('close_active')) {
closeButton.classList.add('close_active');
}
}
};
function setScrollTop(parentElement, element) {
let elementRect = element.getBoundingClientRect();
let parentElementRect = parentElement.getBoundingClientRect();
let currentParentElementScrollTop = parentElement.scrollTop;
parentElement.scrollTop = currentParentElementScrollTop + elementRect.top - parentElementRect.top;
}