Hello All ! (hope you are at best),
I would like to define styles dynamically but it does not work.
These styles are to be applied on nodes of a TreeGrid with :
addColumn(createTemplate()).setClassNameGenerator(new ValueProvider<UITreeNode<DATA>, String>() {
// Style name generator.
@Override
public String apply(UITreeNode<DATA> node) {
return node.getStyle();
}
});
As a test case, I define a string (later it will be built by enumerating styles like words from a dictionary; all combinations for bold, italic, underline, color, … ; each combination has a functional meaning) :
private static final String STYLE =
"import '@vaadin/vaadin-grid/vaadin-grid.js';\n"
+ "import { html } from '@polymer/polymer/lib/utils/html-tag.js';\n"
+ "const $_documentContainer = html`\n"
+ "<dom-module id=\"uitree-style\" theme-for=\"vaadin-grid\">"
+ " <template>"
+ " <style>"
+ " [part~=\"cell\"]
.style1 {"
+ " background: blue;"
+ " }"
+ " [part~=\"cell\"]
.style2 {"
+ " background: yellow;"
+ " }"
+ " </style>"
+ " </template>"
+ "</dom-module>`;\n"
+ "document.head.appendChild($_documentContainer.content);";
Then in the constructor :
VaadinSession session = UI.getCurrent().getSession();
StreamResourceRegistry registry=session.getResourceRegistry();
StreamRegistration resource =
registry.registerResource(new StreamResource("uitree-style.js", new InputStreamFactory() {
@Override
public InputStream createInputStream() {
byte[] bytes = STYLE.getBytes(StandardCharsets.UTF_8);
return new ByteArrayInputStream(bytes);
}
}));
String url = resource.getResourceUri().toString();
System.out.println("url=" + url);
Page page = UI.getCurrent().getPage();
page.addJsModule("file://" + url); // No effect.
I use spring-boot-starter-parent 2.1.6.RELEASE and Vaadin 14.1.16 .
All is fine when exactly the same String STYLE is put in a file “frontend\styles\custom-tree.js” and the annotation @JsModule(“./styles/custom-tree.js”) is used (of course, i could put the definitions of our 64 styles there).
What is the right syntax for the url in method addJsModule ?
Thanks and best regards,
Nicolas Culem