452 lines
12 KiB
JavaScript
452 lines
12 KiB
JavaScript
const processImage = (
|
||
url,
|
||
{ w, h, q = 70, webp = true, g = "sm" } = {},
|
||
) => {
|
||
if (!url) return "";
|
||
const resizePart = w || h ? `/rs:auto:${w || 0}:${h || 0}:0` : "";
|
||
return `https://img.d.aiengines.ir/preset:sharp${resizePart}/q:${q}/g:${g}/plain/${url}${webp ? "@webp" : ""}`;
|
||
};
|
||
|
||
// Self-contained remote AppHeader.
|
||
// No host/runtime dependencies and no window.__UIKIT_REMOTE_DEPS__ access.
|
||
// Font family is inherited from the host application; all other visual styles stay isolated.
|
||
// The component is rendered as an isolated Web Component with Shadow DOM.
|
||
|
||
// Use the original 500×500 src/assets/bi-logo.webp, never a thumbnail.
|
||
// Keep the filename indirect: Vite library builds inline otherwise static new URL() assets.
|
||
// The AppHeader post-build script copies the original next to the remote module.
|
||
const DEFAULT_BI_LOGO_FILE = "bi-logo.webp";
|
||
const DEFAULT_BI_LOGO = new URL(DEFAULT_BI_LOGO_FILE, import.meta.url).href;
|
||
|
||
const DEFAULT_BI_TEXT =
|
||
"موسسه تولید سیستم های خبره و هوشمند اسلامی باقرالعلوم(ع)";
|
||
|
||
const STYLES = `
|
||
:host{
|
||
all:initial!important;
|
||
display:block!important;
|
||
position:sticky!important;
|
||
top:0!important;
|
||
z-index:1100!important;
|
||
width:100%!important;
|
||
height:64px!important;
|
||
min-width:0!important;
|
||
box-sizing:border-box!important;
|
||
flex-shrink:0!important;
|
||
font-family:inherit!important;
|
||
}
|
||
*,*::before,*::after{box-sizing:border-box}
|
||
.header{
|
||
width:100%;
|
||
height:64px;
|
||
flex-shrink:0;
|
||
padding-inline:10.5px;
|
||
border-bottom:1px solid #2B6CB0;
|
||
box-shadow:0 3px 12px rgba(0,0,0,.12);
|
||
background:#3182CE;
|
||
font-family:inherit;
|
||
direction:rtl;
|
||
}
|
||
.row{
|
||
display:flex;
|
||
height:100%;
|
||
width:100%;
|
||
min-width:0;
|
||
align-items:center;
|
||
justify-content:space-between;
|
||
gap:7px;
|
||
}
|
||
.brand-section{
|
||
display:flex;
|
||
min-width:0;
|
||
flex:1;
|
||
align-items:center;
|
||
justify-content:flex-start;
|
||
gap:8.75px;
|
||
}
|
||
.logo{
|
||
display:block;
|
||
width:46px;
|
||
height:auto;
|
||
max-width:100%;
|
||
max-height:48px;
|
||
flex-shrink:0;
|
||
object-fit:contain;
|
||
}
|
||
/* Only opt-in optimized logos use a square, padded image box. */
|
||
.logo.logo--optimized{
|
||
aspect-ratio:1 / 1;
|
||
max-height:none;
|
||
padding:0;
|
||
}
|
||
.brand{
|
||
display:flex;
|
||
min-width:0;
|
||
flex:1;
|
||
flex-direction:column;
|
||
align-items:flex-start;
|
||
justify-content:center;
|
||
gap:0;
|
||
font-weight:700;
|
||
color:#fff;
|
||
}
|
||
.title,.description{
|
||
width:100%;
|
||
overflow:hidden;
|
||
text-overflow:ellipsis;
|
||
white-space:nowrap;
|
||
}
|
||
.title{
|
||
color:#fff;
|
||
font-size:15px;
|
||
font-weight:700;
|
||
line-height:1.375;
|
||
}
|
||
.description{
|
||
color:rgba(255,255,255,.92);
|
||
font-size:10px;
|
||
font-weight:500;
|
||
line-height:1.25;
|
||
}
|
||
.end-items{
|
||
display:flex;
|
||
flex-shrink:0;
|
||
align-items:center;
|
||
height:100%;
|
||
gap:10px;
|
||
}
|
||
.bi,.extra{
|
||
display:flex;
|
||
flex-shrink:0;
|
||
flex-direction:column;
|
||
align-items:center;
|
||
justify-content:center;
|
||
gap:0;
|
||
}
|
||
.bi-logo,.extra-logo{
|
||
display:block;
|
||
width:38px;
|
||
height:auto;
|
||
max-width:100%;
|
||
max-height:46px;
|
||
object-fit:contain;
|
||
}
|
||
/* Match the main logo's measured square content box when optimization is enabled. */
|
||
.bi-logo.bi-logo--optimized{aspect-ratio:1 / 1}
|
||
.bi-text,.extra-text{
|
||
display:none;
|
||
white-space:pre;
|
||
color:#fff;
|
||
font-size:10.5px;
|
||
font-weight:400;
|
||
line-height:normal;
|
||
}
|
||
.extra--image-only{
|
||
height:100%;
|
||
gap:0;
|
||
}
|
||
.extra--image-only .extra-logo{
|
||
width:auto;
|
||
height:100%;
|
||
max-width:none;
|
||
max-height:100%;
|
||
}
|
||
@media screen and (min-width:30em){
|
||
.header{padding-inline:14px}
|
||
.description{font-size:11px}
|
||
}
|
||
@media screen and (min-width:62em){
|
||
:host{
|
||
position:relative!important;
|
||
top:auto!important;
|
||
height:110px!important;
|
||
}
|
||
.header{
|
||
height:110px;
|
||
padding-inline:20px;
|
||
border-bottom-color:#E2E8F0;
|
||
box-shadow:0 4px 10px rgba(0,0,0,.1);
|
||
}
|
||
.brand-section{gap:0}
|
||
.logo{width:100px;max-height:92px}
|
||
.logo.logo--optimized{padding:8px}
|
||
.brand{gap:3.5px}
|
||
.title{font-size:22px}
|
||
.description{
|
||
color:#fff;
|
||
font-size:15px;
|
||
font-weight:400;
|
||
line-height:normal;
|
||
}
|
||
.end-items{gap:18px}
|
||
.bi,.extra{gap:3.5px}
|
||
.bi-logo,.extra-logo{width:60px;max-height:72px}
|
||
.bi-text,.extra-text{display:block}
|
||
}
|
||
`;
|
||
|
||
function createElement(tag, className, text) {
|
||
const element = document.createElement(tag);
|
||
if (className) element.className = className;
|
||
if (text !== undefined && text !== null) element.textContent = String(text);
|
||
return element;
|
||
}
|
||
|
||
class AppHeaderElement extends HTMLElement {
|
||
constructor() {
|
||
super();
|
||
|
||
this._props = {};
|
||
this._disposeOptimizedLogo = null;
|
||
this._disposeOptimizedBiLogo = null;
|
||
this._shadow = this.attachShadow({ mode: "open" });
|
||
|
||
const style = document.createElement("style");
|
||
style.textContent = STYLES;
|
||
this._shadow.appendChild(style);
|
||
|
||
this._mount = document.createElement("div");
|
||
this._shadow.appendChild(this._mount);
|
||
}
|
||
|
||
set componentProps(value) {
|
||
const next = value || {};
|
||
const previous = this._props;
|
||
this._props = next;
|
||
if (!this.isConnected) return;
|
||
const before = Object.keys(previous);
|
||
const after = Object.keys(next);
|
||
if (before.length === after.length && before.every((key) => Object.is(previous[key], next[key]))) {
|
||
return;
|
||
}
|
||
this._render();
|
||
}
|
||
|
||
get componentProps() {
|
||
return this._props;
|
||
}
|
||
|
||
connectedCallback() {
|
||
this._render();
|
||
}
|
||
|
||
disconnectedCallback() {
|
||
this._disposeOptimizedLogo?.();
|
||
this._disposeOptimizedLogo = null;
|
||
this._disposeOptimizedBiLogo?.();
|
||
this._disposeOptimizedBiLogo = null;
|
||
}
|
||
|
||
_setOptimizedLogo(logo, source) {
|
||
// This module runs in the destination document, not on the UI Kit domain.
|
||
// Never resolve the host origin during SSR or inside the React wrapper.
|
||
if (typeof window === "undefined") {
|
||
logo.src = source;
|
||
return;
|
||
}
|
||
|
||
let absoluteSource;
|
||
try {
|
||
const url = new URL(source, window.location.href);
|
||
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
||
logo.src = source;
|
||
return;
|
||
}
|
||
absoluteSource = url.href;
|
||
} catch {
|
||
logo.src = source;
|
||
return;
|
||
}
|
||
|
||
let active = true;
|
||
let failed = false;
|
||
let lastRequestedUrl = "";
|
||
let frameId;
|
||
let resizeObserver;
|
||
|
||
const onError = () => {
|
||
if (!active || failed) return;
|
||
failed = true;
|
||
// If the optimization service cannot serve this image, retain the logo.
|
||
logo.src = source;
|
||
};
|
||
|
||
const update = () => {
|
||
if (!active || failed || !logo.isConnected) return;
|
||
const rect = logo.getBoundingClientRect();
|
||
const boxSize = Math.max(0, Math.round(rect.width));
|
||
if (!boxSize) return;
|
||
|
||
// Match the actual padding: 0 on mobile and 8px on desktop.
|
||
// The optimized remote image must fill the usable content box.
|
||
const logoStyle = window.getComputedStyle(logo);
|
||
const horizontalPadding =
|
||
(parseFloat(logoStyle.paddingLeft) || 0) +
|
||
(parseFloat(logoStyle.paddingRight) || 0);
|
||
const imageSize = Math.max(1, Math.round(boxSize - horizontalPadding));
|
||
const url = processImage(absoluteSource, {
|
||
w: imageSize,
|
||
h: imageSize,
|
||
q: 100,
|
||
webp: true,
|
||
});
|
||
if (url === lastRequestedUrl) return;
|
||
lastRequestedUrl = url;
|
||
logo.src = url;
|
||
};
|
||
|
||
const scheduleUpdate = () => {
|
||
if (!active || failed || frameId !== undefined) return;
|
||
frameId = window.requestAnimationFrame(() => {
|
||
frameId = undefined;
|
||
update();
|
||
});
|
||
};
|
||
|
||
logo.addEventListener("error", onError);
|
||
logo.addEventListener("load", scheduleUpdate);
|
||
|
||
if (typeof ResizeObserver !== "undefined") {
|
||
resizeObserver = new ResizeObserver(scheduleUpdate);
|
||
resizeObserver.observe(logo);
|
||
} else {
|
||
window.addEventListener("resize", scheduleUpdate);
|
||
}
|
||
scheduleUpdate();
|
||
|
||
return () => {
|
||
active = false;
|
||
if (frameId !== undefined) window.cancelAnimationFrame(frameId);
|
||
resizeObserver?.disconnect();
|
||
window.removeEventListener("resize", scheduleUpdate);
|
||
logo.removeEventListener("error", onError);
|
||
logo.removeEventListener("load", scheduleUpdate);
|
||
};
|
||
}
|
||
|
||
_render() {
|
||
if (!this._mount) return;
|
||
|
||
this._disposeOptimizedLogo?.();
|
||
this._disposeOptimizedLogo = null;
|
||
this._disposeOptimizedBiLogo?.();
|
||
this._disposeOptimizedBiLogo = null;
|
||
|
||
const {
|
||
logoSrc,
|
||
logoOptimize = false,
|
||
logoAlt = "logo",
|
||
title,
|
||
description,
|
||
biLogoSrc = DEFAULT_BI_LOGO,
|
||
biLogoOptimize = false,
|
||
biLogoAlt = "نشان موسسه باقرالعلوم",
|
||
biText = DEFAULT_BI_TEXT,
|
||
showBi = true,
|
||
extraLogoSrc,
|
||
extraLogoAlt = "نشان تکمیلی",
|
||
extraText,
|
||
showExtra = false,
|
||
bg,
|
||
backgroundColor,
|
||
dir = "rtl",
|
||
} = this._props;
|
||
|
||
this._mount.replaceChildren();
|
||
|
||
const header = createElement("header", "header");
|
||
header.dir = dir;
|
||
header.style.backgroundColor = bg ?? backgroundColor ?? "#3182CE";
|
||
|
||
const row = createElement("div", "row");
|
||
const brandSection = createElement("div", "brand-section");
|
||
|
||
if (logoSrc) {
|
||
const logo = createElement("img", "logo");
|
||
if (logoOptimize === true) logo.classList.add("logo--optimized");
|
||
logo.alt = logoAlt || "logo";
|
||
logo.loading = logoOptimize ? "eager" : "lazy";
|
||
brandSection.appendChild(logo);
|
||
if (!logoOptimize) logo.src = logoSrc;
|
||
}
|
||
|
||
const brand = createElement("div", "brand");
|
||
|
||
if (title !== undefined && title !== null) {
|
||
brand.appendChild(createElement("div", "title", title));
|
||
}
|
||
|
||
if (description !== undefined && description !== null) {
|
||
brand.appendChild(createElement("div", "description", description));
|
||
}
|
||
|
||
brandSection.appendChild(brand);
|
||
row.appendChild(brandSection);
|
||
|
||
if (showBi || showExtra) {
|
||
const endItems = createElement("div", "end-items");
|
||
|
||
// DOM order is intentional: with RTL flex layout, Extra is rendered at
|
||
// the outer edge and Bi immediately after it, matching the public API.
|
||
if (showBi) {
|
||
const bi = createElement("div", "bi");
|
||
|
||
if (biLogoSrc) {
|
||
const image = createElement("img", "bi-logo");
|
||
if (biLogoOptimize === true) image.classList.add("bi-logo--optimized");
|
||
image.alt = biLogoAlt || "";
|
||
image.loading = biLogoOptimize ? "eager" : "lazy";
|
||
if (!biLogoOptimize) image.src = biLogoSrc;
|
||
bi.appendChild(image);
|
||
}
|
||
|
||
if (biText) {
|
||
bi.appendChild(createElement("div", "bi-text", biText));
|
||
}
|
||
|
||
endItems.appendChild(bi);
|
||
}
|
||
|
||
if (showExtra) {
|
||
const extra = createElement("div", "extra");
|
||
|
||
if (!extraText) {
|
||
extra.classList.add("extra--image-only");
|
||
}
|
||
|
||
if (extraLogoSrc) {
|
||
const image = createElement("img", "extra-logo");
|
||
image.src = extraLogoSrc;
|
||
image.alt = extraLogoAlt || "";
|
||
image.loading = "lazy";
|
||
extra.appendChild(image);
|
||
}
|
||
|
||
if (extraText) {
|
||
extra.appendChild(createElement("div", "extra-text", extraText));
|
||
}
|
||
|
||
endItems.appendChild(extra);
|
||
}
|
||
|
||
row.appendChild(endItems);
|
||
}
|
||
|
||
header.appendChild(row);
|
||
this._mount.appendChild(header);
|
||
|
||
if (logoSrc && logoOptimize === true) {
|
||
const logo = brandSection.querySelector(".logo");
|
||
if (logo) this._disposeOptimizedLogo = this._setOptimizedLogo(logo, logoSrc);
|
||
}
|
||
if (showBi && biLogoSrc && biLogoOptimize === true) {
|
||
const logo = row.querySelector(".bi-logo");
|
||
if (logo) this._disposeOptimizedBiLogo = this._setOptimizedLogo(logo, biLogoSrc);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!customElements.get("uikit-app-header")) {
|
||
customElements.define("uikit-app-header", AppHeaderElement);
|
||
}
|