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