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