Skip to content

Commit cbe317e

Browse files
committed
Merge remote-tracking branch 'remotes/origin/refactor-web-component-1.x' into refactor-2.1
2 parents a8096e1 + 3308bc7 commit cbe317e

File tree

13 files changed

+389
-150
lines changed

13 files changed

+389
-150
lines changed

src/main/java/com/flowingcode/addons/applayout/AppDrawer.java

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121

2222

2323

24-
import java.util.ArrayList;
2524
import java.util.Collections;
2625
import java.util.List;
2726

28-
import com.flowingcode.addons.applayout.menu.MenuItem;
2927
import com.vaadin.flow.component.Component;
3028
import com.vaadin.flow.component.HasComponents;
3129
import com.vaadin.flow.component.Tag;
@@ -56,72 +54,64 @@ public AppDrawer(String title) {
5654
this(new H4(title));
5755
header.getElement().setAttribute("style", "text-align:center");
5856
}
59-
57+
6058
public AppDrawer(Component headerComponent) {
6159
this.header = headerComponent;
6260
getElement().setAttribute("id", "drawer");
6361
setSwipeOpen(true);
64-
this.add(headerComponent);
65-
this.add(pm);
66-
62+
6763
Registration[] r = new Registration[1];
6864
r[0] = getElement().addEventListener("app-drawer-transitioned", ev->{
6965
//need to adjust the height after the drawer has been rendered
7066
pm.getElement().executeJs("this.style.height='calc(100% - '+($0.scrollHeight+16)+'px)'", header);
7167
r[0].remove();
7268
});
69+
70+
getElement().getStyle().set("--fc-separator-background-color", "var(--app-drawer-content-container_-_background-color)");
71+
removeAll();
7372
}
74-
73+
7574
public void setSwipeOpen(boolean swipeOpen) {
7675
getElement().setAttribute("swipe-open", swipeOpen);
7776
}
7877

79-
public void setMenuItems(List<MenuItem> menuItems) {
80-
Component[] components = createComponents(menuItems);
81-
pm.removeAll();
82-
pm.add(components);
83-
}
84-
85-
private Component[] createComponents(List<MenuItem> menuItems) {
86-
List<Component> components = new ArrayList<>();
87-
for (MenuItem menuItem : menuItems) {
88-
if (!menuItem.getSubMenuItems().isEmpty()) {
89-
components.add(collectMenus(menuItem));
78+
@Override
79+
public void add(Component... components) {
80+
for (Component c : components) {
81+
if (c instanceof MenuItem) {
82+
pm.add(c);
9083
} else {
91-
if (menuItem.getIcon()==null && menuItem.getImage()==null) {
92-
PaperItem pi = new PaperItem(menuItem.getLabel(),menuItem.getCommand(), this);
93-
pi.setEnabled(menuItem.isEnabled());
94-
components.add(pi);
95-
menuItem.setRefreshCallback(()->pi.setText(menuItem.getLabel()));
96-
} else {
97-
PaperIconItem pi;
98-
if (menuItem.getImage()!=null) {
99-
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getImage(), menuItem.getCommand(), this);
100-
} else {
101-
pi = new PaperIconItem(menuItem.getLabel(), menuItem.getIcon(), menuItem.getCommand(), this);
102-
}
103-
104-
components.add(pi);
105-
pi.setEnabled(menuItem.isEnabled());
106-
menuItem.setRefreshCallback(()->{
107-
pi.setTitle(menuItem.getLabel());
108-
pi.setIcon(menuItem.getIcon());
109-
});
110-
}
84+
HasComponents.super.add(components);
11185
}
112-
}
113-
return components.toArray(new Component[] {});
114-
}
115-
116-
private CollapseButton collectMenus(MenuItem topMenuItem) {
117-
List<MenuItem> menuItems = topMenuItem.getSubMenuItems();
118-
Component[] components = createComponents(menuItems);
119-
CollapseButton collapseButton = new CollapseButton(topMenuItem.getLabel(), topMenuItem.getIcon(), topMenuItem.getImage(), components);
120-
collapseButton.setEnabled(topMenuItem.isEnabled());
121-
return collapseButton;
122-
}
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);
105+
}
106+
107+
public void setMenuItems(List<MenuItem> menuItems) {
108+
pm.removeAll();
109+
menuItems.stream().forEach(pm::add);
110+
}
123111

124-
void toggle() {
125-
this.getElement().executeJs("this.toggle()");
112+
/**Close the app-drawer.*/
113+
public void close() {
114+
getUI().ifPresent(ui->ui.getPage().executeJavaScript("$0.close()", this));
126115
}
116+
127117
}

src/main/java/com/flowingcode/addons/applayout/AppHeader.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222

2323

24-
import com.flowingcode.addons.applayout.menu.MenuItem;
2524
import com.vaadin.flow.component.Component;
2625
import com.vaadin.flow.component.HasComponents;
2726
import com.vaadin.flow.component.Tag;

src/main/java/com/flowingcode/addons/applayout/AppLayout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
2525

26-
import com.flowingcode.addons.applayout.menu.MenuItem;
2726
import com.vaadin.flow.component.Component;
2827
import com.vaadin.flow.component.dependency.CssImport;
2928
import com.vaadin.flow.component.dependency.HtmlImport;
@@ -54,8 +53,8 @@ public class AppLayout extends Div implements PageConfigurator {
5453

5554
static final String NPM_VERSION = "3.0.2";
5655

57-
AppDrawer drawer;
58-
AppHeader header;
56+
private AppDrawer drawer;
57+
private AppHeader header;
5958

6059
public AppLayout(String title) {
6160
drawer = new AppDrawer(title);
@@ -131,4 +130,5 @@ public void setReveals(boolean reveals) {
131130
public void setSwipeOpen(boolean swipeOpen) {
132131
drawer.setSwipeOpen(swipeOpen);
133132
}
133+
134134
}

src/main/java/com/flowingcode/addons/applayout/AppToolbar.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

27-
import com.flowingcode.addons.applayout.menu.MenuItem;
2827
import com.vaadin.flow.component.Component;
2928
import com.vaadin.flow.component.HasComponents;
3029
import com.vaadin.flow.component.Tag;
@@ -76,25 +75,25 @@ public void setTitle(String title) {
7675
divTitle.setText(title);
7776
}
7877

79-
public void setToolbarIconButtons(MenuItem[] menuItems) {
80-
List<PaperIconButton> toolbarIconButtons = createToolbarIconButtons(menuItems);
78+
public void setToolbarIconButtons(MenuItem[] menuItems) {
8179
this.removeAll();
8280
this.add(menu);
8381
if (ctitle!=null) this.add(ctitle);
8482
if (divTitle!=null) this.add(divTitle);
85-
toolbarIconButtons.forEach(this::add);
83+
this.add(menuItems);
8684
}
87-
85+
86+
/*
8887
private static List<PaperIconButton> createToolbarIconButtons(MenuItem[] menuItems) {
88+
8989
List<PaperIconButton> result = new ArrayList<>();
9090
for (MenuItem menuItem : menuItems) {
91-
PaperIconButton paperIconButton = new PaperIconButton(menuItem.getIcon(),menuItem.getCommand());
91+
PaperIconButton paperIconButton = new PaperIconButton(menuItem.getIcon(), menuItem.getCommand());
9292
paperIconButton.setEnabled(menuItem.isEnabled());
93-
menuItem.setRefreshCallback(() -> paperIconButton.setIcon(menuItem.getIcon()));
9493
result.add(paperIconButton);
9594
}
9695
return result;
97-
}
96+
}*/
9897

9998
public void setMenuIconVisible(boolean visible) {
10099
menu.setVisible(visible);
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*-
2+
* #%L
3+
* App Layout Addon
4+
* %%
5+
* Copyright (C) 2018 - 2019 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.addons.applayout;
21+
22+
23+
24+
import java.util.EnumMap;
25+
import java.util.Optional;
26+
27+
import org.apache.commons.lang3.tuple.Pair;
28+
29+
import com.flowingcode.addons.applayout.MouseClickEvent.MouseButton;
30+
import com.vaadin.flow.component.Component;
31+
import com.vaadin.flow.component.HasEnabled;
32+
import com.vaadin.flow.component.HasOrderedComponents;
33+
import com.vaadin.flow.component.Tag;
34+
import com.vaadin.flow.component.dependency.HtmlImport;
35+
import com.vaadin.flow.component.icon.VaadinIcon;
36+
import com.vaadin.flow.server.Command;
37+
import com.vaadin.flow.shared.Registration;
38+
39+
/**
40+
* Menu item component.
41+
*
42+
* @author mlopez
43+
*
44+
*/
45+
@Tag("fc-menuitem")
46+
@HtmlImport("bower_components/fc-applayout/fc-menuitem.html")
47+
@SuppressWarnings("serial")
48+
public class MenuItem extends Component implements HasEnabled, HasOrderedComponents<MenuItem> {
49+
50+
private EnumMap<MouseButton, Pair<Command, Registration>> commands;
51+
52+
/** No argument constructor */
53+
public MenuItem() {}
54+
55+
/** Create a new instance of {@code MenuItem} with a labelicon. */
56+
public MenuItem(String label) {
57+
this.setLabel(label);
58+
}
59+
60+
/** Create a new instance of {@code MenuItem} with a label and left-button command. */
61+
public MenuItem(String label, Command command) {
62+
this.setLabel(label);
63+
this.setCommand(command);
64+
}
65+
66+
/** Create a new instance of {@code MenuItem} with a label, a {@code VaadinIcon}, and left-button command. */
67+
public MenuItem(String label, VaadinIcon icon, Command command) {
68+
this(label, command);
69+
setIcon(icon.create().getElement().getAttribute("icon"));
70+
}
71+
72+
/** Create a new instance of {@code MenuItem} with a label, an icon, and left-button command. */
73+
public MenuItem(String label, String icon, Command command) {
74+
this(label, command);
75+
setIcon(icon);
76+
}
77+
78+
@Override
79+
public void add(Component... components) {
80+
HasOrderedComponents.super.add(components);
81+
}
82+
83+
/**Adds the given menu items as children of this component.*/
84+
public final MenuItem add(MenuItem... items) {
85+
this.add((Component[])items);
86+
return this;
87+
}
88+
89+
public String getLabel() {
90+
return getElement().getAttribute("label");
91+
}
92+
93+
public MenuItem setLabel(String label) {
94+
getElement().setAttribute("label", label);
95+
return this;
96+
}
97+
98+
public String getIcon() {
99+
return getElement().getAttribute("icon");
100+
}
101+
102+
public MenuItem setIcon(String icon) {
103+
if (icon!=null) {
104+
getElement().setAttribute("icon", icon);
105+
getElement().removeAttribute("image");
106+
} else {
107+
getElement().removeAttribute("icon");
108+
}
109+
return this;
110+
}
111+
112+
public String getImage() {
113+
return getElement().getAttribute("image");
114+
}
115+
116+
public MenuItem setImage(String imageUrl) {
117+
if (imageUrl!=null) {
118+
getElement().setAttribute("image", imageUrl);
119+
getElement().removeAttribute("icon");
120+
} else {
121+
getElement().removeAttribute("image");
122+
}
123+
return this;
124+
}
125+
126+
public MenuItem setCommand(Command command) {
127+
setCommand(MouseButton.LEFT, command);
128+
return this;
129+
}
130+
131+
public MenuItem setCommand(MouseButton button, Command command) {
132+
if (command != null || commands != null && commands.containsKey(button)) {
133+
Optional.ofNullable(commands).orElseGet(()->commands=new EnumMap<>(MouseButton.class)).compute(button, (_button, commandAndRegistration)->{
134+
Optional.ofNullable(commandAndRegistration).map(Pair::getRight).ifPresent(Registration::remove);
135+
return command!=null ? Pair.of(command, addMouseClickEvent(ev->{if (ev.getButton()==button) command.execute();})) : null;
136+
});
137+
}
138+
return this;
139+
}
140+
141+
private Registration addMouseClickEvent(MouseClickListener<MenuItem> listener) {
142+
return getElement().addEventListener("item-click", ev->{
143+
int btnIndex = (int) ev.getEventData().getNumber("event.detail");
144+
MouseButton button = MouseButton.values()[btnIndex];
145+
listener.onComponentEvent(new MouseClickEvent<MenuItem>(this, button, true));
146+
}).addEventData("event.detail");
147+
}
148+
149+
public Command getCommand() {
150+
return getCommand(MouseButton.LEFT);
151+
}
152+
153+
public Command getCommand(MouseButton button) {
154+
return Optional.ofNullable(commands).map(m->m.get(button)).map(Pair::getLeft).orElse(null);
155+
}
156+
157+
}
158+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.flowingcode.addons.applayout;
2+
3+
import com.vaadin.flow.component.Component;
4+
import com.vaadin.flow.component.ComponentEvent;
5+
6+
public class MouseClickEvent<T extends Component> extends ComponentEvent<T> {
7+
8+
private static final long serialVersionUID = 1L;
9+
10+
public enum MouseButton {
11+
LEFT, MIDDLE, RIGHT;
12+
}
13+
14+
private MouseButton button;
15+
16+
public MouseClickEvent(T source, MouseButton button, boolean fromClient) {
17+
super(source, fromClient);
18+
this.button = button;
19+
}
20+
21+
public MouseButton getButton() {
22+
return button;
23+
}
24+
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.flowingcode.addons.applayout;
2+
3+
import com.vaadin.flow.component.Component;
4+
import com.vaadin.flow.component.ComponentEventListener;
5+
6+
@FunctionalInterface
7+
public interface MouseClickListener<T extends Component> extends ComponentEventListener<MouseClickEvent<T>> {
8+
9+
}

0 commit comments

Comments
 (0)