| 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<T extends Element> extends ListBase<T> { | |
| 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 | |
| 161 // TODO(jacobr): this is an inefficient implementation but it is hard to see | 140 // TODO(jacobr): this is an inefficient implementation but it is hard to see |
| 162 // a better option given that we cannot quite force NodeList to be an | 141 // a better option given that we cannot quite force NodeList to be an |
| 163 // ElementList as there are valid cases where a NodeList JavaScript object | 142 // ElementList as there are valid cases where a NodeList JavaScript object |
| 164 // contains Node objects that are not Elements. | 143 // contains Node objects that are not Elements. |
| 165 class _FrozenElementList<T extends Element> extends ListBase<T> implements Eleme
ntList { | 144 class _FrozenElementList<T extends Element> extends ListBase<T> { |
| 166 final List<Node> _nodeList; | 145 final List<Node> _nodeList; |
| 167 | 146 |
| 168 _FrozenElementList._wrap(this._nodeList); | 147 _FrozenElementList._wrap(this._nodeList); |
| 169 | 148 |
| 170 int get length => _nodeList.length; | 149 int get length => _nodeList.length; |
| 171 | 150 |
| 172 Element operator [](int index) => _nodeList[index]; | 151 Element operator [](int index) => _nodeList[index]; |
| 173 | 152 |
| 174 void operator []=(int index, Element value) { | 153 void operator []=(int index, Element value) { |
| 175 throw new UnsupportedError('Cannot modify list'); | 154 throw new UnsupportedError('Cannot modify list'); |
| 176 } | 155 } |
| 177 | 156 |
| 178 void set length(int newLength) { | 157 void set length(int newLength) { |
| 179 throw new UnsupportedError('Cannot modify list'); | 158 throw new UnsupportedError('Cannot modify list'); |
| 180 } | 159 } |
| 181 | 160 |
| 182 void sort([Comparator<Element> compare]) { | 161 void sort([Comparator<Element> compare]) { |
| 183 throw new UnsupportedError('Cannot sort list'); | 162 throw new UnsupportedError('Cannot sort list'); |
| 184 } | 163 } |
| 185 | 164 |
| 186 Element get first => _nodeList.first; | 165 Element get first => _nodeList.first; |
| 187 | 166 |
| 188 Element get last => _nodeList.last; | 167 Element get last => _nodeList.last; |
| 189 | 168 |
| 190 Element get single => _nodeList.single; | 169 Element get single => _nodeList.single; |
| 170 } |
| 191 | 171 |
| 192 CssClassSet get classes => new _MultiElementCssClassSet( | 172 class _ElementCssClassSet extends CssClassSet { |
| 193 _nodeList.where((e) => e is Element)); | |
| 194 | 173 |
| 195 void set classes(Iterable<String> value) { | 174 final Element _element; |
| 196 _nodeList.where((e) => e is Element).forEach((e) => e.classes = value); | 175 |
| 176 _ElementCssClassSet(this._element); |
| 177 |
| 178 Set<String> readClasses() { |
| 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(' '); |
| 197 } | 194 } |
| 198 } | 195 } |
| 199 | 196 |
| 200 /** | 197 /** |
| 201 * An abstract class, which all HTML elements extend. | 198 * An abstract class, which all HTML elements extend. |
| 202 */ | 199 */ |
| 203 $(ANNOTATIONS)abstract class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 200 $(ANNOTATIONS)abstract class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 204 | 201 |
| 205 /** | 202 /** |
| 206 * Creates an HTML element from a valid fragment of HTML. | 203 * Creates an HTML element from a valid fragment of HTML. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 } | 282 } |
| 286 | 283 |
| 287 /** | 284 /** |
| 288 * Finds all descendent elements of this element that match the specified | 285 * Finds all descendent elements of this element that match the specified |
| 289 * group of selectors. | 286 * group of selectors. |
| 290 * | 287 * |
| 291 * [selectors] should be a string using CSS selector syntax. | 288 * [selectors] should be a string using CSS selector syntax. |
| 292 * | 289 * |
| 293 * var items = element.query('.itemClassName'); | 290 * var items = element.query('.itemClassName'); |
| 294 */ | 291 */ |
| 295 ElementList queryAll(String selectors) => | 292 List<Element> queryAll(String selectors) => |
| 296 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); | 293 new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); |
| 297 | 294 |
| 298 /** | 295 /** |
| 299 * The set of CSS classes applied to this element. | 296 * The set of CSS classes applied to this element. |
| 300 * | 297 * |
| 301 * This set makes it easy to add, remove or toggle the classes applied to | 298 * This set makes it easy to add, remove or toggle the classes applied to |
| 302 * this element. | 299 * this element. |
| 303 * | 300 * |
| 304 * element.classes.add('selected'); | 301 * element.classes.add('selected'); |
| 305 * element.classes.toggle('isOnline'); | 302 * element.classes.toggle('isOnline'); |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 const ScrollAlignment._internal(this._value); | 774 const ScrollAlignment._internal(this._value); |
| 778 toString() => 'ScrollAlignment.$_value'; | 775 toString() => 'ScrollAlignment.$_value'; |
| 779 | 776 |
| 780 /// Attempt to align the element to the top of the scrollable area. | 777 /// Attempt to align the element to the top of the scrollable area. |
| 781 static const TOP = const ScrollAlignment._internal('TOP'); | 778 static const TOP = const ScrollAlignment._internal('TOP'); |
| 782 /// Attempt to center the element in the scrollable area. | 779 /// Attempt to center the element in the scrollable area. |
| 783 static const CENTER = const ScrollAlignment._internal('CENTER'); | 780 static const CENTER = const ScrollAlignment._internal('CENTER'); |
| 784 /// Attempt to align the element to the bottom of the scrollable area. | 781 /// Attempt to align the element to the bottom of the scrollable area. |
| 785 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); | 782 static const BOTTOM = const ScrollAlignment._internal('BOTTOM'); |
| 786 } | 783 } |
| OLD | NEW |