Add region based holidays (not only country wide)
This commit is contained in:
parent
eda530ceb9
commit
0504bcc02e
3 changed files with 42 additions and 14 deletions
|
|
@ -5,7 +5,7 @@ import Quickshell
|
||||||
import qs.Components
|
import qs.Components
|
||||||
import qs.Settings
|
import qs.Settings
|
||||||
import Quickshell.Wayland
|
import Quickshell.Wayland
|
||||||
import "root:/Helpers/Holidays.js" as Holidays
|
import "../../Helpers/Holidays.js" as Holidays
|
||||||
|
|
||||||
PanelWithOverlay {
|
PanelWithOverlay {
|
||||||
id: calendarOverlay
|
id: calendarOverlay
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
var _countryCode = null;
|
var _countryCode = null;
|
||||||
|
var _regionCode = null;
|
||||||
|
var _regionName = null;
|
||||||
var _holidaysCache = {};
|
var _holidaysCache = {};
|
||||||
|
|
||||||
function getCountryCode(callback) {
|
function getCountryCode(callback) {
|
||||||
|
|
@ -7,11 +9,13 @@ function getCountryCode(callback) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open("GET", "http://ip-api.com/json/", true);
|
xhr.open("GET", "https://nominatim.openstreetmap.org/search?city="+ Settings.settings.weatherCity+"&country=&format=json&addressdetails=1&extratags=1", true);
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
|
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
|
||||||
var response = JSON.parse(xhr.responseText);
|
var response = JSON.parse(xhr.responseText);
|
||||||
_countryCode = response.countryCode;
|
_countryCode = response?.[0]?.address?.country_code ?? "US";
|
||||||
|
_regionCode = response?.[0]?.address?.["ISO3166-2-lvl4"] ?? "";
|
||||||
|
_regionName = response?.[0]?.address?.state ?? "";
|
||||||
callback(_countryCode);
|
callback(_countryCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -30,17 +34,43 @@ function getHolidays(year, countryCode, callback) {
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
|
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
|
||||||
var holidays = JSON.parse(xhr.responseText);
|
var holidays = JSON.parse(xhr.responseText);
|
||||||
_holidaysCache[cacheKey] = holidays;
|
var augmentedHolidays = filterHolidaysByRegion(holidays);
|
||||||
callback(holidays);
|
_holidaysCache[cacheKey] = augmentedHolidays;
|
||||||
|
callback(augmentedHolidays);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xhr.send();
|
xhr.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function filterHolidaysByRegion(holidays) {
|
||||||
|
if (!_regionCode) {
|
||||||
|
return holidays;
|
||||||
|
}
|
||||||
|
const retHolidays = [];
|
||||||
|
holidays.forEach(function(holiday) {
|
||||||
|
if (holiday.counties?.length > 0) {
|
||||||
|
let found = false;
|
||||||
|
holiday.counties.forEach(function(county) {
|
||||||
|
if (county.toLowerCase() === _regionCode.toLowerCase()) {
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (found) {
|
||||||
|
var regionText = " (" + _regionName + ")";
|
||||||
|
holiday.name = holiday.name + regionText;
|
||||||
|
holiday.localName = holiday.localName + regionText;
|
||||||
|
retHolidays.push(holiday);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
retHolidays.push(holiday);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return retHolidays;
|
||||||
|
}
|
||||||
|
|
||||||
function getHolidaysForMonth(year, month, callback) {
|
function getHolidaysForMonth(year, month, callback) {
|
||||||
getCountryCode(function(countryCode) {
|
getCountryCode(function(countryCode) {
|
||||||
getHolidays(year, countryCode, function(holidays) {
|
getHolidays(year, countryCode, function(holidays) {
|
||||||
// 0-based months (0=Jan, 11=Dec)
|
|
||||||
var filtered = holidays.filter(function(h) {
|
var filtered = holidays.filter(function(h) {
|
||||||
var date = new Date(h.date);
|
var date = new Date(h.date);
|
||||||
return date.getFullYear() === year && date.getMonth() === month;
|
return date.getFullYear() === year && date.getMonth() === month;
|
||||||
|
|
|
||||||
|
|
@ -17,17 +17,12 @@ PanelWindow {
|
||||||
anchors.right: true
|
anchors.right: true
|
||||||
margins.right: 0
|
margins.right: 0
|
||||||
margins.top: 0
|
margins.top: 0
|
||||||
//z: 100
|
|
||||||
//border.color: Theme.outline
|
|
||||||
//border.width: 1
|
|
||||||
WlrLayershell.keyboardFocus: WlrKeyboardFocus.OnDemand
|
WlrLayershell.keyboardFocus: WlrKeyboardFocus.OnDemand
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
color: Theme.backgroundPrimary
|
color: Theme.backgroundPrimary
|
||||||
radius: 24
|
radius: 24
|
||||||
//border.color: Theme.outline
|
|
||||||
//border.width: 1
|
|
||||||
z: 0
|
z: 0
|
||||||
|
|
||||||
ColumnLayout {
|
ColumnLayout {
|
||||||
|
|
@ -187,9 +182,12 @@ PanelWindow {
|
||||||
|
|
||||||
// Release focus when modal becomes invisible
|
// Release focus when modal becomes invisible
|
||||||
onVisibleChanged: {
|
onVisibleChanged: {
|
||||||
if (!visible)
|
if (!visible) {
|
||||||
// Focus will be handled by the individual components
|
// Focus will be handled by the individual components
|
||||||
{}
|
if (typeof weather !== 'undefined' && weather !== null && weather.fetchCityWeather) {
|
||||||
|
weather.fetchCityWeather();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue