Skip to content

Commit 0771911

Browse files
vaadin-bottaefi
andauthored
feat: add API to fetch page direction (#15598) (#15608)
Add API to fetch the document.dir from browser and pass it to the provided callback. Fixes #15597 Co-authored-by: Soroosh Taefi <taefi.soroosh@gmail.com>
1 parent 444befa commit 0771911

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

  • flow-server/src/main/java/com/vaadin/flow/component/page
  • flow-tests/test-root-context/src

‎flow-server/src/main/java/com/vaadin/flow/component/page/Page.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import java.net.URI;
2525
import java.net.URL;
2626
import java.nio.charset.StandardCharsets;
27+
import java.util.Arrays;
2728
import java.util.Objects;
2829
import java.util.function.Function;
2930

3031
import com.vaadin.flow.component.ClientCallable;
3132
import com.vaadin.flow.component.Component;
3233
import com.vaadin.flow.component.ComponentEvent;
34+
import com.vaadin.flow.component.Direction;
3335
import com.vaadin.flow.component.Tag;
3436
import com.vaadin.flow.component.UI;
3537
import com.vaadin.flow.component.dependency.JavaScript;
@@ -655,4 +657,34 @@ public void fetchCurrentURL(SerializableConsumer<URL> callback) {
655657
}
656658
});
657659
}
660+
661+
/**
662+
* Retrieves {@code document.dir} of the current UI from the browser and
663+
* passes it to the {@code callback} parameter. If the {@code document.dir}
664+
* has not been set explicitly, then {@code Direction.LEFT_TO_RIGHT} will be
665+
* the fallback value.
666+
* <p>
667+
* Note that direction is fetched from the browser in an asynchronous
668+
* request and passed to the callback.
669+
* <p>
670+
* In case you need more control over the execution you can use
671+
* {@link #executeJs(String, Serializable...)} by passing
672+
* {@code return document.dir}.
673+
*
674+
* @param callback
675+
* to be notified when the direction is resolved.
676+
*/
677+
public void fetchPageDirection(SerializableConsumer<Direction> callback) {
678+
executeJs("return document.dir").then(String.class, dir -> {
679+
Direction direction = getDirectionByClientName(dir);
680+
callback.accept(direction);
681+
});
682+
}
683+
684+
private Direction getDirectionByClientName(String directionClientName) {
685+
return Arrays.stream(Direction.values())
686+
.filter(direction -> direction.getClientName()
687+
.equals(directionClientName))
688+
.findFirst().orElse(Direction.LEFT_TO_RIGHT);
689+
}
658690
}

‎flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PageView.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.vaadin.flow.uitest.ui;
22

3+
import com.vaadin.flow.component.Direction;
34
import com.vaadin.flow.component.html.Div;
45
import com.vaadin.flow.component.html.IFrame;
56
import com.vaadin.flow.component.html.Input;
7+
import com.vaadin.flow.component.html.Label;
68
import com.vaadin.flow.component.html.NativeButton;
79
import com.vaadin.flow.router.BeforeEnterEvent;
810
import com.vaadin.flow.router.Route;
@@ -78,6 +80,32 @@ public void beforeEnter(BeforeEnterEvent event) {
7880
.info(currentUrl.toString());
7981
}));
8082
}));
83+
84+
Label directionLbl = new Label();
85+
directionLbl.setId("direction-value");
86+
add(directionLbl);
87+
88+
Div fetchDirectionButton = new Div();
89+
fetchDirectionButton.setId("fetch-direction");
90+
fetchDirectionButton.setText("Fetch Page Direction");
91+
fetchDirectionButton.addClickListener(
92+
e -> getUI().ifPresent(ui -> ui.getPage().fetchPageDirection(
93+
direction -> directionLbl.setText(direction.name()))));
94+
add(fetchDirectionButton);
95+
96+
Div setRTLDirectionButton = new Div();
97+
setRTLDirectionButton.setId("set-RTL-direction");
98+
setRTLDirectionButton.setText("Set RTL Direction");
99+
setRTLDirectionButton.addClickListener(e -> getUI()
100+
.ifPresent(ui -> ui.setDirection(Direction.RIGHT_TO_LEFT)));
101+
add(setRTLDirectionButton);
102+
103+
Div setLTRDirectionButton = new Div();
104+
setLTRDirectionButton.setId("set-LTR-direction");
105+
setLTRDirectionButton.setText("Set LTR Direction");
106+
setLTRDirectionButton.addClickListener(e -> getUI()
107+
.ifPresent(ui -> ui.setDirection(Direction.LEFT_TO_RIGHT)));
108+
add(setLTRDirectionButton);
81109
}
82110

83111
}

‎flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/PageIT.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,49 @@ public void testOpenUrlInIFrame() throws InterruptedException {
109109
Matchers.endsWith(BaseHrefView.class.getName()));
110110
}
111111

112+
@Test
113+
public void fetchPageDirection_noDirectionSetExplicitly_leftToRightIsPassedToCallback() {
114+
open();
115+
116+
Assert.assertEquals("",
117+
findElement(By.id("direction-value")).getText());
118+
119+
findElement(By.id("fetch-direction")).click();
120+
121+
Assert.assertEquals("LEFT_TO_RIGHT",
122+
findElement(By.id("direction-value")).getText());
123+
}
124+
125+
@Test
126+
public void fetchPageDirection_setRTLDirection_rightToLeftIsPassedToCallback() {
127+
open();
128+
129+
Assert.assertEquals("",
130+
findElement(By.id("direction-value")).getText());
131+
132+
findElement(By.id("set-RTL-direction")).click();
133+
134+
findElement(By.id("fetch-direction")).click();
135+
136+
Assert.assertEquals("RIGHT_TO_LEFT",
137+
findElement(By.id("direction-value")).getText());
138+
}
139+
140+
@Test
141+
public void fetchPageDirection_setLTRDirection_leftToRightIsPassedToCallback() {
142+
open();
143+
144+
Assert.assertEquals("",
145+
findElement(By.id("direction-value")).getText());
146+
147+
findElement(By.id("set-LTR-direction")).click();
148+
149+
findElement(By.id("fetch-direction")).click();
150+
151+
Assert.assertEquals("LEFT_TO_RIGHT",
152+
findElement(By.id("direction-value")).getText());
153+
}
154+
112155
private String getIframeUrl() {
113156
return (String) ((JavascriptExecutor) driver).executeScript(
114157
"return document.getElementById('newWindow').contentWindow.location.href;");

0 commit comments

Comments
 (0)