-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevented.js
119 lines (119 loc) · 4.26 KB
/
evented.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
var Evented = {
v: 0.1,
events: {},
filters: {},
stop: false,
debug: false,
/**
* Add event handler
* @param {String} name
* @param {Function} function {}
* @param {Int} priority
* @param {Int} overwrite_priority
*/
on: function (name, funct, priority, overwrite_priority) {
if (typeof Evented.events[name] != 'object')
Evented.events[name] = {};
if (!overwrite_priority) {
priority = !!priority ? priority : 50;
while (typeof Evented.events[name][priority] == 'function') {
priority++;
}
}
Evented.events[name][priority] = funct;
if (this.debug) {
console.log('Evented.on', arguments);
}
return Evented;
},
off: function (name, priority) {
(priority ? delete Evented.events[name][priority] : delete Evented.events[name])
},
/*
* Do actions, the output will not be changed
Evented.on('test', function(e,a,b,c,d,e,f,g) { console.log(e,a,b,c,d,e,f,g);});
Evented.trigger('test', [1,2,3,4,5,6]);
*/
trigger: function (name, args, context, async, is_filter) {
Evented._run_functions(false, name, args, context, async, is_filter);
if (this.debug) {
console.log('Evented.trigger', arguments);
}
},
/*
* Do action and change the output
Evented.add_filter('test', function(data) { data.addme += 2; return data; });
Evented.filter('test', [{addme: 5, deleteme: true}]);
*/
filter: function (name, args, context) {
return (Evented.has_filters(name) ? Evented._run_functions(true, name, args, context, false, true) : args[0]);
if (this.debug) {
console.log('Evented.filter', arguments);
}
},
has_filters: function (name) {
for (var key in Evented.filters[name]) {
return true;
}
return false;
},
add_filter: function (name, funct, priority, overwrite_priority) {
if (typeof Evented.filters[name] != 'object')
Evented.filters[name] = {};
if (!overwrite_priority) {
priority = !!priority ? priority : 50;
while (typeof Evented.filters[name][priority] == 'function') {
priority++;
}
}
Evented.filters[name][priority] = funct;
if (this.debug) {
console.log('Evented.add_filter', arguments);
}
},
remove_filter: function (name, priority) {
(priority ? delete Evented.filters[name][priority] : delete Evented.filters[name]);
if (this.debug) {
console.log('Evented.remove_filter', arguments);
}
return true;
},
_run_functions: function (is_filter, name, args, context, async) {
Evented.stop = false;
var Type = is_filter ? Evented.filters : Evented.events;
if (Type[name]) {
if (typeof args == 'undefined') {
args = [];
}
if (is_filter == false) {
args.unshift({type: 'Evented'});
}
var keys = Object.keys(Type[name]);
for (var priority = 0; priority < keys.length; priority++) {
var the_key = keys[priority];
if (typeof Type[name][the_key] == 'function') {
try {
if (async) {
setTimeout(function () {
Type[name][the_key].apply(context, args);
}, 0);
} else {
if (Evented.stop == false) {
if (is_filter) {
filtered = Type[name][the_key].apply(context, args);
if (typeof filtered !== 'undefined') {
args[1] = filtered;
}
} else {
Type[name][the_key].apply(context, args);
}
}
}
} catch (e) {
}
}
}
return is_filter ? args[1] : true;
}
}
}