OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
6 | 6 |
7 // TODO(jacobr): use _Lists.dart to remove some of the duplicated | 7 // TODO(jacobr): use _Lists.dart to remove some of the duplicated |
8 // functionality. | 8 // functionality. |
9 class _ChildrenElementList implements List<Element> { | 9 class _ChildrenElementList extends ListBase<Element> { |
10 // Raw Element. | 10 // Raw Element. |
11 final Element _element; | 11 final Element _element; |
12 final HtmlCollection _childElements; | 12 final HtmlCollection _childElements; |
13 | 13 |
14 _ChildrenElementList._wrap(Element element) | 14 _ChildrenElementList._wrap(Element element) |
15 : _childElements = element.$dom_children, | 15 : _childElements = element.$dom_children, |
16 _element = element; | 16 _element = element; |
17 | 17 |
18 List<Element> toList({ bool growable: true }) { | 18 List<Element> toList({ bool growable: true }) { |
19 List<Element> output; | 19 List<Element> output; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 77 |
78 Iterable expand(Iterable f(Element element)) { | 78 Iterable expand(Iterable f(Element element)) { |
79 return _childElements.expand(f); | 79 return _childElements.expand(f); |
80 } | 80 } |
81 | 81 |
82 bool get isEmpty { | 82 bool get isEmpty { |
83 return _element.$dom_firstElementChild == null; | 83 return _element.$dom_firstElementChild == null; |
84 } | 84 } |
85 | 85 |
86 Iterable<Element> take(int n) { | 86 Iterable<Element> take(int n) { |
87 return IterableMixinWorkaround.takeList(this, n); | 87 return _childElements.take(n); |
88 } | 88 } |
89 | 89 |
90 Iterable<Element> takeWhile(bool test(Element value)) { | 90 Iterable<Element> takeWhile(bool test(Element value)) { |
91 return IterableMixinWorkaround.takeWhile(this, test); | 91 return _childElements.takeWhile(test); |
92 } | 92 } |
93 | 93 |
94 Iterable<Element> skip(int n) { | 94 Iterable<Element> skip(int n) { |
95 return IterableMixinWorkaround.skipList(this, n); | 95 return _childElements.skipList(n); |
96 } | 96 } |
97 | 97 |
98 Iterable<Element> skipWhile(bool test(Element value)) { | 98 Iterable<Element> skipWhile(bool test(Element value)) { |
99 return IterableMixinWorkaround.skipWhile(this, test); | 99 return _childElements.skipWhile(test); |
100 } | 100 } |
101 | 101 |
102 Element firstWhere(bool test(Element value), {Element orElse()}) { | 102 Element firstWhere(bool test(Element value), {Element orElse()}) { |
103 return IterableMixinWorkaround.firstWhere(this, test, orElse); | 103 return _childElements.firstWhere(test, orElse: orElse); |
104 } | 104 } |
105 | 105 |
106 Element lastWhere(bool test(Element value), {Element orElse()}) { | 106 Element lastWhere(bool test(Element value), {Element orElse()}) { |
107 return IterableMixinWorkaround.lastWhereList(this, test, orElse); | 107 return _childElements.lastWhere(test, orElse: orElse); |
108 } | 108 } |
109 | 109 |
110 Element singleWhere(bool test(Element value)) { | 110 Element singleWhere(bool test(Element value)) { |
111 return IterableMixinWorkaround.singleWhere(this, test); | 111 return _childElements.singleWhere(test); |
112 } | 112 } |
113 | 113 |
114 Element elementAt(int index) { | 114 Element elementAt(int index) { |
115 return this[index]; | 115 return this[index]; |
116 } | 116 } |
117 | 117 |
118 int get length { | 118 int get length { |
119 return _childElements.length; | 119 return _childElements.length; |
120 } | 120 } |
121 | 121 |
(...skipping 21 matching lines...) Expand all Loading... |
143 if (iterable is _ChildNodeListLazy) { | 143 if (iterable is _ChildNodeListLazy) { |
144 iterable = new List.from(iterable); | 144 iterable = new List.from(iterable); |
145 } | 145 } |
146 | 146 |
147 for (Element element in iterable) { | 147 for (Element element in iterable) { |
148 _element.append(element); | 148 _element.append(element); |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 Iterable<Element> get reversed { | 152 Iterable<Element> get reversed { |
153 return IterableMixinWorkaround.reversedList(this); | 153 return _childElements.reversed; |
154 } | 154 } |
155 | 155 |
156 void sort([int compare(Element a, Element b)]) { | 156 void sort([int compare(Element a, Element b)]) { |
157 throw new UnsupportedError('TODO(jacobr): should we impl?'); | 157 throw new UnsupportedError('TODO(jacobr): should we impl?'); |
158 } | 158 } |
159 | 159 |
160 dynamic reduce(dynamic initialValue, | 160 dynamic reduce(dynamic initialValue, |
161 dynamic combine(dynamic previousValue, Element element)) { | 161 dynamic combine(dynamic previousValue, Element element)) { |
162 return IterableMixinWorkaround.reduce(this, initialValue, combine); | 162 return _childElements.reduce(initialValue, combine); |
163 } | 163 } |
164 | 164 |
165 dynamic fold(dynamic initialValue, | 165 dynamic fold(dynamic initialValue, |
166 dynamic combine(dynamic previousValue, Element element)) { | 166 dynamic combine(dynamic previousValue, Element element)) { |
167 return IterableMixinWorkaround.fold(this, initialValue, combine); | 167 return _childElements.fold(initialValue, combine); |
168 } | 168 } |
169 | 169 |
170 void setRange(int start, int rangeLength, List from, [int startFrom = 0]) { | 170 void setRange(int start, int rangeLength, List from, |
| 171 [int startFrom = 0]) { |
171 throw new UnimplementedError(); | 172 throw new UnimplementedError(); |
172 } | 173 } |
173 | 174 |
174 void remove(Object object) { | 175 void remove(Object object) { |
175 if (object is Element) { | 176 if (object is Element) { |
176 Element element = object; | 177 Element element = object; |
177 if (identical(element.parentNode, _element)) { | 178 if (identical(element.parentNode, _element)) { |
178 _element.$dom_removeChild(element); | 179 _element.$dom_removeChild(element); |
179 } | 180 } |
180 } | 181 } |
181 } | 182 } |
182 | 183 |
183 void removeAll(Iterable elements) { | 184 void removeAll(Iterable elements) { |
184 IterableMixinWorkaround.removeAll(this, elements); | 185 _childElements.removeAll(elements); |
185 } | 186 } |
186 | 187 |
187 void retainAll(Iterable elements) { | 188 void retainAll(Iterable elements) { |
188 IterableMixinWorkaround.retainAll(this, elements); | 189 _childElements.retainAll(elements); |
189 } | 190 } |
190 | 191 |
191 void removeWhere(bool test(Element element)) { | 192 void removeWhere(bool test(Element element)) { |
192 IterableMixinWorkaround.removeWhere(this, test); | 193 _childElements.removeWhere(test); |
193 } | 194 } |
194 | 195 |
195 void retainWhere(bool test(Element element)) { | 196 void retainWhere(bool test(Element element)) { |
196 IterableMixinWorkaround.retainWhere(this, test); | 197 _childElements.retainWhere(test); |
197 } | 198 } |
198 | 199 |
199 void removeRange(int start, int rangeLength) { | 200 void removeRange(int start, int rangeLength) { |
200 throw new UnimplementedError(); | 201 throw new UnimplementedError(); |
201 } | 202 } |
202 | 203 |
203 void insertRange(int start, int rangeLength, [initialValue = null]) { | 204 void insertRange(int start, int rangeLength, [initialValue = null]) { |
204 throw new UnimplementedError(); | 205 throw new UnimplementedError(); |
205 } | 206 } |
206 | 207 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 if (result == null) throw new StateError("No elements"); | 266 if (result == null) throw new StateError("No elements"); |
266 return result; | 267 return result; |
267 } | 268 } |
268 | 269 |
269 Element get single { | 270 Element get single { |
270 if (length > 1) throw new StateError("More than one element"); | 271 if (length > 1) throw new StateError("More than one element"); |
271 return first; | 272 return first; |
272 } | 273 } |
273 | 274 |
274 Element min([int compare(Element a, Element b)]) { | 275 Element min([int compare(Element a, Element b)]) { |
275 return IterableMixinWorkaround.min(this, compare); | 276 return _childElements.min(compare); |
276 } | 277 } |
277 | 278 |
278 Element max([int compare(Element a, Element b)]) { | 279 Element max([int compare(Element a, Element b)]) { |
279 return IterableMixinWorkaround.max(this, compare); | 280 return _childElements.max(compare); |
280 } | 281 } |
281 | 282 |
282 Map<int, Element> asMap() { | 283 Map<int, Element> asMap() { |
283 return IterableMixinWorkaround.asMapList(this); | 284 return _childElements.asMapList(this); |
284 } | 285 } |
285 | 286 |
286 String toString() { | 287 String toString() { |
287 StringBuffer buffer = new StringBuffer('['); | 288 StringBuffer buffer = new StringBuffer('['); |
288 buffer.writeAll(this, ', '); | 289 buffer.writeAll(this, ', '); |
289 buffer.write(']'); | 290 buffer.write(']'); |
290 return buffer.toString(); | 291 return buffer.toString(); |
291 } | 292 } |
292 } | 293 } |
293 | 294 |
294 // TODO(jacobr): this is an inefficient implementation but it is hard to see | 295 // TODO(jacobr): this is an inefficient implementation but it is hard to see |
295 // a better option given that we cannot quite force NodeList to be an | 296 // a better option given that we cannot quite force NodeList to be an |
296 // ElementList as there are valid cases where a NodeList JavaScript object | 297 // ElementList as there are valid cases where a NodeList JavaScript object |
297 // contains Node objects that are not Elements. | 298 // contains Node objects that are not Elements. |
298 class _FrozenElementList implements List { | 299 class _FrozenElementList extends ListBase<Element> { |
299 final List<Node> _nodeList; | 300 final List<Node> _nodeList; |
300 | 301 |
301 _FrozenElementList._wrap(this._nodeList); | 302 _FrozenElementList._wrap(this._nodeList); |
302 | 303 |
303 bool contains(Element element) { | |
304 for (Element el in this) { | |
305 if (el == element) return true; | |
306 } | |
307 return false; | |
308 } | |
309 | |
310 void forEach(void f(Element element)) { | |
311 for (Element el in this) { | |
312 f(el); | |
313 } | |
314 } | |
315 | |
316 String join([String separator]) { | |
317 return IterableMixinWorkaround.joinList(this, separator); | |
318 } | |
319 | |
320 Iterable map(f(Element element)) { | |
321 return IterableMixinWorkaround.mapList(this, f); | |
322 } | |
323 | |
324 Iterable<Element> where(bool f(Element element)) { | |
325 return IterableMixinWorkaround.where(this, f); | |
326 } | |
327 | |
328 Iterable expand(Iterable f(Element element)) { | |
329 return IterableMixinWorkaround.expand(this, f); | |
330 } | |
331 | |
332 bool every(bool f(Element element)) { | |
333 for(Element element in this) { | |
334 if (!f(element)) { | |
335 return false; | |
336 } | |
337 }; | |
338 return true; | |
339 } | |
340 | |
341 bool any(bool f(Element element)) { | |
342 for(Element element in this) { | |
343 if (f(element)) { | |
344 return true; | |
345 } | |
346 }; | |
347 return false; | |
348 } | |
349 | |
350 List<Element> toList({ bool growable: true }) => | |
351 new List<Element>.from(this, growable: growable); | |
352 Set<Element> toSet() => new Set<Element>.from(this); | |
353 | |
354 Iterable<Element> take(int n) { | |
355 return IterableMixinWorkaround.takeList(this, n); | |
356 } | |
357 | |
358 Iterable<Element> takeWhile(bool test(Element value)) { | |
359 return IterableMixinWorkaround.takeWhile(this, test); | |
360 } | |
361 | |
362 Iterable<Element> skip(int n) { | |
363 return IterableMixinWorkaround.skipList(this, n); | |
364 } | |
365 | |
366 Iterable<Element> skipWhile(bool test(Element value)) { | |
367 return IterableMixinWorkaround.skipWhile(this, test); | |
368 } | |
369 | |
370 Element firstWhere(bool test(Element value), {Element orElse()}) { | |
371 return IterableMixinWorkaround.firstWhere(this, test, orElse); | |
372 } | |
373 | |
374 Element lastWhere(bool test(Element value), {Element orElse()}) { | |
375 return IterableMixinWorkaround.lastWhereList(this, test, orElse); | |
376 } | |
377 | |
378 Element singleWhere(bool test(Element value)) { | |
379 return IterableMixinWorkaround.singleWhere(this, test); | |
380 } | |
381 | |
382 Element elementAt(int index) { | |
383 return this[index]; | |
384 } | |
385 | |
386 bool get isEmpty => _nodeList.isEmpty; | |
387 | |
388 int get length => _nodeList.length; | 304 int get length => _nodeList.length; |
389 | 305 |
390 Element operator [](int index) => _nodeList[index]; | 306 Element operator [](int index) => _nodeList[index]; |
391 | 307 |
392 void operator []=(int index, Element value) { | 308 void operator []=(int index, Element value) { |
393 throw new UnsupportedError(''); | 309 throw new UnsupportedError(''); |
394 } | 310 } |
395 | 311 |
396 void set length(int newLength) { | 312 void set length(int newLength) { |
397 _nodeList.length = newLength; | 313 _nodeList.length = newLength; |
398 } | 314 } |
399 | 315 |
400 void add(Element value) { | 316 void add(Element value) { |
401 throw new UnsupportedError(''); | 317 throw new UnsupportedError(''); |
402 } | 318 } |
403 | 319 |
404 Iterator<Element> get iterator => new _FrozenElementListIterator(this); | |
405 | |
406 void addAll(Iterable<Element> iterable) { | 320 void addAll(Iterable<Element> iterable) { |
407 throw new UnsupportedError(''); | 321 throw new UnsupportedError(''); |
408 } | 322 } |
409 | 323 |
410 Iterable<Element> get reversed { | |
411 return IterableMixinWorkaround.reversedList(this); | |
412 } | |
413 | |
414 void sort([int compare(Element a, Element b)]) { | 324 void sort([int compare(Element a, Element b)]) { |
415 throw new UnsupportedError(''); | 325 throw new UnsupportedError(''); |
416 } | 326 } |
417 | 327 |
418 dynamic reduce(dynamic initialValue, | 328 void setRange(int start, int rangeLength, List from, |
419 dynamic combine(dynamic previousValue, Element element)) { | 329 [int startFrom = 0]) { |
420 return IterableMixinWorkaround.reduce(this, initialValue, combine); | |
421 } | |
422 | |
423 dynamic fold(dynamic initialValue, | |
424 dynamic combine(dynamic previousValue, Element element)) { | |
425 return IterableMixinWorkaround.fold(this, initialValue, combine); | |
426 } | |
427 | |
428 void setRange(int start, int rangeLength, List from, [int startFrom = 0]) { | |
429 throw new UnsupportedError(''); | 330 throw new UnsupportedError(''); |
430 } | 331 } |
431 | 332 |
432 void removeRange(int start, int rangeLength) { | 333 void removeRange(int start, int rangeLength) { |
433 throw new UnsupportedError(''); | 334 throw new UnsupportedError(''); |
434 } | 335 } |
435 | 336 |
436 void insertRange(int start, int rangeLength, [initialValue = null]) { | 337 void insertRange(int start, int rangeLength, [initialValue = null]) { |
437 throw new UnsupportedError(''); | 338 throw new UnsupportedError(''); |
438 } | 339 } |
439 | 340 |
440 List<Element> sublist(int start, [int end]) { | 341 List<Element> sublist(int start, [int end]) { |
441 return new _FrozenElementList._wrap(_nodeList.sublist(start, end)); | 342 return new _FrozenElementList._wrap(_nodeList.sublist(start, end)); |
442 } | 343 } |
443 | 344 |
444 List<Element> getRange(int start, int rangeLength) => | |
445 sublist(start, start + rangeLength); | |
446 | |
447 int indexOf(Element element, [int start = 0]) => | |
448 _nodeList.indexOf(element, start); | |
449 | |
450 int lastIndexOf(Element element, [int start = null]) => | |
451 _nodeList.lastIndexOf(element, start); | |
452 | |
453 void clear() { | 345 void clear() { |
454 throw new UnsupportedError(''); | 346 throw new UnsupportedError(''); |
455 } | 347 } |
456 | 348 |
457 Element removeAt(int index) { | 349 Element removeAt(int index) { |
458 throw new UnsupportedError(''); | 350 throw new UnsupportedError(''); |
459 } | 351 } |
460 | 352 |
461 Element removeLast() { | 353 Element removeLast() { |
462 throw new UnsupportedError(''); | 354 throw new UnsupportedError(''); |
(...skipping 18 matching lines...) Expand all Loading... |
481 void retainWhere(bool test(Element element)) { | 373 void retainWhere(bool test(Element element)) { |
482 throw new UnsupportedError(''); | 374 throw new UnsupportedError(''); |
483 } | 375 } |
484 | 376 |
485 Element get first => _nodeList.first; | 377 Element get first => _nodeList.first; |
486 | 378 |
487 Element get last => _nodeList.last; | 379 Element get last => _nodeList.last; |
488 | 380 |
489 Element get single => _nodeList.single; | 381 Element get single => _nodeList.single; |
490 | 382 |
491 Element min([int compare(Element a, Element b)]) { | |
492 return IterableMixinWorkaround.min(this, compare); | |
493 } | |
494 | |
495 Element max([int compare(Element a, Element b)]) { | |
496 return IterableMixinWorkaround.max(this, compare); | |
497 } | |
498 | |
499 Map<int, Element> asMap() { | |
500 return IterableMixinWorkaround.asMapList(this); | |
501 } | |
502 | |
503 String toString() { | 383 String toString() { |
504 StringBuffer buffer = new StringBuffer('['); | 384 StringBuffer buffer = new StringBuffer('['); |
505 buffer.writeAll(this, ', '); | 385 buffer.writeAll(this, ', '); |
506 buffer.write(']'); | 386 buffer.write(']'); |
507 return buffer.toString(); | 387 return buffer.toString(); |
508 } | 388 } |
509 } | 389 } |
510 | 390 |
511 class _FrozenElementListIterator implements Iterator<Element> { | |
512 final _FrozenElementList _list; | |
513 int _index = -1; | |
514 Element _current; | |
515 | |
516 _FrozenElementListIterator(this._list); | |
517 | |
518 /** | |
519 * Moves to the next element. Returns true if the iterator is positioned | |
520 * at an element. Returns false if it is positioned after the last element. | |
521 */ | |
522 bool moveNext() { | |
523 int nextIndex = _index + 1; | |
524 if (nextIndex < _list.length) { | |
525 _current = _list[nextIndex]; | |
526 _index = nextIndex; | |
527 return true; | |
528 } | |
529 _index = _list.length; | |
530 _current = null; | |
531 return false; | |
532 } | |
533 | |
534 /** | |
535 * Returns the element the [Iterator] is positioned at. | |
536 * | |
537 * Return [:null:] if the iterator is positioned before the first, or | |
538 * after the last element. | |
539 */ | |
540 Element get current => _current; | |
541 } | |
542 | |
543 class _ElementCssClassSet extends CssClassSet { | 391 class _ElementCssClassSet extends CssClassSet { |
544 | 392 |
545 final Element _element; | 393 final Element _element; |
546 | 394 |
547 _ElementCssClassSet(this._element); | 395 _ElementCssClassSet(this._element); |
548 | 396 |
549 Set<String> readClasses() { | 397 Set<String> readClasses() { |
550 var s = new LinkedHashSet<String>(); | 398 var s = new LinkedHashSet<String>(); |
551 var classname = _element.$dom_className; | 399 var classname = _element.$dom_className; |
552 | 400 |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 const ScrollAlignment._internal(this._value); | 993 const ScrollAlignment._internal(this._value); |
1146 toString() => 'ScrollAlignment.$_value'; | 994 toString() => 'ScrollAlignment.$_value'; |
1147 | 995 |
1148 /// Attempt to align the element to the top of the scrollable area. | 996 /// Attempt to align the element to the top of the scrollable area. |
1149 static const TOP = const ScrollAlignment._internal('TOP'); | 997 static const TOP = const ScrollAlignment._internal('TOP'); |
1150 /// Attempt to center the element in the scrollable area. | 998 /// Attempt to center the element in the scrollable area. |
1151 static const CENTER = const ScrollAlignment._internal('CENTER'); | 999 static const CENTER = const ScrollAlignment._internal('CENTER'); |
1152 /// Attempt to align the element to the bottom of the scrollable area. | 1000 /// Attempt to align the element to the bottom of the scrollable area. |
1153 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); | 1001 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); |
1154 } | 1002 } |
OLD | NEW |