| 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 /** A Set that stores the CSS class names for an element. */ | 7 /** A Set that stores the CSS class names for an element. */ |
| 8 abstract class CssClassSet implements Set<String> { | 8 abstract class CssClassSet implements Set<String> { |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Adds the class [value] to the element if it is not on it, removes it if it | 11 * Adds the class [value] to the element if it is not on it, removes it if it |
| 12 * is. | 12 * is. |
| 13 * | 13 * |
| 14 * If [shouldAdd] is true, then we always add that [value] to the element. If | 14 * If [shouldAdd] is true, then we always add that [value] to the element. If |
| 15 * [shouldAdd] is false then we always remove [value] from the element. | 15 * [shouldAdd] is false then we always remove [value] from the element. |
| 16 * |
| 17 * If this corresponds to one element, returns `true` if [value] is present |
| 18 * after the operation, and returns `false` if [value] is absent after the |
| 19 * operation. |
| 20 * |
| 21 * If this corresponds to many elements, `null` is always returned. |
| 16 */ | 22 */ |
| 17 bool toggle(String value, [bool shouldAdd]); | 23 bool toggle(String value, [bool shouldAdd]); |
| 18 | 24 |
| 19 /** | 25 /** |
| 20 * Returns [:true:] if classes cannot be added or removed from this | 26 * Returns [:true:] if classes cannot be added or removed from this |
| 21 * [:CssClassSet:]. | 27 * [:CssClassSet:]. |
| 22 */ | 28 */ |
| 23 bool get frozen; | 29 bool get frozen; |
| 24 | 30 |
| 25 /** | 31 /** |
| 26 * Determine if this element contains the class [value]. | 32 * Determine if this element contains the class [value]. |
| 27 * | 33 * |
| 28 * This is the Dart equivalent of jQuery's | 34 * This is the Dart equivalent of jQuery's |
| 29 * [hasClass](http://api.jquery.com/hasClass/). | 35 * [hasClass](http://api.jquery.com/hasClass/). |
| 30 */ | 36 */ |
| 31 bool contains(String value); | 37 bool contains(String value); |
| 32 | 38 |
| 33 /** | 39 /** |
| 34 * Add the class [value] to element. | 40 * Add the class [value] to element. |
| 35 * | 41 * |
| 36 * This is the Dart equivalent of jQuery's | 42 * This is the Dart equivalent of jQuery's |
| 37 * [addClass](http://api.jquery.com/addClass/). | 43 * [addClass](http://api.jquery.com/addClass/). |
| 38 * | 44 * |
| 39 * If this corresponds to one element. Returns true if [value] was added to | 45 * If this corresponds to one element. Returns true if [value] was added to |
| 40 * the set, otherwise false. | 46 * the set, otherwise false. |
| 41 * | 47 * |
| 42 * If this corresponds to many elements, null is always returned. | 48 * If this corresponds to many elements, `null` is always returned. |
| 43 */ | 49 */ |
| 44 bool add(String value); | 50 bool add(String value); |
| 45 | 51 |
| 46 /** | 52 /** |
| 47 * Remove the class [value] from element, and return true on successful | 53 * Remove the class [value] from element, and return true on successful |
| 48 * removal. | 54 * removal. |
| 49 * | 55 * |
| 50 * This is the Dart equivalent of jQuery's | 56 * This is the Dart equivalent of jQuery's |
| 51 * [removeClass](http://api.jquery.com/removeClass/). | 57 * [removeClass](http://api.jquery.com/removeClass/). |
| 52 */ | 58 */ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 73 * | 79 * |
| 74 * Iterate through [iterable]'s items, and add it if it is not on it, or | 80 * Iterate through [iterable]'s items, and add it if it is not on it, or |
| 75 * remove it if it is. This is the Dart equivalent of jQuery's | 81 * remove it if it is. This is the Dart equivalent of jQuery's |
| 76 * [toggleClass](http://api.jquery.com/toggleClass/). | 82 * [toggleClass](http://api.jquery.com/toggleClass/). |
| 77 * If [shouldAdd] is true, then we always add all the classes in [iterable] | 83 * If [shouldAdd] is true, then we always add all the classes in [iterable] |
| 78 * element. If [shouldAdd] is false then we always remove all the classes in | 84 * element. If [shouldAdd] is false then we always remove all the classes in |
| 79 * [iterable] from the element. | 85 * [iterable] from the element. |
| 80 */ | 86 */ |
| 81 void toggleAll(Iterable<String> iterable, [bool shouldAdd]); | 87 void toggleAll(Iterable<String> iterable, [bool shouldAdd]); |
| 82 } | 88 } |
| 83 | |
| 84 /** | |
| 85 * A set (union) of the CSS classes that are present in a set of elements. | |
| 86 * Implemented separately from _ElementCssClassSet for performance. | |
| 87 */ | |
| 88 class _MultiElementCssClassSet extends CssClassSetImpl { | |
| 89 final Iterable<Element> _elementIterable; | |
| 90 Iterable<_ElementCssClassSet> _elementCssClassSetIterable; | |
| 91 | |
| 92 _MultiElementCssClassSet(this._elementIterable) { | |
| 93 _elementCssClassSetIterable = new List.from(_elementIterable).map( | |
| 94 (e) => new _ElementCssClassSet(e)); | |
| 95 } | |
| 96 | |
| 97 Set<String> readClasses() { | |
| 98 var s = new LinkedHashSet<String>(); | |
| 99 _elementCssClassSetIterable.forEach( | |
| 100 (_ElementCssClassSet e) => s.addAll(e.readClasses())); | |
| 101 return s; | |
| 102 } | |
| 103 | |
| 104 void writeClasses(Set<String> s) { | |
| 105 var classes = s.join(' '); | |
| 106 for (Element e in _elementIterable) { | |
| 107 e.className = classes; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Helper method used to modify the set of css classes on this element. | |
| 113 * | |
| 114 * f - callback with: | |
| 115 * s - a Set of all the css class name currently on this element. | |
| 116 * | |
| 117 * After f returns, the modified set is written to the | |
| 118 * className property of this element. | |
| 119 */ | |
| 120 modify( f(Set<String> s)) { | |
| 121 _elementCssClassSetIterable.forEach((_ElementCssClassSet e) => e.modify(f)); | |
| 122 } | |
| 123 | |
| 124 /** | |
| 125 * Adds the class [value] to the element if it is not on it, removes it if it | |
| 126 * is. | |
| 127 */ | |
| 128 bool toggle(String value, [bool shouldAdd]) => | |
| 129 _elementCssClassSetIterable.fold(false, | |
| 130 (bool changed, _ElementCssClassSet e) => | |
| 131 e.toggle(value, shouldAdd) || changed); | |
| 132 | |
| 133 /** | |
| 134 * Remove the class [value] from element, and return true on successful | |
| 135 * removal. | |
| 136 * | |
| 137 * This is the Dart equivalent of jQuery's | |
| 138 * [removeClass](http://api.jquery.com/removeClass/). | |
| 139 */ | |
| 140 bool remove(Object value) => _elementCssClassSetIterable.fold(false, | |
| 141 (bool changed, _ElementCssClassSet e) => e.remove(value) || changed); | |
| 142 } | |
| 143 | |
| 144 class _ElementCssClassSet extends CssClassSetImpl { | |
| 145 | |
| 146 final Element _element; | |
| 147 | |
| 148 _ElementCssClassSet(this._element); | |
| 149 | |
| 150 Set<String> readClasses() { | |
| 151 var s = new LinkedHashSet<String>(); | |
| 152 var classname = _element.className; | |
| 153 | |
| 154 for (String name in classname.split(' ')) { | |
| 155 String trimmed = name.trim(); | |
| 156 if (!trimmed.isEmpty) { | |
| 157 s.add(trimmed); | |
| 158 } | |
| 159 } | |
| 160 return s; | |
| 161 } | |
| 162 | |
| 163 void writeClasses(Set<String> s) { | |
| 164 _element.className = s.join(' '); | |
| 165 } | |
| 166 } | |
| OLD | NEW |