|
7 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | * you may not use this file except in compliance with the License. |
9 | 9 | * You may obtain a copy of the License at |
10 | | - * |
| 10 | + * |
11 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | - * |
| 12 | + * |
13 | 13 | * Unless required by applicable law or agreed to in writing, software |
14 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
19 | 19 | */ |
20 | 20 | package com.flowingcode.addons.applayout; |
21 | 21 |
|
22 | | - |
23 | | - |
24 | | -import java.util.Collection; |
25 | | -import java.util.Collections; |
26 | | -import java.util.Optional; |
27 | | - |
28 | 22 | import com.vaadin.flow.component.Component; |
29 | 23 | import com.vaadin.flow.component.HasComponents; |
30 | 24 | import com.vaadin.flow.component.Tag; |
|
33 | 27 | import com.vaadin.flow.component.dependency.NpmPackage; |
34 | 28 | import com.vaadin.flow.component.html.H4; |
35 | 29 | import com.vaadin.flow.shared.Registration; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.Optional; |
36 | 33 |
|
37 | 34 | /** |
38 | 35 | * Component that renders the app drawer |
39 | | - * |
40 | | - * @author mlopez |
41 | 36 | * |
| 37 | + * @author mlopez |
42 | 38 | */ |
43 | 39 | @SuppressWarnings("serial") |
44 | 40 | @Tag("app-drawer") |
45 | 41 | @HtmlImport("bower_components/app-layout/app-drawer/app-drawer.html") |
46 | 42 | @HtmlImport("bower_components/iron-scroll-target-behavior/iron-scroll-target-behavior.html") |
47 | | -@NpmPackage(value = "@polymer/app-layout", version= AppLayout.NPM_VERSION) |
| 43 | +@NpmPackage(value = "@polymer/app-layout", version = AppLayout.NPM_VERSION) |
48 | 44 | @JsModule("@polymer/app-layout/app-drawer/app-drawer.js") |
49 | 45 | public class AppDrawer extends Component implements HasComponents { |
50 | | - |
51 | | - private final PaperListbox pm = new PaperListbox(Collections.emptyList()); |
52 | | - private final Component header; |
53 | 46 |
|
54 | | - public AppDrawer(String title) { |
55 | | - this(new H4(title)); |
56 | | - header.getElement().setAttribute("style", "text-align:center"); |
57 | | - } |
58 | | - |
59 | | - public AppDrawer(Component headerComponent) { |
60 | | - this.header = headerComponent; |
61 | | - getElement().setAttribute("id", "drawer"); |
62 | | - setSwipeOpen(true); |
63 | | - |
64 | | - Registration[] r = new Registration[1]; |
65 | | - r[0] = getElement().addEventListener("app-drawer-transitioned", ev->{ |
66 | | - //need to adjust the height after the drawer has been rendered |
67 | | - pm.getElement().executeJs("this.style.height='calc(100% - '+($0.scrollHeight+16)+'px)'", header); |
68 | | - r[0].remove(); |
69 | | - }); |
70 | | - |
71 | | - removeAll(); |
72 | | - } |
73 | | - |
74 | | - public void setSwipeOpen(boolean swipeOpen) { |
75 | | - getElement().setAttribute("swipe-open", swipeOpen); |
76 | | - } |
| 47 | + private final PaperListbox pm = new PaperListbox(Collections.emptyList()); |
| 48 | + private final Component header; |
77 | 49 |
|
78 | | - @Override |
79 | | - public void add(Component... components) { |
80 | | - for (Component c : components) { |
81 | | - if (c instanceof MenuItem) { |
82 | | - pm.add(c); |
83 | | - } else { |
84 | | - HasComponents.super.add(components); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - @Override |
90 | | - public void remove(Component... components) { |
91 | | - for (Component c : components) { |
92 | | - if (c instanceof MenuItem) { |
93 | | - pm.removeAll(); |
94 | | - } else { |
95 | | - HasComponents.super.remove(components); |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - @Override |
101 | | - public void removeAll() { |
102 | | - HasComponents.super.removeAll(); |
103 | | - this.add(header); |
104 | | - this.add(pm); |
| 50 | + public AppDrawer(String title) { |
| 51 | + this(new H4(title)); |
| 52 | + header.getElement().setAttribute("style", "text-align:center"); |
| 53 | + } |
| 54 | + |
| 55 | + public AppDrawer(Component headerComponent) { |
| 56 | + this.header = headerComponent; |
| 57 | + getElement().setAttribute("id", "drawer"); |
| 58 | + setSwipeOpen(true); |
| 59 | + |
| 60 | + Registration[] r = new Registration[1]; |
| 61 | + r[0] = |
| 62 | + getElement() |
| 63 | + .addEventListener( |
| 64 | + "app-drawer-transitioned", |
| 65 | + ev -> { |
| 66 | + // need to adjust the height after the drawer has been rendered |
| 67 | + pm.getElement() |
| 68 | + .executeJs( |
| 69 | + "this.style.height='calc(100% - '+($0.scrollHeight+16)+'px)'", header); |
| 70 | + r[0].remove(); |
| 71 | + }); |
| 72 | + |
| 73 | + removeAll(); |
| 74 | + } |
| 75 | + |
| 76 | + public void setSwipeOpen(boolean swipeOpen) { |
| 77 | + getElement().setAttribute("swipe-open", swipeOpen); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void add(Component... components) { |
| 82 | + for (Component c : components) { |
| 83 | + if (c instanceof MenuItem) { |
| 84 | + pm.add(c); |
| 85 | + } else { |
| 86 | + HasComponents.super.add(components); |
| 87 | + } |
105 | 88 | } |
106 | | - |
107 | | - public void setMenuItems(Collection<? extends Component> menuItems) { |
108 | | - pm.removeAll(); |
109 | | - menuItems.stream().forEach(pm::add); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void remove(Component... components) { |
| 93 | + for (Component c : components) { |
| 94 | + if (c instanceof MenuItem) { |
| 95 | + pm.removeAll(); |
| 96 | + } else { |
| 97 | + HasComponents.super.remove(components); |
| 98 | + } |
110 | 99 | } |
| 100 | + } |
111 | 101 |
|
112 | | - /**Close the app-drawer.*/ |
113 | | - public void close() { |
114 | | - getUI().ifPresent(ui->ui.getPage().executeJs("$0.close()", this)); |
115 | | - } |
| 102 | + @Override |
| 103 | + public void removeAll() { |
| 104 | + HasComponents.super.removeAll(); |
| 105 | + this.add(header); |
| 106 | + this.add(pm); |
| 107 | + } |
116 | 108 |
|
117 | | - static Optional<AppDrawer> findAppDrawer(Component component) { |
118 | | - while (component!=null) { |
119 | | - if (component instanceof AppDrawer) { |
120 | | - return Optional.of((AppDrawer)component); |
121 | | - } else { |
122 | | - component = component.getParent().orElse(null); |
123 | | - } |
124 | | - } |
125 | | - return Optional.empty(); |
126 | | - } |
| 109 | + public void setMenuItems(Collection<? extends Component> menuItems) { |
| 110 | + pm.removeAll(); |
| 111 | + menuItems.stream().forEach(pm::add); |
| 112 | + } |
| 113 | + |
| 114 | + /** Close the app-drawer. */ |
| 115 | + public void close() { |
| 116 | + getUI().ifPresent(ui -> ui.getPage().executeJs("$0.close()", this)); |
| 117 | + } |
| 118 | + |
| 119 | + static Optional<AppDrawer> findAppDrawer(Component component) { |
| 120 | + while (component != null) { |
| 121 | + if (component instanceof AppDrawer) { |
| 122 | + return Optional.of((AppDrawer) component); |
| 123 | + } else { |
| 124 | + component = component.getParent().orElse(null); |
| 125 | + } |
| 126 | + } |
| 127 | + return Optional.empty(); |
| 128 | + } |
127 | 129 | } |
0 commit comments