ramblings/oxygen/js-include/README.md

41 lines
1.5 KiB
Markdown
Raw Normal View History

2021-04-25 12:17:04 +00:00
# Including scripts within Oxygen [JS Operations](https://www.oxygenxml.com/doc/versions/23.1/ug-editor/topics/dg-default-author-operations.html#dg-default-author-operations__jsoperation)
2021-04-25 10:48:11 +00:00
2021-04-25 12:17:04 +00:00
Another limitation of the JS operation is that a single "globals.js" file is loaded before the execution of javascript operation.
2021-04-25 10:48:11 +00:00
2021-04-25 12:17:04 +00:00
This rambling proposes a way to include other scripts at runtime.
2021-04-25 10:48:11 +00:00
## Installation
To do so, add this function to your commons.js file:
```javascript
2021-04-25 12:17:04 +00:00
include = function (filepath, isAbsolute) {
if (isAbsolute === undefined || isAbsolute == false) {
filepath = Packages.ro.sync.ecss.extensions.commons.operations.CommonsOperationsUtil.expandAndResolvePath(authorAccess, filepath);
}
var text = new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths. get (java.net.URI(filepath))));
text = String(text);
eval(text);
}
2021-04-25 10:48:11 +00:00
```
2021-04-25 12:17:04 +00:00
And use the include() function it in the script parameter of the JS operation:
2021-04-25 10:48:11 +00:00
```javascript
2021-04-25 12:17:04 +00:00
doOperation = function () {
include('hello.js');
hello();
2021-04-25 12:18:51 +00:00
}
```
2021-04-25 10:48:11 +00:00
## Limitations
2021-04-25 12:17:04 +00:00
The function needs an authorAccess variable in order to resolve relative URIs and this variable is only available in the scope of the doOperation function. This means that you can't use it
outside of this function call, like, for instance, in the global scope of the common.js script.
2021-04-25 12:37:47 +00:00
## See also
* [debugger](../js-debugger)
* [debugger + inclusion](../js-debugger-include)
2021-04-25 10:48:11 +00:00
# Framework
2021-04-25 12:39:50 +00:00
This directory includes the Oxygen sample framework on which ou can test this technique but it should work on any framework.
2021-04-25 10:48:11 +00:00