167 lines
6.6 KiB
JavaScript
167 lines
6.6 KiB
JavaScript
/* ==========================================================================
|
|
hyperstitionstud.io — Adaptive Mobile Device Detector & Group 3 Mobile UI
|
|
(Plan #66: PLAN-PIECEWISE-MOBILE-DEVICE-DETECTION-AND-ADAPTIVE-MOBILE-UI)
|
|
Affirmed Strategies 1A, 2A, 3A, 4A:
|
|
- Multi-signal synchronous detection (URL ?mobile=1|0, localStorage override,
|
|
navigator.userAgentData.mobile, mobile UA regex, and coarse-pointer /
|
|
<=820px viewport media query) -> sets html[data-device-ui="mobile|desktop"].
|
|
- Strategy 2A: Injects `[ ☰ Menu ]` slide-down 2-column touch navigation
|
|
sheet (#hsMobileNavSheet) into <header> when mobile UI is active, while
|
|
keeping .meta-badges / .top-nav.hs-global-nav as a swipeable touch rail.
|
|
- Strategy 1A: Injects `[ 📱 Mobile UI | 🖥 Desktop UI ]` switcher in footer.
|
|
========================================================================== */
|
|
(function () {
|
|
"use strict";
|
|
|
|
var STORAGE_KEY = "hs_device_ui_override";
|
|
var MOBILE_UA_RE = /\b(Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Windows Phone|Mobile)\b/i;
|
|
|
|
function getCookie(name) {
|
|
try {
|
|
var m = document.cookie.match(new RegExp("(?:^|; )" + name + "=([^;]*)"));
|
|
return m ? decodeURIComponent(m[1]) : "";
|
|
} catch (_) {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
function detectMobileDevice() {
|
|
try {
|
|
var params = new URLSearchParams(window.location.search || "");
|
|
var qMob = params.get("mobile");
|
|
if (qMob === "1" || qMob === "true") {
|
|
try { localStorage.setItem(STORAGE_KEY, "mobile"); } catch (_) {}
|
|
return true;
|
|
}
|
|
if (qMob === "0" || qMob === "false") {
|
|
try { localStorage.setItem(STORAGE_KEY, "desktop"); } catch (_) {}
|
|
return false;
|
|
}
|
|
var saved = "";
|
|
try { saved = localStorage.getItem(STORAGE_KEY) || ""; } catch (_) {}
|
|
if (saved === "mobile") return true;
|
|
if (saved === "desktop") return false;
|
|
} catch (_) {}
|
|
|
|
if (getCookie("hs_device_hint") === "mobile") return true;
|
|
if (typeof navigator !== "undefined") {
|
|
if (navigator.userAgentData && navigator.userAgentData.mobile === true) return true;
|
|
if (MOBILE_UA_RE.test(navigator.userAgent || "")) return true;
|
|
}
|
|
if (typeof window !== "undefined" && typeof window.matchMedia === "function") {
|
|
if (window.matchMedia("(max-width: 820px), (hover: none) and (pointer: coarse)").matches) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function applyDeviceUiMode(mode) {
|
|
var root = document.documentElement;
|
|
if (!root) return;
|
|
root.setAttribute("data-device-ui", mode);
|
|
var switchBtn = document.getElementById("hsMobileUiSwitchBtn");
|
|
if (switchBtn) {
|
|
switchBtn.textContent = mode === "mobile" ? "[ \uD83D\uDCF1 Mobile UI: ON \u2022 Switch to Desktop ]" : "[ \uD83D\uDDA5 Desktop UI \u2022 Switch to Mobile UI ]";
|
|
switchBtn.setAttribute("aria-pressed", mode === "mobile" ? "true" : "false");
|
|
}
|
|
if (mode !== "mobile") {
|
|
var sheet = document.getElementById("hsMobileNavSheet");
|
|
if (sheet) sheet.classList.remove("open");
|
|
var menuBtn = document.getElementById("hsMobileMenuBtn");
|
|
if (menuBtn) menuBtn.setAttribute("aria-expanded", "false");
|
|
}
|
|
}
|
|
|
|
// Run synchronous detection immediately
|
|
var initialMode = detectMobileDevice() ? "mobile" : "desktop";
|
|
applyDeviceUiMode(initialMode);
|
|
|
|
function initMobileNavAndFooterSwitcher() {
|
|
var hdr = document.querySelector("header");
|
|
var navEl = hdr && hdr.querySelector(".top-nav.hs-global-nav, .meta-badges");
|
|
if (hdr && navEl && !document.getElementById("hsMobileMenuBtn")) {
|
|
var menuBtn = document.createElement("button");
|
|
menuBtn.type = "button";
|
|
menuBtn.id = "hsMobileMenuBtn";
|
|
menuBtn.className = "btn hs-mobile-menu-btn";
|
|
menuBtn.setAttribute("aria-expanded", "false");
|
|
menuBtn.setAttribute("aria-controls", "hsMobileNavSheet");
|
|
menuBtn.textContent = "\u2630 Menu";
|
|
|
|
var sheet = document.createElement("div");
|
|
sheet.id = "hsMobileNavSheet";
|
|
sheet.className = "hs-mobile-nav-sheet hs-plate-g3";
|
|
sheet.setAttribute("aria-label", "Mobile Ecosystem Navigation Drawer");
|
|
|
|
var links = navEl.querySelectorAll("a");
|
|
links.forEach(function (a) {
|
|
var clone = a.cloneNode(true);
|
|
clone.removeAttribute("id");
|
|
clone.classList.add("hs-mobile-sheet-link");
|
|
sheet.appendChild(clone);
|
|
});
|
|
|
|
menuBtn.addEventListener("click", function () {
|
|
var isOpen = sheet.classList.toggle("open");
|
|
menuBtn.setAttribute("aria-expanded", isOpen ? "true" : "false");
|
|
menuBtn.textContent = isOpen ? "\u2715 Close" : "\u2630 Menu";
|
|
});
|
|
|
|
var titleRow = hdr.querySelector(".title-row");
|
|
if (titleRow) {
|
|
titleRow.appendChild(menuBtn);
|
|
} else {
|
|
hdr.insertBefore(menuBtn, navEl);
|
|
}
|
|
hdr.parentNode.insertBefore(sheet, hdr.nextSibling);
|
|
}
|
|
|
|
var ftr = document.querySelector("footer");
|
|
if (ftr && !document.getElementById("hsMobileUiSwitchBtn")) {
|
|
var wrap = document.createElement("div");
|
|
wrap.style.cssText = "margin-top:6px;";
|
|
var swBtn = document.createElement("button");
|
|
swBtn.type = "button";
|
|
swBtn.id = "hsMobileUiSwitchBtn";
|
|
swBtn.className = "btn hs-rain-toggle-btn";
|
|
swBtn.addEventListener("click", function () {
|
|
var cur = document.documentElement.getAttribute("data-device-ui") || "desktop";
|
|
var next = cur === "mobile" ? "desktop" : "mobile";
|
|
try { localStorage.setItem(STORAGE_KEY, next); } catch (_) {}
|
|
applyDeviceUiMode(next);
|
|
window.dispatchEvent(new Event("resize"));
|
|
});
|
|
wrap.appendChild(swBtn);
|
|
ftr.appendChild(wrap);
|
|
applyDeviceUiMode(document.documentElement.getAttribute("data-device-ui") || initialMode);
|
|
}
|
|
}
|
|
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", initMobileNavAndFooterSwitcher);
|
|
} else {
|
|
initMobileNavAndFooterSwitcher();
|
|
}
|
|
|
|
window.addEventListener("resize", function () {
|
|
var saved = "";
|
|
try { saved = localStorage.getItem(STORAGE_KEY) || ""; } catch (_) {}
|
|
if (!saved) {
|
|
applyDeviceUiMode(detectMobileDevice() ? "mobile" : "desktop");
|
|
}
|
|
});
|
|
|
|
window.HyperstitionStudio = window.HyperstitionStudio || {};
|
|
window.HyperstitionStudio.MobileUI = {
|
|
detectMobileDevice: detectMobileDevice,
|
|
setMode: function (mode) {
|
|
try { localStorage.setItem(STORAGE_KEY, mode); } catch (_) {}
|
|
applyDeviceUiMode(mode);
|
|
},
|
|
getMode: function () {
|
|
return document.documentElement.getAttribute("data-device-ui") || "desktop";
|
|
}
|
|
};
|
|
})();
|