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.i18n.client.Dictionary;
43  
44  /**
45   * A JSNI wrapper around native javascript functions found in general.js, inline.js and others plus some utilities.
46   * @version $Id$
47   *
48   */
49  public final class LegacyJavascript {
50  
51      private static Dictionary dictionary;
52  
53      static {
54          //TODO move messages we need to this module?
55          LegacyJavascript.exposeMgnlMessagesToGwtDictionary("info.magnolia.module.admininterface.messages");
56          dictionary = Dictionary.getDictionary("mgnlGwtMessages");
57      }
58  
59      private LegacyJavascript() {
60          //do not instantiate it.
61      }
62  
63      public static native void mgnlOpenDialog(String path, String collectionName, String nodeName, String paragraph, String workspace, String dialogPage, String width, String height, String locale) /*-{
64          $wnd.mgnlOpenDialog(path, collectionName, nodeName, paragraph, workspace, dialogPage, width, height, locale);
65      }-*/;
66  
67      public static native void mgnlMoveNodeStart(String id) /*-{
68          $wnd.mgnlMoveNodeStart('',id,'__'+id);
69      }-*/;
70  
71      public static native void mgnlMoveNodeHigh(Object source) /*-{
72          $wnd.mgnlMoveNodeHigh(source);
73      }-*/;
74  
75      public static native void mgnlMoveNodeEnd(Object source, String path) /*-{
76          $wnd.mgnlMoveNodeEnd(source, path);
77      }-*/;
78  
79      public static native void mgnlMoveNodeReset(Object source) /*-{
80          $wnd.mgnlMoveNodeReset(source);
81      }-*/;
82  
83      public static native void mgnlDeleteNode(String path) /*-{
84          $wnd.mgnlDeleteNode(path,'', '');
85      }-*/;
86  
87      /**
88       * Exposes the messages object corresponding to the passed in basename as a global (thus accessible by GWT Dictionary) variable named <em>mgnlGwtMessages</em>.
89       */
90      public static native void exposeMgnlMessagesToGwtDictionary(String basename) /*-{
91          $wnd.mgnlGwtMessages = $wnd.mgnlMessages.messages[basename];
92      }-*/;
93  
94      public static native void showTree(String workspace, String path) /*-{
95          $wnd.MgnlAdminCentral.showTree(workspace, path)
96      }-*/;
97  
98      public static native void mgnlPreview(boolean setPreview) /*-{
99          $wnd.mgnlPreview(setPreview)
100     }-*/;
101 
102     /**
103      * @return <code>true</code> if the current page is in preview mode, <code>false</code> otherwise.
104      */
105     public static native boolean isPreviewMode() /*-{
106         return $wnd.location.href.indexOf('mgnlPreview=true') != -1;
107     }-*/;
108 
109     public static native String getContextPath() /*-{
110         return $wnd.location.protocol + "//"+ $wnd.location.host + $wnd.contextPath + "/"
111     }-*/;
112 
113     public static boolean isNotEmpty(final String string) {
114         return !isEmpty(string);
115     }
116 
117     public static boolean isEmpty(final String string) {
118         return string == null || string.length() == 0;
119     }
120 
121     /**
122      * This method will look for the specified key inside a GWT {@link Dictionary}. If the key does not exist it will return a string
123      * in the form <code>???missing.key???</code>. The keys looked for must reside in <code>info.magnolia.module.admininterface.messages</code> and
124      * MUST end with the special suffix <code>.js</code> (i.e. <code>my.cool.i18nkey.js</code>).
125      * <p><strong>WARNING: this way of exposing i18n messages to GWT is very likely to change in 5.0</strong>
126      */
127     public static String getI18nMessage(final String key) {
128         try {
129             return dictionary.get(key);
130         } catch(RuntimeException e) {
131             GWT.log("key ["+ key +"] was not found in dictionary", e);
132             return "???" + key + "???";
133         }
134     }
135 
136     /**
137      * A String representing the value for the GWT meta property whose content is <em>locale</em>.
138      * See also <a href='http://code.google.com/webtoolkit/doc/latest/DevGuideI18nLocale.html#LocaleSpecifying'>GWT Dev Guide to i18n</a>
139      */
140     public static String detectCurrentLocale() {
141         String locale = "en";
142         final NodeList<Element> meta = Document.get().getDocumentElement().getElementsByTagName("meta");
143         for (int i = 0; i < meta.getLength(); i++) {
144             MetaElement metaTag = MetaElement.as(meta.getItem(i));
145             if ("gwt:property".equals(metaTag.getName()) && metaTag.getContent().contains("locale")) {
146                 String[] split = metaTag.getContent().split("=");
147                 locale = split.length == 2 ? split[1] : "en";
148                 GWT.log("Detected Locale " + locale);
149                 break;
150             }
151         }
152         return locale;
153     }
154 }