|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * GwtMaterial |
| 4 | + * %% |
| 5 | + * Copyright (C) 2015 - 2021 GwtMaterialDesign |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package gwt.material.design.addins.client.banner; |
| 21 | + |
| 22 | +import com.google.gwt.dom.client.Document; |
| 23 | +import com.google.gwt.dom.client.Element; |
| 24 | +import com.google.gwt.event.logical.shared.CloseEvent; |
| 25 | +import com.google.gwt.event.logical.shared.CloseHandler; |
| 26 | +import com.google.gwt.event.logical.shared.OpenEvent; |
| 27 | +import com.google.gwt.event.logical.shared.OpenHandler; |
| 28 | +import com.google.gwt.event.shared.HandlerRegistration; |
| 29 | +import com.google.gwt.user.client.ui.RootPanel; |
| 30 | +import com.google.gwt.user.client.ui.Widget; |
| 31 | +import gwt.material.design.addins.client.MaterialAddins; |
| 32 | +import gwt.material.design.addins.client.banner.event.HasBannerHandlers; |
| 33 | +import gwt.material.design.addins.client.dark.AddinsDarkThemeReloader; |
| 34 | +import gwt.material.design.client.MaterialDesignBase; |
| 35 | +import gwt.material.design.client.base.HasOpenClose; |
| 36 | +import gwt.material.design.client.base.MaterialWidget; |
| 37 | +import gwt.material.design.client.base.mixin.ToggleStyleMixin; |
| 38 | +import gwt.material.design.client.constants.IconType; |
| 39 | +import gwt.material.design.client.ui.MaterialIcon; |
| 40 | +import gwt.material.design.client.ui.MaterialLabel; |
| 41 | +import gwt.material.design.client.ui.MaterialPanel; |
| 42 | + |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +import static gwt.material.design.jquery.client.api.JQuery.$; |
| 48 | + |
| 49 | +public class MaterialBanner extends MaterialWidget implements HasOpenClose, HasBannerHandlers { |
| 50 | + |
| 51 | + private final MaterialIcon icon; |
| 52 | + private final MaterialLabel messageLabel; |
| 53 | + private final MaterialPanel actions; |
| 54 | + private int offsetTop = 0; |
| 55 | + private int durationInMillis = 300; |
| 56 | + private List<Element> targetPushElements; |
| 57 | + |
| 58 | + private ToggleStyleMixin<Widget> openMixin; |
| 59 | + |
| 60 | + static { |
| 61 | + if (MaterialAddins.isDebug()) { |
| 62 | + MaterialDesignBase.injectCss(MaterialBannerDebugClientBundle.INSTANCE.bannerDebugCss()); |
| 63 | + } else { |
| 64 | + MaterialDesignBase.injectCss(MaterialBannerClientBundle.INSTANCE.bannerCss()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public MaterialBanner() { |
| 69 | + super(Document.get().createDivElement(), "banner"); |
| 70 | + |
| 71 | + icon = new MaterialIcon(); |
| 72 | + icon.setVisible(false); |
| 73 | + icon.addStyleName("banner-icon"); |
| 74 | + |
| 75 | + messageLabel = new MaterialLabel(); |
| 76 | + messageLabel.addStyleName("message"); |
| 77 | + |
| 78 | + actions = new MaterialPanel(); |
| 79 | + actions.addStyleName("actions"); |
| 80 | + |
| 81 | + targetPushElements = new ArrayList<>(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void onLoad() { |
| 86 | + super.onLoad(); |
| 87 | + |
| 88 | + add(icon); |
| 89 | + add(messageLabel); |
| 90 | + add(actions); |
| 91 | + |
| 92 | + close(); |
| 93 | + |
| 94 | + AddinsDarkThemeReloader.get().reload(MaterialBannerDarkTheme.class); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void add(Widget child) { |
| 99 | + if (child instanceof MaterialBannerActions) { |
| 100 | + actions.add(child); |
| 101 | + } else { |
| 102 | + super.add(child); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public boolean isOpen() { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void open() { |
| 113 | + getOpenMixin().setOn(true); |
| 114 | + setTop(offsetTop); |
| 115 | + pushTargetElements(getOuterHeight()); |
| 116 | + OpenEvent.fire(this, getMessage()); |
| 117 | + } |
| 118 | + |
| 119 | + public void open(Element... targetElements) { |
| 120 | + setTargetPushElements(Arrays.asList(targetElements)); |
| 121 | + open(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void close() { |
| 126 | + getOpenMixin().setOn(false); |
| 127 | + setTop(-getOuterHeight()); |
| 128 | + pushTargetElements(0); |
| 129 | + CloseEvent.fire(this, getMessage()); |
| 130 | + } |
| 131 | + |
| 132 | + protected void pushTargetElements(int translateY) { |
| 133 | + $(getElement()).css("transition", "all " + durationInMillis + "ms ease"); |
| 134 | + for (Element element : targetPushElements) { |
| 135 | + $(element).css("transition", "all " + durationInMillis + "ms ease"); |
| 136 | + $(element).css("transform", "translateY(" + translateY + "px)"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public int getDurationInMillis() { |
| 141 | + return durationInMillis; |
| 142 | + } |
| 143 | + |
| 144 | + public void setDurationInMillis(int durationInMillis) { |
| 145 | + this.durationInMillis = durationInMillis; |
| 146 | + } |
| 147 | + |
| 148 | + public String getMessage() { |
| 149 | + return messageLabel.getText(); |
| 150 | + } |
| 151 | + |
| 152 | + public void setMessage(String message) { |
| 153 | + if (message != null && !message.isEmpty()) { |
| 154 | + messageLabel.setText(message); |
| 155 | + } else { |
| 156 | + messageLabel.setText(""); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public IconType getIconType() { |
| 161 | + return icon.getIconType(); |
| 162 | + } |
| 163 | + |
| 164 | + public void setIconType(IconType iconType) { |
| 165 | + if (iconType != null) { |
| 166 | + icon.setIconType(iconType); |
| 167 | + icon.setVisible(true); |
| 168 | + } else { |
| 169 | + icon.setVisible(false); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public MaterialIcon getIcon() { |
| 174 | + return icon; |
| 175 | + } |
| 176 | + |
| 177 | + public MaterialLabel getMessageLabel() { |
| 178 | + return messageLabel; |
| 179 | + } |
| 180 | + |
| 181 | + public List<Element> getTargetPushElements() { |
| 182 | + return targetPushElements; |
| 183 | + } |
| 184 | + |
| 185 | + public void setTargetPushElements(List<Element> targetPushElements) { |
| 186 | + this.targetPushElements = targetPushElements; |
| 187 | + } |
| 188 | + |
| 189 | + public int getOffsetTop() { |
| 190 | + return offsetTop; |
| 191 | + } |
| 192 | + |
| 193 | + public void setOffsetTop(int offsetTop) { |
| 194 | + this.offsetTop = offsetTop; |
| 195 | + } |
| 196 | + |
| 197 | + public int getOuterHeight() { |
| 198 | + return $(getElement()).outerHeight(true); |
| 199 | + } |
| 200 | + |
| 201 | + public ToggleStyleMixin<Widget> getOpenMixin() { |
| 202 | + if (openMixin == null) { |
| 203 | + openMixin = new ToggleStyleMixin<>(RootPanel.get(), "banner-open"); |
| 204 | + } |
| 205 | + return openMixin; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public HandlerRegistration addCloseHandler(CloseHandler handler) { |
| 210 | + return addHandler(handler, CloseEvent.getType()); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public HandlerRegistration addOpenHandler(OpenHandler handler) { |
| 215 | + return addHandler(handler, OpenEvent.getType()); |
| 216 | + } |
| 217 | +} |
0 commit comments