001package org.gwtbootstrap3.extras.bootbox.client.options;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2016 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.extras.bootbox.client.callback.SimpleCallback;
024
025import com.google.gwt.core.client.JavaScriptObject;
026
027/**
028 * Bootbox dialog options.
029 *
030 * @author Xiaodong Sun
031 */
032public class DialogOptions extends JavaScriptObject {
033
034    private static final String BUTTON_PREFIX = "bootbox_btn_";
035    private static int BUTTON_INDEX = 0;
036
037    protected DialogOptions() {
038    }
039
040    /**
041     * Creates a new {@link DialogOptions}.
042     *
043     * @param message
044     * @return
045     */
046    public static DialogOptions newOptions(final String message) {
047        DialogOptions options = JavaScriptObject.createObject().cast();
048        options.setMessage(message);
049        return options;
050    }
051
052    final native void setMessage(final String message) /*-{
053        this.message = message;
054    }-*/;
055
056    /**
057     * Adds a header to the dialog and places this text in an H4.
058     *
059     * @param title
060     */
061    public final native void setTitle(final String title) /*-{
062        this.title = title;
063    }-*/;
064
065    /**
066     * The locale settings used to translate the three standard button
067     * labels: <b>OK</b>, <b>CONFIRM</b>, <b>CANCEL</b>.
068     *
069     * @param locale
070     */
071    public final void setLocale(final BootboxLocale locale) {
072        BootboxLocale l = (locale != null) ? locale : BootboxLocale.getDefault();
073        setLocale(l.getLocale());
074    }
075
076    private final native void setLocale(final String locale) /*-{
077        this.locale = locale;
078    }-*/;
079
080    /**
081     * Allows the user to dismiss the dialog by hitting
082     * <code>ESC</code>, which will invoke this function.<br>
083     * <br>
084     * Defaults to <code>null</code> for custom dialogs.
085     *
086     * @param callback
087     */
088    public final native void setOnEscape(final SimpleCallback callback) /*-{
089        if (callback) {
090            this.onEscape = function() {
091                callback.@org.gwtbootstrap3.extras.bootbox.client.callback.SimpleCallback::callback()();
092            };
093        } else {
094            this.onEscape = undefined;
095        }
096    }-*/;
097
098    /**
099     * Whether the dialog should be shown immediately.<br>
100     * <br>
101     * Defaults to <code>true</code>.
102     *
103     * @param show
104     */
105    public final native void setShow(final boolean show) /*-{
106        this.show = show;
107    }-*/;
108
109    /**
110     * Whether the dialog should have a backdrop or not.
111     * Also determines whether clicking on the backdrop dismisses the modal.
112     * <ul>
113     * <li><code>null</code>: The backdrop is displayed, but clicking on it has no effect.</li>
114     * <li><code>true</code>: The backdrop is displayed, and clicking on it dismisses the dialog.</li>
115     * <li><code>false</code>: The backdrop is not displayed.</li>
116     * </ul>
117     * Defaults to <code>null</code>.
118     *
119     * @param backdrop
120     */
121    public final native void setBackdrop(final Boolean backdrop) /*-{
122        if (backdrop == null)
123            this.backdrop = undefined;
124        else
125            this.backdrop = backdrop.@java.lang.Boolean::booleanValue()();
126    }-*/;
127
128    /**
129     * Whether the dialog should have a close button or not.<br>
130     * <br>
131     * Defaults to <code>true</code>.
132     *
133     * @param closeButton
134     */
135    public final native void setCloseButton(final boolean closeButton) /*-{
136        this.closeButton = closeButton;
137    }-*/;
138
139    /**
140     * Animate the dialog in and out.<br>
141     * <br>
142     * Defaults to <code>true</code>.
143     *
144     * @param animate
145     */
146    public final native void setAnimate(final boolean animate) /*-{
147        this.animate = animate;
148    }-*/;
149
150    /**
151     * An additional class to apply to the dialog wrapper.<br>
152     * <br>
153     * Defaults to <code>true</code>.
154     *
155     * @param className
156     */
157    public final native void setClassName(final String className) /*-{
158        this.className = className;
159    }-*/;
160
161    /**
162     * Adds the relevant Bootstrap modal size class to the dialog wrapper.<br>
163     * <br>
164     * Defaults to <code>null</code>.
165     *
166     * @param size
167     */
168    public final native void setSize(final BootboxSize size) /*-{
169        if (size)
170            this.size = size.@org.gwtbootstrap3.extras.bootbox.client.options.BootboxSize::getSize()();
171        else
172            this.size = undefined;
173    }-*/;
174
175    /**
176     * Adds a custom button.
177     *
178     * @param label
179     */
180    public final void addButton(String label) {
181        addButton(label, (String) null);
182    }
183
184    /**
185     * Adds a custom button with a class name.
186     *
187     * @param label
188     * @param className
189     */
190    public final void addButton(String label, String className) {
191        addButton(label, className, SimpleCallback.DEFAULT_SIMPLE_CALLBACK);
192    }
193
194    /**
195     * Adds a custom button with a callback.
196     *
197     * @param label
198     * @param callback
199     */
200    public final void addButton(String label, SimpleCallback callback) {
201        addButton(label, null, callback);
202    }
203
204    /**
205     * Adds a custom button with a class name and a callback.
206     *
207     * @param label
208     * @param className
209     * @param callback
210     */
211    public final void addButton(String label, String className, SimpleCallback callback) {
212        addButton(BUTTON_PREFIX + BUTTON_INDEX++, label, className,
213            callback != null ? callback : SimpleCallback.DEFAULT_SIMPLE_CALLBACK);
214    }
215
216    private final native void addButton(String name, String label, String className, SimpleCallback callback) /*-{
217        this.buttons = this.buttons || {};
218        this.buttons[name] = {
219            label: label,
220            callback: function() {
221                callback.@org.gwtbootstrap3.extras.bootbox.client.callback.SimpleCallback::callback()();
222            }
223        };
224        if (className) {
225            this.buttons[name].className = className;
226        }
227    }-*/;
228
229}