-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
278 lines (247 loc) · 8.15 KB
/
options.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
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
// Save predefined city coordinates
let citiesCoordinates = {
Madrid: [40.4168, -3.7038],
Aranda: [41.668602, -3.6953297],
Zaragoza: [41.654123, -0.879338],
Donosti: [43.3152668, -2.0029028],
Lisboa: [38.723263, -9.130241],
};
const spinnerContainer = document.getElementById("spinner-container");
document.addEventListener("DOMContentLoaded", function () {
chrome.storage.sync.get(
["apiKey", "primaryOrigin", "secondaryOrigin"],
function (data) {
// Check if there is any saved data and populate fields accordingly
if (Object.keys(data).length !== 0) {
document.getElementById("api-key").value = data.apiKey || "";
if (data.primaryOrigin !== undefined) {
document.getElementById("manual-coordinates-primary").value =
data.primaryOrigin.coordinates || "";
document.getElementById("manual-city-primary").value =
data.primaryOrigin.city || "";
}
if (data.secondaryOrigin !== undefined) {
document.getElementById("manual-coordinates-secondary").value =
data.secondaryOrigin.coordinates || "";
document.getElementById("manual-city-secondary").value =
data.secondaryOrigin.city || "";
addButtonActiveClassFromStorage(
"secondary",
data.secondaryOrigin.city,
);
}
updateButtons(data);
addButtonActiveClassFromStorage("primary", data.primaryOrigin.city);
}
spinnerContainer.style.display = "none"; // Hide spinner when data is loaded
},
);
});
// Show error message for invalid inputs
function showError(input, message) {
input.classList.add("input-error");
let errorElement = input.nextElementSibling;
if (errorElement && errorElement.classList.contains("error-message")) {
errorElement.textContent = message;
errorElement.classList.add("active");
}
}
// Clear previous error messages
function clearErrors(inputs) {
inputs.forEach((input) => {
input.classList.remove("input-error");
let errorElement = input.nextElementSibling;
if (errorElement && errorElement.classList.contains("error-message")) {
errorElement.classList.remove("active");
}
});
}
// Dynamically update buttons for available cities
function updateButtons(data) {
let isSecondary = false;
let isActive = false;
const { primaryOrigin, secondaryOrigin } = data;
// Avoid adding duplicate buttons
Object.entries(citiesCoordinates).forEach(([city]) => {
if (
document.querySelectorAll(`.origin-options [data-city='${city}']`)
.length === 0
) {
if (city === primaryOrigin.city) {
isActive = true;
}
const buttonPrimary = createButton(city, isSecondary, isActive);
document
.querySelector(`.origin-options.primary`)
.appendChild(buttonPrimary);
// Create secondary button if necessary
if (secondaryOrigin !== undefined) {
if (city === secondaryOrigin.city) {
isActive = true;
}
isSecondary = true;
const buttonSecondary = createButton(city, isSecondary, isActive);
document
.querySelector(`.origin-options.secondary`)
.appendChild(buttonSecondary);
}
}
});
}
// Add "active" class to stored button selections
function addButtonActiveClassFromStorage(group, cityLabel) {
document
.querySelectorAll(`.origin-options.${group} button`)
.forEach((button) => {
if (button.dataset.city === cityLabel) {
button.classList.add("active");
}
});
}
// Reset all options and clear storage
document.getElementById("resetOptions").addEventListener("click", function () {
chrome.storage.sync.clear();
document.querySelectorAll("input").forEach((input) => (input.value = ""));
removeAllActiveClasses();
alert("All data removed successfully!");
});
// Save user settings
document.getElementById("saveOptions").addEventListener("click", function () {
const coordinatesRegex = /^-?\d+(\.\d+)?\s*,\s*-?\d+(\.\d+)?$/;
let hasErrors = false;
// inputs
const apiKeyInput = document.querySelector("#api-key");
const coordinatesPrimaryInput = document.querySelector(
"#manual-coordinates-primary",
);
const coordinatesSecondaryInput = document.querySelector(
"#manual-coordinates-secondary",
);
const cityPrimaryInput = document.querySelector("#manual-city-primary");
const citySecondaryInput = document.querySelector("#manual-city-secondary");
// end inputs
clearErrors([
apiKeyInput,
cityPrimaryInput,
citySecondaryInput,
coordinatesPrimaryInput,
coordinatesSecondaryInput,
]);
// Validate API key
if (!apiKeyInput.value) {
showError(
apiKeyInput,
"Please set your GraphHopper API Key in the extension settings.",
);
hasErrors = true;
}
// Validate city and coordinates for the primary location
if (!cityPrimaryInput.value) {
showError(
cityPrimaryInput,
"Please enter the city name or select one from the options",
);
hasErrors = true;
}
if (
coordinatesPrimaryInput.value === "" ||
!coordinatesRegex.test(coordinatesPrimaryInput.value)
) {
showError(coordinatesPrimaryInput, "Please enter valid coordinates");
hasErrors = true;
}
let data = {
apiKey: apiKeyInput.value,
primaryOrigin: {
coordinates: coordinatesPrimaryInput.value,
city: cityPrimaryInput.value,
},
};
// If secondary city or coordinates exist, validate them
if (
citySecondaryInput.value !== "" ||
coordinatesSecondaryInput.value !== ""
) {
if (
coordinatesSecondaryInput.value === "" ||
!coordinatesRegex.test(coordinatesSecondaryInput.value)
) {
hasErrors = true;
showError(
coordinatesSecondaryInput,
"Please enter valid secondary coordinates",
);
}
if (!citySecondaryInput.value) {
showError(
citySecondaryInput,
"Please enter the city name or select one from the options",
);
hasErrors = true;
}
data.secondaryOrigin = {
coordinates: coordinatesSecondaryInput.value,
city: citySecondaryInput.value,
};
} else {
chrome.storage.sync.remove("secondaryOrigin");
}
if (hasErrors) {
alert("There seem to be some errors. Check the form and try again.");
return; // Don't save if there are errors
}
chrome.storage.sync.set(data, function () {
alert("Options saved successfully!");
});
});
// Remove all "active" classes from buttons
function removeAllActiveClasses(button) {
if (button) {
button.parentNode
.querySelectorAll("button")
.forEach((button) => button.classList.remove("active"));
button.classList.add("active");
} else {
document
.querySelectorAll(`.origin-options [data-city]`)
.forEach((button) => button.classList.remove("active"));
}
}
// Event listeners for primary city buttons
document
.querySelectorAll(".origin-options.primary button")
.forEach((button) => {
const city = button.getAttribute("data-city");
button.addEventListener("click", function () {
removeAllActiveClasses(button);
const coordinates = citiesCoordinates[city];
document.getElementById("manual-coordinates-primary").value =
coordinates.join(",");
document.getElementById("manual-city-primary").value =
button.getAttribute("data-city");
});
});
// Event listeners for secondary city buttons
document
.querySelectorAll(".origin-options.secondary button")
.forEach((button) => {
const city = button.getAttribute("data-city");
button.addEventListener("click", function () {
removeAllActiveClasses(button);
const coordinates = citiesCoordinates[city];
document.getElementById("manual-coordinates-secondary").value =
coordinates.join(",");
document.getElementById("manual-city-secondary").value =
button.getAttribute("data-city");
});
});
// Helper to create a new button for city options
function createButton(city, isSecondary = false, isActive = true) {
const button = document.createElement("button");
button.setAttribute("data-city", city);
button.innerHTML = city;
if (isActive) {
button.classList.add("active");
}
return button;
}