001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.addins.client.waterfall;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.user.client.ui.Widget;
024import gwt.material.design.addins.client.MaterialAddins;
025import gwt.material.design.addins.client.base.constants.AddinsCssName;
026import gwt.material.design.addins.client.waterfall.js.JsWaterfall;
027import gwt.material.design.client.MaterialDesignBase;
028import gwt.material.design.client.base.JsLoader;
029import gwt.material.design.client.base.MaterialWidget;
030import gwt.material.design.client.constants.Color;
031import gwt.material.design.jquery.client.api.Functions;
032
033//@formatter:off
034
035/**
036 * Material Waterfall - Act like a collapsible header below the nav bar component when scrolling up / down to provide delightful transition of components.
037 * <p>
038 * <h3>XML Namespace Declaration</h3>
039 * <pre>
040 * {@code
041 * xmlns:ma='urn:import:gwt.material.design.addins.client'
042 * }
043 * </pre>
044 * <p>
045 * <h3>UiBinder Usage:</h3>
046 * <pre>
047 * {@code
048 *
049 * <ma:waterfall.MaterialWaterfall backgroundColor="blue" textColor="white" height="280px">
050 *      <m:MaterialPanel addStyleNames="container" paddingTop="20">
051 *          <m:MaterialTitle title="GWT Material" description="Google Material Design UI / UX for GWT Applications."/>
052 *          <m:MaterialAnchorButton text="Get Started" size="LARGE" backgroundColor="blue lighten-2" textColor="white"/>
053 *      </m:MaterialPanel>
054 * </ma:waterfall.MaterialWaterfall>
055 * }
056 * </pre>
057 *
058 * @author kevzlou7979
059 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#waterfall">Material Waterfall</a>
060 */
061//@formatter:on
062public class MaterialWaterfall extends MaterialWidget implements JsLoader {
063
064    static {
065        if (MaterialAddins.isDebug()) {
066            MaterialDesignBase.injectDebugJs(MaterialWaterfallDebugClientBundle.INSTANCE.waterfallJsDebug());
067            MaterialDesignBase.injectCss(MaterialWaterfallDebugClientBundle.INSTANCE.waterfallCssDebug());
068        } else {
069            MaterialDesignBase.injectJs(MaterialWaterfallClientBundle.INSTANCE.waterfallJs());
070            MaterialDesignBase.injectCss(MaterialWaterfallClientBundle.INSTANCE.waterfallCss());
071        }
072    }
073
074    private Functions.Func openCallback;
075    private Functions.Func closeCallback;
076    private double offset;
077
078    public MaterialWaterfall() {
079        super(Document.get().createDivElement(), AddinsCssName.WATERFALL);
080        setShadow(1);
081    }
082
083    public MaterialWaterfall(Color backgroundColor, Color textColor) {
084        this();
085        setBackgroundColor(backgroundColor);
086        setTextColor(textColor);
087    }
088
089    @Override
090    protected void onLoad() {
091        if (openCallback == null) {
092            openCallback = () -> {
093                for (Widget w : getChildren()) {
094                    w.getElement().getStyle().setOpacity(1);
095                }
096            };
097        }
098        if (closeCallback == null) {
099            closeCallback = () -> {
100                for (Widget w : getChildren()) {
101                    w.getElement().getStyle().setOpacity(0);
102                }
103            };
104        }
105        if (offset == 0) {
106            offset = getOffsetHeight();
107        }
108
109        super.onLoad();
110
111        load();
112    }
113
114    @Override
115    public void load() {
116        JsWaterfall.initWaterfall(getElement().getOffsetHeight(), openCallback::call, closeCallback::call, offset);
117    }
118
119    @Override
120    protected void onUnload() {
121        super.onUnload();
122
123        unload();
124    }
125
126    @Override
127    public void unload() {
128        closeCallback = null;
129        openCallback = null;
130    }
131
132    @Override
133    public void reload() {
134        unload();
135        load();
136    }
137
138    public void setCallbacks(Functions.Func openCallback, Functions.Func closeCallback) {
139        this.openCallback = openCallback;
140        this.closeCallback = closeCallback;
141    }
142
143    public double getOffset() {
144        return offset;
145    }
146
147    public void setOffset(double offset) {
148        this.offset = offset;
149    }
150
151
152}