Call js function from backend

Having trouble deploying Oxygen XML Web Author? Got a bug to report? Post it all here.
Isabelle
Posts: 168
Joined: Fri Jan 20, 2017 1:11 pm

Call js function from backend

Post by Isabelle »

Hello,

With Oxygen 27, is it possible to call javascript function from backend ?
We have extended WSEditorListener to implement our own editorSaved(int var1) method.
But we need to show a modal to the user and for that we need to send to frontend a json element and call a javascript function.
Is it possible from java to throw event or call javascript method with parameters ?
Thanks.

Regards,
Isabelle
cosminef
Site Admin
Posts: 233
Joined: Wed Aug 30, 2023 2:33 pm

Re: Call js function from backend

Post by cosminef »

Hello,

You should use the JavaScript API [1] to override the method with the ID "Author/Save".
Do you also want to modify the autosave functionality?

[1] https://www.oxygenxml.com/maven/com/oxy ... 0.0/jsdoc/

Best,
Cosmin
Cosmin Eftenie
www.oxygenxml.com
Isabelle
Posts: 168
Joined: Fri Jan 20, 2017 1:11 pm

Re: Call js function from backend

Post by Isabelle »

Hello,
Do you also want to modify the autosave functionality?
No thanks, only save functionality.

The link you have mentioned does not explain how to override the method with the ID "Author/Save".
But I have found this on your github => web-author-block-save-if-invalid.
I have tried to implement it like this to launch our own validation after save:

Code: Select all

function applicationStarted(pluginWorkspaceAccess) {
  pluginWorkspaceAccess.addEditingSessionLifecycleListener(
    new JavaAdapter(Packages.ro.sync.ecss.extensions.api.webapp.access.WebappEditingSessionLifecycleListener, {
      editingSessionStarted: function (docId, authorDocumentModel) {
        authorDocumentModel.getWSEditor().addEditorListener(new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorListener, {
          editorSaved: function(var1) {
              this.editor.getActionsManager().invokeOperation('SetReadOnlyStatusOperation', {'read-only': true});
              this.editor.getActionsManager().invokeOperation('com.plugin.operations.ValidationOperation', {},
                ((error, result) => {
                  this.editor.getActionsManager().invokeOperation('SetReadOnlyStatusOperation', {'read-only': false});
                  if (error == null) {
                    if (result) {
                      currentValidationResults = JSON.parse(result);
                      createValidationModal(callback);
                    }
                  }
                }));
            return true;
          }
        }))
      }
    }));
}
But it fails on application start with the error "missing ;"
Any idea why ?
Thanks,

Regards,
Isabelle
mihaela
Posts: 515
Joined: Wed May 20, 2009 2:40 pm

Re: Call js function from backend

Post by mihaela »

Hello,

The plugin that you mentioned is a JavaScript-Based Workspace Access Plugin Extension:
https://www.oxygenxml.com/doc/versions/ ... in-js.html
This type of extension can use the same API as the JAVA Workspace Access plugin extension, but the implementation is JavaScript-based and uses the bundled Rhino library to create and work with Java API from the JavaScript code.

So all the code executes on the server side actually so you cannot mix it with the javascript API that is called on the client:
https://www.oxygenxml.com/maven/com/oxy ... 0.0/jsdoc/
This is why you obtain the error above.

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
Isabelle
Posts: 168
Joined: Fri Jan 20, 2017 1:11 pm

Re: Call js function from backend

Post by Isabelle »

Hello,

Ok, I understand the problem.
But then can you tell me, how, in javascript, can I add our custom validation method after save action (only save and not auto save) ?
Thanks,

Regards,
Isabelle
mihaela
Posts: 515
Joined: Wed May 20, 2009 2:40 pm

Re: Call js function from backend

Post by mihaela »

Hello,

Here is a possibility: there is an event called "DIRTY_STATUS_CHANGED" that is triggered on the editor, that you can listen to:
https://www.oxygenxml.com/maven/com/oxy ... es__anchor
When the "isDirty" status is set to false you can trigger your validation.

Check the documentation for a sample code on how to listen on this event:
https://www.oxygenxml.com/maven/com/oxy ... Event.html

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
Isabelle
Posts: 168
Joined: Fri Jan 20, 2017 1:11 pm

Re: Call js function from backend

Post by Isabelle »

Thanks a lot.
I will try that.

Regards,
Isabelle
Isabelle
Posts: 168
Joined: Fri Jan 20, 2017 1:11 pm

Re: Call js function from backend

Post by Isabelle »

Hello,

It works fine.
Thank you.

Regards,
Isabelle
Post Reply