View Javadoc
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.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.user.client.ui.Composite;
42  import com.google.gwt.user.client.ui.SimplePanel;
43  import com.google.gwt.user.client.ui.Widget;
44  import com.google.web.bindery.event.shared.EventBus;
45  import com.vaadin.client.BrowserInfo;
46  import com.vaadin.client.ComputedStyle;
47  
48  /**
49   * GWT implementation of MagnoliaShell client side (the view part basically).
50   */
51  public class PageEditorViewImpl extends Composite implements PageEditorView {
52  
53      public static final String PAGE_EDITOR_CLASS_NAME = "pageEditor";
54  
55      private Listener listener;
56  
57      private PageEditorFrame iframe = new PageEditorFrame();
58  
59      private String url;
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          handler.init();
83          if (BrowserInfo.get().isIE8()) {
84              registerPageEditorIframe(iframeElement);
85          }
86      }
87  
88      private native void registerPageEditorIframe(Element iframeElement) /*-{
89          $wnd.__page_editor_iframe = iframeElement;
90      }-*/;
91  
92      @Override
93      public PageEditorFrame getFrame() {
94          return iframe;
95      }
96  
97      @Override
98      public Widget getContent() {
99          return content;
100     }
101 
102     @Override
103     public void setListener(Listener listener) {
104         this.listener = listener;
105     }
106 
107     @Override
108     public void setUrl(String url) {
109         getFrame().setUrl(url);
110         this.url = url;
111     }
112 
113     @Override
114     public void reload() {
115         handler.reloadIFrame(iframe.getElement());
116     }
117 
118     @Override
119     public void setLastScrollPosition(int lastScrollPosition) {
120         this.lastScrollPosition = lastScrollPosition;
121     }
122 
123     @Override
124     public void resetScrollTop() {
125         if (BrowserInfo.get().isTouchDevice()) {
126             getFrame().getElement().getStyle().setHeight(getFrame().getBody().getOffsetHeight(), Style.Unit.PX);
127             new ComputedStyle(getFrame().getElement());
128         }
129         content.getElement().setScrollTop(lastScrollPosition);
130     }
131 
132     @Override
133     public void initDomEventListeners() {
134         if (BrowserInfo.get().isTouchDevice()) {
135             handler.initNativeTouchSelectionListener(iframe.getBody(), listener);
136         } else {
137             handler.initNativeMouseSelectionListener(iframe.getElement(), iframe.getBody(), listener);
138         }
139         handler.initNativeKeyListener(iframe.getElement());
140         handler.initScrollListener(content.getElement());
141     }
142 
143     @Override
144     public void initKeyEventListeners() {
145         handler.initNativeKeyListener(iframe.getElement());
146     }
147 
148 }