OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 /** | |
8 * A set (union) of the CSS classes that are present in a set of elements. | |
9 * Implemented separately from _ElementCssClassSet for performance. | |
10 */ | |
11 class _MultiElementCssClassSet extends CssClassSetImpl { | |
12 final Iterable<Element> _elementIterable; | |
13 Iterable<_ElementCssClassSet> _elementCssClassSetIterable; | |
14 | |
15 _MultiElementCssClassSet(this._elementIterable) { | |
16 _elementCssClassSetIterable = | |
17 new List.from(_elementIterable).map((e) => new _ElementCssClassSet(e)); | |
18 } | |
19 | |
20 Set<String> readClasses() { | |
21 var s = new LinkedHashSet<String>(); | |
22 _elementCssClassSetIterable | |
23 .forEach((_ElementCssClassSet e) => s.addAll(e.readClasses())); | |
24 return s; | |
25 } | |
26 | |
27 void writeClasses(Set<String> s) { | |
28 var classes = s.join(' '); | |
29 for (Element e in _elementIterable) { | |
30 e.className = classes; | |
31 } | |
32 } | |
33 | |
34 /** | |
35 * Helper method used to modify the set of css classes on this element. | |
36 * | |
37 * f - callback with: | |
38 * s - a Set of all the css class name currently on this element. | |
39 * | |
40 * After f returns, the modified set is written to the | |
41 * className property of this element. | |
42 */ | |
43 modify(f(Set<String> s)) { | |
44 _elementCssClassSetIterable.forEach((_ElementCssClassSet e) => e.modify(f)); | |
45 } | |
46 | |
47 /** | |
48 * Adds the class [value] to the element if it is not on it, removes it if it | |
49 * is. | |
50 */ | |
51 bool toggle(String value, [bool shouldAdd]) => | |
52 _elementCssClassSetIterable.fold( | |
53 false, | |
54 (bool changed, _ElementCssClassSet e) => | |
55 e.toggle(value, shouldAdd) || changed); | |
56 | |
57 /** | |
58 * Remove the class [value] from element, and return true on successful | |
59 * removal. | |
60 * | |
61 * This is the Dart equivalent of jQuery's | |
62 * [removeClass](http://api.jquery.com/removeClass/). | |
63 */ | |
64 bool remove(Object value) => _elementCssClassSetIterable.fold(false, | |
65 (bool changed, _ElementCssClassSet e) => e.remove(value) || changed); | |
66 } | |
67 | |
68 class _ElementCssClassSet extends CssClassSetImpl { | |
69 final Element _element; | |
70 | |
71 _ElementCssClassSet(this._element); | |
72 | |
73 Set<String> readClasses() { | |
74 var s = new LinkedHashSet<String>(); | |
75 var classname = _element.className; | |
76 if (classname is svg.AnimatedString) { | |
77 classname = classname.baseVal; | |
78 } | |
79 | |
80 for (String name in classname.split(' ')) { | |
81 String trimmed = name.trim(); | |
82 if (!trimmed.isEmpty) { | |
83 s.add(trimmed); | |
84 } | |
85 } | |
86 return s; | |
87 } | |
88 | |
89 void writeClasses(Set<String> s) { | |
90 _element.className = s.join(' '); | |
91 } | |
92 } | |
OLD | NEW |