001package com.github.gwtbootstrap.client.ui;
002
003import com.github.gwtbootstrap.client.ui.base.MarkupWidget;
004import com.google.gwt.core.client.Scheduler;
005import com.google.gwt.core.client.Scheduler.ScheduledCommand;
006import com.google.gwt.dom.client.Element;
007import com.google.gwt.user.client.ui.Widget;
008
009/**
010 * Markup widget of Affix.
011 * @author k-ohashi
012 *
013 */
014public class Affix extends MarkupWidget {
015    
016    private int offsetBottom;
017    private int offsetTop;
018    
019    
020    private OffsetHelper offsetHelper = new DefaultOffsetHelper();
021    
022    /**
023     * The helper class for Affix offset .
024     * @author k-ohashi
025     */
026    public interface OffsetHelper {
027        /**
028         * Get offset bottom.
029         * @return offset bottom value.
030         */
031        public int bottom();
032
033        /**
034         * Get offset top.
035         * @return offset top value.
036         */
037        public int top();
038    }
039    
040    /**
041     * Default Behavior Offset Helper.
042     * @author k-ohashi
043     */
044    public class DefaultOffsetHelper implements OffsetHelper {
045
046        /**
047         * {@inheritDoc}
048         */
049        @Override
050        public int bottom() {
051            return offsetBottom;
052        }
053
054        /**
055         * {@inheritDoc}
056         */
057        @Override
058        public int top() {
059            return offsetTop;
060        }
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public Widget asWidget() {
068        if(widget != null) {
069            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
070                
071                @Override
072                public void execute() {
073                    reconfigure();
074                }
075            });
076        }
077        return super.asWidget();
078    }
079    
080    
081    public void reconfigure() {
082        com.google.gwt.user.client.Element element = getWidget().getElement();
083        removeDataIfExists(element);
084        configure(element);
085    }
086    
087    public native void configure(Element e) /*-{
088        var $this = $wnd.jQuery(e);
089        var that = this;
090        $this.affix({
091                offset : {
092                    bottom : function(){
093                        return that.@com.github.gwtbootstrap.client.ui.Affix::getOffsetBottom()();
094                    },
095                    top : function(){
096                        return that.@com.github.gwtbootstrap.client.ui.Affix::getOffsetTop()();
097                    }
098                }
099            });
100    }-*/;
101
102
103    public static native void removeDataIfExists(Element e) /*-{
104        var $this = $wnd.jQuery(e);
105        
106        if(!$this.data("affix")) return;
107        
108        $this.removeData("offset")
109                .removeData("offsetBottom")
110                .removeData("offsetTop")
111                .removeData("affix");
112    }-*/;
113    
114    
115    /**
116     * Get offsetTop
117     * @return offsetTop
118     */
119    public int getOffsetTop() {
120        return offsetHelper.top();
121    }
122
123    /**
124     * Set offsetTop
125     * @param offsetTop offsetTop
126     */
127    public void setOffsetTop(int offsetTop) {
128        this.offsetTop = offsetTop;
129    }
130
131    /**
132     * Get offsetBottom
133     * @return offsetBottom
134     */
135    public int getOffsetBottom() {
136        return offsetHelper.bottom();
137    }
138
139    /**
140     * Set offsetBottom
141     * @param offsetBottom offsetBottom
142     */
143    public void setOffsetBottom(int offsetBottom) {
144        this.offsetBottom = offsetBottom;
145    }
146
147    /**
148     * Set offsetHelper
149     * @param offsetHelper offsetHelper
150     */
151    public void setOffsetHelper(OffsetHelper offsetHelper) {
152        assert offsetHelper != null;
153        this.offsetHelper = offsetHelper;
154    }
155}