View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.admincentral;
35  
36  import static info.magnolia.admincentral.ViewportLayout.VIEW.*;
37  
38  import com.vaadin.ui.Component;
39  import com.vaadin.ui.CssLayout;
40  import com.vaadin.ui.Layout;
41  import com.vaadin.ui.Panel;
42  import com.vaadin.ui.VerticalLayout;
43  
44  import lombok.Getter;
45  
46  /**
47   * View holding all available views and making transitions between them.
48   */
49  public class ViewportLayout extends CssLayout {
50  
51      private static final String MOVE_DOWN = "viewportLayout-move-down";
52      private static final String MOVE_LEFT = "viewportLayout-move-left";
53  
54      private static final String SLIDE_DOWN = "viewportLayout-slide-down";
55      private static final String SLIDE_UP = "viewportLayout-slide-up";
56      private static final String SLIDE_LEFT = "viewportLayout-slide-left";
57      private static final String SLIDE_RIGHT = "viewportLayout-slide-right";
58  
59      private VIEW activeView;
60  
61      private final Component findBarAndAppLauncher;
62      private final CssLayout appView;
63  
64      @Getter
65      private final VerticalLayout appLauncherLayout = new VerticalLayout();
66  
67      @Getter
68      private final Layout findBarResultsLayout = new CssLayout();
69      @Getter
70      private final Panel appLayout = new Panel();
71  
72      public ViewportLayout() {
73  
74          findBarResultsLayout.setWidth(100, Unit.PERCENTAGE);
75          findBarResultsLayout.addStyleName("findbar-results");
76  
77          appLauncherLayout.addStyleName("v-app-launcher");
78          findBarAndAppLauncher = new CssLayout(findBarResultsLayout, appLauncherLayout);
79          findBarAndAppLauncher.setWidth(100, Unit.PERCENTAGE);
80          findBarAndAppLauncher.addStyleName("findbarAndApplauncher");
81  
82          appLayout.setSizeFull();
83          appLayout.addStyleName("apps-viewport");
84          CssLayout appOverlay = new CssLayout();
85          appOverlay.addStyleName("app-overlay");
86          appOverlay.setSizeFull();
87          appOverlay.addLayoutClickListener(event -> {
88              if (appLayout.getContent() != null) {
89                  updateView(APP_LAUNCHER, APP);
90              }
91          });
92          appView = new CssLayout(appLayout, appOverlay) {
93              @Override
94              protected String getCss(Component c) {
95                  return "z-index: " + (c == appLayout && (activeView == APP || appLayout.getContent() == null) ? 2 : 1);
96              }
97          };
98          appView.addStyleNames("apps-viewport");
99  
100 
101         addComponents(findBarAndAppLauncher, appView);
102         addStyleName("viewportLayout");
103         setSizeFull();
104 
105         updateView(null, FIND_BAR);
106     }
107 
108     public void toggleAppLauncher() {
109         if (activeView == APP_LAUNCHER) {
110             if (appLayout.getContent() != null) {
111                 updateView(activeView, APP);
112             }
113         } else {
114             updateView(activeView, APP_LAUNCHER);
115         }
116     }
117 
118     public void toggleFindBar() {
119         if (activeView == FIND_BAR) {
120             if (appLayout.getContent() != null) {
121                 updateView(activeView, APP);
122             }
123         } else {
124             showFindBar();
125         }
126     }
127 
128     public void showFindBar() {
129         if (activeView != FIND_BAR) {
130             updateView(activeView, FIND_BAR);
131         }
132     }
133 
134     public void showApp() {
135         if (activeView != APP) {
136             updateView(activeView, APP);
137         }
138     }
139 
140     public void hideApp() {
141         updateView(APP, findBarAndAppLauncher.getStyleName().contains(MOVE_LEFT) ? APP_LAUNCHER : FIND_BAR);
142     }
143 
144     private void updateView(VIEW from, VIEW to) {
145         disableAnimations();
146 
147         if (from == APP_LAUNCHER) {
148             if (to == APP) {
149                 appView.removeStyleName(MOVE_DOWN);
150                 appView.addStyleName(SLIDE_UP);
151             } else if (to == FIND_BAR) {
152                 findBarAndAppLauncher.removeStyleName(MOVE_LEFT);
153                 findBarAndAppLauncher.addStyleName(SLIDE_RIGHT);
154             }
155         } else if (from == APP) {
156             if (to == APP_LAUNCHER) {
157                 findBarAndAppLauncher.addStyleName(MOVE_LEFT);
158             } else if (to == FIND_BAR) {
159                 findBarAndAppLauncher.removeStyleNames(MOVE_LEFT);
160             }
161             appView.addStyleName(SLIDE_DOWN);
162             appView.addStyleName(MOVE_DOWN);
163         } else if (from == FIND_BAR) {
164             if (to == APP) {
165                 appView.removeStyleName(MOVE_DOWN);
166                 appView.addStyleName(SLIDE_UP);
167             } else if (to == APP_LAUNCHER) {
168                 findBarAndAppLauncher.addStyleName(SLIDE_LEFT);
169                 findBarAndAppLauncher.addStyleName(MOVE_LEFT);
170             }
171         } else {
172             appView.addStyleName(SLIDE_DOWN);
173             appView.addStyleName(MOVE_DOWN);
174         }
175         activeView = to;
176     }
177 
178     public void disableAnimations() {
179         findBarAndAppLauncher.removeStyleNames(SLIDE_LEFT, SLIDE_RIGHT);
180         appView.removeStyleNames(SLIDE_DOWN, SLIDE_UP);
181     }
182 
183     public VIEW getActiveView() {
184         return activeView;
185     }
186 
187     /**
188      * Type of view.
189      */
190     public enum VIEW {
191         APP, FIND_BAR, APP_LAUNCHER
192     }
193 }