| 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 /** |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 final Iterable<Element> _elementIterable; | 89 final Iterable<Element> _elementIterable; |
| 90 Iterable<_ElementCssClassSet> _elementCssClassSetIterable; | 90 Iterable<_ElementCssClassSet> _elementCssClassSetIterable; |
| 91 | 91 |
| 92 _MultiElementCssClassSet(this._elementIterable) { | 92 _MultiElementCssClassSet(this._elementIterable) { |
| 93 _elementCssClassSetIterable = new List.from(_elementIterable).map( | 93 _elementCssClassSetIterable = new List.from(_elementIterable).map( |
| 94 (e) => new _ElementCssClassSet(e)); | 94 (e) => new _ElementCssClassSet(e)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 Set<String> readClasses() { | 97 Set<String> readClasses() { |
| 98 var s = new LinkedHashSet<String>(); | 98 var s = new LinkedHashSet<String>(); |
| 99 _elementCssClassSetIterable.forEach((e) => s.addAll(e.readClasses())); | 99 _elementCssClassSetIterable.forEach( |
| 100 (_ElementCssClassSet e) => s.addAll(e.readClasses())); |
| 100 return s; | 101 return s; |
| 101 } | 102 } |
| 102 | 103 |
| 103 void writeClasses(Set<String> s) { | 104 void writeClasses(Set<String> s) { |
| 104 var classes = new List.from(s).join(' '); | 105 var classes = s.join(' '); |
| 105 for (Element e in _elementIterable) { | 106 for (Element e in _elementIterable) { |
| 106 e.className = classes; | 107 e.className = classes; |
| 107 } | 108 } |
| 108 } | 109 } |
| 109 | 110 |
| 110 /** | 111 /** |
| 111 * Helper method used to modify the set of css classes on this element. | 112 * Helper method used to modify the set of css classes on this element. |
| 112 * | 113 * |
| 113 * f - callback with: | 114 * f - callback with: |
| 114 * s - a Set of all the css class name currently on this element. | 115 * s - a Set of all the css class name currently on this element. |
| 115 * | 116 * |
| 116 * After f returns, the modified set is written to the | 117 * After f returns, the modified set is written to the |
| 117 * className property of this element. | 118 * className property of this element. |
| 118 */ | 119 */ |
| 119 modify( f(Set<String> s)) { | 120 modify( f(Set<String> s)) { |
| 120 _elementCssClassSetIterable.forEach((e) => e.modify(f)); | 121 _elementCssClassSetIterable.forEach((_ElementCssClassSet e) => e.modify(f)); |
| 121 } | 122 } |
| 122 | 123 |
| 123 /** | 124 /** |
| 124 * Adds the class [value] to the element if it is not on it, removes it if it | 125 * Adds the class [value] to the element if it is not on it, removes it if it |
| 125 * is. | 126 * is. |
| 126 */ | 127 */ |
| 127 bool toggle(String value, [bool shouldAdd]) => | 128 bool toggle(String value, [bool shouldAdd]) => |
| 128 _modifyWithReturnValue((e) => e.toggle(value, shouldAdd)); | 129 _elementCssClassSetIterable.fold(false, |
| 130 (bool changed, _ElementCssClassSet e) => |
| 131 e.toggle(value, shouldAdd) || changed); |
| 129 | 132 |
| 130 /** | 133 /** |
| 131 * Remove the class [value] from element, and return true on successful | 134 * Remove the class [value] from element, and return true on successful |
| 132 * removal. | 135 * removal. |
| 133 * | 136 * |
| 134 * This is the Dart equivalent of jQuery's | 137 * This is the Dart equivalent of jQuery's |
| 135 * [removeClass](http://api.jquery.com/removeClass/). | 138 * [removeClass](http://api.jquery.com/removeClass/). |
| 136 */ | 139 */ |
| 137 bool remove(Object value) => _modifyWithReturnValue((e) => e.remove(value)); | 140 bool remove(Object value) => _elementCssClassSetIterable.fold(false, |
| 138 | 141 (bool changed, _ElementCssClassSet e) => e.remove(value) || changed); |
| 139 bool _modifyWithReturnValue(f) => _elementCssClassSetIterable.fold( | |
| 140 false, (prevValue, element) => f(element) || prevValue); | |
| 141 } | 142 } |
| 142 | 143 |
| 143 class _ElementCssClassSet extends CssClassSetImpl { | 144 class _ElementCssClassSet extends CssClassSetImpl { |
| 144 | 145 |
| 145 final Element _element; | 146 final Element _element; |
| 146 | 147 |
| 147 _ElementCssClassSet(this._element); | 148 _ElementCssClassSet(this._element); |
| 148 | 149 |
| 149 Set<String> readClasses() { | 150 Set<String> readClasses() { |
| 150 var s = new LinkedHashSet<String>(); | 151 var s = new LinkedHashSet<String>(); |
| 151 var classname = _element.className; | 152 var classname = _element.className; |
| 152 | 153 |
| 153 for (String name in classname.split(' ')) { | 154 for (String name in classname.split(' ')) { |
| 154 String trimmed = name.trim(); | 155 String trimmed = name.trim(); |
| 155 if (!trimmed.isEmpty) { | 156 if (!trimmed.isEmpty) { |
| 156 s.add(trimmed); | 157 s.add(trimmed); |
| 157 } | 158 } |
| 158 } | 159 } |
| 159 return s; | 160 return s; |
| 160 } | 161 } |
| 161 | 162 |
| 162 void writeClasses(Set<String> s) { | 163 void writeClasses(Set<String> s) { |
| 163 List list = new List.from(s); | |
| 164 _element.className = s.join(' '); | 164 _element.className = s.join(' '); |
| 165 } | 165 } |
| 166 } | 166 } |
| OLD | NEW |