Files
qihuo/static/js/orientation.js
T
dekun c5262a0a54 Add responsive mobile layout, records cards, and tablet settings fold fix.
Mobile gets compact trade/records UI with detail modals; static assets are cache-busted and settings cards fold correctly on tablet grid layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 16:42:38 +08:00

103 lines
3.7 KiB
JavaScript

/* Copyright (c) 2025-2026 马建军. All rights reserved.
* 专有软件 — 未经授权禁止复制、传播、转售。
* 详见 LICENSE.zh-CN.txt
*/
(function () {
function isCoarsePointer() {
return window.matchMedia('(hover: none) and (pointer: coarse)').matches;
}
function isTabletUa() {
var ua = navigator.userAgent || '';
if (/iPad/i.test(ua)) return true;
if (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) return true;
if (/Android/i.test(ua) && !/Mobile/i.test(ua)) return true;
if (/Tablet|PlayBook|Silk/i.test(ua)) return true;
return false;
}
function isPhoneUa() {
var ua = navigator.userAgent || '';
if (isTabletUa()) return false;
if (/iPhone|iPod/i.test(ua)) return true;
if (/Android/i.test(ua) && /Mobile/i.test(ua)) return true;
if (/HarmonyOS|OpenHarmony/i.test(ua) && !/Tablet/i.test(ua)) return true;
if (/Mobile|Windows Phone|IEMobile|BlackBerry/i.test(ua)) return true;
return false;
}
function screenShortSide() {
return Math.min(window.screen.width || 0, window.screen.height || 0);
}
function detectLayout() {
var shortSide = screenShortSide();
var coarse = isCoarsePointer();
if (isPhoneUa()) return 'phone';
if (isTabletUa()) return 'tablet';
if (shortSide > 0 && shortSide < 600) return 'phone';
if (shortSide >= 600 && shortSide <= 1100 && coarse) return 'tablet';
if (window.innerWidth <= 767) return 'phone';
if (window.innerWidth <= 1100 && coarse) return 'tablet';
return 'desktop';
}
function updateLayoutState() {
var root = document.documentElement;
var layout = detectLayout();
var isPhone = layout === 'phone';
var landscape = window.innerWidth > window.innerHeight;
root.dataset.layout = layout;
root.dataset.mobile = isPhone ? '1' : '0';
root.dataset.orientation = isPhone ? 'portrait' : (landscape ? 'landscape' : 'portrait');
root.classList.toggle('layout-phone', isPhone);
root.classList.toggle('layout-tablet', layout === 'tablet');
var nav = document.getElementById('site-nav');
var userBar = document.querySelector('.user-bar');
if (nav && userBar && isPhone) {
nav.setAttribute('data-user-label', (userBar.textContent || '').replace(/\s+/g, ' ').trim());
}
var overlay = document.getElementById('orientation-lock');
var msg = document.getElementById('orientation-lock-msg');
if (!overlay) return;
if (isPhone) {
if (landscape) {
overlay.hidden = false;
if (msg) msg.textContent = '请竖屏使用';
} else {
overlay.hidden = true;
}
return;
}
if (layout === 'tablet' && root.dataset.orientation === 'portrait') {
overlay.hidden = false;
if (msg) msg.textContent = '平板请旋转至横屏使用';
return;
}
overlay.hidden = true;
}
window.qihuoLayout = {
isPhone: function () { return document.documentElement.dataset.mobile === '1'; },
isTablet: function () { return document.documentElement.dataset.layout === 'tablet'; },
refresh: updateLayoutState,
};
updateLayoutState();
window.addEventListener('resize', updateLayoutState);
window.addEventListener('orientationchange', function () {
setTimeout(updateLayoutState, 150);
});
document.addEventListener('visibilitychange', function () {
if (!document.hidden) updateLayoutState();
});
})();