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.widget.PageEditorView;
37  
38  import com.google.gwt.dom.client.Element;
39  import com.google.gwt.event.dom.client.LoadEvent;
40  import com.google.gwt.event.dom.client.LoadHandler;
41  import com.google.gwt.user.client.ui.Frame;
42  
43  /**
44   * WebkitFrameEventHandler. Provides separated implementations to overcome different bugs in the
45   * handling of iframes on webkit browsers including the iPad.
46   */
47  public class WebkitFrameEventHandler extends AbstractFrameEventHandler {
48  
49      private int touchStartY = 0;
50  
51      @Override
52      public void registerLoadHandler() {
53          final Frame frame = getView().getFrame();
54  
55          frame.addLoadHandler(new LoadHandler() {
56              @Override
57              public void onLoad(LoadEvent event) {
58                  bindReadyHandler(frame.getElement());
59              }
60          });
61      }
62  
63      /**
64       * Uses jQuery to detect when the iFrame is ready.
65       */
66      public native void bindReadyHandler(Element iframe) /*-{
67          var that = this;
68          $wnd.$(iframe).ready($entry(function () {
69              that.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.WebkitFrameEventHandler::onFrameReady()()
70          }));
71  
72      }-*/;
73  
74  
75      /**
76       * Custom implementation for iPads of the touch end handling. Suppresses the selection, when scrolling.
77       */
78      @Override
79      public native void initNativeTouchSelectionListener(Element element, PageEditorView.Listener listener) /*-{
80          if (element != 'undefined') {
81              var ref = this;
82              var that = listener;
83              var wndRef = $wnd;
84              element.ontouchend = $entry(function (event) {
85                  var touchEndY = event.changedTouches[0].pageY;
86                  var touchStartY = ref.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.WebkitFrameEventHandler::touchStartY;
87                  if (Math.abs(touchEndY - touchStartY) < 5) {
88                      that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(event.target);
89                  }
90              })
91  
92              element.ontouchstart = $entry(function (event) {
93                  ref.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.WebkitFrameEventHandler::touchStartY = event.targetTouches[0].pageY;
94              })
95  
96          }
97      }-*/;
98  
99      @Override
100     public native void initNativeKeyListener(Element element) /*-{
101         if (element != 'undefined') {
102             element.onkeydown = function(event) {
103                 var eventObj = document.createEventObject ?
104                     document.createEventObject() : document.createEvent("Events");
105 
106                 if(eventObj.initEvent){
107                     eventObj.initEvent("keydown", true, true);
108                 }
109 
110                 eventObj.keyCode = event.keyCode;
111                 eventObj.which = event.keyCode;
112 
113                 element.dispatchEvent ? element.dispatchEvent(eventObj) : element.fireEvent("onkeydown", eventObj);
114 
115                 element.dispatchEvent(event);
116             }
117         }
118     }-*/;
119 
120 }