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