Skip to content

Commit

Permalink
Polyfill Intl.supportedValuesOf, make default timezone be the user's …
Browse files Browse the repository at this point in the history
…PC timezone
  • Loading branch information
NightScript370 committed Mar 6, 2024
1 parent 33eb554 commit 0334813
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</head>
<body class="d-flex">
{% include modals/notSupported.html %}
<script src="https://polyfill.io/v3/polyfill.min.js?features=Intl.supportedValuesOf"></script>
<script src="/assets/js/settings/docLoadBlock.js"></script>
<div class="d-none d-lg-flex flex-column flex-shrink-0" id="sidebar">
<a href="/" id="sidebarLogo" class="link-body-emphasis text-decoration-none" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Zemaneh Yosef - Rabbi Ovadiah Yosef Calendar">
Expand Down
15 changes: 3 additions & 12 deletions assets/js/location.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Order of Operations
// Check if Hebrew: Use

const getJSON = async (/** @type {RequestInfo | URL} */ url) => await (await fetch(url)).json();
const geoLocation = {
locationName: '',
Expand Down Expand Up @@ -29,16 +26,10 @@ function showManualLocationSettings() {
*/
let select = document.getElementById("timezoneInput");
if (!select.options.length) {
if (!Intl.supportedValuesOf) {
let opt = new Option("Your browser does not support Intl.supportedValuesOf().", null, true, true);
opt.disabled = true;
select.options.add(opt);
} else {
for (const timeZone of Intl.supportedValuesOf("timeZone")) {
select.options.add(new Option(timeZone));
}
select.value = Intl.DateTimeFormat().resolvedOptions().timeZone;
for (const timeZone of Intl.supportedValuesOf("timeZone")) {
select.options.add(new Option(timeZone));
}
select.value = Intl.DateTimeFormat().resolvedOptions().timeZone;
}
}

Expand Down
12 changes: 1 addition & 11 deletions assets/js/settings/docLoadBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ var landscapeMQ = window.matchMedia("(prefers-color-scheme: dark)");
landscapeMQ.addEventListener("change", handleThemeChange);
handleThemeChange(landscapeMQ)

var lockSite = (typeof BigInt !== "function" || !("Intl" in window) || typeof Intl.Locale !== "function")

var hebLocal = new Intl.Locale("he")
// @ts-ignore
var calArray = ("getCalendars" in hebLocal ? hebLocal.getCalendars() : hebLocal.calendars)
if (calArray && !calArray.includes("hebrew"))
lockSite = true

// We check if the "getCalendars" exist because Firefox did not implement it,
// yet it implemented a Hebrew calendar
if (lockSite) {
if (typeof BigInt !== "function" || !("Intl" in window) || typeof Intl.supportedValuesOf !== "function" || !Intl.supportedValuesOf('calendar').includes('hebrew')) {
var modal = document.getElementById("unsupportedModal");
modal.classList.add("show");
modal.style.display = "block";
Expand Down
29 changes: 28 additions & 1 deletion assets/js/settings/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const urlParams = new URLSearchParams(queryString);
/** @param {string} param */
const settingsURLOverride = (param) => urlParams.get(param) || localStorage.getItem(param);

window.customTZAlert = false;

/**
* @param {string} variedTocheck
* @param {string} defaultSetting
Expand Down Expand Up @@ -57,7 +59,32 @@ const settings = Object.freeze({
lat: () => parseFloat(settingsURLOverride("lat")),
long: () => parseFloat(settingsURLOverride("long")),
elevation: () => parseFloat(settingsURLOverride("elevation")) || 0,
timezone: () => settingsURLOverride("timeZone")
timezone: () => {
if (settingsURLOverride("timeZone")) {
if (!Intl.supportedValuesOf('timeZone').includes(settingsURLOverride("timeZone")) && !window.customTZAlert) {
alert("custom timezone found; expect issues.")
window.customTZAlert = true;
}

return settingsURLOverride("timeZone");
}

if (Intl.DateTimeFormat() && Intl.DateTimeFormat().resolvedOptions() && Intl.DateTimeFormat().resolvedOptions().timeZone) {
if (!window.customTZAlert) {
alert("timezone not found in the request; using the system local.")
window.customTZAlert = true;
}

return Intl.DateTimeFormat().resolvedOptions().timeZone;
}

if (!window.customTZAlert) {
alert("UTC timezone used due to inability to get system timezone. Please set the timezone via the URL params");
window.customTZAlert = true;
}

return "UTC";
}
},

calendarToggle: {
Expand Down

0 comments on commit 0334813

Please sign in to comment.