2020-06-09 04:02:03 +02:00
|
|
|
class WeatherSettings {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
|
|
this._localStorage = window.localStorage;
|
|
|
|
|
|
|
|
this._appID = '';
|
|
|
|
this._cityID = '';
|
|
|
|
this._units = '';
|
2020-06-12 12:26:39 +02:00
|
|
|
this._locatorMode = '';
|
|
|
|
|
|
|
|
// Geolocation data
|
|
|
|
this._origLongitude = 0;
|
|
|
|
this._origLatitude = 0;
|
|
|
|
this._watchPositionID = 0;
|
|
|
|
|
|
|
|
this._watchGeoOptions = {
|
2020-06-12 12:52:39 +02:00
|
|
|
enableHighAccuracy: true,
|
2020-06-12 12:26:39 +02:00
|
|
|
timeout: 5000,
|
|
|
|
maximumAge: 0
|
|
|
|
};
|
2020-06-09 04:02:03 +02:00
|
|
|
|
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
|
|
|
|
2020-06-12 12:26:39 +02:00
|
|
|
this._weatherSelectLocator = document.querySelector('#weatherSelectLocator');
|
2020-06-09 04:02:03 +02:00
|
|
|
this._weatherSelectUnits = document.querySelector('#weatherSelectUnits');
|
|
|
|
|
|
|
|
this._weatherSettingsReset = document.querySelector('#weatherSettingsReset');
|
|
|
|
this._weatherSettingsApply = document.querySelector('#weatherSettingsApply');
|
|
|
|
|
2020-06-12 12:26:39 +02:00
|
|
|
this._weatherSettingsCityIDGroup = document.querySelector('#weatherSettingsCityID');
|
|
|
|
|
|
|
|
this._getWeatherDataViaCity = weatherScreen.getWeatherDataViaCity;
|
|
|
|
this._getForecastDataViaCity = weatherScreen.getForecastDataViaCity;
|
|
|
|
|
|
|
|
this._getWeatherDataViaGeo = weatherScreen.getWeatherDataViaGeo;
|
|
|
|
this._getForecastDataViaGeo = weatherScreen.getForecastDataViaGeo;
|
2020-06-09 04:02:03 +02:00
|
|
|
|
|
|
|
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-12 12:26:39 +02:00
|
|
|
this._registerWeatherSelectLocatorOnChangeEvent();
|
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');
|
2020-06-12 12:26:39 +02:00
|
|
|
this._localStorage.removeItem('locatorMode');
|
2020-06-09 04:02:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 12:26:39 +02:00
|
|
|
this._weatherSelectLocator.value = 'geolocation';
|
2020-06-09 04:02:03 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 09:49:43 +02:00
|
|
|
// Apply credentials
|
2020-06-12 12:26:39 +02:00
|
|
|
_applyWeatherSettings = (key, city, units, locator) => {
|
2020-06-12 09:49:43 +02:00
|
|
|
this._localStorage.setItem('apiKey', key);
|
|
|
|
this._localStorage.setItem('cityID', city);
|
|
|
|
this._localStorage.setItem('units', units);
|
2020-06-12 12:26:39 +02:00
|
|
|
this._localStorage.setItem('locatorMode', locator);
|
2020-06-12 09:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update credential variables
|
|
|
|
_updateCredentialVariables = () => {
|
2020-06-12 12:26:39 +02:00
|
|
|
this._appID = this._localStorage.getItem('apiKey') || 'API Key';
|
|
|
|
this._cityID = this._localStorage.getItem('cityID') || 'City ID';
|
|
|
|
this._units = this._localStorage.getItem('units') || 'metric';
|
|
|
|
this._locatorMode = this._localStorage.getItem('locatorMode') || 'geolocation';
|
2020-06-12 09:49:43 +02:00
|
|
|
}
|
|
|
|
|
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-12 12:26:39 +02:00
|
|
|
this._weatherSelectLocator.value = this._locatorMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop geolocating
|
|
|
|
_stopGeolocating = () => {
|
|
|
|
|
|
|
|
// Unregister the handler
|
|
|
|
navigator.geolocation.clearWatch(this._watchPositionID);
|
|
|
|
|
|
|
|
// Reset positions
|
|
|
|
this._origLongitude = 0;
|
|
|
|
this._origLatitude = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// You denied the permission request
|
|
|
|
_deniedGeolocation = () => {
|
|
|
|
|
|
|
|
alert(`You denied the request to access your location. As a consequence for your action, ` +
|
|
|
|
`you need to allow it on your browser's settings if you want to use the geolocation functionality. You can just use the City Mode, though.`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch
|
|
|
|
_watchGeoSuccess = pos => {
|
|
|
|
|
|
|
|
const currentCoord = pos.coords;
|
|
|
|
|
|
|
|
if ((this._origLongitude !== currentCoord.longitude) || (this._origLatitude !== currentCoord.latitude)) {
|
|
|
|
|
|
|
|
console.log('update current position');
|
|
|
|
|
|
|
|
// Update origPositions
|
|
|
|
this._origLongitude = currentCoord.longitude;
|
|
|
|
this._origLatitude = currentCoord.latitude;
|
|
|
|
|
|
|
|
// fetch and update widget
|
|
|
|
this._getWeatherDataViaGeo(this._appID, this._units, this._origLongitude, this._origLatitude);
|
|
|
|
this._getForecastDataViaGeo(this._appID, this._units, this._origLongitude, this._origLatitude);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error
|
|
|
|
_watchGeoError = err => {
|
|
|
|
|
|
|
|
console.warn('ERROR(' + err.code + '): ' + err.message);
|
|
|
|
|
|
|
|
if (err.code == err.PERMISSION_DENIED) {
|
|
|
|
|
|
|
|
this._deniedGeolocation();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start watching location
|
|
|
|
_watchGeoPosition = () => {
|
|
|
|
this._watchPositionID = navigator.geolocation.watchPosition(this._watchGeoSuccess, this._watchGeoError, this._watchGeoOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check permission
|
|
|
|
_checkGeoPermission = () => {
|
|
|
|
|
|
|
|
navigator.permissions.query({name:'geolocation'}).then(result => {
|
|
|
|
|
|
|
|
if ((result.state === 'prompt') || (result.state == 'granted')) {
|
|
|
|
|
|
|
|
this._watchGeoPosition();
|
|
|
|
|
|
|
|
} else if (result.state === 'denied') {
|
|
|
|
|
|
|
|
alert('Manually enable the geolocation in your browser settings. How? Who knows?');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locator mode on change event
|
|
|
|
_weatherSelectLocatorOnChangeEvent = e => {
|
|
|
|
|
|
|
|
this._locatorMode = this._weatherSelectLocator.options[this._weatherSelectLocator.selectedIndex].value;
|
|
|
|
|
|
|
|
if (this._locatorMode === 'geolocation') {
|
|
|
|
|
|
|
|
console.log('geolocation');
|
|
|
|
|
|
|
|
this._weatherSettingsCityIDGroup.classList.add('hideWeatherSettings');
|
|
|
|
|
|
|
|
} else if (this._locatorMode === 'city') {
|
|
|
|
|
|
|
|
console.log('city');
|
|
|
|
|
|
|
|
this._weatherSettingsCityIDGroup.classList.remove('hideWeatherSettings');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register on change event
|
|
|
|
_registerWeatherSelectLocatorOnChangeEvent = () => {
|
|
|
|
|
|
|
|
this._weatherSelectLocator.onchange = this._weatherSelectLocatorOnChangeEvent;
|
|
|
|
|
2020-06-09 04:02:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update weather settings
|
|
|
|
_updateWeatherSettings = () => {
|
2020-06-12 12:26:39 +02:00
|
|
|
|
2020-06-12 09:49:43 +02:00
|
|
|
// Update cred vars
|
|
|
|
this._updateCredentialVariables();
|
2020-06-09 04:02:03 +02:00
|
|
|
|
2020-06-12 12:26:39 +02:00
|
|
|
if (this._locatorMode === 'geolocation') {
|
|
|
|
|
|
|
|
this._weatherSettingsCityIDGroup.classList.add('hideWeatherSettings');
|
|
|
|
|
|
|
|
if (navigator.geolocation) {
|
|
|
|
|
|
|
|
this._checkGeoPermission();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
alert(`Oof! It seems your browser doesn't support geolocation.`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (this._locatorMode === 'city') {
|
|
|
|
|
|
|
|
this._weatherSettingsCityIDGroup.classList.remove('hideWeatherSettings');
|
|
|
|
|
|
|
|
// Stop geolocating
|
|
|
|
this._stopGeolocating();
|
|
|
|
|
|
|
|
// Update weather forecast elements
|
|
|
|
this._getWeatherDataViaCity(this._appID, this._cityID, this._units);
|
|
|
|
this._getForecastDataViaCity(this._appID, this._cityID, this._units);
|
|
|
|
|
|
|
|
}
|
2020-06-09 04:02:03 +02:00
|
|
|
|
|
|
|
this._deleteWeatherSettingsValue();
|
|
|
|
this._updateWeatherSettingsPlaceholder();
|
|
|
|
}
|
|
|
|
|
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 12:26:39 +02:00
|
|
|
// Stop geolocating
|
|
|
|
this._stopGeolocating();
|
|
|
|
|
|
|
|
// Update
|
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-12 12:26:39 +02:00
|
|
|
this._weatherSelectUnits.options[this._weatherSelectUnits.selectedIndex].value,
|
|
|
|
this._weatherSelectLocator.options[this._weatherSelectLocator.selectedIndex].value
|
2020-06-09 04:02:03 +02:00
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|