This repository was archived by the owner on Dec 21, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathevents.js
More file actions
99 lines (86 loc) · 2.35 KB
/
events.js
File metadata and controls
99 lines (86 loc) · 2.35 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
import eventExtend from './events-extend';
import {EventStream} from 'vega-dataflow';
import {extend, isArray, toSet} from 'vega-util';
var VIEW = 'view',
TIMER = 'timer',
WINDOW = 'window',
NO_TRAP = {trap: false};
/**
* Initialize event handling configuration.
* @param {object} config - The configuration settings.
* @return {object}
*/
export function initializeEventConfig(config) {
config = extend({}, config);
var def = config.defaults;
if (def) {
if (isArray(def.prevent)) {
def.prevent = toSet(def.prevent);
}
if (isArray(def.allow)) {
def.allow = toSet(def.allow);
}
}
return config;
}
function prevent(view, type) {
var def = view._eventConfig.defaults,
prevent = def && def.prevent,
allow = def && def.allow;
return prevent === false || allow === true ? false
: prevent === true || allow === false ? true
: prevent ? prevent[type]
: allow ? !allow[type]
: view.preventDefault();
}
/**
* Create a new event stream from an event source.
* @param {object} source - The event source to monitor.
* @param {string} type - The event type.
* @param {function(object): boolean} [filter] - Event filter function.
* @return {EventStream}
*/
export function events(source, type, filter) {
var view = this,
s = new EventStream(filter),
send = function(e, item) {
if (source === VIEW && prevent(view, type)) {
e.preventDefault();
}
try {
s.receive(eventExtend(view, e, item));
} catch (error) {
view.error(error);
} finally {
view.run();
}
},
sources;
if (source === TIMER) {
view.timer(send, type);
}
else if (source === VIEW) {
// send traps errors, so use {trap: false} option
view.addEventListener(type, send, NO_TRAP);
}
else {
if (source === WINDOW) {
if (typeof window !== 'undefined') sources = [window];
} else if (typeof document !== 'undefined') {
sources = document.querySelectorAll(source);
}
if (!sources) {
view.warn('Can not resolve event source: ' + source);
} else {
for (var i=0, n=sources.length; i<n; ++i) {
sources[i].addEventListener(type, send);
}
view._eventListeners.push({
type: type,
sources: sources,
handler: send
});
}
}
return s;
}