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.widget;
35  
36  import info.magnolia.ui.vaadin.gwt.client.editor.jsni.AbstractFrameEventHandler;
37  
38  import com.google.gwt.core.client.GWT;
39  import com.google.gwt.dom.client.Element;
40  import com.google.gwt.dom.client.Style;
41  import com.google.gwt.event.dom.client.LoadEvent;
42  import com.google.gwt.event.dom.client.LoadHandler;
43  import com.google.gwt.user.client.ui.Composite;
44  import com.google.gwt.user.client.ui.SimplePanel;
45  import com.google.gwt.user.client.ui.Widget;
46  import com.google.web.bindery.event.shared.EventBus;
47  import com.vaadin.client.BrowserInfo;
48  import com.vaadin.client.ComputedStyle;
49  
50  /**
51   * GWT implementation of MagnoliaShell client side (the view part basically).
52   */
53  public class PageEditorViewImpl extends Composite implements PageEditorView {
54  
55      public static final String PAGE_EDITOR_CLASS_NAME = "pageEditor";
56  
57      private Listener listener;
58  
59      private PageEditorFrameclient/widget/PageEditorFrame.html#PageEditorFrame">PageEditorFrame iframe = new PageEditorFrame();
60  
61      private SimplePanel content;
62  
63      private AbstractFrameEventHandler handler;
64      private int lastScrollPosition;
65  
66      public PageEditorViewImpl(EventBus eventBus) {
67          super();
68          this.handler = GWT.create(AbstractFrameEventHandler.class);
69          this.content = new SimplePanel();
70          handler.setView(this);
71          handler.setEventBus(eventBus);
72          content.setWidget(iframe);
73          initWidget(content);
74          setStyleName(PAGE_EDITOR_CLASS_NAME);
75  
76          final Element iframeElement = iframe.getElement();
77          iframeElement.setAttribute("width", "100%");
78          iframeElement.setAttribute("height", "100%");
79          iframeElement.setAttribute("allowTransparency", "true");
80          iframeElement.setAttribute("frameborder", "0");
81  
82          iframe.addLoadHandler(new LoadHandler() {
83  
84              @Override
85              public void onLoad(LoadEvent event) {
86                  iframe.removeStyleName("iframe-preloader");
87              }
88          });
89  
90          handler.init();
91          if (BrowserInfo.get().isIE8()) {
92              registerPageEditorIframe(iframeElement);
93          }
94      }
95  
96      private native void registerPageEditorIframe(Element iframeElement) /*-{
97          $wnd.__page_editor_iframe = iframeElement;
98      }-*/;
99  
100     @Override
101     public PageEditorFrame getFrame() {
102         return iframe;
103     }
104 
105     @Override
106     public Widget getContent() {
107         return content;
108     }
109 
110     @Override
111     public void setListener(Listener listener) {
112         this.listener = listener;
113     }
114 
115     @Override
116     public void setUrl(String url) {
117         getFrame().setUrl(url);
118     }
119 
120     @Override
121     public void reload() {
122         handler.reloadIFrame(iframe.getElement());
123     }
124 
125     /**
126      * The position is updated by {@link AbstractFrameEventHandler#initScrollListener(Element)} using a scroll event handler.
127      */
128     @Override
129     public void setLastScrollPosition(int lastScrollPosition) {
130         this.lastScrollPosition = lastScrollPosition;
131     }
132 
133     /**
134      * Resets the scroll-position to the latest known one. This is used to keep the position between page changes and also to work-around some browser
135      * defects resetting the scroll position to '0' when selecting elements (components/areas) in the page.
136      */
137     @Override
138     public void resetScrollTop() {
139         //iOS only workaround for scrolling page inside the editor
140         if (BrowserInfo.get().isIOS()) {
141             getFrame().getElement().getStyle().setHeight(getFrame().getBody().getOffsetHeight(), Style.Unit.PX);
142             new ComputedStyle(getFrame().getElement());
143         }
144         content.getElement().setScrollTop(lastScrollPosition);
145     }
146 
147     @Override
148     public void initDomEventListeners() {
149         if (BrowserInfo.get().isTouchDevice()) {
150             handler.initNativeTouchSelectionListener(iframe.getBody(), listener);
151         }
152         handler.initNativeMouseSelectionListener(iframe.getElement(), iframe.getBody(), listener);
153 
154         handler.initNativeKeyListener(iframe.getElement());
155         handler.initScrollListener(content.getElement());
156         handler.initRefreshHandler(iframe.getElement());
157     }
158 
159     @Override
160     public void initKeyEventListeners() {
161         handler.initNativeKeyListener(iframe.getElement());
162     }
163 }