1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65 public class ShellAppLauncher extends FlowPanel {
66
67
68
69
70 public interface Listener {
71
72 void onHideShellAppsRequested();
73 void showShellApp(ShellAppType type);
74 }
75 private final static int DIVET_ANIMATION_SPEED = 400;
76
77 public static final String USER_MENU_CLASS_NAME = "v-shell-user-menu-wrapper";
78
79 private final static String ID = "main-launcher";
80
81 private final Element divetWrapper = DOM.createDiv();
82
83 private final TouchPanel logo = new TouchPanel();
84
85 private final Element userMenu = DOM.createDiv();
86
87 private final Element logoImg = DOM.createImg();
88
89 private final Map<ShellAppType, NavigatorButton> controlsMap = new EnumMap<ShellAppType, NavigatorButton>(ShellAppType.class);
90
91 private JQueryAnimation divetAnimation;
92
93 private Listener listener;
94
95 public ShellAppLauncher() {
96 super();
97 this.divetAnimation = new JQueryAnimation();
98 getElement().setId(ID);
99 construct();
100 bindHandlers();
101 }
102
103 @Override
104 protected void onLoad() {
105 super.onLoad();
106 Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
107 @Override
108 public void execute() {
109 getElement().getStyle().setTop(-60, Unit.PX);
110 JQueryWrapper.select(getElement()).animate(250, new AnimationSettings() {
111 {
112 setProperty("top", 0);
113 }
114 });
115 }
116 });
117 }
118
119 public void setListener(Listener listener) {
120 this.listener = listener;
121 }
122
123 public final void updateDivet() {
124 final ShellAppType type = getActiveShellType();
125 if (type != null) {
126 doUpdateDivetPosition(type, false);
127 }
128 }
129
130 public void setUserMenu(Widget widget) {
131 add(widget, userMenu);
132 }
133
134 public ShellAppType getActiveShellType() {
135 final Iterator<Entry<ShellAppType, NavigatorButton>> it = controlsMap.entrySet().iterator();
136 while (it.hasNext()) {
137 final Entry<ShellAppType, NavigatorButton> entry = it.next();
138 if (entry.getValue().getStyleName().contains("active")) {
139 return entry.getKey();
140 }
141 }
142 return null;
143 }
144
145 public void deactivateControls() {
146 divetWrapper.getStyle().setDisplay(Display.NONE);
147 for (final NavigatorButton button : controlsMap.values()) {
148 button.removeStyleName("active");
149 }
150 }
151
152 public void setIndication(ShellAppType type, int indication) {
153 controlsMap.get(type).setIndication(indication);
154 }
155
156 public void activateControl(final ShellAppType type) {
157 final ShellAppType currentActive = getActiveShellType();
158 if (currentActive != null) {
159 controlsMap.get(currentActive).removeStyleName("active");
160 }
161 doUpdateDivetPosition(type, currentActive != null);
162 final Widget w = controlsMap.get(type);
163 w.addStyleName("active");
164 }
165
166 private void construct() {
167 divetWrapper.setId("divet");
168 logoImg.setId("logo");
169 String baseUrl = GWT.getModuleBaseURL().replace("widgetsets/" + GWT.getModuleName() + "/", "");
170 String logoPath = baseUrl + "themes/admincentraltheme/img/";
171 boolean isIE8 = BrowserInfo.get().isIE8();
172 logoImg.setAttribute("src", logoPath + (isIE8 ? "logo-magnolia.png" : "logo-magnolia.svg"));
173
174 logo.getElement().appendChild(logoImg);
175 add(logo);
176
177 userMenu.setClassName(USER_MENU_CLASS_NAME);
178 getElement().appendChild(userMenu);
179 getElement().appendChild(divetWrapper);
180 for (final ShellAppType appType : ShellAppType.values()) {
181 final NavigatorButton w = new NavigatorButton(appType);
182 w.addTouchEndHandler(new TouchEndHandler() {
183 @Override
184 public void onTouchEnd(TouchEndEvent event) {
185 toggleShellApp(appType);
186 }
187 });
188
189 controlsMap.put(appType, w);
190 add(w);
191 }
192 divetWrapper.getStyle().setDisplay(Display.NONE);
193 }
194
195
196
197
198
199
200 public void toggleShellApp(ShellAppType appType) {
201
202 if (appType == getActiveShellType()) {
203
204 listener.onHideShellAppsRequested();
205 } else {
206
207 listener.showShellApp(appType);
208 }
209 }
210
211
212 private void bindHandlers() {
213 DOM.sinkEvents(getElement(), Event.TOUCHEVENTS);
214 }
215
216 private void doUpdateDivetPosition(final ShellAppType type, boolean animated) {
217 Widget w = controlsMap.get(type);
218 divetWrapper.getStyle().setDisplay(Display.BLOCK);
219 divetWrapper.setClassName(type == ShellAppType.APPLAUNCHER ? "divet-green" : "divet-white");
220 int divetPos = w.getAbsoluteLeft() + (w.getOffsetWidth() / 2) - divetWrapper.getOffsetWidth() / 2;
221 if (animated && divetWrapper.getAbsoluteLeft() != divetPos) {
222 Logger.getLogger(getClass().getName()).log(Level.INFO, "DIVET POS: " + divetPos);
223 divetAnimation.setProperty("left", divetPos);
224 divetAnimation.run(DIVET_ANIMATION_SPEED, divetWrapper);
225
226 } else {
227 divetWrapper.getStyle().setLeft(divetPos, Unit.PX);
228 }
229
230 }
231 }