1 /** 2 * This file Copyright (c) 2012-2015 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.core.client.GWT; 39 import com.google.gwt.dom.client.Document; 40 import com.google.gwt.dom.client.Element; 41 import com.google.gwt.dom.client.NativeEvent; 42 import com.google.gwt.event.dom.client.DomEvent; 43 import com.google.gwt.event.dom.client.LoadEvent; 44 import com.google.gwt.event.dom.client.LoadHandler; 45 import com.google.gwt.user.client.Timer; 46 import com.google.gwt.user.client.ui.Frame; 47 48 /** 49 * WebkitFrameEventHandler. Provides separated implementations to overcome different bugs in the 50 * handling of iframes on webkit browsers including the iPad. 51 * See SCRUM-1593 for details. 52 */ 53 public class WebkitFrameEventHandler extends AbstractFrameEventHandler { 54 55 private MyTimer timer = new MyTimer(); 56 private boolean loaded = false; 57 private int touchStartY = 0; 58 59 @Override 60 public void init() { 61 registerLoadHandler(); 62 } 63 64 @Override 65 public void onFrameReady() { 66 super.onFrameReady(); 67 timer.cancel(); 68 registerUnloadHandler(getView().getFrame().getElement(), timer); 69 } 70 71 /** 72 * Registers two separated implementations of an onload event. 73 * In case the onload is not triggered it's supposed to fallback on a readystate poller. 74 * We have to make sure, that they don't interfere by using a loaded boolean. 75 */ 76 public void registerLoadHandler() { 77 Frame frame = getView().getFrame(); 78 79 frame.addLoadHandler(new LoadHandler() { 80 @Override 81 public void onLoad(LoadEvent event) { 82 if (loaded) { 83 return; 84 } 85 loaded = true; 86 onFrameReady(); 87 } 88 }); 89 timer.setIframe(frame); 90 timer.scheduleRepeating(100); 91 92 } 93 94 @Override 95 public void notifyUrlChange() { 96 loaded = false; 97 timer.scheduleRepeating(100); 98 } 99 100 /** 101 * This function is supposed to trigger an unload event, when the page inside the Iframe is changed. 102 * Doesn't work, that's why browsing inside the iframe is broken for webkit. 103 */ 104 public native void registerUnloadHandler(Element element, Timer timer) /*-{ 105 var that = timer; 106 var poll = $entry(function() { 107 that.@com.google.gwt.user.client.Timer::scheduleRepeating(I)(100); 108 }); 109 element.contentWindow.addEventListener('unload', poll, false); 110 }-*/; 111 112 /** 113 * Custom implementation for iPads of the touch end handling. Suppresses the selection, when scrolling. 114 */ 115 @Override 116 public native void initNativeTouchSelectionListener(Element element, PageEditorView.Listener listener) /*-{ 117 if (element != 'undefined') { 118 var ref = this; 119 var that = listener; 120 var wndRef = $wnd; 121 element.ontouchend = $entry(function (event) { 122 var touchEndY = event.changedTouches[0].pageY; 123 var touchStartY = ref.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.WebkitFrameEventHandler::touchStartY; 124 if (Math.abs(touchEndY - touchStartY) < 5) { 125 that.@info.magnolia.ui.vaadin.gwt.client.widget.PageEditorView.Listener::selectElement(Lcom/google/gwt/dom/client/Element;)(event.target); 126 } 127 }) 128 129 element.ontouchstart = $entry(function (event) { 130 ref.@info.magnolia.ui.vaadin.gwt.client.editor.jsni.WebkitFrameEventHandler::touchStartY = event.targetTouches[0].pageY; 131 }) 132 133 } 134 }-*/; 135 136 @Override 137 public native void initNativeKeyListener(Element element) /*-{ 138 if (element != 'undefined') { 139 element.onkeydown = function(event) { 140 var eventObj = document.createEventObject ? 141 document.createEventObject() : document.createEvent("Events"); 142 143 if(eventObj.initEvent){ 144 eventObj.initEvent("keydown", true, true); 145 } 146 147 eventObj.keyCode = event.keyCode; 148 eventObj.which = event.keyCode; 149 150 element.dispatchEvent ? element.dispatchEvent(eventObj) : element.fireEvent("onkeydown", eventObj); 151 152 element.dispatchEvent(event); 153 } 154 } 155 }-*/; 156 157 /** 158 * Poller to check the readystate of the contentdocument in the iframe. 159 */ 160 private class MyTimer extends Timer { 161 162 Frame iframe; 163 164 @Override 165 public void run() { 166 GWT.log("timer started"); 167 String readyState = getReadyState(iframe.getElement()); 168 169 if (readyState != null && !readyState.isEmpty()) { 170 GWT.log("doc.readyState" + readyState); 171 } 172 173 if ("interactive".equals(readyState)) { 174 NativeEvent event = Document.get().createLoadEvent(); 175 DomEvent.fireNativeEvent(event, iframe); 176 } 177 } 178 179 public void setIframe(Frame iframe) { 180 this.iframe = iframe; 181 } 182 183 public final native String getReadyState(Element element) /*-{ 184 if (element.contentWindow != null) { 185 return element.contentWindow.document.readyState; 186 } 187 return ''; 188 }-*/; 189 } 190 }