| 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; |
| 6 | 6 |
| 7 abstract class CssClassSet implements Set<String> { | 7 abstract class CssClassSet implements Set<String> { |
| 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 [token] 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) { |
| 18 Set<String> s = readClasses(); | 18 Set<String> s = readClasses(); |
| 19 bool result = false; | 19 bool result = false; |
| 20 if (s.contains(value)) { | 20 if (s.contains(value)) { |
| 21 s.remove(value); | 21 s.remove(value); |
| 22 } else { | 22 } else { |
| 23 s.add(value); | 23 s.add(value); |
| 24 result = true; | 24 result = true; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 int get length => readClasses().length; | 59 int get length => readClasses().length; |
| 60 | 60 |
| 61 dynamic reduce(dynamic initialValue, | 61 dynamic reduce(dynamic initialValue, |
| 62 dynamic combine(dynamic previousValue, String element)) { | 62 dynamic combine(dynamic previousValue, String element)) { |
| 63 return readClasses().reduce(initialValue, combine); | 63 return readClasses().reduce(initialValue, combine); |
| 64 } | 64 } |
| 65 // interface Collection - END | 65 // interface Collection - END |
| 66 | 66 |
| 67 // interface Set - BEGIN | 67 // interface Set - BEGIN |
| 68 /** |
| 69 * Determine if this element contains the class [value]. |
| 70 * |
| 71 * This is the Dart equivalent of jQuery's |
| 72 * [hasClass](http://api.jquery.com/hasClass/). |
| 73 */ |
| 68 bool contains(String value) => readClasses().contains(value); | 74 bool contains(String value) => readClasses().contains(value); |
| 69 | 75 |
| 76 /** |
| 77 * Add the class [value] to element. |
| 78 * |
| 79 * This is the Dart equivalent of jQuery's |
| 80 * [addClass](http://api.jquery.com/addClass/). |
| 81 */ |
| 70 void add(String value) { | 82 void add(String value) { |
| 71 // TODO - figure out if we need to do any validation here | 83 // TODO - figure out if we need to do any validation here |
| 72 // or if the browser natively does enough. | 84 // or if the browser natively does enough. |
| 73 _modify((s) => s.add(value)); | 85 _modify((s) => s.add(value)); |
| 74 } | 86 } |
| 75 | 87 |
| 88 /** |
| 89 * Remove the class [value] from element, and return true on successful |
| 90 * removal. |
| 91 * |
| 92 * This is the Dart equivalent of jQuery's |
| 93 * [removeClass](http://api.jquery.com/removeClass/). |
| 94 */ |
| 76 bool remove(Object value) { | 95 bool remove(Object value) { |
| 77 if (value is! String) return false; | 96 if (value is! String) return false; |
| 78 Set<String> s = readClasses(); | 97 Set<String> s = readClasses(); |
| 79 bool result = s.remove(value); | 98 bool result = s.remove(value); |
| 80 writeClasses(s); | 99 writeClasses(s); |
| 81 return result; | 100 return result; |
| 82 } | 101 } |
| 83 | 102 |
| 103 /** |
| 104 * Add all classes specified in [iterable] to element. |
| 105 * |
| 106 * This is the Dart equivalent of jQuery's |
| 107 * [addClass](http://api.jquery.com/addClass/). |
| 108 */ |
| 84 void addAll(Iterable<String> iterable) { | 109 void addAll(Iterable<String> iterable) { |
| 85 // TODO - see comment above about validation. | 110 // TODO - see comment above about validation. |
| 86 _modify((s) => s.addAll(iterable)); | 111 _modify((s) => s.addAll(iterable)); |
| 87 } | 112 } |
| 88 | 113 |
| 114 /** |
| 115 * Remove all classes specified in [iterable] from element. |
| 116 * |
| 117 * This is the Dart equivalent of jQuery's |
| 118 * [removeClass](http://api.jquery.com/removeClass/). |
| 119 */ |
| 89 void removeAll(Iterable<String> iterable) { | 120 void removeAll(Iterable<String> iterable) { |
| 90 _modify((s) => s.removeAll(iterable)); | 121 _modify((s) => s.removeAll(iterable)); |
| 91 } | 122 } |
| 92 | 123 |
| 124 /** |
| 125 * Toggles all classes specified in [iterable] on element. |
| 126 * |
| 127 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 128 * remove it if it is. This is the Dart equivalent of jQuery's |
| 129 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 130 */ |
| 131 void toggleAll(Iterable<String> iterable) { |
| 132 iterable.forEach(toggle); |
| 133 } |
| 134 |
| 93 void retainAll(Iterable<String> iterable) { | 135 void retainAll(Iterable<String> iterable) { |
| 94 _modify((s) => s.retainAll(iterable)); | 136 _modify((s) => s.retainAll(iterable)); |
| 95 } | 137 } |
| 96 | 138 |
| 97 void removeWhere(bool test(String name)) { | 139 void removeWhere(bool test(String name)) { |
| 98 _modify((s) => s.removeWhere(test)); | 140 _modify((s) => s.removeWhere(test)); |
| 99 } | 141 } |
| 100 | 142 |
| 101 void retainWhere(bool test(String name)) { | 143 void retainWhere(bool test(String name)) { |
| 102 _modify((s) => s.retainWhere(test)); | 144 _modify((s) => s.retainWhere(test)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 */ | 210 */ |
| 169 Set<String> readClasses(); | 211 Set<String> readClasses(); |
| 170 | 212 |
| 171 /** | 213 /** |
| 172 * Join all the elements of a set into one string and write | 214 * Join all the elements of a set into one string and write |
| 173 * back to the element. | 215 * back to the element. |
| 174 * This is intended to be overridden by specific implementations. | 216 * This is intended to be overridden by specific implementations. |
| 175 */ | 217 */ |
| 176 void writeClasses(Set<String> s); | 218 void writeClasses(Set<String> s); |
| 177 } | 219 } |
| OLD | NEW |