View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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  import info.magnolia.ui.vaadin.gwt.client.editor.jsni.event.FrameLoadedEvent;
37  import info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView;
38  
39  import com.google.gwt.dom.client.Element;
40  import com.google.web.bindery.event.shared.EventBus;
41  
42  /**
43   * AbstractFrameEventHandler.
44   */
45  abstract public class AbstractFrameEventHandler {
46  
47  
48      private EventBus eventBus;
49  
50      private PageEditorView view;
51  
52  
53      /**
54       * Force iframe to be reloaded. for example when content has been updated.
55       */
56      public native void reloadIFrame(Element iframeElement) /*-{
57          iframeElement.contentWindow.location.reload();
58      }-*/;
59  
60  
61      public abstract void notifyUrlChange();
62  
63      /**
64       * This functionality was added in 4.5. Not triggered at the moment.
65       */
66      private native void onPageEditorReady() /*-{
67          var callbacks = $wnd.mgnl.PageEditor.onPageEditorReadyCallbacks
68          if(typeof callbacks != 'undefined') {
69              for(var i=0; i < callbacks.length; i++) {
70                  callbacks[i].apply();
71              }
72          }
73      }-*/;
74  
75      public EventBus getEventBus() {
76          return eventBus;
77      }
78  
79      public void setEventBus(EventBus eventBus) {
80          this.eventBus = eventBus;
81      }
82  
83      public void setView(PageEditorView view) {
84          this.view = view;
85      }
86  
87      public PageEditorView getView() {
88          return view;
89      }
90  
91      public abstract void init();
92  
93      public void onFrameReady() {
94          eventBus.fireEvent(new FrameLoadedEvent(view.getFrame()));
95      }
96  
97      /**
98       * Takes care of the mouse up events for selecting elements inside the page editor.
99       * Unfortunately the GWT handlers do not work, so using jsni.
100      */
101     public native void initNativeTouchSelectionListener(Element element, PageEditorView.Listener listener) /*-{
102         if (element != 'undefined') {
103             var ref = this;
104             var that = listener;
105             element.contentDocument.ontouchend = function(event) {
106                 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(event.target);
107             }
108         }
109     }-*/;
110 
111     /**
112      * Takes care of the touch end events for selecting elements inside the page editor.
113      * Unfortunately the GWT handlers do not work, so using jsni.
114      */
115     public native void initNativeMouseSelectionListener(Element element, PageEditorView.Listener listener) /*-{
116         if (element != 'undefined') {
117             var that = listener;
118             element.onmouseup = function(event) {
119                 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(event.target);
120             }
121         }
122     }-*/;
123 
124     /**
125      * Catches key events on the contentDocument of the frame {@link Element} and fires it on the frame to enable event bubbling
126      * from the frame up to the DOM.
127      */
128     public abstract void initNativeKeyListener(Element element);
129 
130     public native void initScrollListener(Element element) /*-{
131         if (element != 'undefined') {
132             var that = this;
133             var view = that.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler::view;
134             element.onscroll = function(event) {
135                 var scrollTop = event.target.scrollTop;
136                 if (scrollTop > 0) {
137                     view.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView::setLastScrollPosition(I)(scrollTop);
138                 }
139             }
140         }
141     }-*/;
142 }