123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import BasicEvent from "../Basic/BasicEvent";
- import BasicObject from "../Basic/BasicObject";
- import Tool from "../Tool/Tool";
- /**
- * 事件管理器 - 逻辑层
- */
- export default class EventManager {
- /**事件名划分事件列表 */
- private static nameEvent: Map<string, Array<BasicEvent>>;
- /**事件对象划分事件列表 */
- private static targetEvent: Map<any, Array<BasicEvent>>;
- /**事件对象列表 */
- private static eventTarget: Map<any, Array<BasicObject>>;
- /**是否允许事件传递 */
- private static isEventPropagation: Map<any, boolean>;
- private constructor () {
-
- }
- /**创建 */
- public static create (): void {
- Tool.log("生成事件管理器");
-
- this.nameEvent = new Map<string, Array<BasicEvent>>();
- this.targetEvent = new Map<any, Array<BasicEvent>>();
- this.eventTarget = new Map<any, Array<BasicObject>>();
- this.isEventPropagation = new Map<any, boolean>();
- }
- /**销毁 */
- public static destroy () {
- Tool.log("销毁事件管理器");
- this.nameEvent.forEach((value: BasicEvent[], key: string) => {
- for (let index: number = 0; index < value.length; index++) {
- value[index] = null;
- }
- });
- this.nameEvent = null;
- this.targetEvent = null;
- this.eventTarget = null;
- this.isEventPropagation = null;
- }
- /**注册事件 */
- public static onEvent (event: string, target: any, callback: Function, priority?: number, once?: boolean): void {
- let basicEvent: BasicEvent = new BasicEvent();
- basicEvent.event = event;
- basicEvent.target = target;
- basicEvent.callback = callback;
- basicEvent.priority = priority || 0;
- basicEvent.once = once || false;
- basicEvent.stopPropagation = () => {
- this.isEventPropagation.set(basicEvent.event, false);
- };
- // 添加事件
- let nameEvent: Array<BasicEvent> = [];
- if (this.nameEvent.has(basicEvent.event) == false) {
- this.nameEvent.set(basicEvent.event, []);
- }
- nameEvent = this.nameEvent.get(basicEvent.event);
- nameEvent.push(basicEvent);
- this.nameEvent.set(basicEvent.event, nameEvent);
- let targetEvent: Array<BasicEvent> = [];
- if (this.targetEvent.has(basicEvent.target) == false) {
- this.targetEvent.set(basicEvent.target, []);
- }
- targetEvent = this.targetEvent.get(basicEvent.target);
- targetEvent.push(basicEvent);
- this.targetEvent.set(basicEvent.target, targetEvent);
- let eventTarget: Array<BasicObject> = [];
- if (this.eventTarget.has(target.id) == false) {
- this.eventTarget.set(target.id, []);
- }
- eventTarget = this.eventTarget.get(target.id);
- if (eventTarget.includes(target) == false) {
- eventTarget.push(target);
- this.eventTarget.set(target.id, eventTarget);
- }
- // 排序优化
- if (nameEvent.length > 1) {
- // 优先级排序
- nameEvent.sort((a: BasicEvent, b: BasicEvent): any => {
- return b.priority - a.priority;
- });
- let temp: Array<BasicObject> = [];
- for (let i = 0; i < nameEvent.length; i++) {
- for (let j = 0; j < eventTarget.length; j++) {
- if (nameEvent[i].target == eventTarget[j]) {
- temp.push(eventTarget[j]);
- eventTarget.splice(j, 1);
- break;
- }
- }
- }
- eventTarget = temp.concat(eventTarget);
- this.eventTarget.set(target.id, eventTarget);
- }
- }
-
- /**注销事件 */
- public static offEvent (event: string, target: any, callback: Function): void {
- let nameEvent: Array<BasicEvent> = this.nameEvent.get(event);
- if (nameEvent == null) {
- return;
- }
- for (let j: number = 0; j < nameEvent.length; j++) {
- let basicEvent: BasicEvent = nameEvent[j];
- if (callback == basicEvent.callback && basicEvent.target.id == target.id) {
- nameEvent.splice(j, 1);
- j--;
- let targetEvent: Array<BasicEvent> = this.targetEvent.get(basicEvent.target);
- if (targetEvent != null) {
- for (let k: number = 0; k < targetEvent.length; k++) {
- if (event == targetEvent[k].event && callback == basicEvent.callback || targetEvent[k].target == null) {
- targetEvent.splice(k, 1);
- k--;
- }
- }
- this.targetEvent.set(basicEvent.target, targetEvent);
- }
- }
- }
- }
- /**注销事件 */
- public static offEventByTarget (target: any): void {
- let targetEvent: Array<BasicEvent> = this.targetEvent.get(target);
- if (targetEvent == null) {
- return;
- }
- for (let j: number = 0; j < targetEvent.length; j++) {
- let basicEvent: BasicEvent = targetEvent[j];
- let nameEvent: Array<BasicEvent> = this.nameEvent.get(basicEvent.event);
- if (nameEvent != null) {
- for (let k: number = 0; k < nameEvent.length; k++) {
- if (basicEvent.target == nameEvent[k].target || nameEvent[k].target == null) {
- nameEvent.splice(k, 1);
- k--;
- }
- }
- this.nameEvent.set(basicEvent.event, nameEvent);
- }
- }
- this.targetEvent.delete(target);
- this.eventTarget.delete(target.id);
- }
- /**发送事件 */
- public static sendEvent (event: string, ...args: any[]): void {
- if (this.nameEvent == null ||this.nameEvent.has(event) == false) {
- return;
- }
- this.isEventPropagation.set(event, true);
- let nameEvent: Array<BasicEvent> = this.nameEvent.get(event).concat([]);
- if (nameEvent != null) {
- for (let index: number = 0; index < nameEvent.length; index++) {
- if (this.isEventPropagation.get(event) == false) {
- return;
- }
- let basicEvent: BasicEvent = nameEvent[index];
- if (basicEvent.target != null && (basicEvent.target.isEnabled == true || basicEvent.target.enabled == true)) {
- let _args = [].concat(args).concat([basicEvent]);
- basicEvent.callback.apply(basicEvent.target, _args);
- if (basicEvent.once == true) {
- this.offEvent(basicEvent.event, basicEvent.target, basicEvent.callback);
- }
- }
- }
- }
- }
- /**发送事件 */
- public static sendEventByTargetID (event: string, id: string, ...args: any[]): void {
- if (this.nameEvent == null ||this.nameEvent.has(event) == false) {
- return;
- }
- this.isEventPropagation.set(event, true);
- let eventTarget: Array<BasicObject> = this.eventTarget.get(id);
- for (let i in eventTarget) {
- let targetEvent: Array<BasicEvent> = this.targetEvent.get(eventTarget[i]);
- if (targetEvent != null) {
- for (let index: number = 0; index < targetEvent.length; index++) {
- let basicEvent: BasicEvent = targetEvent[index];
- if (event == basicEvent.event && (basicEvent.target.isEnabled == true || basicEvent.target.enabled == true)) {
- let _args = [].concat(args).concat([basicEvent]);
- basicEvent.callback.apply(basicEvent.target, _args);
- if (basicEvent.once == true) {
- this.offEvent(basicEvent.event, basicEvent.target, basicEvent.callback);
- }
- break;
- }
- }
- }
- }
- }
- }
|