|
1 | | -/*- |
2 | | - * #%L |
3 | | - * App Layout Addon |
4 | | - * %% |
5 | | - * Copyright (C) 2018 - 2021 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 | | -import com.vaadin.flow.component.Component; |
23 | | -import com.vaadin.flow.component.Tag; |
24 | | -import com.vaadin.flow.component.dependency.CssImport; |
25 | | -import com.vaadin.flow.component.dependency.JsModule; |
26 | | -import com.vaadin.flow.component.dependency.NpmPackage; |
27 | | -import com.vaadin.flow.component.html.Div; |
28 | | -import com.vaadin.flow.component.html.Image; |
29 | | -import com.vaadin.flow.server.InitialPageSettings; |
30 | | -import com.vaadin.flow.server.PageConfigurator; |
31 | | -import java.util.ArrayList; |
32 | | -import java.util.Arrays; |
33 | | -import java.util.List; |
34 | | - |
35 | | -/** |
36 | | - * Component that renders the div that contains the entire layout. |
37 | | - * |
38 | | - * @author mlopez |
39 | | - */ |
40 | | -@SuppressWarnings("serial") |
41 | | -@Tag("fc-applayout") |
42 | | -@JsModule("@flowingcode/fc-applayout/fc-applayout.js") |
43 | | -@NpmPackage(value = "@flowingcode/fc-applayout", version = "0.9.3") |
44 | | -@CssImport(value = "./styles/applayout-styles.css", themeFor = "fc-applayout") |
45 | | -public class AppLayout extends Div implements PageConfigurator { |
46 | | - |
47 | | - private static final String PROFILE_SLOT_NAME = "profile"; |
48 | | - private static final String APP_LAYOUT_TITLE_SLOT_NAME = "title"; |
49 | | - private static final String TITLE_ATTRIBUTE_NAME = "title"; |
50 | | - private final List<Component> menuItems = new ArrayList<>(); |
51 | | - private final List<Component> toolbarComponents = new ArrayList<>(); |
52 | | - |
53 | | - public AppLayout(String title) { |
54 | | - this(null, title, null); |
55 | | - } |
56 | | - |
57 | | - public AppLayout(Component menuHeader, String title) { |
58 | | - this(menuHeader, title, null); |
59 | | - } |
60 | | - |
61 | | - public AppLayout(Image logo, Component menuHeader, String title) { |
62 | | - this(menuHeader, title, logo); |
63 | | - } |
64 | | - |
65 | | - private AppLayout(Component menuHeader, String aTitle, Image aLogo) { |
66 | | - if (aLogo != null) { |
67 | | - addToTitleSection(aLogo); |
68 | | - } |
69 | | - if (menuHeader != null) { |
70 | | - setMenuHeader(menuHeader); |
71 | | - } |
72 | | - Div title = new Div(); |
73 | | - title.setText(aTitle); |
74 | | - addToTitleSection(title); |
75 | | - } |
76 | | - |
77 | | - public void addToTitleSection(Component component) { |
78 | | - component.getElement().setAttribute("slot", APP_LAYOUT_TITLE_SLOT_NAME); |
79 | | - add(component); |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Sets the component to be shown before the menu in the drawer. |
84 | | - * @param menuHeader |
85 | | - */ |
86 | | - public void setMenuHeader(Component menuHeader) { |
87 | | - getChildren().filter(item->PROFILE_SLOT_NAME.equals(item.getElement().getAttribute("slot"))).forEach(this::remove); |
88 | | - menuHeader.getElement().setAttribute("slot", PROFILE_SLOT_NAME); |
89 | | - add(menuHeader); |
90 | | - } |
91 | | - |
92 | | - public void setMenuItems(Component... someMenuitems) { |
93 | | - this.menuItems.addAll(Arrays.asList(someMenuitems)); |
94 | | - this.menuItems.forEach(item -> item.getElement().setAttribute("slot", "menu")); |
95 | | - this.add(someMenuitems); |
96 | | - } |
97 | | - |
98 | | - public void clearMenuItems() { |
99 | | - this.getChildren() |
100 | | - .forEach( |
101 | | - item -> { |
102 | | - if (this.menuItems.contains(item)) this.remove(item); |
103 | | - }); |
104 | | - this.menuItems.clear(); |
105 | | - } |
106 | | - |
107 | | - public void setToolbarIconButtons(Component... components) { |
108 | | - toolbarComponents.forEach(this::remove); |
109 | | - addToolbarIconButtons(components); |
110 | | - } |
111 | | - |
112 | | - public void addToolbarIconButtons(Component... components) { |
113 | | - List<Component> componentsToAdd = Arrays.asList(components); |
114 | | - componentsToAdd.forEach(comp -> comp.getElement().setAttribute("slot", "toolbar")); |
115 | | - toolbarComponents.addAll(componentsToAdd); |
116 | | - this.add(components); |
117 | | - } |
118 | | - |
119 | | - public void addToolbarIconButtonAsFirst(Component component) { |
120 | | - toolbarComponents.add(0, component); |
121 | | - toolbarComponents.forEach(this::remove); |
122 | | - addToolbarIconButtons(toolbarComponents.toArray(new Component[toolbarComponents.size()])); |
123 | | - } |
124 | | - |
125 | | - public void clearToolbarIconButtons() { |
126 | | - toolbarComponents.forEach(this::remove); |
127 | | - toolbarComponents.clear(); |
128 | | - } |
129 | | - |
130 | | - public void setMenuVisible(boolean visible) { |
131 | | - this.getElement().setProperty("drawerVisible", visible); |
132 | | - } |
133 | | - |
134 | | - public boolean isMenuVisible() { |
135 | | - return this.getElement().getProperty("drawerVisible", true); |
136 | | - } |
137 | | - |
138 | | - @Override |
139 | | - public void configurePage(InitialPageSettings settings) { |
140 | | - settings.addMetaTag("viewport", "width=device-width, initial-scale=1.0"); |
141 | | - } |
142 | | - |
143 | | - /** |
144 | | - * Sets the toolbar title |
145 | | - * @param caption |
146 | | - */ |
147 | | - public void setCaption(String caption) { |
148 | | - this.getElement().setAttribute(TITLE_ATTRIBUTE_NAME, caption); |
149 | | - } |
150 | | - |
151 | | - /** |
152 | | - * Sets the fixed attribute so it mantains the header fixed at the top |
153 | | - * so it never moves away. |
154 | | - * @param fixed |
155 | | - */ |
156 | | - public void setFixed(boolean fixed) { |
157 | | - this.getElement().setAttribute("fixed", fixed); |
158 | | - } |
159 | | - |
160 | | - /** |
161 | | - * Sets the reveals attribute so it slides back the header when scrolling |
162 | | - * back up. |
163 | | - * @param reveals |
164 | | - */ |
165 | | - public void setReveals(boolean reveals) { |
166 | | - this.getElement().setAttribute("reveals", reveals); |
167 | | - } |
168 | | - |
169 | | - /** |
170 | | - * Sets the swipeOpen attribute so it creates an area at the edge of the |
171 | | - * screen to swipe open the app-drawer |
172 | | - * @param swipeOpen |
173 | | - */ |
174 | | - public void setSwipeOpen(boolean swipeOpen) { |
175 | | - this.getElement().setAttribute("swipeOpen", swipeOpen); |
176 | | - } |
177 | | - |
178 | | -} |
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * App Layout Addon |
| 4 | + * %% |
| 5 | + * Copyright (C) 2018 - 2021 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 | +import com.vaadin.flow.component.Component; |
| 23 | +import com.vaadin.flow.component.Tag; |
| 24 | +import com.vaadin.flow.component.dependency.CssImport; |
| 25 | +import com.vaadin.flow.component.dependency.JsModule; |
| 26 | +import com.vaadin.flow.component.dependency.NpmPackage; |
| 27 | +import com.vaadin.flow.component.html.Div; |
| 28 | +import com.vaadin.flow.component.html.Image; |
| 29 | +import com.vaadin.flow.server.InitialPageSettings; |
| 30 | +import com.vaadin.flow.server.PageConfigurator; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/** |
| 36 | + * Component that renders the div that contains the entire layout. |
| 37 | + * |
| 38 | + * @author mlopez |
| 39 | + */ |
| 40 | +@SuppressWarnings("serial") |
| 41 | +@Tag("fc-applayout") |
| 42 | +@JsModule("@flowingcode/fc-applayout/fc-applayout.js") |
| 43 | +@NpmPackage(value = "@flowingcode/fc-applayout", version = "0.9.5") |
| 44 | +@CssImport(value = "./styles/applayout-styles.css", themeFor = "fc-applayout") |
| 45 | +public class AppLayout extends Div implements PageConfigurator { |
| 46 | + |
| 47 | + private static final String PROFILE_SLOT_NAME = "profile"; |
| 48 | + private static final String APP_LAYOUT_TITLE_SLOT_NAME = "title"; |
| 49 | + private static final String TITLE_ATTRIBUTE_NAME = "title"; |
| 50 | + private final List<Component> menuItems = new ArrayList<>(); |
| 51 | + private final List<Component> toolbarComponents = new ArrayList<>(); |
| 52 | + |
| 53 | + public AppLayout(String title) { |
| 54 | + this(null, title, null); |
| 55 | + } |
| 56 | + |
| 57 | + public AppLayout(Component menuHeader, String title) { |
| 58 | + this(menuHeader, title, null); |
| 59 | + } |
| 60 | + |
| 61 | + public AppLayout(Image logo, Component menuHeader, String title) { |
| 62 | + this(menuHeader, title, logo); |
| 63 | + } |
| 64 | + |
| 65 | + private AppLayout(Component menuHeader, String aTitle, Image aLogo) { |
| 66 | + if (aLogo != null) { |
| 67 | + addToTitleSection(aLogo); |
| 68 | + } |
| 69 | + if (menuHeader != null) { |
| 70 | + setMenuHeader(menuHeader); |
| 71 | + } |
| 72 | + Div title = new Div(); |
| 73 | + title.setText(aTitle); |
| 74 | + addToTitleSection(title); |
| 75 | + } |
| 76 | + |
| 77 | + public void addToTitleSection(Component component) { |
| 78 | + component.getElement().setAttribute("slot", APP_LAYOUT_TITLE_SLOT_NAME); |
| 79 | + add(component); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sets the component to be shown before the menu in the drawer. |
| 84 | + * @param menuHeader |
| 85 | + */ |
| 86 | + public void setMenuHeader(Component menuHeader) { |
| 87 | + getChildren().filter(item->PROFILE_SLOT_NAME.equals(item.getElement().getAttribute("slot"))).forEach(this::remove); |
| 88 | + menuHeader.getElement().setAttribute("slot", PROFILE_SLOT_NAME); |
| 89 | + add(menuHeader); |
| 90 | + } |
| 91 | + |
| 92 | + public void setMenuItems(Component... someMenuitems) { |
| 93 | + this.menuItems.addAll(Arrays.asList(someMenuitems)); |
| 94 | + this.menuItems.forEach(item -> item.getElement().setAttribute("slot", "menu")); |
| 95 | + this.add(someMenuitems); |
| 96 | + } |
| 97 | + |
| 98 | + public void clearMenuItems() { |
| 99 | + this.getChildren() |
| 100 | + .forEach( |
| 101 | + item -> { |
| 102 | + if (this.menuItems.contains(item)) this.remove(item); |
| 103 | + }); |
| 104 | + this.menuItems.clear(); |
| 105 | + } |
| 106 | + |
| 107 | + public void setToolbarIconButtons(Component... components) { |
| 108 | + toolbarComponents.forEach(this::remove); |
| 109 | + addToolbarIconButtons(components); |
| 110 | + } |
| 111 | + |
| 112 | + public void addToolbarIconButtons(Component... components) { |
| 113 | + List<Component> componentsToAdd = Arrays.asList(components); |
| 114 | + componentsToAdd.forEach(comp -> comp.getElement().setAttribute("slot", "toolbar")); |
| 115 | + toolbarComponents.addAll(componentsToAdd); |
| 116 | + this.add(components); |
| 117 | + } |
| 118 | + |
| 119 | + public void addToolbarIconButtonAsFirst(Component component) { |
| 120 | + toolbarComponents.add(0, component); |
| 121 | + toolbarComponents.forEach(this::remove); |
| 122 | + addToolbarIconButtons(toolbarComponents.toArray(new Component[toolbarComponents.size()])); |
| 123 | + } |
| 124 | + |
| 125 | + public void clearToolbarIconButtons() { |
| 126 | + toolbarComponents.forEach(this::remove); |
| 127 | + toolbarComponents.clear(); |
| 128 | + } |
| 129 | + |
| 130 | + public void setMenuVisible(boolean visible) { |
| 131 | + this.getElement().setProperty("drawerVisible", visible); |
| 132 | + } |
| 133 | + |
| 134 | + public boolean isMenuVisible() { |
| 135 | + return this.getElement().getProperty("drawerVisible", true); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void configurePage(InitialPageSettings settings) { |
| 140 | + settings.addMetaTag("viewport", "width=device-width, initial-scale=1.0"); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Sets the toolbar title |
| 145 | + * @param caption |
| 146 | + */ |
| 147 | + public void setCaption(String caption) { |
| 148 | + this.getElement().setAttribute(TITLE_ATTRIBUTE_NAME, caption); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Sets the fixed attribute so it mantains the header fixed at the top |
| 153 | + * so it never moves away. |
| 154 | + * @param fixed |
| 155 | + */ |
| 156 | + public void setFixed(boolean fixed) { |
| 157 | + this.getElement().setAttribute("fixed", fixed); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the reveals attribute so it slides back the header when scrolling |
| 162 | + * back up. |
| 163 | + * @param reveals |
| 164 | + */ |
| 165 | + public void setReveals(boolean reveals) { |
| 166 | + this.getElement().setAttribute("reveals", reveals); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Sets the swipeOpen attribute so it creates an area at the edge of the |
| 171 | + * screen to swipe open the app-drawer |
| 172 | + * @param swipeOpen |
| 173 | + */ |
| 174 | + public void setSwipeOpen(boolean swipeOpen) { |
| 175 | + this.getElement().setAttribute("swipeOpen", swipeOpen); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments