OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 abstract class CssClassSet implements Set<String> { |
| 6 |
| 7 String toString() { |
| 8 return Strings.join(new List.from(readClasses()), ' '); |
| 9 } |
| 10 |
| 11 /** |
| 12 * Adds the class [token] to the element if it is not on it, removes it if it |
| 13 * is. |
| 14 */ |
| 15 bool toggle(String value) { |
| 16 Set<String> s = readClasses(); |
| 17 bool result = false; |
| 18 if (s.contains(value)) { |
| 19 s.remove(value); |
| 20 } else { |
| 21 s.add(value); |
| 22 result = true; |
| 23 } |
| 24 writeClasses(s); |
| 25 return result; |
| 26 } |
| 27 |
| 28 /** |
| 29 * Returns [:true:] if classes cannot be added or removed from this |
| 30 * [:CssClassSet:]. |
| 31 */ |
| 32 bool get frozen => false; |
| 33 |
| 34 // interface Iterable - BEGIN |
| 35 Iterator<String> iterator() => readClasses().iterator(); |
| 36 // interface Iterable - END |
| 37 |
| 38 // interface Collection - BEGIN |
| 39 void forEach(void f(String element)) { |
| 40 readClasses().forEach(f); |
| 41 } |
| 42 |
| 43 Collection map(f(String element)) => readClasses().map(f); |
| 44 |
| 45 Collection<String> filter(bool f(String element)) => readClasses().filter(f); |
| 46 |
| 47 bool every(bool f(String element)) => readClasses().every(f); |
| 48 |
| 49 bool some(bool f(String element)) => readClasses().some(f); |
| 50 |
| 51 bool get isEmpty => readClasses().isEmpty; |
| 52 |
| 53 int get length =>readClasses().length; |
| 54 // interface Collection - END |
| 55 |
| 56 // interface Set - BEGIN |
| 57 bool contains(String value) => readClasses().contains(value); |
| 58 |
| 59 void add(String value) { |
| 60 // TODO - figure out if we need to do any validation here |
| 61 // or if the browser natively does enough |
| 62 _modify((s) => s.add(value)); |
| 63 } |
| 64 |
| 65 bool remove(String value) { |
| 66 Set<String> s = readClasses(); |
| 67 bool result = s.remove(value); |
| 68 writeClasses(s); |
| 69 return result; |
| 70 } |
| 71 |
| 72 void addAll(Collection<String> collection) { |
| 73 // TODO - see comment above about validation |
| 74 _modify((s) => s.addAll(collection)); |
| 75 } |
| 76 |
| 77 void removeAll(Collection<String> collection) { |
| 78 _modify((s) => s.removeAll(collection)); |
| 79 } |
| 80 |
| 81 bool isSubsetOf(Collection<String> collection) => |
| 82 readClasses().isSubsetOf(collection); |
| 83 |
| 84 bool containsAll(Collection<String> collection) => |
| 85 readClasses().containsAll(collection); |
| 86 |
| 87 Set<String> intersection(Collection<String> other) => |
| 88 readClasses().intersection(other); |
| 89 |
| 90 void clear() { |
| 91 _modify((s) => s.clear()); |
| 92 } |
| 93 // interface Set - END |
| 94 |
| 95 /** |
| 96 * Helper method used to modify the set of css classes on this element. |
| 97 * |
| 98 * f - callback with: |
| 99 * s - a Set of all the css class name currently on this element. |
| 100 * |
| 101 * After f returns, the modified set is written to the |
| 102 * className property of this element. |
| 103 */ |
| 104 void _modify( f(Set<String> s)) { |
| 105 Set<String> s = readClasses(); |
| 106 f(s); |
| 107 writeClasses(s); |
| 108 } |
| 109 |
| 110 /** |
| 111 * Read the class names from the Element class property, |
| 112 * and put them into a set (duplicates are discarded). |
| 113 * This is intended to be overridden by specific implementations. |
| 114 */ |
| 115 Set<String> readClasses(); |
| 116 |
| 117 /** |
| 118 * Join all the elements of a set into one string and write |
| 119 * back to the element. |
| 120 * This is intended to be overridden by specific implementations. |
| 121 */ |
| 122 void writeClasses(Set<String> s); |
| 123 } |
OLD | NEW |