View Javadoc
1   /**
2    * This file Copyright (c) 2012-2019 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.editor.jsni.event.RefreshEvent;
38  import info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView;
39  
40  import com.google.gwt.dom.client.Element;
41  import com.google.web.bindery.event.shared.EventBus;
42  
43  /**
44   * Common functionality used by all browsers to handle frame events.
45   */
46  abstract public class AbstractFrameEventHandler {
47  
48      private EventBus eventBus;
49      private PageEditorView view;
50  
51      public void init() {
52          registerLoadHandler();
53      }
54  
55      protected abstract void registerLoadHandler();
56  
57      /**
58       * Force iframe to be reloaded. for example when content has been updated.
59       */
60      public native void reloadIFrame(Element iframeElement) /*-{
61          iframeElement.contentWindow.location.reload();
62      }-*/;
63  
64  
65      public EventBus getEventBus() {
66          return eventBus;
67      }
68  
69      public void setEventBus(EventBus eventBus) {
70          this.eventBus = eventBus;
71      }
72  
73      public void setView(PageEditorView view) {
74          this.view = view;
75      }
76  
77      public PageEditorView getView() {
78          return view;
79      }
80  
81      public void onFrameReady() {
82          eventBus.fireEvent(new FrameLoadedEvent(view.getFrame()));
83      }
84  
85      public void onRefresh() {
86          eventBus.fireEvent(new RefreshEvent());
87      }
88  
89      public native void initRefreshHandler(Element iframe) /*-{
90          var that = this;
91          $wnd.mgnlRefresh = $entry(function () {
92              that.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler::onRefresh()()
93          });
94      }-*/;
95  
96      public native void initFrameReadyHandler(Element iframe) /*-{
97          var that = this;
98          $wnd.mgnlFrameReady = $entry(function () {
99              that.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler::onFrameReady()()
100         });
101     }-*/;
102 
103     /**
104      * Takes care of the mouse up events for selecting elements inside the page editor.
105      * Unfortunately the GWT handlers do not work, so using jsni.
106      */
107     public native void initNativeTouchSelectionListener(Element element, PageEditorView.Listener listener) /*-{
108         if (element != 'undefined') {
109             var ref = this;
110             var that = listener;
111             element.ontouchend = $entry(function (event) {
112                 event = event || $wnd.__page_editor_iframe.contentWindow.event;
113                 var target = event.target || event.srcElement;
114                 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(target);
115             })
116         }
117     }-*/;
118 
119     /**
120      * Takes care of the touch end events for selecting elements inside the page editor.
121      * Unfortunately the GWT handlers do not work, so using jsni.
122      */
123     public native void initNativeMouseSelectionListener(Element iframeElement, Element element, final PageEditorView.Listener listener) /*-{
124         if (element != 'undefined') {
125             var that = listener;
126             element.onmouseup = $entry(function (event) {
127                 event = event || iframeElement.contentWindow.event;
128                 var target = event.target || event.srcElement;
129                 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(target);
130             })
131         }
132     }-*/;
133 
134     /**
135      * Catches key events on the contentDocument of the frame {@link Element} and fires it on the frame to enable event bubbling
136      * from the frame up to the DOM.
137      */
138     public abstract void initNativeKeyListener(Element element);
139 
140     /**
141      * Registers a scroll-event handler on the passed element and updates the scroll position in {@link PageEditorView#setLastScrollPosition(int)}.
142      */
143     public native void initScrollListener(Element element) /*-{
144         if (element != 'undefined') {
145             var that = this;
146             var view = that.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler::view;
147             element.onscroll = $entry(function (event) {
148                 event = event || iframeElement.contentWindow.event;
149                 var target = event.target || event.srcElement;
150                 var scrollTop = target.scrollTop;
151                 if (scrollTop > 0) {
152                     view.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView::setLastScrollPosition(I)(scrollTop);
153                 }
154             })
155         }
156     }-*/;
157 }