the-glorious-startpage/js/weather-settings.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-06-07 09:18:09 +02:00
// Set constiable
2020-06-04 05:55:34 +02:00
let appID, cityID, units;
// Input elements
// Textboxes
2020-06-07 09:18:09 +02:00
const apiBox = document.getElementById("apiBox");
const cityBox = document.getElementById("cityBox");
2020-06-04 05:55:34 +02:00
// Select menu
2020-06-07 09:18:09 +02:00
const weatherSelectUnits = document.getElementById("weatherSelectUnits");
2020-06-04 05:55:34 +02:00
// Div buttons
2020-06-07 09:18:09 +02:00
const weatherSettingsReset = document.getElementById("weatherSettingsReset");
const weatherSettingsApply = document.getElementById("weatherSettingsApply");
2020-06-04 05:55:34 +02:00
// Apply credentials
const applyWeatherSettings = (key, city, units) => {
localStorage.setItem('apiKey', key);
localStorage.setItem('cityID', city);
localStorage.setItem('units', units);
}
// Clear credentials
const resetWeatherSettings = () => {
localStorage.removeItem('apiKey');
localStorage.removeItem('cityID');
localStorage.removeItem('units');
}
// Update functions
const deleteWeatherSettingsValue = () => {
apiBox.value = '';
cityBox.value = '';
weatherSelectUnits.value = "metric";
}
const updateWeatherSettingsPlaceholder = () => {
apiBox.placeholder = String(appID) || "API Key";
cityBox.placeholder = String(cityID) || "City ID";
weatherSelectUnits.value = String(units) || "metric";
}
const updateWeatherSettings = () => {
appID = localStorage.getItem('apiKey') || "API Key";
cityID = localStorage.getItem('cityID') || "City ID";
units = localStorage.getItem('units') || "metric";
// Update weather forecast elements
getWeatherData(appID, cityID, units);
getForecastData(appID, cityID, units);
deleteWeatherSettingsValue();
updateWeatherSettingsPlaceholder();
}
// Reset button was pressed
weatherSettingsReset.onclick = () => {
2020-06-04 05:55:34 +02:00
// Reset keys
resetWeatherSettings();
updateWeatherSettings();
alert('Credentials deleted!');
}
// Apply settings
weatherSettingsApply.onclick = () => {
2020-06-04 05:55:34 +02:00
applyWeatherSettings(
apiBox.value || apiBox.placeholder,
cityBox.value || cityBox.placeholder,
weatherSelectUnits.options[weatherSelectUnits.selectedIndex].value
);
updateWeatherSettings();
alert('Successfully updated!');
}
window.onload = updateWeatherSettings();