View Javadoc
1   /**
2    * This file Copyright (c) 2012-2016 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 PageEditorFrame iframe = new PageEditorFrame();
60  
61      private String url;
62  
63      private SimplePanel content;
64  
65      private AbstractFrameEventHandler handler;
66      private int lastScrollPosition;
67  
68      public PageEditorViewImpl(EventBus eventBus) {
69          super();
70          this.handler = GWT.create(AbstractFrameEventHandler.class);
71          this.content = new SimplePanel();
72          handler.setView(this);
73          handler.setEventBus(eventBus);
74          content.setWidget(iframe);
75          initWidget(content);
76          setStyleName(PAGE_EDITOR_CLASS_NAME);
77  
78          final Element iframeElement = iframe.getElement();
79          iframeElement.setAttribute("width", "100%");
80          iframeElement.setAttribute("height", "100%");
81          iframeElement.setAttribute("allowTransparency", "true");
82          iframeElement.setAttribute("frameborder", "0");
83  
84          iframe.addLoadHandler(new LoadHandler() {
85  
86              @Override
87              public void onLoad(LoadEvent event) {
88                  iframe.removeStyleName("iframe-preloader");
89              }
90          });
91  
92          handler.init();
93          if (BrowserInfo.get().isIE8()) {
94              registerPageEditorIframe(iframeElement);
95          }
96      }
97  
98      private native void registerPageEditorIframe(Element iframeElement) /*-{
99          $wnd.__page_editor_iframe = iframeElement;
100     }-*/;
101 
102     @Override
103     public PageEditorFrame getFrame() {
104         return iframe;
105     }
106 
107     @Override
108     public Widget getContent() {
109         return content;
110     }
111 
112     @Override
113     public void setListener(Listener listener) {
114         this.listener = listener;
115     }
116 
117     @Override
118     public void setUrl(String url) {
119         getFrame().setUrl(url);
120         this.url = url;
121     }
122 
123     @Override
124     public void reload() {
125         handler.reloadIFrame(iframe.getElement());
126     }
127 
128     /**
129      * The position is updated by {@link AbstractFrameEventHandler#initScrollListener(Element)} using a scroll event handler.
130      */
131     @Override
132     public void setLastScrollPosition(int lastScrollPosition) {
133         this.lastScrollPosition = lastScrollPosition;
134     }
135 
136     /**
137      * 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
138      * defects resetting the scroll position to '0' when selecting elements (components/areas) in the page.
139      */
140     @Override
141     public void resetScrollTop() {
142         if (BrowserInfo.get().isTouchDevice()) {
143             getFrame().getElement().getStyle().setHeight(getFrame().getBody().getOffsetHeight(), Style.Unit.PX);
144             new ComputedStyle(getFrame().getElement());
145         }
146         content.getElement().setScrollTop(lastScrollPosition);
147     }
148 
149     @Override
150     public void initDomEventListeners() {
151         if (BrowserInfo.get().isTouchDevice()) {
152             handler.initNativeTouchSelectionListener(iframe.getBody(), listener);
153         } else {
154             handler.initNativeMouseSelectionListener(iframe.getElement(), iframe.getBody(), listener);
155         }
156         handler.initNativeKeyListener(iframe.getElement());
157         handler.initScrollListener(content.getElement());
158     }
159 
160     @Override
161     public void initKeyEventListeners() {
162         handler.initNativeKeyListener(iframe.getElement());
163     }
164 
165 }