jqm4gwt / jqm4gwt

GWT library for jQuery Mobile
Apache License 2.0
53 stars 18 forks source link

Implemented DatePicker based on http://dev.jtsage.com/jQM-DateBox2/ #40

Closed slavap closed 11 years ago

slavap commented 11 years ago

Please add to plugins.

import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sksamuel.jqm4gwt.form.elements.JQMText;

/**
 * See <a href="http://dev.jtsage.com/jQM-DateBox2/demos/mode/calbox.html">jQueryMobile - DateBox</a>
 * <p><a href="http://dev.jtsage.com/jQM-DateBox2/demos/install.html">Install instructions</a></p>
 * <p> Shortly the following scrips must be added: </p>
 * <pre>
 * {@literal <link rel="stylesheet" href="javascript/jqm-datebox.min.css" />}
 * {@literal <script src="javascript/jqm-datebox.comp.calbox.min.js"></script>}
 * {@literal <script src="javascript/jquery.mobile.datebox.i18n.en_US.utf8.js"></script>}
 * And datebox.png should be placed into javascript/image/datebox.png when using "new style".
 * </pre>
 */
public class JQMCalBox extends JQMText {

    /** <a href="http://dev.jtsage.com/jQM-DateBox2/demos/api/dateformat.html">Available Date Format Options</a> */
    public static final String FMT_MMDDYY = "%m/%d/%y";

    protected static final String MODE_CALBOX = "\"mode\": \"calbox\"";
    protected static final String USE_NEW_STYLE = "\"useNewStyle\":";
    protected static final String OVERRIDE_DATE_FMT = "\"overrideDateFormat\":";
    protected static final String NO_HEADER = "\"calNoHeader\":";
    protected static final String USE_PICKERS = "\"calUsePickers\":";
    protected static final String WEEK_START_DAY = "\"overrideCalStartDay\":";
    protected static final String USE_TODAY_BUTTON = "\"useTodayButton\":";
    protected static final String SQUARE_DATE_BUTTONS = "\"calControlGroup\":";

    // input is private in parent, so have to be initialized indirectly on initInput()
    private TextBox input = null;

    private Boolean useNewStyle = true;
    private String dateFormat = null;
    private Boolean usePickers = null;
    private Integer weekStartDay = null;
    private Boolean useTodayButton = null;
    private Boolean squareDateButtons = null;

    public JQMCalBox() {
        this(null);
    }

    public JQMCalBox(String text) {
        super(text);
        setType("date");
        initInput();
        setInputAttribute("data-role", "datebox");
        refreshDataOptions();
    }

    protected String bool2Str(boolean value) {
        return value ? "true" : "false";
    }

    protected String constructDataOptions() {
        StringBuilder sb = new StringBuilder();
        sb.append('{').append(MODE_CALBOX);
        if (useNewStyle != null) {
            sb.append(',').append(USE_NEW_STYLE).append(bool2Str(useNewStyle));
        }
        if (dateFormat != null && !dateFormat.isEmpty()) {
            sb.append(',').append(OVERRIDE_DATE_FMT).append('"').append(dateFormat).append('"');
        }
        if (usePickers != null) {
            sb.append(',').append(NO_HEADER).append(bool2Str(usePickers));
            sb.append(',').append(USE_PICKERS).append(bool2Str(usePickers));
        }
        if (weekStartDay != null) {
            sb.append(',').append(WEEK_START_DAY).append(String.valueOf(weekStartDay));
        }
        if (useTodayButton != null) {
            sb.append(',').append(USE_TODAY_BUTTON).append(bool2Str(useTodayButton));
        }
        if (squareDateButtons != null) {
            sb.append(',').append(SQUARE_DATE_BUTTONS).append(bool2Str(squareDateButtons));
        }
        sb.append('}');
        return sb.toString();
    }

    protected void refreshDataOptions() {
        setInputAttribute("data-options", constructDataOptions());
    }

    private void initInput() {
        Widget widget = getWidget();
        if (widget instanceof ComplexPanel) {
            ComplexPanel p = (ComplexPanel) widget;
            for (int i = 0; i < p.getWidgetCount(); i++) {
                Widget w = p.getWidget(i);
                if (w instanceof TextBox) {
                    input = (TextBox) w;
                    break;
                }
            }
        }
    }

    private void setInputAttribute(String name, String value) {
        if (input == null) return;
        input.getElement().setAttribute(name, value);
    }

    public Boolean getUseNewStyle() {
        return useNewStyle;
    }

    public void setUseNewStyle(Boolean useNewStyle) {
        this.useNewStyle = useNewStyle;
        refreshDataOptions();
    }

    public String getDateFormat() {
        return dateFormat;
    }

    /**
     * @param dateFormat - <a href="http://dev.jtsage.com/jQM-DateBox2/demos/api/dateformat.html">Available Date Format Options</a>
     */
    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
        refreshDataOptions();
    }

    public Boolean getUsePickers() {
        return usePickers;
    }

    public void setUsePickers(Boolean usePickers) {
        this.usePickers = usePickers;
        refreshDataOptions();
    }

    public Integer getWeekStartDay() {
        return weekStartDay;
    }

    /**
     * @param weekStartDay - 0-6, where 0=Sunday, 1=Monday...
     */
    public void setWeekStartDay(Integer weekStartDay) {
        this.weekStartDay = weekStartDay;
        refreshDataOptions();
    }

    public Boolean getUseTodayButton() {
        return useTodayButton;
    }

    public void setUseTodayButton(Boolean useTodayButton) {
        this.useTodayButton = useTodayButton;
        refreshDataOptions();
    }

    public Boolean getSquareDateButtons() {
        return squareDateButtons;
    }

    public void setSquareDateButtons(Boolean squareDateButtons) {
        this.squareDateButtons = squareDateButtons;
        refreshDataOptions();
    }

}
JaysonRaymond commented 11 years ago

Slava - please fork the repo, make your additions, then do a pull request to get them reviewed for adding back into the main line.

JaysonRaymond commented 11 years ago

Also, thank you for your efforts - we look forward to incorporating it. Contribution guidelines can be found at https://github.com/sksamuel/jqm4gwt/wiki/Contributors-Guidelines

slavap commented 11 years ago

Will do it next few days. Sorry, I'm kind of overloaded with my current development :-(