-
Notifications
You must be signed in to change notification settings - Fork 1
/
czEvent.js
140 lines (136 loc) · 3.95 KB
/
czEvent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Created by czzou on 2016/6/30.
*/
( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
module.exports = factory( global, true );
} else {
factory( global );
}
}(typeof window !== "undefined" ? window : this,function(window,noGlobal){
/**
* 定义一个事件单元类
* @param eventname
* @constructor
*/
function EventMeta(eventname){
this.name=eventname;
this.handlerMap={};
this.handlerList=[];
}
/**
* 绑定事件监听
* @param type
* @param handler
*/
function addEventListener(eventname,handler){
var EventHandlerMap=this.getEventHandlerMap();
var meta=EventHandlerMap[eventname];
if(!meta){
meta=EventHandlerMap[eventname]=new EventMeta(eventname);
}
var handlerId=handler.__handlerId;
if(!handlerId){
EventHandlerMap.__handlerId++;
handler.__handlerId=EventHandlerMap.__handlerId;
handlerId=handler.__handlerId;
}
//对于同一事件的同一处理函数,不重复绑定
if(!meta.handlerMap[handlerId]){
meta.handlerMap[handlerId]=handler;
meta.handlerList.push(handler);
}
}
/**
* 绑定只执行一次的handler
* @param type
* @param handler
*/
function once(eventname,handler){
handler.__once=true;
this.addEventListener(eventname,handler);
}
/**
* 移除事件监听
* @param type
* @param handdler
*/
function removeEventListener(eventname,handdler){
var EventHandlerMap=this.getEventHandlerMap();
var meta=EventHandlerMap[eventname];
if(!meta) return;
//移除一个handler的绑定
if(handdler && handdler.__handlerId){
var index=meta.handlerList.indexOf(handdler);
if(index>-1){
meta.handlerList.splice(index,1);
delete meta.handlerMap[handdler.__handlerId];
return;
}
}
//移除所有handler
meta.handlerMap={};
meta.handlerList.length=0;
}
/**
* 触发事件
* @param eventname
*/
function trigger(){
var EventHandlerMap=this.getEventHandlerMap();
var args=[];
var that=this;
for(var i=0;i<arguments.length;i++){
args.push(arguments[i]);
}
var eventname=args.splice(0,1);
var meta=EventHandlerMap[eventname];
if(!meta) return;
var onceHandlerList=[];
//依次同步执行handler
meta.handlerList.forEach(function(handler){
if(handler.__once){
onceHandlerList.push(handler)
}
handler.apply(this,args);
});
//清除绑定为once的事件
onceHandlerList.forEach(function(handler){
that.removeEventListener(eventname,handler);
});
}
function getEventHandlerMap(){
var EventHandlerMap=this.__EventHandlerMap;
if(EventHandlerMap) return EventHandlerMap;
return this.__EventHandlerMap={__handlerId:0};
}
var prototype={
addEventListener:addEventListener,
once:once,
removeEventListener:removeEventListener,
trigger:trigger,
getEventHandlerMap:getEventHandlerMap
}
function czEvent(fn,asObj){
if(asObj){
for(var key in prototype){
Object.defineProperty(fn,key,{
value:prototype[key]
})
}
return;
}
if(typeof fn!=="function") return;
fn.prototype=Object.create(prototype);
}
if ( typeof define === "function" && define.amd ) {
define( "czEvent", [], function() {
return czEvent;
} );
}
if ( !noGlobal ) {
window.czEvent = czEvent;
}
return czEvent;
}))