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
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 TouchPanelt/ui/client/widget/touch/TouchPanel.html#TouchPanel">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 void setSticker(Widget widget) {
137
138
139 insert(widget, userMenu, 0, true);
140 }
141
142 public ShellAppType getActiveShellType() {
143 final Iterator<Entry<ShellAppType, NavigatorButton>> it = controlsMap.entrySet().iterator();
144 while (it.hasNext()) {
145 final Entry<ShellAppType, NavigatorButton> entry = it.next();
146 if (entry.getValue().getStyleName().contains("active")) {
147 return entry.getKey();
148 }
149 }
150 return null;
151 }
152
153 public void deactivateControls() {
154 divetWrapper.getStyle().setDisplay(Display.NONE);
155 for (final NavigatorButton button : controlsMap.values()) {
156 button.removeStyleName("active");
157 }
158 }
159
160 public void setIndication(ShellAppType type, int indication) {
161 controlsMap.get(type).setIndication(indication);
162 }
163
164 public void activateControl(final ShellAppType type) {
165 final ShellAppType currentActive = getActiveShellType();
166 if (currentActive != null) {
167 controlsMap.get(currentActive).removeStyleName("active");
168 }
169 doUpdateDivetPosition(type, currentActive != null);
170 final Widget w = controlsMap.get(type);
171 w.addStyleName("active");
172 }
173
174 private void construct() {
175 divetWrapper.setId("divet");
176 logoImg.setId("logo");
177 String baseUrl = GWT.getModuleBaseURL().replace("widgetsets/" + GWT.getModuleName() + "/", "");
178 String logoPath = baseUrl + "themes/admincentraltheme/img/";
179 boolean isIE8 = BrowserInfo.get().isIE8();
180 logoImg.setAttribute("src", logoPath + (isIE8 ? "logo-magnolia.png" : "logo-magnolia.svg"));
181
182 logo.getElement().appendChild(logoImg);
183 add(logo);
184
185 userMenu.setClassName(USER_MENU_CLASS_NAME);
186 getElement().appendChild(userMenu);
187 getElement().appendChild(divetWrapper);
188 for (final ShellAppType appType : ShellAppType.values()) {
189 final NavigatorButtondin/gwt/client/magnoliashell/shell/NavigatorButton.html#NavigatorButton">NavigatorButton w = new NavigatorButton(appType);
190 w.addTouchEndHandler(new TouchEndHandler() {
191 @Override
192 public void onTouchEnd(TouchEndEvent event) {
193 toggleShellApp(appType);
194 }
195 });
196
197 controlsMap.put(appType, w);
198 add(w);
199 }
200 divetWrapper.getStyle().setDisplay(Display.NONE);
201 }
202
203
204
205
206
207
208 public void toggleShellApp(ShellAppType appType) {
209
210 if (appType == getActiveShellType()) {
211
212 listener.onHideShellAppsRequested();
213 } else {
214
215 listener.showShellApp(appType);
216 }
217 }
218
219 private void bindHandlers() {
220 DOM.sinkEvents(getElement(), Event.TOUCHEVENTS);
221 }
222
223 private void doUpdateDivetPosition(final ShellAppType type, boolean animated) {
224 Widget w = controlsMap.get(type);
225 divetWrapper.getStyle().setDisplay(Display.BLOCK);
226
227 switch (type) {
228 case APPLAUNCHER:
229 divetWrapper.setClassName("divet-green");
230 break;
231 case PULSE:
232 divetWrapper.setClassName("divet-gray");
233 break;
234 case FAVORITE:
235 divetWrapper.setClassName("divet-white");
236 break;
237 default:
238 divetWrapper.setClassName("divet-white");
239 }
240
241 int divetPos = w.getAbsoluteLeft() + (w.getOffsetWidth() / 2) - divetWrapper.getOffsetWidth() / 2;
242 if (animated && divetWrapper.getAbsoluteLeft() != divetPos) {
243 Logger.getLogger(getClass().getName()).log(Level.INFO, "DIVET POS: " + divetPos);
244 divetAnimation.setProperty("left", divetPos);
245 divetAnimation.run(DIVET_ANIMATION_SPEED, divetWrapper);
246
247 } else {
248 divetWrapper.getStyle().setLeft(divetPos, Unit.PX);
249 }
250
251 }
252 }