Class PlaceManager

java.lang.Object
org.jboss.elemento.router.PlaceManager

public class PlaceManager extends Object
The place manager is the central part of the router. It keeps track of registered places, handles navigation events, and updates the DOM accordingly. The place manager can be customized using builder like methods and has a start() method to show the initial page.

public static class TimeLoader implements LoadData<String> {

    @Override
    public Promise<String> load(Place place, Parameter parameter) {
        String area = parameter.get("area");
        String location = parameter.get("location");
        String url = "https://worldtimeapi.org/api/timezone/" + area + "/" + location;
        return fetch(url)
                .then(Response::json)
                .then(json -> {
                    JsPropertyMap<String> map = Js.cast(json);
                    return Promise.resolve(map.get("datetime"));
                });
    }
}

public static class TimePage implements Page {

    @Override
    public Iterable<HTMLElement> elements(Place place, Parameter parameter, LoadedData data) {
        String area = parameter.get("area");
        String location = parameter.get("location");
        String currentTime = data.get();
        return asList(
                h(1, "Current time").element(),
                p()
                        .add("It's ")
                        .add(span().text(currentTime))
                        .add(" in " + area + "/" + location)
                        .element());
    }
}

public static class HomePage implements Page {

    @Override
    public Iterable<HTMLElement> elements(Place place, Parameter parameter, LoadedData data) {
        return asList(
                h(1, "Welcome").element(),
                p()
                        .add("What time is it in ")
                        .add(a("/time/Europe/Berlin").text("Berlin"))
                        .add("?")
                        .element());
    }
}

public static class Application {

    public void entryPoint() {
        body().add(div().id("main"));

        Places places = Places.places()
                .add(place("/"), HomePage::new)
                .add(place("/time/:area/:location")
                        .loader(new TimeLoader()), HomePage::new);

        PlaceManager placeManager = new PlaceManager()
                .root(By.id("main"))
                .register(places);
        placeManager.start();
    }
}
See Also: