-
Notifications
You must be signed in to change notification settings - Fork 268
/
lazy.browser.js
196 lines (164 loc) · 5.04 KB
/
lazy.browser.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
(function(Lazy) {
/**
* A seqence of DOM nodes.
*
* You can get a `DomSequence` by wrapping a `NodeList` or `HTMLCollection`
* with `Lazy`:
*
* var paragraphs = Lazy(document.querySelectorAll('p'));
*
* @public
* @constructor
* @param {NodeList|HTMLCollection} source The underlying collection of DOM
* nodes.
*/
function DomSequence(source) {
this.source = source;
}
DomSequence.prototype = new Lazy.ArrayLikeSequence();
DomSequence.prototype.get = function(i) {
return this.source[i];
};
DomSequence.prototype.length = function() {
return this.source.length;
};
/**
* Provides a sequence comprising all of this sequence's nodes and their
* descendents (children, grandchildren, etc.).
*
* @public
* @returns {Sequence}
*/
DomSequence.prototype.flatten = function() {
return new FlattenedDomSequence(this.source);
};
function FlattenedDomSequence(source) {
this.source = source;
}
FlattenedDomSequence.prototype = new Lazy.Sequence();
FlattenedDomSequence.prototype.each = function(fn) {
var i = 0,
done = false;
Lazy(this.source).each(function(child) {
if (fn(child, i++) === false) {
return false;
}
Lazy(child.children).flatten().each(function(descendent) {
if (fn(descendent, i++) === false) {
done = true;
return false;
}
});
if (done) {
return false;
}
});
};
/**
* Creates a sequence comprising all of the `Event` objects from the given
* event propagating through the node(s) in the current sequence.
*
* @public
* @param {string} eventName The name of the event to catch.
* @returns {AsyncSequence}
*/
DomSequence.prototype.on = function(eventName) {
return new DomEventSequence(this.source, eventName);
};
function DomEventSequence(elements, eventName) {
this.elements = elements;
this.eventName = eventName;
}
DomEventSequence.prototype = new Lazy.Sequence();
/**
* Handles every event in this sequence.
*
* @param {function(Event):*} fn The function to call on each event in the
* sequence. Return false from the function to stop handling the events.
*/
DomEventSequence.prototype.each = function(fn) {
var elements = this.elements,
eventName = this.eventName;
var listener = function(e) {
if (fn(e) === false) {
for (var i = 0; i < elements.length; ++i) {
elements[i].removeEventListener(eventName, listener);
}
}
};
for (var i = 0; i < elements.length; ++i) {
elements[i].addEventListener(this.eventName, listener);
}
};
/**
* Creates a {@link Sequence} from the specified DOM events triggered on the
* given element. This sequence works asynchronously, so synchronous methods
* such as {@code indexOf}, {@code any}, and {@code toArray} won't work.
*
* @param {Element} element The DOM element to capture events from.
* @param {string} eventName The name of the event type (e.g., 'keypress')
* that will make up this sequence.
* @return {Sequence} The sequence of events.
*/
Lazy.events = Lazy.deprecate(
"Lazy.events is deprecated. Use Lazy(element[s]).on('event') instead",
function(element, eventName) {
return new DomEventSequence(element, eventName);
}
);
/**
* A `StreamingHttpSequence` is a {@link StreamLikeSequence} comprising the
* chunks of data that are streamed in response to an HTTP request.
*
* @public
* @param {string} url The URL of the HTTP request.
* @constructor
*/
function StreamingHttpSequence(url) {
this.url = url;
}
StreamingHttpSequence.prototype = new Lazy.StreamLikeSequence();
StreamingHttpSequence.prototype.each = function each(fn) {
var request = new XMLHttpRequest(),
index = 0,
aborted = false;
request.open("GET", this.url);
var handle = new Lazy.AsyncHandle();
var listener = function listener(data) {
if (!aborted) {
data = request.responseText.substring(index);
try {
if (fn(data) === false) {
request.removeEventListener("progress", listener, false);
request.abort();
aborted = true;
handle._resolve(false);
}
index += data.length;
} catch (e) {
handle._reject(e);
}
}
};
request.addEventListener("progress", listener, false);
request.addEventListener("load", function() {
handle._resolve(true);
});
request.send();
return handle;
};
Lazy.makeHttpRequest = function(url) {
return new StreamingHttpSequence(url);
};
/*
* Add support for `Lazy(NodeList)` and `Lazy(HTMLCollection)`.
*/
Lazy.extensions || (Lazy.extensions = []);
Lazy.extensions.push(function(source) {
if (source instanceof NodeList || source instanceof HTMLCollection) {
return new DomSequence(source);
} else if (source instanceof Element) {
return new DomSequence([source]);
}
});
}(window.Lazy));