OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 // TODO(jmesserly): everything in this file is copied straight from "dart:html". |
| 6 library html.dom.src; |
| 7 |
| 8 import 'dart:collection'; |
| 9 import 'package:html/dom.dart'; |
| 10 |
| 11 class ElementCssClassSet extends CssClassSetImpl { |
| 12 final Element _element; |
| 13 |
| 14 ElementCssClassSet(this._element); |
| 15 |
| 16 Set<String> readClasses() { |
| 17 var s = new LinkedHashSet<String>(); |
| 18 var classname = _element.className; |
| 19 |
| 20 for (String name in classname.split(' ')) { |
| 21 String trimmed = name.trim(); |
| 22 if (!trimmed.isEmpty) { |
| 23 s.add(trimmed); |
| 24 } |
| 25 } |
| 26 return s; |
| 27 } |
| 28 |
| 29 void writeClasses(Set<String> s) { |
| 30 _element.className = s.join(' '); |
| 31 } |
| 32 } |
| 33 |
| 34 /** A Set that stores the CSS class names for an element. */ |
| 35 abstract class CssClassSet implements Set<String> { |
| 36 |
| 37 /** |
| 38 * Adds the class [value] to the element if it is not on it, removes it if it |
| 39 * is. |
| 40 * |
| 41 * If [shouldAdd] is true, then we always add that [value] to the element. If |
| 42 * [shouldAdd] is false then we always remove [value] from the element. |
| 43 */ |
| 44 bool toggle(String value, [bool shouldAdd]); |
| 45 |
| 46 /** |
| 47 * Returns [:true:] if classes cannot be added or removed from this |
| 48 * [:CssClassSet:]. |
| 49 */ |
| 50 bool get frozen; |
| 51 |
| 52 /** |
| 53 * Determine if this element contains the class [value]. |
| 54 * |
| 55 * This is the Dart equivalent of jQuery's |
| 56 * [hasClass](http://api.jquery.com/hasClass/). |
| 57 */ |
| 58 bool contains(String value); |
| 59 |
| 60 /** |
| 61 * Add the class [value] to element. |
| 62 * |
| 63 * This is the Dart equivalent of jQuery's |
| 64 * [addClass](http://api.jquery.com/addClass/). |
| 65 * |
| 66 * If this corresponds to one element. Returns true if [value] was added to |
| 67 * the set, otherwise false. |
| 68 * |
| 69 * If this corresponds to many elements, null is always returned. |
| 70 */ |
| 71 bool add(String value); |
| 72 |
| 73 /** |
| 74 * Remove the class [value] from element, and return true on successful |
| 75 * removal. |
| 76 * |
| 77 * This is the Dart equivalent of jQuery's |
| 78 * [removeClass](http://api.jquery.com/removeClass/). |
| 79 */ |
| 80 bool remove(Object value); |
| 81 |
| 82 /** |
| 83 * Add all classes specified in [iterable] to element. |
| 84 * |
| 85 * This is the Dart equivalent of jQuery's |
| 86 * [addClass](http://api.jquery.com/addClass/). |
| 87 */ |
| 88 void addAll(Iterable<String> iterable); |
| 89 |
| 90 /** |
| 91 * Remove all classes specified in [iterable] from element. |
| 92 * |
| 93 * This is the Dart equivalent of jQuery's |
| 94 * [removeClass](http://api.jquery.com/removeClass/). |
| 95 */ |
| 96 void removeAll(Iterable<String> iterable); |
| 97 |
| 98 /** |
| 99 * Toggles all classes specified in [iterable] on element. |
| 100 * |
| 101 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 102 * remove it if it is. This is the Dart equivalent of jQuery's |
| 103 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 104 * If [shouldAdd] is true, then we always add all the classes in [iterable] |
| 105 * element. If [shouldAdd] is false then we always remove all the classes in |
| 106 * [iterable] from the element. |
| 107 */ |
| 108 void toggleAll(Iterable<String> iterable, [bool shouldAdd]); |
| 109 } |
| 110 |
| 111 abstract class CssClassSetImpl implements CssClassSet { |
| 112 String toString() { |
| 113 return readClasses().join(' '); |
| 114 } |
| 115 |
| 116 /** |
| 117 * Adds the class [value] to the element if it is not on it, removes it if it |
| 118 * is. |
| 119 * |
| 120 * If [shouldAdd] is true, then we always add that [value] to the element. If |
| 121 * [shouldAdd] is false then we always remove [value] from the element. |
| 122 */ |
| 123 bool toggle(String value, [bool shouldAdd]) { |
| 124 Set<String> s = readClasses(); |
| 125 bool result = false; |
| 126 if (shouldAdd == null) shouldAdd = !s.contains(value); |
| 127 if (shouldAdd) { |
| 128 s.add(value); |
| 129 result = true; |
| 130 } else { |
| 131 s.remove(value); |
| 132 } |
| 133 writeClasses(s); |
| 134 return result; |
| 135 } |
| 136 |
| 137 /** |
| 138 * Returns [:true:] if classes cannot be added or removed from this |
| 139 * [:CssClassSet:]. |
| 140 */ |
| 141 bool get frozen => false; |
| 142 |
| 143 // interface Iterable - BEGIN |
| 144 Iterator<String> get iterator => readClasses().iterator; |
| 145 // interface Iterable - END |
| 146 |
| 147 // interface Collection - BEGIN |
| 148 void forEach(void f(String element)) { |
| 149 readClasses().forEach(f); |
| 150 } |
| 151 |
| 152 String join([String separator = ""]) => readClasses().join(separator); |
| 153 |
| 154 Iterable map(f(String element)) => readClasses().map(f); |
| 155 |
| 156 Iterable<String> where(bool f(String element)) => readClasses().where(f); |
| 157 |
| 158 Iterable expand(Iterable f(String element)) => readClasses().expand(f); |
| 159 |
| 160 bool every(bool f(String element)) => readClasses().every(f); |
| 161 |
| 162 bool any(bool f(String element)) => readClasses().any(f); |
| 163 |
| 164 bool get isEmpty => readClasses().isEmpty; |
| 165 |
| 166 bool get isNotEmpty => readClasses().isNotEmpty; |
| 167 |
| 168 int get length => readClasses().length; |
| 169 |
| 170 String reduce(String combine(String value, String element)) { |
| 171 return readClasses().reduce(combine); |
| 172 } |
| 173 |
| 174 dynamic fold(dynamic initialValue, |
| 175 dynamic combine(dynamic previousValue, String element)) { |
| 176 return readClasses().fold(initialValue, combine); |
| 177 } |
| 178 // interface Collection - END |
| 179 |
| 180 // interface Set - BEGIN |
| 181 /** |
| 182 * Determine if this element contains the class [value]. |
| 183 * |
| 184 * This is the Dart equivalent of jQuery's |
| 185 * [hasClass](http://api.jquery.com/hasClass/). |
| 186 */ |
| 187 bool contains(String value) => readClasses().contains(value); |
| 188 |
| 189 /** Lookup from the Set interface. Not interesting for a String set. */ |
| 190 String lookup(String value) => contains(value) ? value : null; |
| 191 |
| 192 /** |
| 193 * Add the class [value] to element. |
| 194 * |
| 195 * This is the Dart equivalent of jQuery's |
| 196 * [addClass](http://api.jquery.com/addClass/). |
| 197 */ |
| 198 bool add(String value) { |
| 199 // TODO - figure out if we need to do any validation here |
| 200 // or if the browser natively does enough. |
| 201 return modify((s) => s.add(value)); |
| 202 } |
| 203 |
| 204 /** |
| 205 * Remove the class [value] from element, and return true on successful |
| 206 * removal. |
| 207 * |
| 208 * This is the Dart equivalent of jQuery's |
| 209 * [removeClass](http://api.jquery.com/removeClass/). |
| 210 */ |
| 211 bool remove(Object value) { |
| 212 if (value is! String) return false; |
| 213 Set<String> s = readClasses(); |
| 214 bool result = s.remove(value); |
| 215 writeClasses(s); |
| 216 return result; |
| 217 } |
| 218 |
| 219 /** |
| 220 * Add all classes specified in [iterable] to element. |
| 221 * |
| 222 * This is the Dart equivalent of jQuery's |
| 223 * [addClass](http://api.jquery.com/addClass/). |
| 224 */ |
| 225 void addAll(Iterable<String> iterable) { |
| 226 // TODO - see comment above about validation. |
| 227 modify((s) => s.addAll(iterable)); |
| 228 } |
| 229 |
| 230 /** |
| 231 * Remove all classes specified in [iterable] from element. |
| 232 * |
| 233 * This is the Dart equivalent of jQuery's |
| 234 * [removeClass](http://api.jquery.com/removeClass/). |
| 235 */ |
| 236 void removeAll(Iterable<String> iterable) { |
| 237 modify((s) => s.removeAll(iterable)); |
| 238 } |
| 239 |
| 240 /** |
| 241 * Toggles all classes specified in [iterable] on element. |
| 242 * |
| 243 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 244 * remove it if it is. This is the Dart equivalent of jQuery's |
| 245 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 246 * If [shouldAdd] is true, then we always add all the classes in [iterable] |
| 247 * element. If [shouldAdd] is false then we always remove all the classes in |
| 248 * [iterable] from the element. |
| 249 */ |
| 250 void toggleAll(Iterable<String> iterable, [bool shouldAdd]) { |
| 251 iterable.forEach((e) => toggle(e, shouldAdd)); |
| 252 } |
| 253 |
| 254 void retainAll(Iterable<String> iterable) { |
| 255 modify((s) => s.retainAll(iterable)); |
| 256 } |
| 257 |
| 258 void removeWhere(bool test(String name)) { |
| 259 modify((s) => s.removeWhere(test)); |
| 260 } |
| 261 |
| 262 void retainWhere(bool test(String name)) { |
| 263 modify((s) => s.retainWhere(test)); |
| 264 } |
| 265 |
| 266 bool containsAll(Iterable<String> collection) => |
| 267 readClasses().containsAll(collection); |
| 268 |
| 269 Set<String> intersection(Set<String> other) => |
| 270 readClasses().intersection(other); |
| 271 |
| 272 Set<String> union(Set<String> other) => readClasses().union(other); |
| 273 |
| 274 Set<String> difference(Set<String> other) => readClasses().difference(other); |
| 275 |
| 276 String get first => readClasses().first; |
| 277 String get last => readClasses().last; |
| 278 String get single => readClasses().single; |
| 279 List<String> toList({bool growable: true}) => |
| 280 readClasses().toList(growable: growable); |
| 281 Set<String> toSet() => readClasses().toSet(); |
| 282 Iterable<String> take(int n) => readClasses().take(n); |
| 283 Iterable<String> takeWhile(bool test(String value)) => |
| 284 readClasses().takeWhile(test); |
| 285 Iterable<String> skip(int n) => readClasses().skip(n); |
| 286 Iterable<String> skipWhile(bool test(String value)) => |
| 287 readClasses().skipWhile(test); |
| 288 dynamic firstWhere(bool test(String value), {Object orElse()}) => |
| 289 readClasses().firstWhere(test, orElse: orElse); |
| 290 dynamic lastWhere(bool test(String value), {Object orElse()}) => |
| 291 readClasses().lastWhere(test, orElse: orElse); |
| 292 String singleWhere(bool test(String value)) => |
| 293 readClasses().singleWhere(test); |
| 294 String elementAt(int index) => readClasses().elementAt(index); |
| 295 |
| 296 void clear() { |
| 297 modify((s) => s.clear()); |
| 298 } |
| 299 // interface Set - END |
| 300 |
| 301 /** |
| 302 * Helper method used to modify the set of css classes on this element. |
| 303 * |
| 304 * f - callback with: |
| 305 * s - a Set of all the css class name currently on this element. |
| 306 * |
| 307 * After f returns, the modified set is written to the |
| 308 * className property of this element. |
| 309 */ |
| 310 modify(f(Set<String> s)) { |
| 311 Set<String> s = readClasses(); |
| 312 var ret = f(s); |
| 313 writeClasses(s); |
| 314 return ret; |
| 315 } |
| 316 |
| 317 /** |
| 318 * Read the class names from the Element class property, |
| 319 * and put them into a set (duplicates are discarded). |
| 320 * This is intended to be overridden by specific implementations. |
| 321 */ |
| 322 Set<String> readClasses(); |
| 323 |
| 324 /** |
| 325 * Join all the elements of a set into one string and write |
| 326 * back to the element. |
| 327 * This is intended to be overridden by specific implementations. |
| 328 */ |
| 329 void writeClasses(Set<String> s); |
| 330 } |
OLD | NEW |