|
| 1 | +/* |
| 2 | + * Copyright © 2019 Dominokit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.dominokit.domino.ui.style; |
| 18 | + |
| 19 | +import elemental2.dom.Element; |
| 20 | +import java.util.function.Supplier; |
| 21 | +import org.dominokit.domino.ui.IsElement; |
| 22 | + |
| 23 | +/** |
| 24 | + * A utility class to facilitate the conditional application or removal of CSS classes based on |
| 25 | + * boolean conditions. This class acts as a wrapper around the {@link CssClass} interface, with the |
| 26 | + * provision for conditional application or removal of styles. |
| 27 | + * |
| 28 | + * @see CssClass |
| 29 | + * @author [Your Name or Organization] |
| 30 | + */ |
| 31 | +public class ConditionalCssClass implements CssClass { |
| 32 | + |
| 33 | + private CssClass cssClass; |
| 34 | + private Supplier<Boolean> condition; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates an instance with a specified {@link CssClass} and a condition flag. |
| 38 | + * |
| 39 | + * @param cssClass The CSS class to be conditionally applied or removed. |
| 40 | + * @param condition Condition flag to determine if the class should be applied (true) or removed |
| 41 | + * (false). |
| 42 | + * @return A new instance of {@code ConditionalCssClass}. |
| 43 | + */ |
| 44 | + public static ConditionalCssClass of(CssClass cssClass, Supplier<Boolean> condition) { |
| 45 | + return new ConditionalCssClass(cssClass, condition); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates an instance with a specified {@link HasCssClass} and a condition flag. This method |
| 50 | + * extracts the {@link CssClass} from the provided {@link HasCssClass}. |
| 51 | + * |
| 52 | + * @param cssClass The object implementing {@link HasCssClass} whose CSS class will be extracted. |
| 53 | + * @param condition Condition flag to determine if the class should be applied (true) or removed |
| 54 | + * (false). |
| 55 | + * @return A new instance of {@code ConditionalCssClass}. |
| 56 | + */ |
| 57 | + public static ConditionalCssClass of(HasCssClass cssClass, Supplier<Boolean> condition) { |
| 58 | + return new ConditionalCssClass(cssClass.getCssClass(), condition); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates an instance with a specified CSS class string and a condition flag. |
| 63 | + * |
| 64 | + * @param cssClass The string representation of the CSS class to be conditionally applied or |
| 65 | + * removed. |
| 66 | + * @param condition Condition flag to determine if the class should be applied (true) or removed |
| 67 | + * (false). |
| 68 | + * @return A new instance of {@code ConditionalCssClass}. |
| 69 | + */ |
| 70 | + public static ConditionalCssClass of(String cssClass, Supplier<Boolean> condition) { |
| 71 | + return new ConditionalCssClass(() -> cssClass, condition); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Initializes the {@code ConditionalCssClass} with a provided {@link CssClass} and a condition |
| 76 | + * flag. |
| 77 | + * |
| 78 | + * @param cssClass The CSS class to be managed. |
| 79 | + * @param condition Condition flag to determine if the class should be applied (true) or removed |
| 80 | + * (false). |
| 81 | + */ |
| 82 | + public ConditionalCssClass(CssClass cssClass, Supplier<Boolean> condition) { |
| 83 | + this.cssClass = cssClass; |
| 84 | + this.condition = condition; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Applies or removes the CSS class on the provided element based on the condition flag. |
| 89 | + * |
| 90 | + * @param element The DOM element to which the CSS class will be applied or removed. |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public void apply(Element element) { |
| 94 | + apply(element, condition); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Applies or removes the CSS class on the provided element based on the given condition flag. |
| 99 | + * |
| 100 | + * @param element The DOM element to which the CSS class will be applied or removed. |
| 101 | + * @param condition Condition flag to determine if the class should be applied (true) or removed |
| 102 | + * (false). |
| 103 | + */ |
| 104 | + public void apply(Element element, Supplier<Boolean> condition) { |
| 105 | + if (condition.get()) { |
| 106 | + cssClass.apply(element); |
| 107 | + } else { |
| 108 | + remove(element); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Applies or removes the CSS class on the provided {@link IsElement} based on the specified |
| 114 | + * condition. |
| 115 | + * |
| 116 | + * @param element The UI element (implementing {@link IsElement}) on which the CSS class will be |
| 117 | + * conditionally applied or removed. |
| 118 | + * @param condition Condition flag to determine if the class should be applied (true) or removed |
| 119 | + * (false). |
| 120 | + */ |
| 121 | + public void apply(IsElement<?> element, Supplier<Boolean> condition) { |
| 122 | + apply(element.element(), condition); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Checks if the CSS class is applied to the specified DOM {@link Element}. |
| 127 | + * |
| 128 | + * @param element The DOM element to check. |
| 129 | + * @return {@code true} if the CSS class is applied to the element, {@code false} otherwise. |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public boolean isAppliedTo(Element element) { |
| 133 | + return cssClass.isAppliedTo(element); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Checks if the CSS class is applied to the specified {@link IsElement}. |
| 138 | + * |
| 139 | + * @param element The UI element (implementing {@link IsElement}) to check. |
| 140 | + * @return {@code true} if the CSS class is applied to the element, {@code false} otherwise. |
| 141 | + */ |
| 142 | + @Override |
| 143 | + public boolean isAppliedTo(IsElement<?> element) { |
| 144 | + return cssClass.isAppliedTo(element); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Removes the CSS class from the specified DOM {@link Element}. |
| 149 | + * |
| 150 | + * @param element The DOM element from which the CSS class will be removed. |
| 151 | + */ |
| 152 | + @Override |
| 153 | + public void remove(Element element) { |
| 154 | + cssClass.remove(element); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Removes the CSS class from the specified {@link IsElement}. |
| 159 | + * |
| 160 | + * @param element The UI element (implementing {@link IsElement}) from which the CSS class will be |
| 161 | + * removed. |
| 162 | + */ |
| 163 | + @Override |
| 164 | + public void remove(IsElement<?> element) { |
| 165 | + cssClass.remove(element); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Retrieves the CSS class string associated with this instance. |
| 170 | + * |
| 171 | + * @return The string representation of the associated CSS class. |
| 172 | + */ |
| 173 | + @Override |
| 174 | + public String getCssClass() { |
| 175 | + return cssClass.getCssClass(); |
| 176 | + } |
| 177 | +} |
0 commit comments