| 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 html_common; | 5 part of html_common; |
| 6 | 6 |
| 7 abstract class CssClassSetImpl implements CssClassSet { | 7 abstract class CssClassSetImpl implements CssClassSet { |
| 8 | 8 |
| 9 String toString() { | 9 String toString() { |
| 10 return readClasses().join(' '); | 10 return readClasses().join(' '); |
| 11 } | 11 } |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Adds the class [value] to the element if it is not on it, removes it if it | 14 * Adds the class [value] to the element if it is not on it, removes it if it |
| 15 * is. | 15 * is. |
| 16 * |
| 17 * If [shouldAdd] is true, then we always add that [value] to the element. If |
| 18 * [shouldAdd] is false then we always remove [value] from the element. |
| 16 */ | 19 */ |
| 17 bool toggle(String value) { | 20 bool toggle(String value, [bool shouldAdd]) { |
| 18 Set<String> s = readClasses(); | 21 Set<String> s = readClasses(); |
| 19 bool result = false; | 22 bool result = false; |
| 20 if (s.contains(value)) { | 23 if (shouldAdd == null) shouldAdd = !s.contains(value); |
| 21 s.remove(value); | 24 if (shouldAdd) { |
| 22 } else { | |
| 23 s.add(value); | 25 s.add(value); |
| 24 result = true; | 26 result = true; |
| 27 } else { |
| 28 s.remove(value); |
| 25 } | 29 } |
| 26 writeClasses(s); | 30 writeClasses(s); |
| 27 return result; | 31 return result; |
| 28 } | 32 } |
| 29 | 33 |
| 30 /** | 34 /** |
| 31 * Returns [:true:] if classes cannot be added or removed from this | 35 * Returns [:true:] if classes cannot be added or removed from this |
| 32 * [:CssClassSet:]. | 36 * [:CssClassSet:]. |
| 33 */ | 37 */ |
| 34 bool get frozen => false; | 38 bool get frozen => false; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 void removeAll(Iterable<String> iterable) { | 130 void removeAll(Iterable<String> iterable) { |
| 127 modify((s) => s.removeAll(iterable)); | 131 modify((s) => s.removeAll(iterable)); |
| 128 } | 132 } |
| 129 | 133 |
| 130 /** | 134 /** |
| 131 * Toggles all classes specified in [iterable] on element. | 135 * Toggles all classes specified in [iterable] on element. |
| 132 * | 136 * |
| 133 * Iterate through [iterable]'s items, and add it if it is not on it, or | 137 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 134 * remove it if it is. This is the Dart equivalent of jQuery's | 138 * remove it if it is. This is the Dart equivalent of jQuery's |
| 135 * [toggleClass](http://api.jquery.com/toggleClass/). | 139 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 140 * If [shouldAdd] is true, then we always add all the classes in [iterable] |
| 141 * element. If [shouldAdd] is false then we always remove all the classes in |
| 142 * [iterable] from the element. |
| 136 */ | 143 */ |
| 137 void toggleAll(Iterable<String> iterable) { | 144 void toggleAll(Iterable<String> iterable, [bool shouldAdd]) { |
| 138 iterable.forEach(toggle); | 145 iterable.forEach((e) => toggle(e, shouldAdd)); |
| 139 } | 146 } |
| 140 | 147 |
| 141 void retainAll(Iterable<String> iterable) { | 148 void retainAll(Iterable<String> iterable) { |
| 142 modify((s) => s.retainAll(iterable)); | 149 modify((s) => s.retainAll(iterable)); |
| 143 } | 150 } |
| 144 | 151 |
| 145 void removeWhere(bool test(String name)) { | 152 void removeWhere(bool test(String name)) { |
| 146 modify((s) => s.removeWhere(test)); | 153 modify((s) => s.removeWhere(test)); |
| 147 } | 154 } |
| 148 | 155 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 */ | 216 */ |
| 210 Set<String> readClasses(); | 217 Set<String> readClasses(); |
| 211 | 218 |
| 212 /** | 219 /** |
| 213 * Join all the elements of a set into one string and write | 220 * Join all the elements of a set into one string and write |
| 214 * back to the element. | 221 * back to the element. |
| 215 * This is intended to be overridden by specific implementations. | 222 * This is intended to be overridden by specific implementations. |
| 216 */ | 223 */ |
| 217 void writeClasses(Set<String> s); | 224 void writeClasses(Set<String> s); |
| 218 } | 225 } |
| OLD | NEW |