-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
272 lines (235 loc) · 6.7 KB
/
events.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
var qt = require('node-qt');
var constants = require('./constants');
var slice = require('./utility').slice;
var reverse = require('./utility').reverse;
module.exports = Emitter;
// ###########################################
// ### Resolve event objects to end values ###
// ###########################################
var enums = {
button: reverse(qt.MouseButton),
color: reverse(qt.GlobalColor),
key: reverse(qt.Key)
};
function mods(value, lookup, obj){
obj = obj || {};
for (var name in lookup) {
if ((value & lookup[name]) > 0) obj[name] = true;
}
return obj;
}
// ########################################
// ### Map QT events to JS style events ###
// ########################################
function translate(proto){
function Ctor(name){
this.name = name+'Event';
translate.events.push(name);
}
Ctor.prototype = proto;
return Ctor;
}
translate.events = [];
function needsAdapter(emitter){
for (var i=0; i < translate.events.length; i++) {
if (translate.events[i] in emitter) return true;
}
return false;
}
var TEvent = translate({});
var TMouseEvent = translate({
emit: function(emitter, event){
mods(event.modifiers, constants.modifiers, event);
delete event.modifiers;
}
})
var TKeyEvent = translate({
emit: function(emitter, event){
event.key = enums.key[event.key()];
mods(event.modifiers, constants.modifiers, event);
delete event.modifiers;
}
});
var TTrackEvent = translate({
first: function(emitter){
emitter.mouseTracking = true;
},
last: function(emitter){
emitter.mouseTracking = false;
}
});
var map = {
mousemove: new TTrackEvent('mouseMove'),
mousedown: new TMouseEvent('mousePress'),
mouseup: new TMouseEvent('mouseRelease'),
mouseenter: new TEvent('enter'),
mouseleave: new TEvent('leave'),
keydown: new TKeyEvent('keyPress'),
keyup: new TKeyEvent('keyRelease'),
resize: new TEvent('resize'),
move: new TEvent('move'),
paint: new TEvent('paint'),
close: new TEvent('close'),
};
// intercept listener add and remove so we can additionally hit the QT handler
function adaptEmitter(emitter){
var origOn = emitter.on;
var origOff = emitter.off;
var origOffAll = emitter.offAll;
var originals = {};
for (var k in map) {
if (map[k].name in emitter) {
originals[k] = emitter[map[k].name];
delete emitter[map[k].name];
}
}
var refs = new WeakMap;
emitter.on = function on(event, callback){
var self = this;
var events = event === '*' ? Object.keys(originals) : event.split(' ');
var refcount = refs.has(this) ? refs.get(this) : refs.set(this, {});
events.forEach(function(event){
if (!(event in map)) return;
if (refcount[event]) return refcount[event]++;
refcount[event] = 1;
if (map[event].first) map[event].first(self);
originals[event].call(self, function(e){
if (map[event].emit) map[event].emit(self, e);
if (e) {
e.type = event;
Object.defineProperty(e, 'target', { value: self });
} else {
e = new Event(event, self);
}
self.emit(e);
});
});
origOn.apply(this, arguments);
}
emitter.off = function off(event){
var self = this;
var events = event === '*' ? Object.keys(originals) : event.split(' ');
var refcount = refs.has(this) ? refs.get(this) : refs.set(this, {});
events.forEach(function(event){
if (event in originals && refcount[event] && !--refcount[event]) {
if (map[event].last) {
map[event].last(self);
}
originals[event].call(self);
}
});
return origOff.apply(this, arguments);
}
emitter.offAll = function offAll(){
if (refs.has(this)) {
var self = this;
var refcount = refs.get(this);
refs.delete(this);
Object.keys(refcount).forEach(function(event){
if (refcount[event]) {
originals[event].call(self);
}
});
}
return origOffAll.apply(this, arguments);
}
}
// ### Generic Event ###
function Event(type, target){
this.type = type;
this.target = target;
}
function ErrorEvent(error, event, target){
Event.call(this, 'error', target)
this.error = error;
this.event = event;
}
// ##############################################
// ### Prototype and initializer for emitters ###
// ##############################################
function Emitter(){
emitters.set(this, {});
receivers.set(this, this);
}
// forward on event subscriptions from one object to another
Emitter.forward = function forward(from, to){
if (!emitters.has(to)) {
emitters.set(to, {});
receivers.set(to, to);
}
emitters.set(from, emitters.get(to));
from.on = to.on.bind(to);
from.off = to.off.bind(to);
}
Emitter.castEmitter = function castEmitter(obj){
if (Object.getPrototypeOf(obj) === Object.prototype) {
obj.__proto__ = Emitter.prototype;
} else {
var proto = obj;
while (proto = Object.getPrototypeOf(proto)) {
if (proto === null || proto === Object.prototype) break;
obj = proto;
}
obj.__proto__ = Emitter.prototype;
}
if (needsAdapter(obj)) {
adaptEmitter(obj);
}
}
var emitters = new WeakMap;
var receivers = new WeakMap;
// standardish emitter that is able to be ignorant of QT needs
Emitter.prototype = {
constructor: Emitter,
on: function on(events, listener){
var listeners = emitters.get(this);
events.split(' ').forEach(function(event){
if (event in listeners){
listeners[event].push(listener);
} else {
listeners[event] = [listener];
}
});
},
off: function off(events, listener){
var listeners = emitters.get(this);
events.split(' ').forEach(function(event){
if (listeners[event]) {
listeners[event].splice(listeners[event].indexOf(listener), 1);
}
});
},
offAll: function offAll(event){
delete emitters.get(this)[event];
},
once: function once(event, listener){
var self = this;
this.on(event, function(){
self.off(event, listener);
return listener.apply(receivers.get(self), arguments);
});
},
emit: function emit(type){
var event, events = emitters.get(this);
if (typeof type !== 'string') {
event = type;
type = event.type;
}
if (events['*']) {
var listeners = events[type] ? events['*'].concat(events[type]) : events['*'];
} else {
var listeners = events[type];
}
if (listeners) {
event = event || new Event(type, receivers.get(this));
var args = [event].concat(slice(arguments, 1));
for (var i=0; i < listeners.length; i++) {
try {
listeners[i].apply(this, args);
} catch (e) {
this.emit(new ErrorEvent(e, event, listeners[i]));
}
}
}
},
};