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          handler.initFrameReadyHandler(iframeElement);
92          if (BrowserInfo.get().isIE8()) {
93              registerPageEditorIframe(iframeElement);
94          }
95      }
96  
97      private native void registerPageEditorIframe(Element iframeElement) /*-{
98          $wnd.__page_editor_iframe = iframeElement;
99      }-*/;
100 
101     @Override
102     public PageEditorFrame getFrame() {
103         return iframe;
104     }
105 
106     @Override
107     public Widget getContent() {
108         return content;
109     }
110 
111     @Override
112     public void setListener(Listener listener) {
113         this.listener = listener;
114     }
115 
116     @Override
117     public void setUrl(String url) {
118         getFrame().setUrl(url);
119     }
120 
121     @Override
122     public void reload() {
123         handler.reloadIFrame(iframe.getElement());
124     }
125 
126     /**
127      * The position is updated by {@link AbstractFrameEventHandler#initScrollListener(Element)} using a scroll event handler.
128      */
129     @Override
130     public void setLastScrollPosition(int lastScrollPosition) {
131         this.lastScrollPosition = lastScrollPosition;
132     }
133 
134     /**
135      * 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
136      * defects resetting the scroll position to '0' when selecting elements (components/areas) in the page.
137      */
138     @Override
139     public void resetScrollTop() {
140         //iOS only workaround for scrolling page inside the editor
141         if (BrowserInfo.get().isIOS()) {
142             getFrame().getElement().getStyle().setHeight(getFrame().getBody().getOffsetHeight(), Style.Unit.PX);
143             new ComputedStyle(getFrame().getElement());
144         }
145         content.getElement().setScrollTop(lastScrollPosition);
146     }
147 
148     @Override
149     public void initDomEventListeners() {
150         if (BrowserInfo.get().isTouchDevice()) {
151             handler.initNativeTouchSelectionListener(iframe.getBody(), listener);
152         }
153         handler.initNativeMouseSelectionListener(iframe.getElement(), iframe.getBody(), listener);
154 
155         handler.initNativeKeyListener(iframe.getElement());
156         handler.initScrollListener(content.getElement());
157         handler.initRefreshHandler(iframe.getElement());
158     }
159 
160     @Override
161     public void initKeyEventListeners() {
162         handler.initNativeKeyListener(iframe.getElement());
163     }
164 }