-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
365 lines (282 loc) · 7.46 KB
/
index.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* @module enot
*/
var slice = require('sliced');
var emitter = require('emmy').prototype;
var eachCSV = require('each-csv');
var isFn = require('mutype/is-fn');
var isObject = require('mutype/is-object');
var isString = require('mutype/is-string');
var isArrayLike = require('mutype/is-array-like');
var unprefix = require('mustring/unprefix');
var q = require('queried');
var paren = require('parenthesis');
var _on = require('emmy/on'), _off= require('emmy/off'), _emit = require('emmy/emit'), _once = require('emmy/once');
//TODO: query multiple targets in on/off/emit - now callback is got improperly
/**
* @constructor
*
* Mixins any object passed.
* Implements EventEmitter interface.
* Static methods below are useful as wrappers.
*/
function Enot (target) {
if (!target) return target;
//mixin any object passed
for (var meth in proto) {
target[meth] = proto[meth];
}
return target;
}
/**
* Prototype should be instanceof Emitter
* also fill basic event methods like `listeners`
*/
var proto = Enot.prototype = Object.create(emitter);
//prototype methods
proto['on'] = function (a,b,c) {
on(this, a,b,c);
return this;
};
proto['once'] = function (a,b,c) {
once(this, a,b,c);
return this;
};
proto['off'] = function (a,b) {
//call super off (some fns could’ve been bound outside)
_off(this, a,b);
off(this, a,b);
return this;
};
proto['emit'] = function () {
emit.apply(this, [this].concat(slice(arguments)));
return this;
};
/**
* Storage of modified fns for orig fns, per-event
*
* e.g.
* fn: {evt: [cb1, cb2, ...]}
*/
var cbCache = new WeakMap;
/**
* Static wrapper API
*/
var on = Enot['on'] = function on () {
invoke(function (target, ref, fn) {
if (!fn) return;
var parts = getParts(ref);
var evt = parts[1].split(':')[0];
//get fn wrapper with pseudos applied
var modFn = getCallback(target, parts[1], fn);
//save modified fn to the callback cache to unbind
var cbSet, cbList;
if (!(cbSet = cbCache.get(fn))) cbCache.set(fn, cbSet = {});
cbList = cbSet[evt] || (cbSet[evt] = []);
cbList.push(modFn);
_on(target, evt, modFn);
}, arguments);
return Enot;
};
var off = Enot['off'] = function off () {
invoke(function (target, ref, fn) {
var parts = getParts(ref);
var evt = parts[1].split(':')[0];
//clean cb reference
if (fn) {
var cbSet = cbCache.get(fn);
if (!cbSet) return;
var cbList = cbSet[evt];
if (!cbList) return;
for (var i = cbList.length; i--;) {
_off(target, evt, cbList[i]);
//FIXME: remove reference to avoid leaks
// cbList.splice(i,1);
}
} else {
_off(target, evt, fn);
}
}, arguments);
return Enot;
};
var emit = Enot['emit'] = function emit () {
invoke(function (target, ref) {
if (isString(ref)) {
ref = getParts(ref)[1].split(':')[0];
}
_emit.apply(target, [target, ref].concat(slice(arguments, 2)));
}, arguments);
return Enot;
};
var once = Enot['once'] = function once () {
_once.apply(this, arguments);
return Enot;
}
/** Redirect, parse event notations and call target method */
function invoke (fn, args) {
var target = args[0], refs = args[1];
//if no target specified - use mediator
//refs can be both object/string, so first check an fn
// emit('document click'), emit(document, 'click')
// on(document, {sett}), on('document sett')
if (isFn(refs) || isString(target)) {
target = null;
refs = args[0];
args = slice(args, 0);
}
else {
args = slice(args, 1);
}
//ignore absent evtRefs/fn
if (!refs) return;
//shorten args, exclude refs
args = slice(args, 1);
//batch refs
if (isObject(refs)) {
for (var evtRefs in refs) {
eachCSV(evtRefs, function (evtRef) {
invoke(fn, [target, evtRef].concat(refs[evtRef]));
});
}
return;
}
//string refs
if (isString(refs)) {
eachCSV(refs, function (evtRef) {
//get targets to apply
var parts = getParts(evtRef);
var targets = getTargets(target, parts[0]);
//iterate over each target
for (var i = 0, l = targets.length; i < l; i++) {
fn.apply(targets[i], [targets[i], evtRef].concat(args));
}
});
return;
}
//non-string refs
fn.apply(target, [target, refs].concat(args));
}
/**
* Detect event/target parts in event reference.
* Event reference (basic event notation) looks:
* '[target] event'
* Event goes last.
*
* Exact order, in theory, could be detected
* because list of exact tags is known
* as well as dashed-notation for custom elements
* so the order could be guessed
* but it complicates parsing
* and it is illogical (Subject → Verb scheme is natural).
*
* @example
* 'a b' → ['a', 'b']
* 'document click': ['document', 'click']
*
*
* @param {string} str Event notation
*
* @return {Array} Result of parsing: ['<target part>', '<event part>']
*/
function getParts(str) {
var result = ['',''];
//get event name - the last token
var eventString = str.match(/[\w\.\:\$\-]+(?:\:[\w\.\:\-\$]+(?:\(.+\))?)*$/)[0];
//remainder is a target reference - parse target
result[0] = str.slice(0, -eventString.length).trim();
//parse event
result[1] = unprefix(eventString, 'on');
return result;
}
/**
* Get targets by a query string.
* Ensures result is array.
*
* @param {Element|Object} target A target(s) to relate
* @param {string} str Target reference
*
* @return {Array} Resulting targets found
*/
function getTargets (target, str) {
// console.log('parseTarget `' + str + '`', target)
//no target means global target (mediator)
if (!target) target = [document];
//no string means self evt
if (!str) {
return isArrayLike(target) ? target : [target];
}
//some reserved words
if(str === 'window') return [window];
else if(str === 'document') return [document];
//query relative selector
else {
return q.all(str, target);
}
}
/**
* Get callback for the event string
* basically wrap passed event
*
* @example
* 'click:once:on(.element)' - apply `once` wrapper, `on` wrapper
*
* @param {function} fn An initial function to wrap
* @param {string} str A string containing event part
*
* @return {function} Constructed callback for the event string
*/
function getCallback (target, evtStr, fn) {
if (!fn) return;
var targetFn = fn;
//escape all parentheses
var parens = paren.parse(evtStr);
//get pseudos list & evt
var pseudoList = parens[0].split(':');
var evt = pseudoList.shift();
//wrap each modifier
pseudoList
//:once should go last
.sort(function (a, b) {
if (a === 'once' || a === 'one') return -1;
if (b === 'once' || b === 'one') return 1;
return 0;
})
.forEach(function (pseudo) {
//get pseudo name/ref parts from parenthesis token
var parts = pseudo.split('\\');
var pseudoName = parts[0];
var pseudoParams = paren.stringify(parens[parts[1]], parens);
//for :once method pass special `off` as param
if (!pseudoParams) pseudoParams = function (target, evt, cb) {
off(target, evt, cb);
off(target, evt, fn);
};
if (pseudos[pseudoName]) {
targetFn = pseudos[pseudoName](target, evt, targetFn, pseudoParams);
}
});
return targetFn;
}
/**
* Dics of pseudos.
* Includes all xtags ones: delegate, pass.
*
* @module enot/pseudos
*/
var pseudos = {};
pseudos['on'] =
pseudos['delegate'] =
require('emmy/delegate').wrap;
pseudos['not'] =
require('emmy/not').wrap;
pseudos['pass'] =
pseudos['keypass'] =
require('emmy/keypass').wrap;
pseudos['one'] =
pseudos['once'] =
require('emmy/once').wrap;
pseudos['throttle'] =
require('emmy/throttle').wrap;
pseudos['later'] =
require('emmy/later').wrap;
module.exports = Enot;