77 * Licensed under the Apache License, Version 2.0 (the "License");
88 * you may not use this file except in compliance with the License.
99 * You may obtain a copy of the License at
10- *
10+ *
1111 * http://www.apache.org/licenses/LICENSE-2.0
12- *
12+ *
1313 * Unless required by applicable law or agreed to in writing, software
1414 * distributed under the License is distributed on an "AS IS" BASIS,
1515 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2020package gwt .material .design .addins .client .banner ;
2121
2222import com .google .gwt .dom .client .Document ;
23+ import com .google .gwt .dom .client .Element ;
2324import com .google .gwt .event .logical .shared .CloseEvent ;
2425import com .google .gwt .event .logical .shared .CloseHandler ;
2526import com .google .gwt .event .logical .shared .OpenEvent ;
@@ -45,9 +46,9 @@ public class MaterialBanner extends MaterialWidget implements HasOpenClose, HasB
4546 private final MaterialIcon icon ;
4647 private final MaterialLabel messageLabel ;
4748 private final MaterialPanel actions ;
48- private int translateY = 0 ;
49+ private int offsetTop = 0 ;
4950 private int durationInMillis = 300 ;
50- private Widget targetContainer ;
51+ private Element targetElement ;
5152
5253 private ToggleStyleMixin <MaterialBanner > openMixin ;
5354
@@ -81,8 +82,7 @@ protected void onLoad() {
8182 add (messageLabel );
8283 add (actions );
8384
84-
85- updateTransition ();
85+ close ();
8686
8787 AddinsDarkThemeReloader .get ().reload (MaterialBannerDarkTheme .class );
8888 }
@@ -96,41 +96,46 @@ public void add(Widget child) {
9696 }
9797 }
9898
99+ @ Override
100+ public boolean isOpen () {
101+ return false ;
102+ }
103+
99104 @ Override
100105 public void open () {
101106 getOpenMixin ().setOn (true );
107+
108+ setTop (offsetTop );
109+ pushTargetContainer (getOuterHeight ());
102110 OpenEvent .fire (this , getMessage ());
103- calculateTranslateY ();
111+ }
112+
113+ public void open (Element targetElement ) {
114+ setTargetElement (targetElement );
115+ open ();
104116 }
105117
106118 @ Override
107119 public void close () {
108120 getOpenMixin ().setOn (false );
109- CloseEvent .fire (this , getMessage ());
110- pushTargetContainer (0 );
111- }
112121
113- @ Override
114- public boolean isOpen () {
115- return false ;
116- }
122+ setTop (-getOuterHeight ());
123+ pushTargetContainer (0 );
117124
118- protected void calculateTranslateY () {
119- translateY = $ (getElement ()).outerHeight (true );
120- setTransform ("translateY(" + -translateY + "px)" );
121- pushTargetContainer (translateY );
125+ CloseEvent .fire (this , getMessage ());
122126 }
123127
124128 protected void pushTargetContainer (int translateY ) {
125- if (targetContainer != null ) {
126- $ (targetContainer .getElement ()).css ("transform" , "translateY(" + translateY + "px)" );
129+ updateTransition ();
130+ if (targetElement != null ) {
131+ $ (targetElement ).css ("transform" , "translateY(" + translateY + "px)" );
127132 }
128133 }
129134
130135 protected void updateTransition () {
131136 $ (getElement ()).css ("transition" , "all " + durationInMillis + "ms ease" );
132- if (targetContainer != null ) {
133- $ (targetContainer . getElement () ).css ("transition" , "all " + durationInMillis + "ms ease" );
137+ if (targetElement != null ) {
138+ $ (targetElement ).css ("transition" , "all " + durationInMillis + "ms ease" );
134139 }
135140 }
136141
@@ -175,12 +180,28 @@ public MaterialLabel getMessageLabel() {
175180 return messageLabel ;
176181 }
177182
178- public Widget getTargetContainer ( ) {
179- return targetContainer ;
183+ public void setTargetContainer ( Widget targetContainer ) {
184+ this . targetElement = targetContainer . getElement () ;
180185 }
181186
182- public void setTargetContainer (Widget targetContainer ) {
183- this .targetContainer = targetContainer ;
187+ public Element getTargetElement () {
188+ return targetElement ;
189+ }
190+
191+ public void setTargetElement (Element targetElement ) {
192+ this .targetElement = targetElement ;
193+ }
194+
195+ public int getOffsetTop () {
196+ return offsetTop ;
197+ }
198+
199+ public void setOffsetTop (int offsetTop ) {
200+ this .offsetTop = offsetTop ;
201+ }
202+
203+ public int getOuterHeight () {
204+ return $ (getElement ()).outerHeight (true );
184205 }
185206
186207 public ToggleStyleMixin <MaterialBanner > getOpenMixin () {
0 commit comments