| 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; | 5 part of html_common; |
| 6 | 6 |
| 7 abstract class CssClassSet implements Set<String> { | 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 */ | 16 */ |
| 17 bool toggle(String value) { | 17 bool toggle(String value) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 79 |
| 80 /** | 80 /** |
| 81 * Add the class [value] to element. | 81 * Add the class [value] to element. |
| 82 * | 82 * |
| 83 * This is the Dart equivalent of jQuery's | 83 * This is the Dart equivalent of jQuery's |
| 84 * [addClass](http://api.jquery.com/addClass/). | 84 * [addClass](http://api.jquery.com/addClass/). |
| 85 */ | 85 */ |
| 86 void add(String value) { | 86 void add(String value) { |
| 87 // TODO - figure out if we need to do any validation here | 87 // TODO - figure out if we need to do any validation here |
| 88 // or if the browser natively does enough. | 88 // or if the browser natively does enough. |
| 89 _modify((s) => s.add(value)); | 89 modify((s) => s.add(value)); |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** | 92 /** |
| 93 * Remove the class [value] from element, and return true on successful | 93 * Remove the class [value] from element, and return true on successful |
| 94 * removal. | 94 * removal. |
| 95 * | 95 * |
| 96 * This is the Dart equivalent of jQuery's | 96 * This is the Dart equivalent of jQuery's |
| 97 * [removeClass](http://api.jquery.com/removeClass/). | 97 * [removeClass](http://api.jquery.com/removeClass/). |
| 98 */ | 98 */ |
| 99 bool remove(Object value) { | 99 bool remove(Object value) { |
| 100 if (value is! String) return false; | 100 if (value is! String) return false; |
| 101 Set<String> s = readClasses(); | 101 Set<String> s = readClasses(); |
| 102 bool result = s.remove(value); | 102 bool result = s.remove(value); |
| 103 writeClasses(s); | 103 writeClasses(s); |
| 104 return result; | 104 return result; |
| 105 } | 105 } |
| 106 | 106 |
| 107 /** | 107 /** |
| 108 * Add all classes specified in [iterable] to element. | 108 * Add all classes specified in [iterable] to element. |
| 109 * | 109 * |
| 110 * This is the Dart equivalent of jQuery's | 110 * This is the Dart equivalent of jQuery's |
| 111 * [addClass](http://api.jquery.com/addClass/). | 111 * [addClass](http://api.jquery.com/addClass/). |
| 112 */ | 112 */ |
| 113 void addAll(Iterable<String> iterable) { | 113 void addAll(Iterable<String> iterable) { |
| 114 // TODO - see comment above about validation. | 114 // TODO - see comment above about validation. |
| 115 _modify((s) => s.addAll(iterable)); | 115 modify((s) => s.addAll(iterable)); |
| 116 } | 116 } |
| 117 | 117 |
| 118 /** | 118 /** |
| 119 * Remove all classes specified in [iterable] from element. | 119 * Remove all classes specified in [iterable] from element. |
| 120 * | 120 * |
| 121 * This is the Dart equivalent of jQuery's | 121 * This is the Dart equivalent of jQuery's |
| 122 * [removeClass](http://api.jquery.com/removeClass/). | 122 * [removeClass](http://api.jquery.com/removeClass/). |
| 123 */ | 123 */ |
| 124 void removeAll(Iterable<String> iterable) { | 124 void removeAll(Iterable<String> iterable) { |
| 125 _modify((s) => s.removeAll(iterable)); | 125 modify((s) => s.removeAll(iterable)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * Toggles all classes specified in [iterable] on element. | 129 * Toggles all classes specified in [iterable] on element. |
| 130 * | 130 * |
| 131 * Iterate through [iterable]'s items, and add it if it is not on it, or | 131 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 132 * remove it if it is. This is the Dart equivalent of jQuery's | 132 * remove it if it is. This is the Dart equivalent of jQuery's |
| 133 * [toggleClass](http://api.jquery.com/toggleClass/). | 133 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 134 */ | 134 */ |
| 135 void toggleAll(Iterable<String> iterable) { | 135 void toggleAll(Iterable<String> iterable) { |
| 136 iterable.forEach(toggle); | 136 iterable.forEach(toggle); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void retainAll(Iterable<String> iterable) { | 139 void retainAll(Iterable<String> iterable) { |
| 140 _modify((s) => s.retainAll(iterable)); | 140 modify((s) => s.retainAll(iterable)); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void removeWhere(bool test(String name)) { | 143 void removeWhere(bool test(String name)) { |
| 144 _modify((s) => s.removeWhere(test)); | 144 modify((s) => s.removeWhere(test)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void retainWhere(bool test(String name)) { | 147 void retainWhere(bool test(String name)) { |
| 148 _modify((s) => s.retainWhere(test)); | 148 modify((s) => s.retainWhere(test)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 bool containsAll(Iterable<String> collection) => | 151 bool containsAll(Iterable<String> collection) => |
| 152 readClasses().containsAll(collection); | 152 readClasses().containsAll(collection); |
| 153 | 153 |
| 154 Set<String> intersection(Set<String> other) => | 154 Set<String> intersection(Set<String> other) => |
| 155 readClasses().intersection(other); | 155 readClasses().intersection(other); |
| 156 | 156 |
| 157 Set<String> union(Set<String> other) => | 157 Set<String> union(Set<String> other) => |
| 158 readClasses().union(other); | 158 readClasses().union(other); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 174 readClasses().skipWhile(test); | 174 readClasses().skipWhile(test); |
| 175 String firstWhere(bool test(String value), { String orElse() }) => | 175 String firstWhere(bool test(String value), { String orElse() }) => |
| 176 readClasses().firstWhere(test, orElse: orElse); | 176 readClasses().firstWhere(test, orElse: orElse); |
| 177 String lastWhere(bool test(String value), {String orElse()}) => | 177 String lastWhere(bool test(String value), {String orElse()}) => |
| 178 readClasses().lastWhere(test, orElse: orElse); | 178 readClasses().lastWhere(test, orElse: orElse); |
| 179 String singleWhere(bool test(String value)) => | 179 String singleWhere(bool test(String value)) => |
| 180 readClasses().singleWhere(test); | 180 readClasses().singleWhere(test); |
| 181 String elementAt(int index) => readClasses().elementAt(index); | 181 String elementAt(int index) => readClasses().elementAt(index); |
| 182 | 182 |
| 183 void clear() { | 183 void clear() { |
| 184 _modify((s) => s.clear()); | 184 modify((s) => s.clear()); |
| 185 } | 185 } |
| 186 // interface Set - END | 186 // interface Set - END |
| 187 | 187 |
| 188 /** | 188 /** |
| 189 * Helper method used to modify the set of css classes on this element. | 189 * Helper method used to modify the set of css classes on this element. |
| 190 * | 190 * |
| 191 * f - callback with: | 191 * f - callback with: |
| 192 * s - a Set of all the css class name currently on this element. | 192 * s - a Set of all the css class name currently on this element. |
| 193 * | 193 * |
| 194 * After f returns, the modified set is written to the | 194 * After f returns, the modified set is written to the |
| 195 * className property of this element. | 195 * className property of this element. |
| 196 */ | 196 */ |
| 197 void _modify( f(Set<String> s)) { | 197 void modify( f(Set<String> s)) { |
| 198 Set<String> s = readClasses(); | 198 Set<String> s = readClasses(); |
| 199 f(s); | 199 f(s); |
| 200 writeClasses(s); | 200 writeClasses(s); |
| 201 } | 201 } |
| 202 | 202 |
| 203 /** | 203 /** |
| 204 * Read the class names from the Element class property, | 204 * Read the class names from the Element class property, |
| 205 * and put them into a set (duplicates are discarded). | 205 * and put them into a set (duplicates are discarded). |
| 206 * This is intended to be overridden by specific implementations. | 206 * This is intended to be overridden by specific implementations. |
| 207 */ | 207 */ |
| 208 Set<String> readClasses(); | 208 Set<String> readClasses(); |
| 209 | 209 |
| 210 /** | 210 /** |
| 211 * Join all the elements of a set into one string and write | 211 * Join all the elements of a set into one string and write |
| 212 * back to the element. | 212 * back to the element. |
| 213 * This is intended to be overridden by specific implementations. | 213 * This is intended to be overridden by specific implementations. |
| 214 */ | 214 */ |
| 215 void writeClasses(Set<String> s); | 215 void writeClasses(Set<String> s); |
| 216 } | 216 } |
| OLD | NEW |