Skip to content

Commit 854f716

Browse files
committed
Add AbstractFcAppRouterLayout
1 parent 0281bb5 commit 854f716

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
import java.util.Optional;
23+
24+
import com.flowingcode.addons.applayout.AppLayout;
25+
import com.vaadin.flow.component.AttachEvent;
26+
import com.vaadin.flow.component.HasElement;
27+
import com.vaadin.flow.component.html.Div;
28+
import com.vaadin.flow.router.HasDynamicTitle;
29+
import com.vaadin.flow.router.PageTitle;
30+
import com.vaadin.flow.router.RouterLayout;
31+
32+
/**
33+
* Convenience class for using an AppLayout as a parent layout in a Flow application.
34+
* Basic usage involves extending this class and implementing
35+
* the {@code configure} method.
36+
*/
37+
public abstract class AbstractFcAppRouterLayout extends Div implements RouterLayout {
38+
39+
private static final long serialVersionUID = 1L;
40+
41+
private AppLayout app;
42+
43+
public AbstractFcAppRouterLayout() {
44+
setSizeFull();
45+
getElement().getStyle().set("display", "flex");
46+
getElement().getStyle().set("flex-direction", "column");
47+
app = new AppLayout("");
48+
app.setHeight("32px");
49+
app.addClassName("compact");
50+
app.setFixed(true);
51+
app.setSwipeOpen(false);
52+
add(app);
53+
}
54+
55+
@Override
56+
protected void onAttach(AttachEvent attachEvent) {
57+
super.onAttach(attachEvent);
58+
configure(app);
59+
}
60+
61+
protected abstract void configure(AppLayout app);
62+
63+
@Override
64+
public final void showRouterLayoutContent(HasElement content) {
65+
showRouterLayoutContent(app,content);
66+
}
67+
68+
protected void showRouterLayoutContent(AppLayout app, HasElement content) {
69+
content.getElement().getStyle().set("flex-grow", "1");
70+
content.getElement().getStyle().set("display", "flex");
71+
content.getElement().getStyle().set("flex-direction", "column");
72+
app.setCaption(getCaption(content));
73+
RouterLayout.super.showRouterLayoutContent(content);
74+
}
75+
76+
private String getCaption(HasElement content) {
77+
if (content instanceof HasDynamicTitle) {
78+
return ((HasDynamicTitle)content).getPageTitle();
79+
} else {
80+
return Optional.ofNullable(content.getClass().getAnnotation(PageTitle.class))
81+
.map(PageTitle::value).orElse("");
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)