How To Implement ExtraWindowSettingsInjector Hook
Introduction
This document explains how to implement ExtraWindowSettingsInjector Hook. This hook is called after each window load, at the end of WindowSettingsActionHandler class.
Hook Implementation
This hook is implemented by extending the ExtraWindowSettingsInjector class has just one method to implement: doAddSetting. This method receives two parameters:
- parameters
- the parameters Map of the current WindowSettingsActionHandler execution.
- json
- the JSONObject response instance of the WindowSettingsActionHandler.
It returns a Map<String, Object> with all the extra settings desired to be included in the WindowSettingsActionHandler response. These extra settings are included in the callbacks response data object inside the extraSettings property. If the WindowSettingsActionHandler.EXTRA_CALLBACK key is used with a String or List<String> as value it is possible to return some JavaScript function names that are executed on the callback of the WindowSettingsActionHandler. These JavaScript functions can be implemented to use the extraSettings included in the Map. These function only receipt the original data object of the callback as argument.
Example
This example shows an alert every time the Product window is loaded. You can find the code described below in the org.openbravo.platform.features module.
The java implementation add an extra setting called showAlert with the boolean true as its value. It also adds the OB.OBPF.showAlert JavaScript function using the extraCallbacks key. <source lang="java">public class ExtraSettingsInjectorExample implements ExtraWindowSettingsInjector {
@Override public Map<String, Object> doAddSetting(Map<String, Object> parameters, JSONObject json) throws OBException { Map<String, Object> extraSettings = new HashMap<String, Object>(); String strWindowId = (String) parameters.get("windowId"); if ("140".equals(strWindowId)) {
// Set extraSettings extraSettings.put("showAlert", true);
// Set extraCallbacks List<String> callbackList = new ArrayList<String>(); extraSettings.put("extraCallbacks", callbackList);
// Add extraCallbacks callbackList.add("OB.OBPF.showAlert"); } return extraSettings; }
} </source>
The JavaScript implementation shows an alert in case the extraSettings.showAlert is true.
<source lang="javascript">OB.OBPF = OB.OBPF || {};
OB.OBPF = {
showAlert: function (data) {
if (!data|| !data.extraSettings || !data.extraSettings.showAlert) { return; } alert("Window opened"); }
}; </source>