Skip to content

Commit d84ea88

Browse files
authored
feat: add i18n property to AppLayout (#2448)
1 parent 81e15d7 commit d84ea88

4 files changed

Lines changed: 243 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.applayout.examples;
17+
18+
import com.vaadin.flow.component.applayout.AppLayout;
19+
import com.vaadin.flow.component.applayout.AppLayout.AppLayoutI18n;
20+
import com.vaadin.flow.component.html.Div;
21+
import com.vaadin.flow.component.html.NativeButton;
22+
import com.vaadin.flow.router.Route;
23+
24+
@Route("vaadin-app-layout/i18n")
25+
public class AppLayoutI18nPage extends Div {
26+
public AppLayoutI18nPage() {
27+
AppLayout layout = new AppLayout();
28+
add(layout);
29+
30+
NativeButton toggleAttachedButton = new NativeButton("toggle attached",
31+
e -> {
32+
if (layout.getParent().isPresent()) {
33+
remove(layout);
34+
} else {
35+
add(layout);
36+
}
37+
});
38+
toggleAttachedButton.setId("toggle-attached");
39+
40+
NativeButton setI18nButton = new NativeButton("set i18n", e -> {
41+
AppLayoutI18n i18n = new AppLayout.AppLayoutI18n()
42+
.setDrawer("Custom drawer");
43+
layout.setI18n(i18n);
44+
});
45+
setI18nButton.setId("set-i18n");
46+
47+
NativeButton setEmptyI18nButton = new NativeButton("set empty i18n",
48+
e -> {
49+
AppLayoutI18n i18n = new AppLayout.AppLayoutI18n();
50+
layout.setI18n(i18n);
51+
});
52+
setEmptyI18nButton.setId("set-empty-i18n");
53+
54+
add(setI18nButton, setEmptyI18nButton, toggleAttachedButton);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.applayout.test;
17+
18+
import com.vaadin.flow.component.applayout.testbench.AppLayoutElement;
19+
import com.vaadin.flow.testutil.TestPath;
20+
import com.vaadin.tests.AbstractComponentIT;
21+
22+
import org.junit.Test;
23+
import org.junit.Assert;
24+
import org.junit.Before;
25+
26+
@TestPath("vaadin-app-layout/i18n")
27+
public class AppLayoutI18nIT extends AbstractComponentIT {
28+
private AppLayoutElement layout;
29+
30+
@Before
31+
public void init() {
32+
open();
33+
layout = $(AppLayoutElement.class).first();
34+
}
35+
36+
@Test
37+
public void setEmptyI18n_defaultI18nIsNotOverridden() {
38+
clickButton("set-empty-i18n");
39+
40+
Assert.assertNotNull(
41+
"The i18n drawer property should contain the default value",
42+
layout.getPropertyString("i18n", "drawer"));
43+
}
44+
45+
@Test
46+
public void setI18n_i18nIsUpdated() {
47+
clickButton("set-i18n");
48+
49+
Assert.assertEquals(
50+
"The i18n drawer property should contain a custom value",
51+
"Custom drawer", layout.getPropertyString("i18n", "drawer"));
52+
}
53+
54+
@Test
55+
public void setI18n_detach_attach_i18nIsPersisted() {
56+
clickButton("set-i18n");
57+
clickButton("toggle-attached");
58+
clickButton("toggle-attached");
59+
60+
layout = $(AppLayoutElement.class).first();
61+
62+
Assert.assertEquals(
63+
"The i18n drawer property should contain a custom value",
64+
"Custom drawer", layout.getPropertyString("i18n", "drawer"));
65+
}
66+
67+
private void clickButton(String id) {
68+
$("button").id(id).click();
69+
}
70+
}

‎vaadin-app-layout-flow-parent/vaadin-app-layout-flow/src/main/java/com/vaadin/flow/component/applayout/AppLayout.java‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@
2020
* #L%
2121
*/
2222

23+
import com.vaadin.flow.component.AttachEvent;
2324
import com.vaadin.flow.component.Component;
2425
import com.vaadin.flow.component.HasElement;
2526
import com.vaadin.flow.component.PropertyDescriptor;
2627
import com.vaadin.flow.component.PropertyDescriptors;
2728
import com.vaadin.flow.component.Synchronize;
2829
import com.vaadin.flow.component.Tag;
30+
import com.vaadin.flow.component.UI;
2931
import com.vaadin.flow.component.dependency.JsModule;
3032
import com.vaadin.flow.component.dependency.NpmPackage;
33+
import com.vaadin.flow.function.SerializableConsumer;
34+
import com.vaadin.flow.internal.JsonSerializer;
3135
import com.vaadin.flow.router.RouterLayout;
3236

37+
import java.io.Serializable;
3338
import java.util.Objects;
3439

40+
import elemental.json.JsonObject;
41+
import elemental.json.JsonType;
42+
3543
/**
3644
* Server-side component for the {@code <vaadin-app-layout>} element. Provides a
3745
* quick and easy way to get a common application layout.
@@ -51,6 +59,77 @@ public class AppLayout extends Component implements RouterLayout {
5159

5260
private Component content;
5361

62+
private AppLayoutI18n i18n;
63+
64+
/**
65+
* Gets the internationalization object previously set for this component.
66+
* <p>
67+
* Note: updating the i18n object that is returned from this method will not
68+
* update the the component, unless it is set again using
69+
* {@link AppLayout#setI18n(AppLayoutI18n)}
70+
*
71+
* @return the i18n object. It will be <code>null</code>, if the i18n
72+
* properties are not set.
73+
*/
74+
public AppLayoutI18n getI18n() {
75+
return i18n;
76+
}
77+
78+
/**
79+
* Sets the internationalization properties for this component.
80+
*
81+
* @param i18n
82+
* the internationalized properties, not <code>null</code>
83+
*/
84+
public void setI18n(AppLayoutI18n i18n) {
85+
Objects.requireNonNull(i18n,
86+
"The I18N properties object should not be null");
87+
this.i18n = i18n;
88+
89+
runBeforeClientResponse(ui -> {
90+
if (i18n == this.i18n) {
91+
setI18nWithJS();
92+
}
93+
});
94+
}
95+
96+
private void setI18nWithJS() {
97+
JsonObject i18nJson = (JsonObject) JsonSerializer.toJson(this.i18n);
98+
99+
// Remove properties with null values to prevent errors in web
100+
// component
101+
removeNullValuesFromJsonObject(i18nJson);
102+
103+
// Assign new I18N object to WC, by merging the existing
104+
// WC I18N, and the values from the new AppLayoutI18n instance,
105+
// into an empty object
106+
getElement().executeJs("this.i18n = Object.assign({}, this.i18n, $0);",
107+
i18nJson);
108+
}
109+
110+
@Override
111+
protected void onAttach(AttachEvent attachEvent) {
112+
super.onAttach(attachEvent);
113+
114+
// Element state is not persisted across attach/detach
115+
if (this.i18n != null) {
116+
setI18nWithJS();
117+
}
118+
}
119+
120+
private void removeNullValuesFromJsonObject(JsonObject jsonObject) {
121+
for (String key : jsonObject.keys()) {
122+
if (jsonObject.get(key).getType() == JsonType.NULL) {
123+
jsonObject.remove(key);
124+
}
125+
}
126+
}
127+
128+
private void runBeforeClientResponse(SerializableConsumer<UI> command) {
129+
getElement().getNode().runWhenAttached(ui -> ui
130+
.beforeClientResponse(this, context -> command.accept(ui)));
131+
}
132+
54133
/**
55134
* @see #setPrimarySection(Section)
56135
* @return value for the primarySection property. Default is
@@ -284,4 +363,34 @@ public static Section fromWebcomponentValue(String webcomponentValue) {
284363
: null;
285364
}
286365
}
366+
367+
/**
368+
* The internationalization properties for {@link AppLayout}
369+
*/
370+
public static class AppLayoutI18n implements Serializable {
371+
private String drawer;
372+
373+
/**
374+
* Gets the text for the `aria-label` attribute on the drawer.
375+
*
376+
* @return the drawer aria-label
377+
*/
378+
public String getDrawer() {
379+
return drawer;
380+
}
381+
382+
/**
383+
* Sets the text for the `aria-label` attribute on the drawer. The
384+
* attribute is set when the drawer is in the overlay mode and announced
385+
* once the drawer is opened.
386+
*
387+
* @param drawer
388+
* the drawer aria-label
389+
* @return this instance for method chaining
390+
*/
391+
public AppLayoutI18n setDrawer(String drawer) {
392+
this.drawer = drawer;
393+
return this;
394+
}
395+
}
287396
}

‎vaadin-app-layout-flow-parent/vaadin-app-layout-flow/src/test/java/com/vaadin/flow/component/applayout/AppLayoutTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ public void testDrawerClose() {
171171
testDrawerOpened(false);
172172
}
173173

174+
@Test
175+
public void setI18n() {
176+
AppLayout.AppLayoutI18n i18n = new AppLayout.AppLayoutI18n()
177+
.setDrawer("Custom Drawer");
178+
systemUnderTest.setI18n(i18n);
179+
assertEquals(i18n, systemUnderTest.getI18n());
180+
}
181+
174182
private void testDrawerOpened(boolean expectedDrawerOpened) {
175183
assertEquals(expectedDrawerOpened, systemUnderTest.getElement()
176184
.getProperty("drawerOpened", false));

0 commit comments

Comments
 (0)