View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.ui.vaadin.gwt.client.magnoliashell.shell;
35  
36  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.AnimationSettings;
37  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper;
38  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.JQueryAnimation;
39  import info.magnolia.ui.vaadin.gwt.client.shared.magnoliashell.ShellAppType;
40  
41  import java.util.EnumMap;
42  import java.util.Iterator;
43  import java.util.Map;
44  import java.util.Map.Entry;
45  import java.util.logging.Level;
46  import java.util.logging.Logger;
47  
48  import com.google.gwt.core.client.GWT;
49  import com.google.gwt.core.client.Scheduler;
50  import com.google.gwt.dom.client.Style.Display;
51  import com.google.gwt.dom.client.Style.Unit;
52  import com.google.gwt.user.client.DOM;
53  import com.google.gwt.user.client.Element;
54  import com.google.gwt.user.client.Event;
55  import com.google.gwt.user.client.ui.FlowPanel;
56  import com.google.gwt.user.client.ui.Widget;
57  import com.googlecode.mgwt.dom.client.event.touch.TouchEndEvent;
58  import com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler;
59  import com.googlecode.mgwt.ui.client.widget.touch.TouchPanel;
60  import com.vaadin.client.BrowserInfo;
61  
62  /**
63   * Navigation bar.
64   */
65  public class ShellAppLauncher extends FlowPanel {
66  
67      /**
68       * Listener.
69       */
70      public interface Listener {
71  
72          void onHideShellAppsRequested();
73  
74          void showShellApp(ShellAppType type);
75      }
76  
77      private final static int DIVET_ANIMATION_SPEED = 400;
78  
79      public static final String USER_MENU_CLASS_NAME = "v-shell-user-menu-wrapper";
80  
81      private final static String ID = "main-launcher";
82  
83      private final Element divetWrapper = DOM.createDiv();
84  
85      private final TouchPanel logo = new TouchPanel();
86  
87      private final Element userMenu = DOM.createDiv();
88  
89      private final Element logoImg = DOM.createImg();
90  
91      private final Map<ShellAppType, NavigatorButton> controlsMap = new EnumMap<ShellAppType, NavigatorButton>(ShellAppType.class);
92  
93      private JQueryAnimation divetAnimation;
94  
95      private Listener listener;
96  
97      public ShellAppLauncher() {
98          super();
99          this.divetAnimation = new JQueryAnimation();
100         getElement().setId(ID);
101         construct();
102         bindHandlers();
103     }
104 
105     @Override
106     protected void onLoad() {
107         super.onLoad();
108         Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
109             @Override
110             public void execute() {
111                 getElement().getStyle().setTop(-60, Unit.PX);
112                 JQueryWrapper.select(getElement()).animate(250, new AnimationSettings() {
113                     {
114                         setProperty("top", 0);
115                     }
116                 });
117             }
118         });
119     }
120 
121     public void setListener(Listener listener) {
122         this.listener = listener;
123     }
124 
125     public final void updateDivet() {
126         final ShellAppType type = getActiveShellType();
127         if (type != null) {
128             doUpdateDivetPosition(type, false);
129         }
130     }
131 
132     public void setUserMenu(Widget widget) {
133         add(widget, userMenu);
134     }
135 
136     public ShellAppType getActiveShellType() {
137         final Iterator<Entry<ShellAppType, NavigatorButton>> it = controlsMap.entrySet().iterator();
138         while (it.hasNext()) {
139             final Entry<ShellAppType, NavigatorButton> entry = it.next();
140             if (entry.getValue().getStyleName().contains("active")) {
141                 return entry.getKey();
142             }
143         }
144         return null;
145     }
146 
147     public void deactivateControls() {
148         divetWrapper.getStyle().setDisplay(Display.NONE);
149         for (final NavigatorButton button : controlsMap.values()) {
150             button.removeStyleName("active");
151         }
152     }
153 
154     public void setIndication(ShellAppType type, int indication) {
155         controlsMap.get(type).setIndication(indication);
156     }
157 
158     public void activateControl(final ShellAppType type) {
159         final ShellAppType currentActive = getActiveShellType();
160         if (currentActive != null) {
161             controlsMap.get(currentActive).removeStyleName("active");
162         }
163         doUpdateDivetPosition(type, currentActive != null);
164         final Widget w = controlsMap.get(type);
165         w.addStyleName("active");
166     }
167 
168     private void construct() {
169         divetWrapper.setId("divet");
170         logoImg.setId("logo");
171         String baseUrl = GWT.getModuleBaseURL().replace("widgetsets/" + GWT.getModuleName() + "/", "");
172         String logoPath = baseUrl + "themes/admincentraltheme/img/";
173         boolean isIE8 = BrowserInfo.get().isIE8();
174         logoImg.setAttribute("src", logoPath + (isIE8 ? "logo-magnolia.png" : "logo-magnolia.svg"));
175 
176         logo.getElement().appendChild(logoImg);
177         add(logo);
178 
179         userMenu.setClassName(USER_MENU_CLASS_NAME);
180         getElement().appendChild(userMenu);
181         getElement().appendChild(divetWrapper);
182         for (final ShellAppType appType : ShellAppType.values()) {
183             final NavigatorButton w = new NavigatorButton(appType);
184             w.addTouchEndHandler(new TouchEndHandler() {
185                 @Override
186                 public void onTouchEnd(TouchEndEvent event) {
187                     toggleShellApp(appType);
188                 }
189             });
190 
191             controlsMap.put(appType, w);
192             add(w);
193         }
194         divetWrapper.getStyle().setDisplay(Display.NONE);
195     }
196 
197     /**
198      * Toggle the 'openness' of the specified shellApp.
199      * 
200      * @param appType
201      */
202     public void toggleShellApp(ShellAppType appType) {
203         // Has user clicked on the active shell app?
204         if (appType == getActiveShellType()) {
205             // if open then close it.
206             listener.onHideShellAppsRequested();
207         } else {
208             // If closed, then open it.
209             listener.showShellApp(appType);
210         }
211     }
212 
213     private void bindHandlers() {
214         DOM.sinkEvents(getElement(), Event.TOUCHEVENTS);
215     }
216 
217     private void doUpdateDivetPosition(final ShellAppType type, boolean animated) {
218         Widget w = controlsMap.get(type);
219         divetWrapper.getStyle().setDisplay(Display.BLOCK);
220 
221         switch (type) {
222         case APPLAUNCHER:
223             divetWrapper.setClassName("divet-green");
224             break;
225         case PULSE:
226             divetWrapper.setClassName("divet-gray");
227             break;
228         case FAVORITE:
229             divetWrapper.setClassName("divet-white");
230             break;
231         default:
232             divetWrapper.setClassName("divet-white");
233         }
234 
235         int divetPos = w.getAbsoluteLeft() + (w.getOffsetWidth() / 2) - divetWrapper.getOffsetWidth() / 2;
236         if (animated && divetWrapper.getAbsoluteLeft() != divetPos) {
237             Logger.getLogger(getClass().getName()).log(Level.INFO, "DIVET POS: " + divetPos);
238             divetAnimation.setProperty("left", divetPos);
239             divetAnimation.run(DIVET_ANIMATION_SPEED, divetWrapper);
240 
241         } else {
242             divetWrapper.getStyle().setLeft(divetPos, Unit.PX);
243         }
244 
245     }
246 }