View Javadoc

1   /**
2    * This file Copyright (c) 2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.templating.editor.client.jsni;
35  
36  
37  import com.google.gwt.core.client.GWT;
38  import com.google.gwt.dom.client.Document;
39  import com.google.gwt.dom.client.Element;
40  import com.google.gwt.dom.client.MetaElement;
41  import com.google.gwt.dom.client.NodeList;
42  import com.google.gwt.http.client.UrlBuilder;
43  import com.google.gwt.i18n.client.Dictionary;
44  import com.google.gwt.user.client.Window;
45  
46  /**
47   * A JSNI wrapper around native javascript functions found in general.js, inline.js and others plus some utilities.
48   * @version $Id$
49   *
50   */
51  public final class JavascriptUtils {
52  
53      private static Dictionary dictionary;
54  
55      static {
56          //TODO move messages we need to this module?
57          JavascriptUtils.exposeMgnlMessagesToGwtDictionary("info.magnolia.module.admininterface.messages");
58          dictionary = Dictionary.getDictionary("mgnlGwtMessages");
59      }
60  
61      private JavascriptUtils() {
62          //do not instantiate it.
63      }
64  
65      public static native void mgnlOpenDialog(String path, String collectionName, String nodeName, String paragraph, String workspace, String dialogPage, String width, String height, String locale) /*-{
66          $wnd.mgnlOpenDialog(path, collectionName, nodeName, paragraph, workspace, dialogPage, width, height, locale);
67      }-*/;
68  
69      public static native void mgnlMoveNodeStart(String id) /*-{
70          $wnd.mgnlMoveNodeStart('',id,'__'+id);
71      }-*/;
72  
73      public static native void mgnlMoveNodeHigh(Object source) /*-{
74          $wnd.mgnlMoveNodeHigh(source);
75      }-*/;
76  
77      public static native void mgnlMoveNodeEnd(Object source, String path) /*-{
78          $wnd.mgnlMoveNodeEnd(source, path);
79      }-*/;
80  
81      public static native void mgnlMoveNodeReset(Object source) /*-{
82          $wnd.mgnlMoveNodeReset(source);
83      }-*/;
84  
85      public static native void mgnlDeleteNode(String path) /*-{
86          $wnd.mgnlDeleteNode(path,'', '');
87      }-*/;
88  
89      /**
90       * Exposes the messages object corresponding to the passed in basename as a global (thus accessible by GWT Dictionary) variable named <em>mgnlGwtMessages</em>.
91       */
92      public static native void exposeMgnlMessagesToGwtDictionary(String basename) /*-{
93          $wnd.mgnlGwtMessages = $wnd.mgnlMessages.messages[basename];
94      }-*/;
95  
96      public static native void showTree(String workspace, String path) /*-{
97          $wnd.MgnlAdminCentral.showTree(workspace, path)
98      }-*/;
99  
100     public static native String getContextPath() /*-{
101         return $wnd.location.protocol + "//"+ $wnd.location.host + $wnd.contextPath + "/"
102     }-*/;
103 
104     public static boolean isNotEmpty(final String string) {
105         return !isEmpty(string);
106     }
107 
108     public static boolean isEmpty(final String string) {
109         return string == null || string.length() == 0;
110     }
111 
112     /**
113      * This method will look for the specified key inside a GWT {@link Dictionary} named <code>mgnlGwtMessages</code>. If the key does not exist it will return a string
114      * in the form <code>???missing.key???</code>. The keys looked for must reside in <code>info.magnolia.module.admininterface.messages</code> and
115      * MUST end with the special suffix <code>.js</code> (i.e. <code>my.cool.i18nkey.js</code>).
116      * <p><strong>WARNING: this way of exposing i18n messages to GWT is very likely to change in 5.0</strong>
117      */
118     public static String getI18nMessage(final String key) {
119         try {
120             return dictionary.get(key);
121         } catch(RuntimeException e) {
122             GWT.log("key ["+ key +"] was not found in dictionary", e);
123             return "???" + key + "???";
124         }
125     }
126 
127     /**
128      * A String representing the value for the GWT meta property whose content is <em>locale</em>.
129      * See also <a href='http://code.google.com/webtoolkit/doc/latest/DevGuideI18nLocale.html#LocaleSpecifying'>GWT Dev Guide to i18n</a>
130      */
131     public static String detectCurrentLocale() {
132         String locale = "en";
133         final NodeList<Element> meta = Document.get().getDocumentElement().getElementsByTagName("meta");
134         for (int i = 0; i < meta.getLength(); i++) {
135             MetaElement metaTag = MetaElement.as(meta.getItem(i));
136             if ("gwt:property".equals(metaTag.getName()) && metaTag.getContent().contains("locale")) {
137                 String[] split = metaTag.getContent().split("=");
138                 locale = split.length == 2 ? split[1] : "en";
139                 GWT.log("Detected Locale " + locale);
140                 break;
141             }
142         }
143         return locale;
144     }
145 
146     public static void moveComponent(String idTarget, String idSource, String path) {
147 
148         String pathSource = path+"//"+idSource;
149         String pathTarget = path+"//"+idTarget;
150 
151         UrlBuilder urlBuilder = Window.Location.createUrlBuilder();
152         urlBuilder.removeParameter("mgnlIntercept");
153         urlBuilder.removeParameter("mgnlPathSelected");
154         urlBuilder.removeParameter("mgnlPathSortAbove");
155 
156         urlBuilder.setParameter("mgnlIntercept", "NODE_SORT");
157         urlBuilder.setParameter("mgnlPathSelected", pathSource);
158         urlBuilder.setParameter("mgnlPathSortAbove",pathTarget);
159 
160         Window.Location.replace(urlBuilder.buildString());
161 
162     }
163 }