001package org.gwtbootstrap3.extras.bootbox.client;
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.ConfirmCallback;
024import org.gwtbootstrap3.extras.bootbox.client.callback.PromptCallback;
025import org.gwtbootstrap3.extras.bootbox.client.callback.SimpleCallback;
026import org.gwtbootstrap3.extras.bootbox.client.options.AlertOptions;
027import org.gwtbootstrap3.extras.bootbox.client.options.BootboxLocale;
028import org.gwtbootstrap3.extras.bootbox.client.options.ConfirmOptions;
029import org.gwtbootstrap3.extras.bootbox.client.options.DialogOptions;
030import org.gwtbootstrap3.extras.bootbox.client.options.PromptOptions;
031
032/**
033 * Bootbox.js is a small JavaScript library which allows you
034 * to create programmatic dialog boxes using Bootstrap modals.
035 *
036 * @author Xiaodong Sun
037 * @see http://bootboxjs.com/
038 */
039public class Bootbox {
040
041    /**
042     * Displays a message in a modal dialog box.
043     *
044     * @param msg the message to be displayed.
045     */
046    public static native void alert(String msg) /*-{
047        $wnd.bootbox.alert(msg);
048    }-*/;
049
050    /**
051     * Displays a message in a modal dialog box.
052     * With callback handler.
053     *
054     * @param msg      the message to be displayed.
055     * @param callback the callback handler.
056     */
057    public static native void alert(String msg, SimpleCallback callback) /*-{
058        $wnd.bootbox.alert(msg, function () {
059            callback.@org.gwtbootstrap3.extras.bootbox.client.callback.SimpleCallback::callback()();
060        });
061    }-*/;
062
063    /**
064     * Displays a customized alert with the given {@link AlertOptions}.
065     *
066     * @param options
067     */
068    public static native void alert(AlertOptions options) /*-{
069        $wnd.bootbox.alert(options);
070    }-*/;
071
072    /**
073     * Displays a message in a modal dialog box, along with the standard 'OK' and
074     * 'Cancel' buttons.
075     *
076     * @param msg      the message to be displayed.
077     * @param callback the callback handler.
078     */
079    public static native void confirm(String msg, ConfirmCallback callback) /*-{
080        $wnd.bootbox.confirm(msg, function (result) {
081            callback.@org.gwtbootstrap3.extras.bootbox.client.callback.ConfirmCallback::callback(Z)(result);
082        });
083    }-*/;
084
085    /**
086     * Displays a customized confirm with the given {@link ConfirmOptions}.
087     *
088     * @param options
089     */
090    public static native void confirm(ConfirmOptions options) /*-{
091        $wnd.bootbox.confirm(options);
092    }-*/;
093
094    /**
095     * Displays a request for information in a modal dialog box, along with the
096     * standard 'OK' and 'Cancel' buttons.
097     *
098     * @param msg      the message to be displayed.
099     * @param callback the callback handler.
100     */
101    public static native void prompt(String msg, PromptCallback callback) /*-{
102        $wnd.bootbox.prompt(msg, function (result) {
103            callback.@org.gwtbootstrap3.extras.bootbox.client.callback.PromptCallback::callback(Ljava/lang/String;)(result);
104        });
105    }-*/;
106
107    /**
108     * Displays a customized prompt with the given {@link PromptOptions}.
109     *
110     * @param options
111     */
112    public static native void prompt(PromptOptions options) /*-{
113        $wnd.bootbox.prompt(options);
114    }-*/;
115
116    /**
117     * Displays a completely customizable dialog in a modal dialog box.
118     *
119     * @param options the dialog options.
120     */
121    public static native void dialog(final DialogOptions options) /*-{
122        $wnd.bootbox.dialog(options);
123    }-*/;
124
125    /**
126     * Sets a callback when dialog gets initialized.
127     *
128     * @param callback
129     */
130    public static native void init(SimpleCallback callback) /*-{
131        $wnd.bootbox.init(function() {
132            if (callback)
133                callback.@org.gwtbootstrap3.extras.bootbox.client.callback.SimpleCallback::callback()();
134        });
135    }-*/;
136
137    /**
138     * Set many of the default options shown in the dialog example.<br>
139     * <br>
140     * Many of these options are also applied to the basic wrapper methods
141     * and can be overridden whenever the wrapper methods are invoked
142     * with a single options argument.
143     *
144     * @param options
145     */
146    public static native void setDefaults(DialogOptions options) /*-{
147        $wnd.bootbox.setDefaults(options);
148    }-*/;
149
150    /**
151     * Sets a locale.
152     *
153     * @param locale if <code>null</code>, defaults to {@link BootboxLocale#EN}.
154     */
155    public static void setLocale(final BootboxLocale locale) {
156        BootboxLocale l = (locale != null) ? locale : BootboxLocale.getDefault();
157        setLocale(l.getLocale());
158    }
159
160    private static native void setLocale(String locale) /*-{
161        $wnd.bootbox.setLocale(locale);
162    }-*/;
163
164    /**
165     * Hide all currently active bootbox dialogs.
166     * <p>Individual dialogs can be closed as per normal Bootstrap dialogs: dialog.modal('hide').
167     */
168    public static native void hideAll() /*-{
169        $wnd.bootbox.hideAll();
170    }-*/;
171}