@whitesev/pops
Version:
弹窗库,包含了alert、confirm、prompt、drawer、folder、loading、iframe、panel、tooltip、searchSuggestion、rightClickMenu组件
168 lines (157 loc) • 5.37 kB
text/typescript
import { GlobalConfig } from "../../config/GlobalConfig";
import { EventEmiter } from "../../event/EventEmiter";
import { PopsElementHandler } from "../../handler/PopsElementHandler";
import { PopsHandler } from "../../handler/PopsHandler";
import { PopsInstHandler } from "../../handler/PopsInstHandler";
import { PopsCSS } from "../../PopsCSS";
import type { EventMap } from "../../types/EventEmitter";
import type { PopsType } from "../../types/main";
import { popsDOMUtils } from "../../utils/PopsDOMUtils";
import { popsUtils } from "../../utils/PopsUtils";
import { PopsAlertDefaultConfig } from "./defaultConfig";
import type { PopsAlertConfig } from "./types";
export const PopsAlert = {
init(__config__: PopsAlertConfig) {
const guid = popsUtils.getRandomGUID();
// 设置当前类型
const popsType: PopsType = "alert";
let config = PopsAlertDefaultConfig();
config = popsUtils.assign(config, GlobalConfig.getGlobalConfig());
config = popsUtils.assign(config, __config__);
config = PopsHandler.handleOnly(popsType, config);
const emitter = config.emitter ?? new EventEmiter<EventMap>(popsType);
const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow(config);
PopsHandler.handleInit($shadowRoot, [
{
name: "index",
css: PopsCSS.index,
},
{
name: "ninePalaceGridPosition",
css: PopsCSS.ninePalaceGridPosition,
},
{
name: "scrollbar",
css: PopsCSS.scrollbar,
},
{
name: "button",
css: PopsCSS.button,
},
{
name: "anim",
css: PopsCSS.anim,
},
{
name: "common",
css: PopsCSS.common,
},
{
name: "skeleton",
css: PopsCSS.skeletonCSS,
},
{
name: "alertCSS",
css: PopsCSS.alertCSS,
},
]);
// 先把z-index提取出来
const zIndex = PopsHandler.getTargerOrFunctionValue(config.zIndex);
const maskHTML = PopsElementHandler.createMask(guid, zIndex);
const headerBtnHTML = PopsElementHandler.createHeader(popsType, config);
const bottomBtnHTML = PopsElementHandler.createBottom(popsType, config);
const { headerStyle, headerPStyle } = PopsElementHandler.createHeaderStyle(popsType, config);
const { contentStyle, contentPStyle } = PopsElementHandler.createContentStyle(popsType, config);
const animHTML = PopsElementHandler.createAnim(
guid,
popsType,
config,
/*html*/ `
<div class="pops-title pops-${popsType}-title" style="text-align: ${config.title.position};${headerStyle}">${
config.title.html
? config.title.text
: `<p pops class="pops-${popsType}-title-text" style="${headerPStyle}">${config.title.text}</p>`
}${headerBtnHTML}</div>
<div class="pops-content pops-${popsType}-content" style="${contentStyle}">${
config.content.html
? config.content.text
: `<p pops class="pops-${popsType}-content-text" style="${contentPStyle}">${config.content.text}</p>`
}</div>${bottomBtnHTML}`,
bottomBtnHTML,
zIndex
);
/**
* 弹窗的主元素,包括动画层
*/
const $anim = PopsElementHandler.parseElement<HTMLDivElement>(animHTML);
const {
$pops: $pops,
$headerBtnClose: $headerCloseBtn,
$btnOk: btnOkElement,
$title: $title,
} = PopsHandler.handleQueryElement($anim, popsType);
/** 遮罩层元素 */
let $mask: HTMLDivElement | undefined = void 0;
/** 已创建的元素列表 */
const $elList: HTMLElement[] = [$anim];
// 遮罩层元素
if (config.mask.enable) {
const handleMask = PopsHandler.handleMask({
type: popsType,
guid: guid,
config: config,
animElement: $anim,
maskHTML: maskHTML,
});
$mask = handleMask.maskElement;
$elList.push($mask);
}
// 处理返回的配置
const evtConfig = PopsHandler.handleEventConfig(
config,
guid,
$shadowContainer,
$shadowRoot,
popsType,
$anim,
$pops,
emitter,
$mask
);
const result = PopsHandler.handleResultConfig(evtConfig);
// 为顶部右边的关闭按钮添加点击事件
PopsHandler.handleClickEvent("close", $headerCloseBtn, evtConfig, config.btn.close?.callback);
// 为底部ok按钮添加点击事件
PopsHandler.handleClickEvent("ok", btnOkElement, evtConfig, config.btn.ok?.callback);
// 创建到页面中
popsDOMUtils.append($shadowRoot, $elList);
emitter.emit("pops:before-append-to-page", $shadowRoot, $shadowContainer);
popsDOMUtils.appendBody($shadowContainer);
if ($mask != null) {
// 添加遮罩层
$anim.after($mask);
}
// 保存
PopsHandler.handlePush(popsType, {
guid: guid,
$anim: $anim,
$pops: $pops!,
$mask: $mask!,
$shadowContainer: $shadowContainer,
$shadowRoot: $shadowRoot,
config: config,
emitter,
});
// 拖拽
if (config.drag) {
PopsInstHandler.drag($pops!, {
dragElement: $title!,
limit: config.dragLimit,
extraDistance: config.dragExtraDistance!,
moveCallBack: config.dragMoveCallBack!,
endCallBack: config.dragEndCallBack!,
});
}
return result;
},
};