Currently, I am utilizing j2html to generate HTML pages through Java. When the HTML calls any JavaScript function, my approach so far has been to place the JavaScript in a separate .js file. However, it seems more practical to include functions in the main application's JavaScript file only if they are general-purpose functions used across multiple pages. Functions specifically tailored for one page should be stored elsewhere. Creating separate .js files for each page has become an administrative hassle, prompting me to consider putting them directly into the HTML near the calling code.
Although this is doable, I face readability challenges with the JavaScript code within my Java code compared to when it resides in a separate .js file.
Is there a way I can write it in a cleaner and more readable manner?
private Tag artwork()
{
return script(rawHtml("function artwork() {\n" +
" \n" +
" select = document.getElementById('fsArtwork');\n" +
" selected = select.options[select.selectedIndex].value;\n" +
" if(selected==0)\n" +
" {\n" +
" \tdocument.getElementById('saveArtworkFilename').disabled = true;\n" +
" }\n" +
" else\n" +
" {\n" +
" \tdocument.getElementById('saveArtworkFilename').disabled = false;\n" +
" }\n" +
"}"));
}
Regarding the use of templates which have been suggested; I haven't adopted them due to several reasons:
1. My webpages are currently not designed using templates, so transitioning to a template-based system would require a significant overhaul from scratch.
2. For consistency purposes, rewriting all pages in templates would restrict my creative freedom in how I structure and design them.
In essence, the main benefits of templates such as separating responsibilities between web developers and server developers do not apply to my current situation.
Exploring Other Solutions
While attempting various solutions, I encountered an issue where the generated page could not find the external JavaScript file 'artwork.js'. The initialization was set up correctly in the code, but the location within the main classpath caused confusion.
staticFiles.externalLocation(Platform.getPlatformReportFolder().getPath());
staticFiles.location("");
scriptWithInlineFile_min("javascript/artwork.js"),
Upon further analysis, I realized that the problem stemmed from using a relative path instead of an absolute path for the JavaScript file reference.
Resolving Deployment Issues
After correcting the file path, the solution worked smoothly in development environments. However, upon building and running outside of development mode, the file was once again not found, even though it existed within the main jar file under '/javascript' directory.
Update: Bug Fix
Finding a resolution led me to discover a bug in j2html (https://github.com/tipsy/j2html/issues/84). This bug was addressed in version 1.2.1, surpassing the version (1.2.0) I had been using. Subsequently, everything began working as intended.
Conclusion
While this method may not strictly adhere to conventional templating practices, it serves its purpose effectively for my needs. By incorporating page-specific JavaScript into a file and embedding it directly into the rendered HTML file within the main jar file, deployment concerns typically associated with standard files are mitigated.