Chromium Code Reviews| 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 class _ChildrenElementList extends ListBase<Element> { | 7 class _ChildrenElementList extends ListBase<Element> { |
| 8 // Raw Element. | 8 // Raw Element. |
| 9 final Element _element; | 9 final Element _element; |
| 10 final HtmlCollection _childElements; | 10 final HtmlCollection _childElements; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 if (result == null) throw new StateError("No elements"); | 130 if (result == null) throw new StateError("No elements"); |
| 131 return result; | 131 return result; |
| 132 } | 132 } |
| 133 | 133 |
| 134 Element get single { | 134 Element get single { |
| 135 if (length > 1) throw new StateError("More than one element"); | 135 if (length > 1) throw new StateError("More than one element"); |
| 136 return first; | 136 return first; |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 /** | |
| 141 * An immutable list containing HTML elements. This list contains some | |
| 142 * additional methods for ease of CSS manipulation on a group of elements. | |
| 143 */ | |
| 144 abstract class ElementList extends ListBase { | |
|
blois
2013/05/03 22:16:06
I'd make it:
ElementList<T extends Element> extend
Emily Fortuna
2013/05/03 23:00:09
Done.
| |
| 145 /** | |
| 146 * The union of all CSS classes applied to the elements in this list. | |
| 147 * | |
| 148 * This set makes it easy to add, remove or toggle (add if not present, remove | |
| 149 * if present) the classes applied to a collection of elements. | |
| 150 * | |
| 151 * htmlList.classes.add('selected'); | |
| 152 * htmlList.classes.toggle('isOnline'); | |
| 153 * htmlList.classes.remove('selected'); | |
| 154 */ | |
| 155 CssClassSet get classes; | |
| 156 | |
| 157 /** Replace the classes with `value` for every element in this list. */ | |
| 158 set classes(Iterable<String> value); | |
| 159 } | |
| 160 | |
| 140 // TODO(jacobr): this is an inefficient implementation but it is hard to see | 161 // TODO(jacobr): this is an inefficient implementation but it is hard to see |
| 141 // a better option given that we cannot quite force NodeList to be an | 162 // a better option given that we cannot quite force NodeList to be an |
| 142 // ElementList as there are valid cases where a NodeList JavaScript object | 163 // ElementList as there are valid cases where a NodeList JavaScript object |
| 143 // contains Node objects that are not Elements. | 164 // contains Node objects that are not Elements. |
| 144 class _FrozenElementList<T extends Element> extends ListBase<T> { | 165 class _FrozenElementList<T extends Element> extends ListBase<T> implements Eleme ntList { |
| 145 final List<Node> _nodeList; | 166 final List<Node> _nodeList; |
| 146 | 167 |
| 147 _FrozenElementList._wrap(this._nodeList); | 168 _FrozenElementList._wrap(this._nodeList); |
| 148 | 169 |
| 149 int get length => _nodeList.length; | 170 int get length => _nodeList.length; |
| 150 | 171 |
| 151 Element operator [](int index) => _nodeList[index]; | 172 Element operator [](int index) => _nodeList[index]; |
| 152 | 173 |
| 153 void operator []=(int index, Element value) { | 174 void operator []=(int index, Element value) { |
| 154 throw new UnsupportedError('Cannot modify list'); | 175 throw new UnsupportedError('Cannot modify list'); |
| 155 } | 176 } |
| 156 | 177 |
| 157 void set length(int newLength) { | 178 void set length(int newLength) { |
| 158 throw new UnsupportedError('Cannot modify list'); | 179 throw new UnsupportedError('Cannot modify list'); |
| 159 } | 180 } |
| 160 | 181 |
| 161 void sort([Comparator<Element> compare]) { | 182 void sort([Comparator<Element> compare]) { |
| 162 throw new UnsupportedError('Cannot sort list'); | 183 throw new UnsupportedError('Cannot sort list'); |
| 163 } | 184 } |
| 164 | 185 |
| 165 Element get first => _nodeList.first; | 186 Element get first => _nodeList.first; |
| 166 | 187 |
| 167 Element get last => _nodeList.last; | 188 Element get last => _nodeList.last; |
| 168 | 189 |
| 169 Element get single => _nodeList.single; | 190 Element get single => _nodeList.single; |
| 170 } | |
| 171 | 191 |
| 172 class _ElementCssClassSet extends CssClassSet { | 192 CssClassSet get classes => new _MultiElementCssClassSet( |
| 193 _nodeList.where((e) => e is Element)); | |
| 173 | 194 |
| 174 final Element _element; | 195 void set classes(Iterable<String> value) { |
| 175 | 196 _nodeList.where((e) => e is Element).forEach((e) { |
| 176 _ElementCssClassSet(this._element); | 197 e.classes.clear(); |
|
blois
2013/05/03 22:16:06
already a setter for classes, just:
e.classes = va
Emily Fortuna
2013/05/03 23:00:09
Done.
| |
| 177 | 198 e.classes.addAll(value); |
| 178 Set<String> readClasses() { | 199 }); |
| 179 var s = new LinkedHashSet<String>(); | |
| 180 var classname = _element.$dom_className; | |
| 181 | |
| 182 for (String name in classname.split(' ')) { | |
| 183 String trimmed = name.trim(); | |
| 184 if (!trimmed.isEmpty) { | |
| 185 s.add(trimmed); | |
| 186 } | |
| 187 } | |
| 188 return s; | |
| 189 } | |
| 190 | |
| 191 void writeClasses(Set<String> s) { | |
| 192 List list = new List.from(s); | |
| 193 _element.$dom_className = s.join(' '); | |
| 194 } | 200 } |
| 195 } | 201 } |
| 196 | 202 |
| 197 /** | 203 /** |
| 198 * An abstract class, which all HTML elements extend. | 204 * An abstract class, which all HTML elements extend. |
| 199 */ | 205 */ |
| 200 $(ANNOTATIONS)abstract class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 206 $(ANNOTATIONS)abstract class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 201 | 207 |
| 202 /** | 208 /** |
| 203 * Creates an HTML element from a valid fragment of HTML. | 209 * Creates an HTML element from a valid fragment of HTML. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 } | 288 } |
| 283 | 289 |
| 284 /** | 290 /** |
| 285 * Finds all descendent elements of this element that match the specified | 291 * Finds all descendent elements of this element that match the specified |
| 286 * group of selectors. | 292 * group of selectors. |
| 287 * | 293 * |
| 288 * [selectors] should be a string using CSS selector syntax. | 294 * [selectors] should be a string using CSS selector syntax. |
| 289 * | 295 * |
| 290 * var items = element.query('.itemClassName'); | 296 * var items = element.query('.itemClassName'); |
| 291 */ | 297 */ |
| 292 List<Element> queryAll(String selectors) => | 298 ElementList queryAll(String selectors) => |
| 293 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); | 299 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); |
| 294 | 300 |
| 295 /** | 301 /** |
| 296 * The set of CSS classes applied to this element. | 302 * The set of CSS classes applied to this element. |
| 297 * | 303 * |
| 298 * This set makes it easy to add, remove or toggle the classes applied to | 304 * This set makes it easy to add, remove or toggle the classes applied to |
| 299 * this element. | 305 * this element. |
| 300 * | 306 * |
| 301 * element.classes.add('selected'); | 307 * element.classes.add('selected'); |
| 302 * element.classes.toggle('isOnline'); | 308 * element.classes.toggle('isOnline'); |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 774 const ScrollAlignment._internal(this._value); | 780 const ScrollAlignment._internal(this._value); |
| 775 toString() => 'ScrollAlignment.$_value'; | 781 toString() => 'ScrollAlignment.$_value'; |
| 776 | 782 |
| 777 /// Attempt to align the element to the top of the scrollable area. | 783 /// Attempt to align the element to the top of the scrollable area. |
| 778 static const TOP = const ScrollAlignment._internal('TOP'); | 784 static const TOP = const ScrollAlignment._internal('TOP'); |
| 779 /// Attempt to center the element in the scrollable area. | 785 /// Attempt to center the element in the scrollable area. |
| 780 static const CENTER = const ScrollAlignment._internal('CENTER'); | 786 static const CENTER = const ScrollAlignment._internal('CENTER'); |
| 781 /// Attempt to align the element to the bottom of the scrollable area. | 787 /// Attempt to align the element to the bottom of the scrollable area. |
| 782 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); | 788 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); |
| 783 } | 789 } |
| OLD | NEW |