import type { PopsAlertConfig } from "../components/alert/types";
import type { PopsConfirmConfig } from "../components/confirm/types";
import type { PopsIframeConfig } from "../components/iframe/types";
import { PopsIcon } from "../PopsIcon";
import type { PopsIconType } from "../types/icon";
import type {
PopsSupportAnimConfig,
PopsSupportAnimConfigType,
PopsSupportBottomButtonConfig,
PopsSupportBottomButtonConfigType,
PopsSupportContentConfig,
PopsSupportContentConfigType,
PopsSupportHeaderTitleConfig,
PopsSupportHeaderTitleConfigType,
} from "../types/main";
import { popsDOMUtils } from "../utils/PopsDOMUtils";
export const PopsElementHandler = {
/**
* 获取遮罩层HTML
* @param guid
* @param zIndex z-index
* @param style
*/
createMask(guid: string, zIndex: number = 101, style = ""): string {
zIndex = zIndex - 100;
if (style.startsWith(";")) {
style = style.replace(";", "");
}
return /*html*/ `
`;
},
/**
* 获取动画层HTML
* @param guid
* @param type
* @param config
* @param html
* @param bottomBtnHTML
* @param zIndex
*/
createAnim(
guid: string,
type: PopsSupportAnimConfigType,
config: PopsSupportAnimConfig[keyof PopsSupportAnimConfig],
html = "",
bottomBtnHTML = "",
zIndex: number
) {
const __config__ = config as PopsAlertConfig;
let popsAnimStyle = "";
let popsStyle = "";
const popsPosition = __config__.position || "";
if (config.zIndex != null) {
popsAnimStyle += `z-index: ${zIndex};`;
// popsStyle += `z-index: ${zIndex};`;
}
if (__config__.width != null) {
popsStyle += `width: ${__config__.width};`;
}
if (__config__.height != null) {
popsStyle += `height: ${__config__.height};`;
}
const hasBottomBtn = bottomBtnHTML.trim() === "" ? false : true;
const popsClassNameList = ["pops"];
if (config.class) {
popsClassNameList.push(...config.class.split(" "));
}
return /*html*/ `
${
config.style != null ? /*html*/ `` : ""
}
${config.lightStyle != null ? /*html*/ `` : ""}
${config.darkStyle != null ? /*html*/ `` : ""}
${html}
`;
},
/**
* 获取顶部按钮层HTML
* @param type
* @param config
*/
createHeader(
type: PopsSupportHeaderTitleConfigType,
config: PopsSupportHeaderTitleConfig[keyof PopsSupportHeaderTitleConfig]
): string {
if (!config.btn) {
return "";
}
const confirm_config = config as PopsConfirmConfig;
if (type !== "iframe" && !confirm_config.btn?.close?.enable) {
return "";
}
let resultHTML = "";
// let btnStyle = "";
let closeHTML = "";
const iframe_config = config as PopsIframeConfig;
if (type === "iframe" && iframe_config.topRightButton?.trim() !== "") {
// iframe的
let topRightButtonHTML = "";
iframe_config.topRightButton.split("|").forEach((item: string) => {
// 最小化、最大化、窗口化、关闭按钮
item = item.toLowerCase();
topRightButtonHTML += /*html*/ `
`;
});
resultHTML = /*html*/ `
`;
} else {
if (confirm_config.btn?.close?.enable) {
// 关闭按钮
closeHTML = /*html*/ `
`;
}
resultHTML = closeHTML;
}
return resultHTML;
},
/**
* 获取标题style
* @param type 弹窗类型
* @param config 弹窗配置
*/
createHeaderStyle(
type: PopsSupportHeaderTitleConfigType,
config: PopsSupportHeaderTitleConfig[keyof PopsSupportHeaderTitleConfig]
) {
return {
headerStyle: config?.title?.html ? config?.title?.style || "" : "",
headerPStyle: config?.title?.html ? "" : config?.title?.style || "",
};
},
/**
* 获取底部按钮层HTML
* @param type
* @param config
*/
createBottom(
type: PopsSupportBottomButtonConfigType,
config: Omit
): string {
if (config.btn == null) {
// 未设置btn参数
return "";
}
const confirm_config = config as Required;
if (!(config.btn?.ok?.enable || confirm_config.btn?.cancel?.enable || confirm_config.btn?.other?.enable)) {
// 确定、取消、其它按钮都未启用直接返回
return "";
}
let btnStyle = "";
let resultHTML = "";
let okHTML = "";
let cancelHTML = "";
let ohterHTML = "";
if (config.btn.position) {
btnStyle += `justify-content: ${config.btn.position};`;
}
if (confirm_config.btn.reverse) {
btnStyle += "flex-direction: row-reverse;";
}
if (config.btn?.ok?.enable) {
// 处理确定按钮的尺寸问题
let okButtonSizeClassName = "";
if (config.btn.ok.size === "large") {
okButtonSizeClassName = "pops-button-" + config.btn.ok.size;
} else if (config.btn.ok.size === "small") {
okButtonSizeClassName = "pops-button-" + config.btn.ok.size;
}
let okIconHTML = "";
const okIcon = confirm_config.btn.ok!.icon! as PopsIconType | string;
if (okIcon !== "") {
// 判断图标是否是svg库内的
let iconHTML = "";
if (PopsIcon.hasIcon(okIcon)) {
iconHTML = PopsIcon.getIcon(okIcon)!;
} else {
iconHTML = okIcon;
}
iconHTML = iconHTML || "";
okIconHTML = /*html*/ `${iconHTML}`;
}
okHTML = /*html*/ `
`;
}
if (confirm_config.btn?.cancel?.enable) {
// 处理取消按钮的尺寸问题
let cancelButtonSizeClassName = "";
if (confirm_config.btn.cancel.size === "large") {
cancelButtonSizeClassName = "pops-button-" + confirm_config.btn.cancel.size;
} else if (confirm_config.btn.cancel.size === "small") {
cancelButtonSizeClassName = "pops-button-" + confirm_config.btn.cancel.size;
}
let cancelIconHTML = "";
const cancelIcon = confirm_config.btn.cancel!.icon as PopsIconType | string;
if (cancelIcon !== "") {
let iconHTML = "";
// 判断图标是否是svg库内的
if (PopsIcon.hasIcon(cancelIcon)) {
iconHTML = PopsIcon.getIcon(cancelIcon)!;
} else {
iconHTML = cancelIcon;
}
iconHTML = iconHTML || "";
cancelIconHTML = /*html*/ `${iconHTML}`;
}
cancelHTML = /*html*/ `
`;
}
if (confirm_config.btn?.other?.enable) {
// 处理其它按钮的尺寸问题
let otherButtonSizeClassName = "";
if (confirm_config.btn.other.size === "large") {
otherButtonSizeClassName = "pops-button-" + confirm_config.btn.other.size;
} else if (confirm_config.btn.other.size === "small") {
otherButtonSizeClassName = "pops-button-" + confirm_config.btn.other.size;
}
let otherIconHTML = "";
const otherIcon = confirm_config.btn.other!.icon as PopsIconType | string;
if (otherIcon !== "") {
let iconHTML = "";
// 判断图标是否是svg库内的
if (PopsIcon.hasIcon(otherIcon)) {
iconHTML = PopsIcon.getIcon(otherIcon)!;
} else {
otherIcon;
}
iconHTML = iconHTML || "";
otherIconHTML = /*html*/ `${iconHTML}`;
}
ohterHTML = /*html*/ `
`;
}
if (confirm_config.btn.merge) {
let flexStyle = "display: flex;";
if (confirm_config.btn.mergeReverse) {
flexStyle += "flex-direction: row-reverse;";
} else {
flexStyle += "flex-direction: row;";
}
resultHTML = /*html*/ `
${ohterHTML}
${okHTML}${cancelHTML}
`;
} else {
resultHTML = /*html*/ `${okHTML}${cancelHTML}${ohterHTML}
`;
}
return resultHTML;
},
/**
* 获取内容style
* @param type 弹窗类型
* @param config 弹窗配置
*/
createContentStyle(
type: PopsSupportContentConfigType,
config: PopsSupportContentConfig[keyof PopsSupportContentConfig]
) {
return {
contentStyle: (config?.content as any)?.html ? config?.content?.style || "" : "",
contentPStyle: (config?.content as any)?.html ? "" : config?.content?.style || "",
};
},
/**
* 将html转换成元素
* @param html
*/
parseElement(html: string): T {
return popsDOMUtils.parseTextToDOM(html);
},
/**
* 添加样式元素
*/
addStyle($parent: HTMLElement | ShadowRoot, style?: string | null) {
if (style == null) return;
const $css = popsDOMUtils.createElement(
"style",
{
innerHTML: style,
},
{
type: "text/css",
"data-name": "general",
}
);
$parent.appendChild($css);
return $css;
},
/**
* 添加在浅色模式下生效的style元素
*/
addLightStyle($parent: HTMLElement | ShadowRoot, style?: string | null) {
const darkCSS = /*css*/ `
@media (prefers-color-scheme: light) {
${style ?? ""}
}
`;
const $css = this.addStyle($parent, darkCSS);
if (!$css) {
return;
}
$css.setAttribute("data-name", "light");
return $css;
},
/**
* 添加在深色模式下生效的style元素
*/
addDarkStyle($parent: HTMLElement | ShadowRoot, style?: string | null) {
const darkCSS = /*css*/ `
@media (prefers-color-scheme: dark) {
${style ?? ""}
}
`;
const $css = this.addStyle($parent, darkCSS);
if (!$css) {
return;
}
$css.setAttribute("data-name", "dark");
return $css;
},
};