3

I have java class which extends the Vaadin Div class, which should be a cookie warning popup (implementation is not yet complete):

//imports

@CssImport("css/info_popup.css")
@JsModule("js/script.js")
@Tag("info_popup")
public class CookiesDiv extends Div {

    public CookiesDiv() {
        this.setId("info_popup");

        Span span = new Span("By continuing to this site, you accept the EULA and the cookies. ");
        span.add(new Anchor("./info", "Learn more"));
        this.add(span);

        this.add(new Button("GOT IT", event -> {
            Page page = UI.getCurrent().getPage();
            this.getId().ifPresent(id -> page.executeJs("fadeOut('" + id + "')"));
        }));
    }
}

When the 'GOT IT' button is pressed, a JS function written in the 'script.js' is called, which animates the div and then removes it from the page:

window.fadeOut = function(target) {
    target.style = 'animation: fade-out 0.5s ease-out 0s forwards'
    setTimeout(function() {
        target.style = 'display: none'
    }, 500)
}

My issue is: the file is successfully imported, but when I test the button the browser console throws the following error: "Uncaught ReferenceError: fadeOut is not defined".

I read some forums which said that in Vaadin 14 changing function fadeOut(target) {} to window.fadeOut = function(target) {} works, but feels like nothing changed.

This is my main tree:

main
├───java
│   └───com
│       └───Achille004
│           └───FreeTransfer
│               ├───backend
│               │   ├───database
│               │   ├───handlers
│               │   └───security
│               └───frontend
│                   ├───css
│                   ├───js
│                   ├───pages
│                   └───views
└───resources
    ├───META-INF
    │   └───resources
    │       └───img
    ├───static
    └───templates

CookiesDiv.java is located into pages and css and js modules are in their own folder.

I could not find other stuff online, so I am just lost. Does someone know how to fix?

P.S. I do not know if it could help: most of the proprieties in the CSS file are ignored, but I still have to do some research about it.

Achille004
  • 78
  • 5

1 Answers1

3

Your imports should be

@CssImport("./css/info_popup.css")
@JsModule("./js/script.js")
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • Well adding ./ to the paths gives a module resolving error. I've added the main tree to the question. – Achille004 Aug 11 '22 at 13:36
  • PS: As you can see without using `./` should work, in fact I trued to write in the JavaScript file this simple function `window.testCall = function() { window.alert('Works!') }` and it gives no error. – Achille004 Aug 11 '22 at 13:44
  • The files must be in /frontend/css resp. js – Simon Martinelli Aug 11 '22 at 13:58
  • It works, thank you. Also one last thing, if i wanted to keep my css and js folders as they were before, do I have to write the entire path every time or there is a way to make it easier, maybe with some application proprieties or something like that? – Achille004 Aug 11 '22 at 14:18
  • 1
    No this will never work. You have to follow the Vaadin directory structure. Once you will use a theme this will also go to the frontend folder – Simon Martinelli Aug 11 '22 at 14:30