Search code examples
jsfomnifaces

How I can override some Javascript function from Omnifaces?


I want to override the Javascript function called from o:highlight component from Omnifaces. I see that the taglib calls the function OmniFaces.Highlight available here:

https://github.com/omnifaces/omnifaces/blob/develop/src/main/resources/META-INF/resources/omnifaces/highlight.unminified.js

How I can override this function to use my own implementation?

The only way I see at this time is overriding the Java class org.omnifaces.component.script.Highlight to call another function name.

There are another way? I don't like to override all class content because in the next time I update the Omnifaces, I need to check if the class was changed.


Solution

  • Just redefine the function in JavaScript context.

    OmniFaces.Highlight.apply = function(clientIds, styleClass, doFocus) {
        // ...
    }
    

    You only need to make sure this function is defined after omnifaces.js is loaded. You can ensure this by including your script with the function via a <h:outputScript target="head"> inside <h:body>.

    <h:head>
        ...
    </h:head>
    <h:body>
        <h:outputScript name="yourscript.js" target="head" />
        ...
    </h:body>
    

    Alternatively, post an issue to OmniFaces guys and have them to implement the desired functionality you intented to change.