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.ui.vaadin.gwt.client.editor.jsni;
35  
36  
37  import info.magnolia.ui.vaadin.gwt.client.editor.i18n.DummyDictionary;
38  
39  import com.google.gwt.core.client.GWT;
40  import com.google.gwt.dom.client.Document;
41  import com.google.gwt.dom.client.Element;
42  import com.google.gwt.dom.client.MetaElement;
43  import com.google.gwt.dom.client.NodeList;
44  import com.google.gwt.http.client.UrlBuilder;
45  import com.google.gwt.user.client.Window;
46  
47  /**
48   * A JSNI wrapper around native javascript functions found in general.js, inline.js and others plus some utilities.
49   */
50  @Deprecated
51  public final class JavascriptUtils {
52  
53      private static DummyDictionary dictionary = new DummyDictionary();
54  
55      private static String windowLocation;
56  
57      public static String getWindowLocation() {
58          return windowLocation;
59      }
60  
61      public static void setWindowLocation(String windowLocation) {
62          JavascriptUtils.windowLocation = windowLocation;
63      }
64  
65      private final static String EDITOR_COOKIE_PATH = "/";
66      private final static String EDITOR_COOKIE_NAMESPACE = "editor-";
67      private final static String editorPositionCookieName = EDITOR_COOKIE_NAMESPACE + "position-";
68      private final static String editorContentIdCookieName = EDITOR_COOKIE_NAMESPACE + "content-id-";
69  
70      static {
71          //TODO these calls raise an error
72          //JavascriptUtils.exposeMgnlMessagesToGwtDictionary("info.magnolia.module.admininterface.messages");
73          //dictionary = Dictionary.getDictionary("mgnlGwtMessages");
74      }
75  
76      private JavascriptUtils() {
77          //do not instantiate it.
78      }
79  
80      public static native void mgnlOpenDialog(String path, String collectionName, String nodeName, String paragraph, String workspace, String dialogPage, String width, String height, String locale) /*-{
81          $wnd.mgnlOpenDialog(path, collectionName, nodeName, paragraph, workspace, dialogPage, width, height, locale);
82      }-*/;
83  
84      public static native void mgnlMoveNodeStart(String id) /*-{
85          $wnd.mgnlMoveNodeStart('',id,'__'+id);
86      }-*/;
87  
88      public static native void mgnlMoveNodeHigh(Object source) /*-{
89          $wnd.mgnlMoveNodeHigh(source);
90      }-*/;
91  
92      public static native void mgnlMoveNodeEnd(Object source, String path) /*-{
93          $wnd.mgnlMoveNodeEnd(source, path);
94      }-*/;
95  
96      public static native void mgnlMoveNodeReset(Object source) /*-{
97          $wnd.mgnlMoveNodeReset(source);
98      }-*/;
99  
100     public static native void mgnlDeleteNode(String path) /*-{
101         $wnd.mgnlDeleteNode(path,'', '');
102     }-*/;
103 
104     /**
105      * Exposes the messages object corresponding to the passed in basename as a global (thus accessible by GWT Dictionary) variable named <em>mgnlGwtMessages</em>.
106      */
107     public static native void exposeMgnlMessagesToGwtDictionary(String basename) /*-{
108         $wnd.mgnlGwtMessages = $wnd.mgnlMessages.messages[basename];
109     }-*/;
110 
111     public static native void showTree(String workspace, String path) /*-{
112         $wnd.MgnlAdminCentral.showTree(workspace, path)
113     }-*/;
114 
115     public static native String getContextPath() /*-{
116         return $wnd.location.protocol + "//"+ $wnd.location.host + $wnd.contextPath + "/"
117     }-*/;
118 
119     public static boolean isNotEmpty(final String string) {
120         return !isEmpty(string);
121     }
122 
123     public static boolean isEmpty(final String string) {
124         return string == null || string.length() == 0;
125     }
126 
127     /**
128      * 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
129      * in the form <code>???missing.key???</code>. The keys looked for must reside in <code>info.magnolia.module.admininterface.messages</code> and
130      * MUST end with the special suffix <code>.js</code> (i.e. <code>my.cool.i18nkey.js</code>).
131      * <p><strong>WARNING: this way of exposing i18n messages to GWT is very likely to change in 5.0</strong>
132      */
133     public static String getI18nMessage(final String key) {
134         return dictionary.get(key);
135     }
136 
137     /**
138      * A String representing the value for the GWT meta property whose content is <em>locale</em>.
139      * See also <a href='http://code.google.com/webtoolkit/doc/latest/DevGuideI18nLocale.html#LocaleSpecifying'>GWT Dev Guide to i18n</a>
140      */
141     public static String detectCurrentLocale() {
142         String locale = "en";
143         final NodeList<Element> meta = Document.get().getDocumentElement().getElementsByTagName("meta");
144         for (int i = 0; i < meta.getLength(); i++) {
145             MetaElement metaTag = MetaElement.as(meta.getItem(i));
146             if ("gwt:property".equals(metaTag.getName()) && metaTag.getContent().contains("locale")) {
147                 String[] split = metaTag.getContent().split("=");
148                 locale = split.length == 2 ? split[1] : "en";
149                 GWT.log("Detected Locale " + locale);
150                 break;
151             }
152         }
153         return locale;
154     }
155 
156     public static void moveComponent(String idTarget, String idSource, String path, String order) {
157         if (isEmpty(idTarget) || isEmpty(idSource) || isEmpty(path) || idTarget.equals(idSource)) {
158             return;
159         }
160         String pathSource = path + "//" + idSource;
161         String pathTarget = path + "//" + idTarget;
162 
163         UrlBuilder urlBuilder = Window.Location.createUrlBuilder();
164         urlBuilder.removeParameter("mgnlIntercept");
165         urlBuilder.removeParameter("mgnlPathSelected");
166         urlBuilder.removeParameter("mgnlPathTarget");
167         urlBuilder.removeParameter("order");
168 
169         urlBuilder.setParameter("mgnlIntercept", "NODE_SORT");
170         urlBuilder.setParameter("mgnlPathSelected", pathSource);
171         urlBuilder.setParameter("mgnlPathTarget", pathTarget);
172         urlBuilder.setParameter("order", order);
173 
174         Window.Location.replace(urlBuilder.buildString());
175 
176     }
177 
178 
179     /**
180      * Removes all editor cookies not belonging to this page/module. Each time we navigate to a different link within the webapp
181      * the GWT module is unloaded and then reloaded, we need a clean slate when accessing cookies on a given page to avoid weird
182      * behaviour, i.e. page scrolling to last saved position when you come back to it after having been on a different page.
183      */
184 /*    public static void resetEditorCookies() {
185         for(String cookie : Cookies.getCookieNames()) {
186             if(cookie.startsWith(EDITOR_COOKIE_NAMESPACE) && !(getEditorContentIdUniqueCookieName().equals(cookie)
187                     || getEditorPositionUniqueCookieName().equals(cookie))) {
188                 Cookies.removeCookie(cookie, EDITOR_COOKIE_PATH);
189             }
190         }
191     }
192 
193     public static void setEditorPositionCookie(String value) {
194         Cookies.setCookie(getEditorPositionUniqueCookieName(), value, null, null, EDITOR_COOKIE_PATH, false);
195     }
196 
197     private static String getEditorPositionUniqueCookieName() {
198         return editorPositionCookieName + getWindowLocation();
199     }
200 
201     public static void setEditorContentIdCookie(String value) {
202         Cookies.setCookie(getEditorContentIdUniqueCookieName(), value, null, null, EDITOR_COOKIE_PATH, false);
203     }
204 
205     private static String getEditorContentIdUniqueCookieName() {
206         return editorContentIdCookieName + getWindowLocation();
207     }
208 
209     public static void getCookiePosition() {
210         String position = Cookies.getCookie(getEditorPositionUniqueCookieName());
211         if(position!=null){
212             String[] tokens = position.split(":");
213             int left = Integer.parseInt(tokens[0]);
214             int top = Integer.parseInt(tokens[1]);
215             GWT.log("Scrolling to position left:" + left +", top: "+ top);
216             Window.scrollTo(left, top);
217         }
218     }
219 
220     public static void getCookieContentId() {
221         String contentId = Cookies.getCookie(getEditorContentIdUniqueCookieName());
222         MgnlElement selectedMgnlElement = null;
223         if(contentId != null) {
224             selectedMgnlElement = VPageEditor.getModel().findMgnlElementByContentId(contentId);
225         }
226         if(selectedMgnlElement != null) {
227             VPageEditor.getModel().getFocusModel().onLoadSelect(selectedMgnlElement);
228         }
229         else {
230             VPageEditor.getModel().getFocusModel().reset();
231         }
232     }
233 
234     public static void removeEditorContentIdCookie() {
235         Cookies.removeCookie(getEditorContentIdUniqueCookieName(), EDITOR_COOKIE_PATH);
236     }
237 
238     public static void removeEditorPositionCookie() {
239         Cookies.removeCookie(getEditorPositionUniqueCookieName(), EDITOR_COOKIE_PATH);
240     }*/
241 }