001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.util;
029
030import java.util.Map;
031
032import com.google.gwt.core.client.JavaScriptObject;
033import com.google.gwt.core.client.JsArrayString;
034import com.google.gwt.user.client.Command;
035import com.google.gwt.user.client.rpc.AsyncCallback;
036
037/**
038 * A class containing generic Javascript utility methods.<p>
039 */
040public final class CmsJsUtil {
041
042    /**
043     * Prevent instantiation.<p>
044     */
045    private CmsJsUtil() {
046
047        // do nothing
048    }
049
050    /**
051     * Calls a named global function (i.e. a function-valued attribute of window) with a string argument.<p>
052     *
053     * @param name the function name
054     * @param param the function parameter
055     */
056    public static native void callNamedFunctionWithString(String name, String param) /*-{
057        var w = $wnd;
058        w[name](param);
059    }-*/;
060
061    /**
062     * Calls a named global function (i.e. a function-valued attribute of window) with a string argument.<p>
063     *
064     * @param name the function name
065     * @param param1 a function parameter
066     * @param param2 a function parameter
067     */
068    public static native void callNamedFunctionWithString2(String name, String param1, String param2) /*-{
069        var w = $wnd;
070        w[name](param1, param2);
071    }-*/;
072
073    /**
074     * Calls a JS function with a string parameter.<p>
075     *
076     * @param func the Javascript function
077     * @param param the string parameter
078     */
079    public static native void callWithString(JavaScriptObject func, String param) /*-{
080        func(param);
081    }-*/;
082
083    /**
084     * Closes the browser window.
085     */
086    public static native void closeWindow() /*-{
087        $wnd.close();
088    }-*/;
089
090    /**
091     * Creates a Command object which calls the given native JS function.
092     *
093     * @param func a Javascript function
094     * @return a Command object which calls the native function
095     */
096    public static Command convertCallbackToCommand(final JavaScriptObject func) {
097
098        return new Command() {
099
100            public void execute() {
101
102                callWithString(func, null);
103            }
104        };
105
106    }
107
108    /**
109     * Iterates over attributes of a Javascript object and copies them to a string map.
110     *
111     * <p>Converts all values to strings.
112     *
113     *
114     * @param jso the Javascript object
115     * @param map the map to fill
116     */
117    public static native void fillStringMapFromJsObject(JavaScriptObject jso, Map<String, String> map) /*-{
118        var k;
119        for (k in jso) {
120            var v = jso[k];
121            map.@java.util.Map::put(Ljava/lang/Object;Ljava/lang/Object;)(k, String(v));
122        }
123    }-*/;
124
125    /**
126     * Opens the given URI in the current browser window, ensuring that a request to the server is triggered.
127     *
128     * @param uri the URI to open
129     */
130    public static native void forceLoadUri(String uri) /*-{
131        try {
132            var target = new URL(uri, $wnd.location.href);
133            var source = $wnd.location;
134            if (target.hostname === source.hostname
135                    && target.port === source.port
136                    && target.pathname === source.pathname
137                    && target.search === source.search) {
138                $wnd.location.hash = target.hash;
139                $wnd.location.reload();
140            } else {
141                $wnd.location.href = uri;
142            }
143        } catch (e) {
144            $wnd.location.href = uri;
145        }
146    }-*/;
147
148    /**
149     * Reads an attribute from a Javascript object.<p>
150     *
151     * @param jso the Javascript object
152     * @param attr the name of the attribute
153     * @return the value of the attribute
154     */
155    public static native JavaScriptObject getAttribute(JavaScriptObject jso, String attr) /*-{
156        return jso[attr];
157    }-*/;
158
159    /**
160     * Reads a string-valued attribute from a Javascript object.<p>
161     *
162     * @param jso the Javascript object
163     * @param attr the name of the attribute
164     * @return the value of the attribute
165     */
166    public static native String getAttributeString(JavaScriptObject jso, String attr) /*-{
167        return jso[attr];
168    }-*/;
169
170    public static native String getLocalStorage(String key) /*-{
171        return $wnd.localStorage[key];
172    }-*/;
173
174    /**
175     * Gets the current window as a Javascript object.<p>
176     *
177     * @return the current window
178     */
179    public static native JavaScriptObject getWindow() /*-{
180        var result = $wnd;
181        return result;
182    }-*/;
183
184    /**
185     * Creates an empty Javascript string array.
186     *
187     * @return the new array
188     */
189    public static native JsArrayString newArray() /*-{
190        return [];
191    }-*/;
192
193    /**
194     * Creates a JavaScript object from a JSON string.
195     *
196     * @param json the JSON string
197     * @return the JavaScript object parsed from the JSON string
198     */
199    public static native JavaScriptObject parseJSON(String json) /*-{
200        return JSON.parse(json);
201    }-*/;
202
203    /**
204     * Sets an attribute of the given Javascript object to a new value.<p>
205     *
206     * @param jso the object to modify
207     * @param attr the attribute to set
208     * @param newValue the new attribute value
209     */
210    public static native void setAttribute(JavaScriptObject jso, String attr, JavaScriptObject newValue) /*-{
211        jso[attr] = newValue;
212    }-*/;
213
214    public static native JavaScriptObject toJavaScriptObject(Object o) /*-{
215        return o;
216    }-*/;
217
218    /**
219     * Wraps a native JavaScript callback taking a string argument into an AsyncCallback so that it can easily be called from GWT Java code.<p>
220     *
221     * @param func the function to wrap
222     * @return the AsyncCallback wrapper
223     */
224    public static AsyncCallback<String> wrapCallback(final JavaScriptObject func) {
225
226        return new AsyncCallback<String>() {
227
228            public void onFailure(Throwable caught) {
229
230                // TODO Auto-generated method stub
231
232            }
233
234            public void onSuccess(String result) {
235
236                callWithString(func, result);
237            }
238
239        };
240    }
241
242}