View Javadoc

1   /**
2    * This file Copyright (c) 2012-2014 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.magnoliashell.viewport;
35  
36  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryCallback;
37  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper;
38  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.FadeAnimation;
39  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.SlideAnimation;
40  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.widget.ShellAppsViewportWidget;
41  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.widget.ViewportWidget;
42  
43  import com.google.gwt.dom.client.Style;
44  import com.google.gwt.user.client.Element;
45  import com.google.gwt.user.client.ui.Widget;
46  import com.vaadin.client.Util;
47  
48  /**
49   * The ShellAppsTransitionDelegate provides custom transition logic when activating viewport or a
50   * specific app. It also defines its own slide and fade transitions (not those from JQueryWrapper)
51   * because it might animate other CSS properties and can then result in CSS3 transitions through the
52   * jquery.animate-enhanced.min.js plugin.
53   */
54  public class ShellAppsTransitionDelegate implements TransitionDelegate {
55  
56      private final static int SLIDE_DURATION = 600;
57  
58      private final static int FADE_DURATION = 600;
59  
60      private final static int ALPHA_MIN = 0;
61  
62      private final static int ALPHA_MAX = 1;
63  
64      private SlideAnimation slideUpAnimation;
65  
66      private SlideAnimation slideDownAnimation;
67  
68      private FadeAnimation fadeOutAnimation;
69  
70      private FadeAnimation fadeInAnimation;
71  
72      private ShellAppsViewportWidget viewport;
73  
74      public ShellAppsTransitionDelegate(final ShellAppsViewportWidget viewport) {
75          this.viewport = viewport;
76          initAnimations();
77      }
78  
79      private void initAnimations() {
80          this.fadeInAnimation = new FadeAnimation(ALPHA_MAX, false) {
81              @Override
82              protected void onStart() {
83                  super.onStart();
84                  Style style = getCurrentElement().getStyle();
85                  String currentOpacity = style.getOpacity();
86                  if (currentOpacity == null || currentOpacity.isEmpty()) {
87                      style.setOpacity(0d);
88                  }
89                  style.clearDisplay();
90              }
91          };
92          this.fadeInAnimation.addCallback(new JQueryCallback() {
93              @Override
94              public void execute(JQueryWrapper query) {
95                  viewport.onShellAppLoaded(Util.<Widget>findWidget(query.get(0), null));
96              }
97          });
98  
99  
100         this.slideUpAnimation = new SlideAnimation(false);
101         this.slideDownAnimation = new SlideAnimation(false);
102 
103         this.slideDownAnimation.addCallback(new JQueryCallback() {
104             @Override
105             public void execute(JQueryWrapper query) {
106                 if (!slideDownAnimation.isCancelled()) {
107                     viewport.onShellAppLoaded(viewport.getVisibleChild());
108                 }
109             }
110         });
111 
112         this.slideUpAnimation.addCallback(new JQueryCallback() {
113             @Override
114             public void execute(JQueryWrapper query) {
115                 if (!slideUpAnimation.isCancelled()) {
116                     viewport.onShellAppsHidden();
117                 }
118             }
119         });
120         this.fadeOutAnimation = new FadeAnimation(ALPHA_MIN, false);
121     }
122 
123     /**
124      * Slides down if active, fades out if inactive - except if the viewport is closing.
125      */
126     @Override
127     public void setActive(final ViewportWidget viewport, boolean active) {
128         final Element viewportElement = viewport.getElement();
129         if (active) {
130             boolean isViewportCurrentlyVisible = viewport.isVisible();
131             viewport.setVisible(true);
132             // The Shell App viewport might not be visible due 'display' value,
133             // but it's top could still be equal to 0.
134             if (!isViewportCurrentlyVisible) {
135                 viewportElement.getStyle().setTop(-viewportElement.getOffsetHeight(), Style.Unit.PX);
136             }
137             slideUpAnimation.cancel();
138             slideDownAnimation.setTargetValue(0);
139             slideDownAnimation.run(SLIDE_DURATION, viewportElement);
140         } else {
141             slideDownAnimation.cancel();
142             slideUpAnimation.setTargetValue(-viewport.getOffsetHeight());
143             slideUpAnimation.run(SLIDE_DURATION, viewportElement);
144         }
145     }
146 
147     /**
148      * Cross-fades between shell apps.
149      */
150     @Override
151     public void setVisibleChild(final ViewportWidget viewport, final Widget visibleChild) {
152         if (viewport.getVisibleChild() == null || !((ShellAppsViewportWidget)viewport).isActive()) {
153             // do not fade if first widget or viewport not active yet
154             viewport.showChildNoTransition(visibleChild);
155         } else {
156             fadeInAnimation.run(FADE_DURATION, visibleChild.getElement());
157             fadeOutAnimation.run(FADE_DURATION, viewport.getVisibleChild().getElement());
158         }
159     }
160 
161     @Override
162     public boolean inProgress() {
163         return fadeInAnimation.isRunning() ||
164                fadeOutAnimation.isRunning() ||
165                slideDownAnimation.isRunning() ||
166                slideUpAnimation.isRunning();
167     }
168 }