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      /**
97       * Takes care of the mouse up events for selecting elements inside the page editor.
98       * Unfortunately the GWT handlers do not work, so using jsni.
99       */
100     public native void initNativeTouchSelectionListener(Element element, PageEditorView.Listener listener) /*-{
101         if (element != 'undefined') {
102             var ref = this;
103             var that = listener;
104             element.ontouchend = $entry(function (event) {
105                 event = event || $wnd.__page_editor_iframe.contentWindow.event;
106                 var target = event.target || event.srcElement;
107                 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(target);
108             })
109         }
110     }-*/;
111 
112     /**
113      * Takes care of the touch end events for selecting elements inside the page editor.
114      * Unfortunately the GWT handlers do not work, so using jsni.
115      */
116     public native void initNativeMouseSelectionListener(Element iframeElement, Element element, final PageEditorView.Listener listener) /*-{
117         if (element != 'undefined') {
118             var that = listener;
119             element.onmouseup = $entry(function (event) {
120                 event = event || iframeElement.contentWindow.event;
121                 var target = event.target || event.srcElement;
122                 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(target);
123             })
124         }
125     }-*/;
126 
127     /**
128      * Catches key events on the contentDocument of the frame {@link Element} and fires it on the frame to enable event bubbling
129      * from the frame up to the DOM.
130      */
131     public abstract void initNativeKeyListener(Element element);
132 
133     /**
134      * Registers a scroll-event handler on the passed element and updates the scroll position in {@link PageEditorView#setLastScrollPosition(int)}.
135      */
136     public native void initScrollListener(Element element) /*-{
137         if (element != 'undefined') {
138             var that = this;
139             var view = that.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler::view;
140             element.onscroll = $entry(function (event) {
141                 event = event || iframeElement.contentWindow.event;
142                 var target = event.target || event.srcElement;
143                 var scrollTop = target.scrollTop;
144                 if (scrollTop > 0) {
145                     view.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView::setLastScrollPosition(I)(scrollTop);
146                 }
147             })
148         }
149     }-*/;
150 }