/
AbstractSideNav.java
699 lines (591 loc) · 19.8 KB
/
AbstractSideNav.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2017 GwtMaterialDesign
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package gwt.material.design.client.base;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.client.accessibility.AccessibilityControl;
import gwt.material.design.client.base.density.Density;
import gwt.material.design.client.base.helper.DOMHelper;
import gwt.material.design.client.base.mixin.DensityMixin;
import gwt.material.design.client.base.mixin.OverlayStyleMixin;
import gwt.material.design.client.base.mixin.StyleMixin;
import gwt.material.design.client.base.viewport.ViewPort;
import gwt.material.design.client.base.viewport.WidthBoundary;
import gwt.material.design.client.constants.*;
import gwt.material.design.client.events.*;
import gwt.material.design.client.js.JsMaterialElement;
import gwt.material.design.client.js.JsSideNavOptions;
import gwt.material.design.client.ui.*;
import gwt.material.design.client.ui.html.ListItem;
import gwt.material.design.jquery.client.api.JQueryElement;
import static gwt.material.design.client.js.JsMaterialElement.$;
//@formatter:off
/**
* AbstractSideNav handles the creation and Ui logic for
* different sidenavs, you can easily setup any kind of logic for
* you sidenav behaviour in {@link AbstractSideNav#setup()}.
*
* @author kevzlou7979
*/
//@formatter:on
public abstract class AbstractSideNav extends MaterialWidget
implements JsLoader, HasSelectables, HasInOutDurationTransition, HasSideNavHandlers, HasOverlayStyle, HasOpenClose, HasDensity {
protected int width = 240;
protected int inDuration = 400;
protected int outDuration = 200;
protected boolean open;
protected boolean closeOnClick;
protected boolean alwaysShowActivator = true;
protected boolean allowBodyScroll = true;
protected Edge edge = Edge.LEFT;
protected Boolean showOnAttach;
protected Element activator;
protected WidthBoundary closingBoundary;
protected ViewPort autoHideViewport;
protected OverlayOption overlayOption = OverlayOption.create();
private StyleMixin<MaterialSideNav> typeMixin;
private OverlayStyleMixin<AbstractSideNav> overlayStyleMixin;
private DensityMixin<AbstractSideNav> densityMixin;
public AbstractSideNav() {
super(Document.get().createULElement(), CssName.SIDE_NAV);
}
public AbstractSideNav(final Widget... widgets) {
this();
for (final Widget w : widgets) {
add(w);
}
}
public AbstractSideNav(SideNavType type) {
this();
setType(type);
}
@Override
protected void onLoad() {
super.onLoad();
load();
setupShowOnAttach();
}
protected void setupDefaultOverlayStyle() {
overlayOption.setVisibility(Style.Visibility.HIDDEN);
setOverlayOption(overlayOption);
}
protected void setupShowOnAttach() {
if (showOnAttach != null) {
// Ensure the side nav starts closed
$(activator).trigger("menu-in", null);
if (showOnAttach) {
Scheduler.get().scheduleDeferred(() -> {
// We are ignoring cases with mobile
if (Window.getClientWidth() > 960) {
open();
}
});
}
} else {
if (Window.getClientWidth() > 960) {
$(activator).trigger("menu-out", null);
}
}
}
@Override
protected void onUnload() {
super.onUnload();
unload();
}
public Widget wrap(Widget child) {
if (child instanceof MaterialImage) {
child.getElement().getStyle().setProperty("border", "1px solid #e9e9e9");
child.getElement().getStyle().setProperty("textAlign", "center");
}
// Check whether the widget is not selectable by default
boolean isNotSelectable = false;
if (child instanceof MaterialWidget) {
MaterialWidget widget = (MaterialWidget) child;
if (widget.getInitialClasses() != null) {
if (widget.getInitialClasses().length > 0) {
if (child instanceof HasNoSideNavSelection) {
isNotSelectable = true;
}
}
}
}
if (!(child instanceof ListItem)) {
// Direct list item not collapsible
final ListItem listItem = new ListItem();
if (child instanceof MaterialCollapsible) {
listItem.getElement().getStyle().setBackgroundColor("transparent");
}
if (child instanceof HasWaves) {
listItem.setWaves(((HasWaves) child).getWaves());
((HasWaves) child).setWaves(null);
}
if (child instanceof HasNoSideNavSelection) {
super.add(child);
} else {
listItem.add(child);
child = listItem;
}
}
// Collapsible and Side Porfile should not be selectable
final Widget finalChild = child;
if (!isNotSelectable) {
// Active click handler
registerHandler(finalChild.addDomHandler(event -> {
clearActive();
finalChild.addStyleName(CssName.ACTIVE);
}, ClickEvent.getType()));
}
child.getElement().getStyle().setDisplay(Style.Display.BLOCK);
return child;
}
@Override
public void add(Widget child) {
super.add(wrap(child));
}
@Override
protected void insert(Widget child, com.google.gwt.user.client.Element container, int beforeIndex, boolean domInsert) {
super.insert(wrap(child), container, beforeIndex, domInsert);
}
protected void pushElement(Element element, int value) {
applyTransition($(element).asElement());
if (getEdge() == Edge.RIGHT) {
$(element).css("paddingRight", value + "px");
} else {
$(element).css("paddingLeft", value + "px");
}
}
protected void pushElementMargin(Element element, int value) {
applyTransition($(element).asElement());
if (getEdge() == Edge.LEFT) {
$(element).css("margin-left", value + "px");
} else {
$(element).css("margin-right", value + "px");
}
}
protected void applyBodyScroll() {
if (isAllowBodyScroll()) {
$("header").css("width", "100%");
$("header").css("position", "fixed");
$("header").css("zIndex", "997");
$(getElement()).css("position", "fixed");
}
}
protected void applyTransition(Element element) {
applyTransition(element, "all");
}
protected void applyTransition(Element element, String property) {
int duration;
if (isOpen()) {
duration = inDuration;
} else {
duration = outDuration;
}
if (element != null) {
setTransition(new TransitionConfig(element, duration, 0, property, "cubic-bezier(0, 0, 0.2, 1)"));
}
}
@Override
public void clearActive() {
clearActiveClass(this);
ClearActiveEvent.fire(this);
}
public void setActive(int index) {
clearActive();
getWidget(index).addStyleName(CssName.ACTIVE);
}
@Override
public void load() {
load(true);
}
@Override
public void unload() {
activator = null;
$(".drag-target").remove();
}
/**
* Reinitialize the side nav configurations when changing properties.
*/
@Override
public void reload() {
unload();
load(false);
}
protected void load(boolean strict) {
try {
activator = DOMHelper.getElementByAttribute("data-activates", getId());
MaterialWidget navMenu = getNavMenu();
if (navMenu != null) {
navMenu.setShowOn(ShowOn.SHOW_ON_MED_DOWN);
if (alwaysShowActivator && !getTypeMixin().getStyle().equals(SideNavType.FIXED.getCssName())) {
navMenu.setShowOn(ShowOn.SHOW_ON_LARGE);
} else {
navMenu.setHideOn(HideOn.HIDE_ON_LARGE);
}
navMenu.removeStyleName(CssName.NAVMENU_PERMANENT);
// Register Nav Menu Accessibility
AccessibilityControl.get().registerWidget(navMenu);
}
} catch (Exception ex) {
if (strict) {
throw new IllegalArgumentException(
"Could not setup MaterialSideNav please ensure you have " +
"MaterialNavBar with an activator setup to match this widgets id.", ex);
}
}
setup();
setupDefaultOverlayStyle();
JsSideNavOptions options = new JsSideNavOptions();
options.menuWidth = width;
options.edge = edge != null ? edge.getCssName() : null;
options.closeOnClick = closeOnClick;
JsMaterialElement element = $(activator);
element.sideNav(options);
element.off(SideNavEvents.SIDE_NAV_CLOSING);
element.on(SideNavEvents.SIDE_NAV_CLOSING, e1 -> {
onClosing();
return true;
});
element.off(SideNavEvents.SIDE_NAV_CLOSED);
element.on(SideNavEvents.SIDE_NAV_CLOSED, e1 -> {
onClosed();
return true;
});
element.off(SideNavEvents.SIDE_NAV_OPENING);
element.on(SideNavEvents.SIDE_NAV_OPENING, e1 -> {
onOpening();
return true;
});
element.off(SideNavEvents.SIDE_NAV_OPENED);
element.on(SideNavEvents.SIDE_NAV_OPENED, e1 -> {
onOpened();
return true;
});
element.off(SideNavEvents.SIDE_NAV_OVERLAY_ATTACHED);
element.on(SideNavEvents.SIDE_NAV_OVERLAY_ATTACHED, e1 -> {
onOverlayAttached();
return true;
});
$(".collapsible-header").on("click", (e, param1) -> {
//e.stopPropagation();
return true;
});
}
/**
* Override the type of your sidenav.
* Used by {@link MaterialSideNavDrawer}, {@link MaterialSideNavCard}, {@link MaterialSideNavMini}, {@link MaterialSideNavPush}
*/
protected abstract void setup();
@Override
protected void onDetach() {
super.onDetach();
MaterialWidget navMenu = getNavMenu();
if (navMenu != null) {
navMenu.removeStyleName(ShowOn.SHOW_ON_LARGE.getCssName());
navMenu.removeStyleName(ShowOn.SHOW_ON_MED_DOWN.getCssName());
}
pushElement(getHeader(), 0);
pushElement(getMain(), 0);
pushElementMargin(getFooter(), 0);
}
protected Element getMain() {
return $("main").asElement();
}
protected Element getHeader() {
return $("header").asElement();
}
protected Element getFooter() {
return $("footer").asElement();
}
@Override
public void setWidth(String width) {
setWidth(Integer.parseInt(width));
}
/**
* Set the menu's width in pixels.
*/
public void setWidth(int width) {
this.width = width;
getElement().getStyle().setWidth(width, Style.Unit.PX);
}
public int getWidth() {
return width;
}
public boolean isCloseOnClick() {
return closeOnClick;
}
/**
* Close the side nav menu when an \<a\> tag is clicked
* from inside it. Note that if you want this to work you
* must wrap your item within a {@link MaterialLink}.
*/
public void setCloseOnClick(boolean closeOnClick) {
this.closeOnClick = closeOnClick;
}
public Edge getEdge() {
return edge;
}
/**
* Set which edge of the window the menu should attach to.
*/
public void setEdge(Edge edge) {
this.edge = edge;
}
protected void setType(SideNavType type) {
getTypeMixin().setStyle(type.getCssName());
}
protected boolean isSmall() {
return !gwt.material.design.client.js.Window.matchMedia("all and (max-width: 992px)");
}
protected MaterialWidget getNavMenu() {
Element navMenuElement = DOMHelper.getElementByAttribute("data-activates", getId());
if (navMenuElement != null) {
return new MaterialWidget(navMenuElement);
}
return null;
}
protected void onClosing() {
open = false;
$("#sidenav-overlay").remove();
SideNavClosingEvent.fire(this);
resetOverlayStyle();
}
protected void onClosed() {
SideNavClosedEvent.fire(this);
}
protected void onOpening() {
open = true;
$("#sidenav-overlay").each((param1, element) -> {
if (element != null) {
element.removeFromParent();
}
});
SideNavOpeningEvent.fire(this);
}
protected void onOpened() {
if (allowBodyScroll) {
RootPanel.getBodyElement().getStyle().clearOverflow();
}
String overlayZIndex = $("#sidenav-overlay").css("zIndex");
$(".drag-target").css("zIndex", (overlayZIndex != null ? Integer.parseInt(overlayZIndex) : 1) + "");
SideNavOpenedEvent.fire(this);
}
protected void onOverlayAttached() {
applyOverlayStyle(getOverlayElement());
}
/**
* Hide the overlay menu.
*/
public void hideOverlay() {
$("#sidenav-overlay").remove();
}
/**
* Replaced with {@link #open()}
*/
@Deprecated
public void show() {
open();
}
/**
* Show the sidenav using the activator element
*/
@Override
public void open() {
$("#sidenav-overlay").remove();
$(activator).sideNav("show");
}
/**
* Replaced with {@link #close()}
*/
@Deprecated
public void hide() {
close();
}
/**
* Hide the sidenav using the activator element
*/
@Override
public void close() {
$(activator).sideNav("hide");
}
@Override
public boolean isOpen() {
return open;
}
/**
* Will the body have scroll capability
* while the menu is open.
*/
public boolean isAllowBodyScroll() {
return allowBodyScroll;
}
/**
* Allow the body to maintain its scroll capability
* while the menu is visible.
*/
public void setAllowBodyScroll(boolean allowBodyScroll) {
this.allowBodyScroll = allowBodyScroll;
}
/**
* Will the activator always be shown.
*/
public boolean isAlwaysShowActivator() {
return alwaysShowActivator;
}
/**
* Disable the hiding of your activator element.
*/
public void setAlwaysShowActivator(boolean alwaysShowActivator) {
this.alwaysShowActivator = alwaysShowActivator;
}
/**
* Will the menu forcefully show on attachment.
*/
public boolean isShowOnAttach() {
return showOnAttach != null && showOnAttach;
}
/**
* Show the menu upon attachment.<br>
* Note that you shouldn't apply this setting if you want your side nav to appear static.
* otherwise when set to <code>true</code> will slide in from the left.
*/
public void setShowOnAttach(boolean showOnAttach) {
this.showOnAttach = showOnAttach;
}
public boolean isAutoHideOnResize() {
return autoHideViewport != null;
}
/**
* When enabled the sidenav will auto hide / collapse it durating browser resized.
* Default true
*/
public void setAutoHideOnResize(boolean autoHideOnResize) {
if (autoHideOnResize) {
autoHideViewport = ViewPort.when(getClosingBoundary()).then(param1 -> hide());
} else {
if (autoHideViewport != null) {
autoHideViewport.destroy();
autoHideViewport = null;
}
}
}
@Override
public void setEnabled(boolean enabled) {
getEnabledMixin().setEnabled(this, enabled);
}
@Override
public void setInDuration(int inDuration) {
this.inDuration = inDuration;
}
@Override
public int getInDuration() {
return inDuration;
}
@Override
public void setOutDuration(int outDuration) {
this.outDuration = outDuration;
}
@Override
public int getOutDuration() {
return outDuration;
}
@Override
public void setOverlayOption(OverlayOption overlayOption) {
getOverlayStyleMixin().setOverlayOption(overlayOption);
}
@Override
public OverlayOption getOverlayOption() {
return getOverlayStyleMixin().getOverlayOption();
}
@Override
public void applyOverlayStyle(JQueryElement overlayElement) {
getOverlayStyleMixin().applyOverlayStyle(getOverlayElement());
}
@Override
public void resetOverlayStyle() {
getOverlayStyleMixin().resetOverlayStyle();
}
public void setClosingBoundary(WidthBoundary closingBoundary) {
this.closingBoundary = closingBoundary;
}
public WidthBoundary getClosingBoundary() {
if (closingBoundary == null) {
closingBoundary = new WidthBoundary(0, 992);
}
return closingBoundary;
}
@Override
public void setDensity(Density density) {
getDensityMixin().setDensity(density);
}
@Override
public Density getDensity() {
return getDensityMixin().getDensity();
}
public Element getActivator() {
return activator;
}
public JQueryElement getOverlayElement() {
return $("#sidenav-overlay");
}
public JQueryElement getDragTargetElement() {
return $(".drag-target");
}
@Override
public HandlerRegistration addOpeningHandler(SideNavOpeningEvent.SideNavOpeningHandler handler) {
return addHandler(handler, SideNavOpeningEvent.TYPE);
}
@Override
public HandlerRegistration addOpenedHandler(SideNavOpenedEvent.SideNavOpenedHandler handler) {
return addHandler(handler, SideNavOpenedEvent.TYPE);
}
@Override
public HandlerRegistration addClosingHandler(SideNavClosingEvent.SideNavClosingHandler handler) {
return addHandler(handler, SideNavClosingEvent.TYPE);
}
@Override
public HandlerRegistration addClosedHandler(SideNavClosedEvent.SideNavClosedHandler handler) {
return addHandler(handler, SideNavClosedEvent.TYPE);
}
protected StyleMixin<MaterialSideNav> getTypeMixin() {
if (typeMixin == null) {
typeMixin = new StyleMixin(this);
}
return typeMixin;
}
protected OverlayStyleMixin<AbstractSideNav> getOverlayStyleMixin() {
if (overlayStyleMixin == null) {
overlayStyleMixin = new OverlayStyleMixin<>(this);
}
return overlayStyleMixin;
}
protected DensityMixin<AbstractSideNav> getDensityMixin() {
if (densityMixin == null) {
densityMixin = new DensityMixin<>(this);
}
return densityMixin;
}
}