diff --git a/source/Context/Popups.ts b/source/Context/Popups.ts index 30cc1fb..ef88393 100644 --- a/source/Context/Popups.ts +++ b/source/Context/Popups.ts @@ -1,4 +1,7 @@ import { ReactNode } from "react"; +import { Emitter } from "@Model/Emitter"; + +type IPopupConstructor = new (controller: PopupController, id: string) => Popup; /** * 弹窗类型 @@ -23,7 +26,7 @@ class Popup { /** * 渲染层级 */ - public index: number = 0; + public index: number = Infinity; /** * react 节点 @@ -51,10 +54,14 @@ class Popup { } } +interface IPopupControllerEvent { + popupChange: void; +} + /** * 弹窗模型 */ -class PopupController { +class PopupController extends Emitter { /** * ID 序列号 @@ -66,20 +73,54 @@ class PopupController { */ public popups: Popup[] = []; + /** + * 排序并重置序号 + */ public sortPopup() { this.popups = this.popups.sort((a, b) => a.index - b.index); + this.popups = this.popups.map((popup, index) => { + popup.index = (index + 1); + return popup; + }); } - public newPopup(): Popup { - let newPopup = new Popup(this, `P-${this.idIndex ++}`); + /** + * 实例化并开启一个弹窗 + */ + public showPopup

(popup?: P): Popup { + let newPopup = new (popup ?? Popup)(this, `P-${this.idIndex ++}`); this.popups.push(newPopup); + this.emit("popupChange"); return newPopup; } - public closePopup(popup: Popup | string) { - + /** + * 关闭一个弹窗 + */ + public closePopup(popup: Popup | string): Popup | undefined { + let id: string; + if (popup instanceof Popup) { + id = popup.id; + } else { + id = popup; + } + let closePopup: Popup | undefined; + this.popups = this.popups.filter( + currentPopup => { + let isDelete = currentPopup.id === id; + if (isDelete) { + closePopup = currentPopup; + return false; + } else { + return true; + } + } + ); + if (closePopup) { + this.emit("popupChange"); + } + return closePopup; } - } export { Popup, PopupController } \ No newline at end of file