forked from podgorniy/javascript-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_events.js
More file actions
72 lines (63 loc) · 1.36 KB
/
Copy pathcustom_events.js
File metadata and controls
72 lines (63 loc) · 1.36 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
var ce = (function () {
var event_storage,
global_context;
event_storage = {};
global_context = this;
function index_of (arr, el) {
var io,
i;
io = Array.prototype.indexOf;
if (io) {
return io.call(arr, el);
} else {
for (i = 0; i < arr.length; i += 1) {
if (el === arr[i]) {
return i;
}
}
}
}
function watch (event_name, callback) {
if (!event_storage[event_name]) {
event_storage[event_name] = [];
}
event_storage[event_name].push(callback);
}
function unwatch (event_name, callback) {
var event_callbacks,
callback_position;
event_callbacks = event_storage[event_name];
if (event_callbacks) {
if (callback) {
callback_position = index_of(event_callbacks, callback);
if (callback_position !== -1) {
event_callbacks.splice(callback_position, 1);
if (!event_callbacks.length) {
delete event_storage[event_name];
}
return true;
}
} else {
delete event_storage[event_name];
return true;
}
}
return false;
}
function fire (event_name, data) {
var event_callbacks,
i;
event_callbacks = event_storage[event_name];
if (event_callbacks) {
for (i = 0; i < event_callbacks.length; i += 1) {
event_callbacks[i].call(global_context, data);
}
}
}
return {
watch : watch,
unwatch : unwatch,
fire : fire,
_event_storage : event_storage
};
}());