This repository was archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathfromevent.js
105 lines (93 loc) · 3.62 KB
/
fromevent.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
function isNodeList(el) {
if (root.StaticNodeList) {
// IE8 Specific
// instanceof is slower than Object#toString, but Object#toString will not work as intended in IE8
return el instanceof root.StaticNodeList || el instanceof root.NodeList;
} else {
return Object.prototype.toString.call(el) === '[object NodeList]';
}
}
function ListenDisposable(e, n, fn) {
this._e = e;
this._n = n;
this._fn = fn;
this._e.addEventListener(this._n, this._fn, false);
this.isDisposed = false;
}
ListenDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this._e.removeEventListener(this._n, this._fn, false);
this.isDisposed = true;
}
};
function createEventListener (el, eventName, handler) {
var disposables = new CompositeDisposable();
// Asume NodeList or HTMLCollection
var elemToString = Object.prototype.toString.call(el);
if (isNodeList(el) || elemToString === '[object HTMLCollection]') {
for (var i = 0, len = el.length; i < len; i++) {
disposables.add(createEventListener(el.item(i), eventName, handler));
}
} else if (el) {
disposables.add(new ListenDisposable(el, eventName, handler));
}
return disposables;
}
/**
* Configuration option to determine whether to use native events only
*/
Rx.config.useNativeEvents = false;
var EventObservable = (function(__super__) {
inherits(EventObservable, __super__);
function EventObservable(el, name, fn) {
this._el = el;
this._n = name;
this._fn = fn;
__super__.call(this);
}
function createHandler(o, fn) {
return function handler () {
var results = arguments[0];
if (isFunction(fn)) {
results = tryCatch(fn).apply(null, arguments);
if (results === errorObj) { return o.onError(results.e); }
}
o.onNext(results);
};
}
EventObservable.prototype.subscribeCore = function (o) {
return createEventListener(
this._el,
this._n,
createHandler(o, this._fn));
};
return EventObservable;
}(ObservableBase));
/**
* Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
* @param {Object} element The DOMElement or NodeList to attach a listener.
* @param {String} eventName The event name to attach the observable sequence.
* @param {Object} eventListenerOptions An object describing EventListenerOptions
* @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
* @returns {Observable} An observable sequence of events from the specified element and the specified event.
*/
Observable.fromEvent = function (element, eventName, selector, eventListenerOptions) {
// Node.js specific
if (element.addListener) {
return fromEventPattern(
function (h) { element.addListener(eventName, h, eventListenerOptions); },
function (h) { element.removeListener(eventName, h, eventListenerOptions); },
selector);
}
// Use only if non-native events are allowed
if (!Rx.config.useNativeEvents) {
// Handles jq, Angular.js, Zepto, Marionette, Ember.js
if (typeof element.on === 'function' && typeof element.off === 'function') {
return fromEventPattern(
function (h) { element.on(eventName, h, eventListenerOptions); },
function (h) { element.off(eventName, h, eventListenerOptions); },
selector);
}
}
return new EventObservable(element, eventName, selector).publish().refCount();
};