调整UI
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
var header = document.querySelector(".site-header");
|
||||
var drawer = document.querySelector("[data-drawer]");
|
||||
var openBtn = document.querySelector("[data-drawer-open]");
|
||||
var userMenu = document.querySelector("[data-user-menu]");
|
||||
|
||||
if (header) {
|
||||
var threshold = 48;
|
||||
@@ -30,44 +29,6 @@
|
||||
if ("ResizeObserver" in window) new ResizeObserver(measure).observe(header);
|
||||
}
|
||||
|
||||
// 用户下拉菜单
|
||||
if (userMenu) {
|
||||
var toggle = userMenu.querySelector("[data-user-menu-toggle]");
|
||||
|
||||
function openUserMenu() {
|
||||
userMenu.classList.add("is-open");
|
||||
if (toggle) toggle.setAttribute("aria-expanded", "true");
|
||||
}
|
||||
|
||||
function closeUserMenu() {
|
||||
userMenu.classList.remove("is-open");
|
||||
if (toggle) toggle.setAttribute("aria-expanded", "false");
|
||||
}
|
||||
|
||||
function toggleUserMenu(e) {
|
||||
e.stopPropagation();
|
||||
if (userMenu.classList.contains("is-open")) {
|
||||
closeUserMenu();
|
||||
} else {
|
||||
openUserMenu();
|
||||
}
|
||||
}
|
||||
|
||||
if (toggle) {
|
||||
toggle.addEventListener("click", toggleUserMenu);
|
||||
}
|
||||
|
||||
document.addEventListener("click", function (e) {
|
||||
if (!userMenu.contains(e.target)) {
|
||||
closeUserMenu();
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("keydown", function (e) {
|
||||
if (e.key === "Escape") closeUserMenu();
|
||||
});
|
||||
}
|
||||
|
||||
// 左侧抽屉
|
||||
if (drawer && openBtn) {
|
||||
var closeEls = drawer.querySelectorAll("[data-drawer-close]");
|
||||
@@ -99,35 +60,6 @@
|
||||
});
|
||||
}
|
||||
|
||||
function openAccountSwitch(event) {
|
||||
if (event) event.preventDefault();
|
||||
|
||||
if (userMenu) {
|
||||
userMenu.classList.remove("is-open");
|
||||
var userToggle = userMenu.querySelector("[data-user-menu-toggle]");
|
||||
if (userToggle) userToggle.setAttribute("aria-expanded", "false");
|
||||
}
|
||||
if (drawer) {
|
||||
drawer.classList.remove("is-open");
|
||||
document.body.classList.remove("drawer-open");
|
||||
if (openBtn) openBtn.setAttribute("aria-expanded", "false");
|
||||
}
|
||||
|
||||
if (typeof window.openAccountSwitchLogin === "function") {
|
||||
window.openAccountSwitchLogin();
|
||||
return;
|
||||
}
|
||||
if (typeof window.openLoginPop === "function") {
|
||||
window.openLoginPop();
|
||||
return;
|
||||
}
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
document.querySelectorAll("[data-account-switch]").forEach(function (btn) {
|
||||
btn.addEventListener("click", openAccountSwitch);
|
||||
});
|
||||
|
||||
// 搜索表单:走路径 /search/{q},不用 query;同时写入本地搜索历史
|
||||
var SEARCH_HISTORY_KEY = "hgdj_search_history";
|
||||
var SEARCH_HISTORY_MAX = 10;
|
||||
@@ -526,108 +458,4 @@
|
||||
closeDropdown();
|
||||
});
|
||||
});
|
||||
|
||||
var PLACEHOLDER = "/static/images/placeholder.png?v=2";
|
||||
var DEFAULT_AVATAR = "/static/images/profile/avatar.png";
|
||||
|
||||
function usableAvatar(url) {
|
||||
var u = String(url || "").trim();
|
||||
if (!u) return "";
|
||||
if (/^https?:\/\//i.test(u) || u.indexOf("/static/") === 0) return u;
|
||||
return "";
|
||||
}
|
||||
|
||||
function setAvatarImg(img, url) {
|
||||
if (!img) return;
|
||||
var src = usableAvatar(url);
|
||||
img.removeAttribute("data-decrypted");
|
||||
img.removeAttribute("data-decrypt-failed");
|
||||
if (src && typeof window.shouldDecryptImage === "function" && window.shouldDecryptImage(src)) {
|
||||
img.classList.add("lazy");
|
||||
img.src = PLACEHOLDER;
|
||||
img.setAttribute("data-src", src);
|
||||
img.setAttribute("data-lazy-eager", "1");
|
||||
} else {
|
||||
img.classList.remove("lazy");
|
||||
img.removeAttribute("data-src");
|
||||
img.removeAttribute("data-lazy-eager");
|
||||
img.src = src || DEFAULT_AVATAR;
|
||||
}
|
||||
}
|
||||
|
||||
function refreshAvatarDecrypt(root) {
|
||||
var scope = root || document;
|
||||
if (typeof window.initLazyImages === "function") {
|
||||
window.initLazyImages(scope, { eager: true });
|
||||
return;
|
||||
}
|
||||
if (typeof window.loadLazyNode !== "function") return;
|
||||
scope
|
||||
.querySelectorAll(
|
||||
"img.user-chip__avatar.lazy[data-src], img.user-menu__avatar.lazy[data-src], img.drawer__avatar.lazy[data-src], img.play-composer__avatar-img.lazy[data-src]"
|
||||
)
|
||||
.forEach(function (img) {
|
||||
window.loadLazyNode(img);
|
||||
});
|
||||
}
|
||||
|
||||
function formatRecharge(raw) {
|
||||
var n = Math.floor(Number(raw));
|
||||
if (!isFinite(n) || n < 0) n = 0;
|
||||
var s = String(n);
|
||||
if (s.length <= 3) return s;
|
||||
var out = [];
|
||||
var rem = s.length % 3;
|
||||
if (rem) out.push(s.slice(0, rem));
|
||||
for (var i = rem; i < s.length; i += 3) out.push(s.slice(i, i + 3));
|
||||
return out.join(",");
|
||||
}
|
||||
|
||||
function syncHeaderUser() {
|
||||
var session =
|
||||
window.CookieUtil && typeof CookieUtil.getAuthSession === "function"
|
||||
? CookieUtil.getAuthSession()
|
||||
: null;
|
||||
if (!session) return;
|
||||
|
||||
var avatar = usableAvatar(session.avatar);
|
||||
document.querySelectorAll("[data-header-avatar]").forEach(function (img) {
|
||||
if (avatar) setAvatarImg(img, avatar);
|
||||
});
|
||||
|
||||
var name = String(session.nickName || session.accountNo || "").trim();
|
||||
if (name) {
|
||||
document.querySelectorAll(".user-chip__name, .user-menu__meta strong, .drawer__meta strong").forEach(function (el) {
|
||||
el.textContent = name;
|
||||
});
|
||||
}
|
||||
|
||||
if (session.userId) {
|
||||
document.querySelectorAll(".user-menu__meta span, .drawer__meta span").forEach(function (el) {
|
||||
if (/^ID\b/.test(String(el.textContent || "").trim())) {
|
||||
el.textContent = "ID " + session.userId;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(session, "recharge") && session.recharge !== "") {
|
||||
var text = formatRecharge(session.recharge);
|
||||
document.querySelectorAll("[data-header-recharge]").forEach(function (el) {
|
||||
el.textContent = text;
|
||||
});
|
||||
}
|
||||
|
||||
refreshAvatarDecrypt(document);
|
||||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", syncHeaderUser);
|
||||
} else {
|
||||
syncHeaderUser();
|
||||
}
|
||||
document.addEventListener("auth:login", syncHeaderUser);
|
||||
document.addEventListener("auth:logout", syncHeaderUser);
|
||||
window.refreshAvatarDecrypt = refreshAvatarDecrypt;
|
||||
window.setAvatarImg = setAvatarImg;
|
||||
window.syncHeaderUser = syncHeaderUser;
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user