001package org.gwtbootstrap3.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.gwtbootstrap3.client.shared.event.HiddenEvent;
024import org.gwtbootstrap3.client.shared.event.HiddenHandler;
025import org.gwtbootstrap3.client.shared.event.HideEvent;
026import org.gwtbootstrap3.client.shared.event.HideHandler;
027import org.gwtbootstrap3.client.shared.event.ShowEvent;
028import org.gwtbootstrap3.client.shared.event.ShowHandler;
029import org.gwtbootstrap3.client.shared.event.ShownEvent;
030import org.gwtbootstrap3.client.shared.event.ShownHandler;
031import org.gwtbootstrap3.client.ui.constants.Styles;
032import org.gwtbootstrap3.client.ui.html.Div;
033
034import com.google.gwt.event.shared.HandlerRegistration;
035import com.google.gwt.user.client.Event;
036
037/**
038 * @author Grant Slender
039 */
040public class Collapse extends Div {
041    private static final String TOGGLE = "toggle";
042    private static final String SHOW = "show";
043    private static final String HIDE = "hide";
044
045    private boolean toggle = true;
046
047    public Collapse() {
048        // Set the default styles
049        setStyleName(Styles.COLLAPSE);
050    }
051
052    @Override
053    protected void onLoad() {
054        super.onLoad();
055
056        // Bind jquery events
057        bindJavaScriptEvents(getElement());
058
059        // Configure the collapse
060        if(toggle) {
061            addStyleName(Styles.IN);
062        }
063    }
064
065    @Override
066    protected void onUnload() {
067        super.onUnload();
068
069        // Unbind the events
070        unbindJavaScriptEvents(getElement());
071    }
072
073    /**
074     * Sets the default state to show or hide. Show is true.
075     *
076     * @param toggle toggle the collapse
077     */
078    public void setToggle(final boolean toggle) {
079        this.toggle = toggle;
080    }
081
082    /**
083     * Causes the collapse to show or hide
084     */
085    public void toggle() {
086        fireMethod(getElement(), TOGGLE);
087    }
088
089    /**
090     * Causes the collapse to show
091     */
092    public void show() {
093        fireMethod(getElement(), SHOW);
094    }
095
096    /**
097     * Causes the collapse to hide
098     */
099    public void hide() {
100        fireMethod(getElement(), HIDE);
101    }
102
103    public boolean isShown() {
104        return this.getElement().hasClassName(Styles.IN);
105    }
106    
107    public boolean isHidden() {
108      return !isShown();
109    }
110    
111    public boolean isCollapsing() {
112        return this.getElement().hasClassName(Styles.COLLAPSING);
113    }
114    
115    public HandlerRegistration addShowHandler(final ShowHandler showHandler) {
116        return addHandler(showHandler, ShowEvent.getType());
117    }
118
119    public HandlerRegistration addShownHandler(final ShownHandler shownHandler) {
120        return addHandler(shownHandler, ShownEvent.getType());
121    }
122
123    public HandlerRegistration addHideHandler(final HideHandler hideHandler) {
124        return addHandler(hideHandler, HideEvent.getType());
125    }
126
127    public HandlerRegistration addHiddenHandler(final HiddenHandler hiddenHandler) {
128        return addHandler(hiddenHandler, HiddenEvent.getType());
129    }
130
131    /**
132     * Fired when the collapse is starting to show
133     */
134    private void onShow(final Event evt) {
135        fireEvent(new ShowEvent(evt));
136    }
137
138    /**
139     * Fired when the collapse has shown
140     */
141    private void onShown(final Event evt) {
142        fireEvent(new ShownEvent(evt));
143    }
144
145    /**
146     * Fired when the collapse is starting to hide
147     */
148    private void onHide(final Event evt) {
149        fireEvent(new HideEvent(evt));
150    }
151
152    /**
153     * Fired when the collapse has hidden
154     */
155    private void onHidden(final Event evt) {
156        fireEvent(new HiddenEvent(evt));
157    }
158
159    private native void bindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{
160        var target = this;
161        var $collapse = $wnd.jQuery(e);
162
163        $collapse.on('show.bs.collapse', function (evt) {
164            target.@org.gwtbootstrap3.client.ui.Collapse::onShow(Lcom/google/gwt/user/client/Event;)(evt);
165        });
166
167        $collapse.on('shown.bs.collapse', function (evt) {
168            target.@org.gwtbootstrap3.client.ui.Collapse::onShown(Lcom/google/gwt/user/client/Event;)(evt);
169        });
170
171        $collapse.on('hide.bs.collapse', function (evt) {
172            target.@org.gwtbootstrap3.client.ui.Collapse::onHide(Lcom/google/gwt/user/client/Event;)(evt);
173        });
174
175        $collapse.on('hidden.bs.collapse', function (evt) {
176            target.@org.gwtbootstrap3.client.ui.Collapse::onHidden(Lcom/google/gwt/user/client/Event;)(evt);
177        });
178    }-*/;
179
180    private native void unbindJavaScriptEvents(final com.google.gwt.dom.client.Element e) /*-{
181        $wnd.jQuery(e).off('show.bs.collapse');
182        $wnd.jQuery(e).off('shown.bs.collapse');
183        $wnd.jQuery(e).off('hide.bs.collapse');
184        $wnd.jQuery(e).off('hidden.bs.collapse');
185    }-*/;
186
187    private native void collapse(final com.google.gwt.dom.client.Element e, final boolean toggle) /*-{
188        $wnd.jQuery(e).collapse({
189            toggle: toggle
190        });
191    }-*/;
192
193    private native void fireMethod(final com.google.gwt.dom.client.Element e, String method) /*-{
194        $wnd.jQuery(e).collapse(method);
195    }-*/;
196
197    private native void fireMethod(final com.google.gwt.dom.client.Element e, int slideNumber) /*-{
198        $wnd.jQuery(e).collapse(slideNumber);
199    }-*/;
200}