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

114 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class WeatherSettings {
constructor() {
this._localStorage = window.localStorage;
this._appID = '';
this._cityID = '';
this._units = '';
2020-06-12 08:44:48 +02:00
this._apiKeySet = document.querySelector('#apiKeySet');
this._cityIDSet = document.querySelector('#cityIDSet');
2020-06-09 04:02:03 +02:00
this._weatherSelectUnits = document.querySelector('#weatherSelectUnits');
this._weatherSettingsReset = document.querySelector('#weatherSettingsReset');
this._weatherSettingsApply = document.querySelector('#weatherSettingsApply');
this.getWeatherData = weatherScreen.getWeatherData;
this.getForecastData = weatherScreen.getForecastData;
this._init();
}
2020-06-12 09:49:43 +02:00
_init = () => {
2020-06-12 09:55:54 +02:00
this._updateWeatherSettings();
2020-06-12 09:49:43 +02:00
this._registerWeatherResetOnClickEvent();
this._registerWeatherApplyOnClickEvent();
2020-06-09 04:02:03 +02:00
}
// Clear credentials
2020-06-12 09:49:43 +02:00
_clearWeatherCredentials = () => {
2020-06-09 04:02:03 +02:00
this._localStorage.removeItem('apiKey');
this._localStorage.removeItem('cityID');
this._localStorage.removeItem('units');
}
// Reset textboxes
_deleteWeatherSettingsValue = () => {
2020-06-12 08:44:48 +02:00
this._apiKeySet.value = '';
this._cityIDSet.value = '';
2020-06-09 04:02:03 +02:00
this._weatherSelectUnits.value = 'metric';
}
2020-06-12 09:49:43 +02:00
// Apply credentials
_applyWeatherSettings = (key, city, units) => {
this._localStorage.setItem('apiKey', key);
this._localStorage.setItem('cityID', city);
this._localStorage.setItem('units', units);
}
// Update credential variables
_updateCredentialVariables = () => {
this._appID = localStorage.getItem('apiKey') || 'API Key';
this._cityID = localStorage.getItem('cityID') || 'City ID';
this._units = localStorage.getItem('units') || 'metric';
}
2020-06-09 04:02:03 +02:00
// Update textbox placeholders
_updateWeatherSettingsPlaceholder = () => {
2020-06-12 08:44:48 +02:00
this._apiKeySet.placeholder = this._appID;
this._cityIDSet.placeholder = this._cityID;
2020-06-12 01:46:44 +02:00
this._weatherSelectUnits.value = this._units;
2020-06-09 04:02:03 +02:00
}
// Update weather settings
_updateWeatherSettings = () => {
2020-06-12 09:49:43 +02:00
// Update cred vars
this._updateCredentialVariables();
2020-06-09 04:02:03 +02:00
// Update weather forecast elements
this.getWeatherData(this._appID, this._cityID, this._units);
this.getForecastData(this._appID, this._cityID, this._units);
this._deleteWeatherSettingsValue();
this._updateWeatherSettingsPlaceholder();
2020-06-12 09:49:43 +02:00
2020-06-09 04:02:03 +02:00
}
2020-06-12 09:49:43 +02:00
// Reset
2020-06-09 04:02:03 +02:00
_weatherResetOnClickEvent = e => {
// Reset keys
2020-06-12 09:49:43 +02:00
this._clearWeatherCredentials();
2020-06-09 04:02:03 +02:00
2020-06-12 09:49:43 +02:00
this._updateCredentialVariables();
this._deleteWeatherSettingsValue();
this._updateWeatherSettingsPlaceholder();
alert('Credentials deleted!');
2020-06-09 04:02:03 +02:00
}
2020-06-12 09:49:43 +02:00
// Apply Onclick event
2020-06-09 04:02:03 +02:00
_weatherApplyOnClickEvent = e => {
2020-06-12 09:49:43 +02:00
// Update credentials/Settings
2020-06-09 04:02:03 +02:00
this._applyWeatherSettings(
2020-06-12 08:44:48 +02:00
this._apiKeySet.value || this._apiKeySet.placeholder,
this._cityIDSet.value || this._cityIDSet.placeholder,
2020-06-09 04:02:03 +02:00
this._weatherSelectUnits.options[this._weatherSelectUnits.selectedIndex].value
);
2020-06-12 09:49:43 +02:00
2020-06-09 04:02:03 +02:00
this._updateWeatherSettings();
alert('Successfully updated!');
}
2020-06-12 09:49:43 +02:00
_registerWeatherResetOnClickEvent = () => {
this._weatherSettingsReset.onclick = this._weatherResetOnClickEvent;
}
2020-06-09 04:02:03 +02:00
_registerWeatherApplyOnClickEvent = () => {
this._weatherSettingsApply.onclick = this._weatherApplyOnClickEvent;
}
}