Files
jiapu/dist/public/js/page-effects.js
T

2 lines
6.1 KiB
JavaScript

(function(){const w=/^(?:javascript|data|vbscript|mailto|tel):/i,m="genealogy_public_user_v1";let f;function d(){try{return window.localStorage}catch(e){return null}}function A(e){try{const t=new URL(String(e||"").trim());return t.protocol==="https:"||t.protocol==="http:"?t.href:""}catch(t){return""}}function g(e){const t=String(e&&e.nickName||"").trim(),r=A(e&&(e.avatarUrl||e.avatarFile&&e.avatarFile.accessUrl));return t?{nickName:t,avatarUrl:r}:null}function h(){const e=d();if(!e)return null;try{return g(JSON.parse(e.getItem(m)||"null"))}catch(t){return null}}function y(){const e=d();try{return e&&e.getItem("genealogy_auth_token")||""}catch(t){return""}}function E(e){const t=document.createElement("a"),r=document.createElement("span"),a=document.createElement("span"),o=document.createElement("span"),s=Array.from(e.nickName)[0]||"家";if(t.className="nav-user-entry",t.href="profile.html",t.setAttribute("aria-label",`${e.nickName}的个人中心`),r.className="nav-user-avatar",a.className="nav-user-avatar-fallback",a.textContent=s,r.appendChild(a),e.avatarUrl){const n=document.createElement("img");n.src=e.avatarUrl,n.alt="",n.loading="lazy",n.decoding="async",n.addEventListener("error",()=>n.remove()),r.prepend(n)}return o.className="nav-user-name",o.textContent=e.nickName,t.append(r,o),t}function S(e){return new Promise((t,r)=>{const a=document.querySelector(`script[src="${e}"]`);if(a&&a.dataset.loaded==="true"){t();return}if(a){t();return}const o=a||document.createElement("script");o.addEventListener("load",()=>{o.dataset.loaded="true",t()},{once:!0}),o.addEventListener("error",r,{once:!0}),a||(o.src=e,document.head.appendChild(o))})}function N(){return window.GenealogyApi&&window.GenealogyApi.defaultClient?Promise.resolve(window.GenealogyApi.defaultClient):(f||(f=["utils/StorageUtil.js","utils/axios.js","utils/AxiosRequestUtil.js","utils/ApiClient.js"].reduce((e,t)=>e.then(()=>S(t)),Promise.resolve()).then(()=>window.GenealogyApi&&window.GenealogyApi.defaultClient)),f)}function L(e){if(!e||e.tagName!=="A"||e.hasAttribute("download"))return!1;const t=(e.getAttribute("href")||"").trim();return!!(t&&t!=="#"&&!t.startsWith("#")&&!w.test(t))}function p(e){if(!L(e))return;e.classList.contains("brand")&&e.getAttribute("href")!=="index.html"&&e.setAttribute("href","index.html"),e.target!=="_blank"&&(e.target="_blank");const t=(e.getAttribute("rel")||"").split(/\s+/).filter(Boolean);t.includes("noopener")||e.setAttribute("rel",[...t,"noopener"].join(" "))}function v(e){e.nodeType===1&&(e.matches("a[href]")&&p(e),e.querySelectorAll("a[href]").forEach(p))}function P(){v(document.documentElement),new MutationObserver(e=>{e.forEach(t=>{if(t.type==="attributes"){p(t.target);return}t.addedNodes.forEach(v)})}).observe(document.documentElement,{subtree:!0,childList:!0,attributes:!0,attributeFilter:["href","target","download"]})}function b(){const e=y(),t=h()||{nickName:"我的账户",avatarUrl:""};e&&document.querySelectorAll(".nav-actions").forEach(r=>{const a=r.querySelector('a[href="login.html"]'),o=r.querySelector('a[href="register.html"]'),s=r.querySelector('a[href="profile-data.html"]'),n=r.querySelector(".nav-user-entry"),i=r.querySelector('a[href="create-genealogy.html"]'),u=E(t),l=n||a||s||i;l?l.replaceWith(u):r.prepend(u),a&&a.isConnected&&a.remove(),s&&s.isConnected&&s.remove(),n&&n.isConnected&&n.remove(),o&&o.remove(),i&&(i.href="profile-create-family.html")})}window.PublicNavigation={storeUser:function(e){const t=d(),r=g(e);return!t||!r?!1:(t.setItem(m,JSON.stringify(r)),b(),!0)},clearUser:function(){const e=d();e&&e.removeItem(m)}};async function U(){if(!(!y()||h()||document.body.matches(".page-profile, .page-profile-module")))try{const e=await N();e&&window.PublicNavigation.storeUser(await e.currentProfile())}catch(e){window.PublicNavigation.clearUser()}}function k(){const e=document.body,t=document.querySelector(".site-header"),r=t&&t.querySelector(".nav"),a=r&&r.querySelector(".nav-links"),o=r&&r.querySelector(".nav-actions");if(!e||e.matches(".page-profile, .page-profile-module")||!t||!r||!a||r.querySelector("[data-site-menu-toggle]"))return;const s="site-mobile-menu",n=document.createElement("button"),i=document.createElement("div"),u=document.createElement("div");n.className="site-menu-toggle",n.type="button",n.textContent="菜单",n.setAttribute("data-site-menu-toggle",""),n.setAttribute("aria-expanded","false"),n.setAttribute("aria-controls",s),i.className="site-mobile-menu",i.id=s,i.hidden=!0,i.setAttribute("data-site-mobile-menu",""),i.setAttribute("aria-hidden","true"),u.className="container site-mobile-menu-inner",[...a.querySelectorAll("a"),...o?o.querySelectorAll("a"):[]].forEach(c=>{u.appendChild(c.cloneNode(!0))}),i.appendChild(u),r.appendChild(n),t.appendChild(i),e.classList.add("has-site-mobile-menu");const l=c=>{i.hidden=!c,i.classList.toggle("is-open",c),i.setAttribute("aria-hidden",c?"false":"true"),n.setAttribute("aria-expanded",c?"true":"false")};n.addEventListener("click",c=>{c.stopPropagation(),l(n.getAttribute("aria-expanded")!=="true")}),i.addEventListener("click",c=>{c.target.closest("a")&&l(!1),c.stopPropagation()}),document.addEventListener("click",()=>l(!1)),document.addEventListener("keydown",c=>{c.key!=="Escape"||i.hidden||(l(!1),n.focus())})}P(),b(),k(),U();const q=window.matchMedia("(hover: hover) and (pointer: fine)").matches,C=window.matchMedia("(prefers-reduced-motion: reduce)").matches;!q||C||(document.querySelectorAll(".tilt-card").forEach(e=>{e.addEventListener("pointermove",t=>{const r=e.getBoundingClientRect(),a=(t.clientX-r.left)/r.width-.5,o=(t.clientY-r.top)/r.height-.5;e.style.setProperty("--tilt-y",`${a*5}deg`),e.style.setProperty("--tilt-x",`${o*-5}deg`)}),e.addEventListener("pointerleave",()=>{e.style.setProperty("--tilt-y","0deg"),e.style.setProperty("--tilt-x","0deg")})}),document.querySelectorAll(".magnetic").forEach(e=>{e.classList.contains("brand")||e.querySelector(".brand-logo")||(e.addEventListener("pointermove",t=>{const r=e.getBoundingClientRect(),a=t.clientX-r.left-r.width/2,o=t.clientY-r.top-r.height/2;e.style.transform=`translate(${a*.08}px, ${o*.08}px)`}),e.addEventListener("pointerleave",()=>{e.style.transform=""}))}))})();