Hello,
I could not find documentation on how to add stylesheets and JavaScript
files to all pages.
the wiki page
(http://wiki.apache.org/tapestry/Tapestry5HowToAddValidators) is
questioning if this is possible as well.
I build the following working solution from exploring the Tapestry code.
Can someone please review the code. I am not sure, if this is the
correct way. I would like to add it to the wiki. In my opinion it should
be in the default documentation as well.
Possible target pages in the wiki
http://wiki.apache.org/tapestry/Tapestry5AndJavaScriptExplained
Snippet from my AppModule
/**
* Insert global stylesheets and JavaScript files
* @param configuration
* @param symbolSource
* @param assetSource
*/
public void
contributeMarkupRenderer(OrderedConfiguration<MarkupRendererFilter>
configuration,
final SymbolSource symbolSource, final AssetSource assetSource) {
MarkupRendererFilter injectDefaultStylesheet = new
MarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, MarkupRenderer
renderer) {
String globalCss[] =
{"de/laliluna/example/components/mystyles.css"};
RenderSupport renderSupport =
environment.peek(RenderSupport.class);
List<Asset> assets = createAssets(globalCss,
symbolSource, assetSource);
for (Asset stylesheet : assets) {
renderSupport.addStylesheetLink(stylesheet, null);
}
renderer.renderMarkup(writer);
}
};
MarkupRendererFilter injectDefaultJavaScripts = new
MarkupRendererFilter() {
public void renderMarkup(MarkupWriter writer, MarkupRenderer
renderer) {
String globalCss[] =
{"de/laliluna/example/components/myscript.js",
"de/laliluna/example/pages/input/myvalidators.js"};
RenderSupport renderSupport =
environment.peek(RenderSupport.class);
List<Asset> assets = createAssets(globalCss,
symbolSource, assetSource);
for (Asset stylesheet : assets) {
renderSupport.addScriptLink(stylesheet);
}
renderer.renderMarkup(writer);
}
};
configuration.add("InjectMyDefaultStyleheet",
injectDefaultStylesheet, "after:RenderSupport");
configuration.add("InjectMyDefaultJavaScripts",
injectDefaultJavaScripts, "after:RenderSupport");
}
private List<Asset> createAssets(String[] globalCss, SymbolSource
symbolSource, AssetSource assetSource) {
List<Asset> assets = CollectionFactory.newList();
for (String path : globalCss) {
String expanded = symbolSource.expandSymbols(path);
Asset asset = assetSource.getAsset(null, expanded, null);
assets.add(asset);
}
return assets;
}
--
Best Regards / Viele Grüße
Sebastian Hennebrueder
-----
Software Developer and Trainer for Hibernate / Java Persistence
http://www.laliluna.de
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org