-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.js
More file actions
128 lines (109 loc) · 2.93 KB
/
event.js
File metadata and controls
128 lines (109 loc) · 2.93 KB
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
/**
* 事件管理组件
*
* 版权所有(C) 2013 EchoFUN (xukai.ken@gmail.com)
*
* 2013.12.12 初始化文件。
* 2013.12.15 修复无法整个迭代unbind数组的bug。
*
* Example:
* var test = document.getElementById('test');
* evt(test).bind('custom', function() {
*
* })
*
* evt(test).trigger('custom');
*
*/
;var cacheId = 'cache' + setTimeout(function() {}, 0);
var evt = (function(global, cacheId) {
'use strict';
var isIE = !!document.attachEvent;
var typeExpress = /^[a-z]+$/i;
// 缓存所有的事件。
var events = {};
// 产生一个唯一的键值。
var globalId = 1;
function wrap(node) {
return new _wrap(node);
}
function _wrap(node) {
var _node = (node && node.nodeType == 1) ? node : '';
if (!_node) {
return 'Invalidate node !';
}
_node[cacheId] || (_node[cacheId] = globalId++);
this._node = _node;
}
function _type(o) {
var type = {}.toString.call(o);
return type.slice(8, -1).toLowerCase();
}
var proto = _wrap.prototype;
proto.bind = function(type, handle, capture) {
if (_type(type) !== 'string' || !typeExpress.test(type) || _type(handle) !== 'function') {
return 'Invalidate params !';
}
var node = this._node;
if (isIE) {
node.attachEvent('on' + type, handle);
} else {
node.addEventListener(type, handle, capture);
}
// 如果还未开始对这个对象有缓存,则加入缓存序列。
if (!events[node[cacheId]]) {
events[node[cacheId]] = {};
}
var evts = events[node[cacheId]];
if (evts && evts[type]) {
evts[type].push(handle);
} else {
evts[type] = [handle];
}
return this;
};
proto.unbind = function(type, handle, capture) {
if (_type(type) !== 'string' || !typeExpress.test(type) || (handle && (_type(handle) !== 'function'))) {
return 'Invalidate params !';
}
var node = this._node;
var evts = events[node[cacheId]];
if (!evts[type]) {
return 'Not exist specified event !';
}
if (isIE) {
node.detachEvent('on' + type, handle);
} else {
node.removeEventListener(type, handle, capture || false);
}
if (handle) {
// Fix bug for cannot iterate the total array.
var evtsList = evts[type], tempList = [];
for (var i in evtsList) {
if (evtsList[i] !== handle) {
tempList.push(evtsList[i]);
}
}
evts[type] = tempList;
} else {
evts[type] = [];
}
return this;
};
proto.trigger = function(type) {
if (_type(type) !== 'string') {
return 'Invalidate params !';
}
var args = Array.prototype.slice.call(arguments);
args.shift();
var evts = events[this._node[cacheId]];
var evtsList = evts[type];
if (evtsList && evtsList.length) {
for (var i in evtsList) {
evtsList[i].apply(this._node, args);
}
}
return this;
};
return wrap;
})(this, cacheId);