@Override
public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
FacesContext facesContext = behaviorContext.getFacesContext();
ComponentControlBehavior controlBehavior = (ComponentControlBehavior) behavior;
String apiFunctionName = controlBehavior.getOperation();
String targetSourceString = controlBehavior.getTarget();
String selector = controlBehavior.getSelector();
// Fix https://issues.jboss.org/browse/RF-9745
if (isEmpty(apiFunctionName) || (isEmpty(targetSourceString) && isEmpty(selector))) {
throw new IllegalArgumentException(
"One of the necessary attributes is null or empty. Check operation attribute and selector or target attributes.");
}
JSFunctionDefinition callback = new JSFunctionDefinition();
callback.addParameter(new JSReference(REF_EVENT));
callback.addParameter(new JSReference(REF_COMPONENT));
// create callback function
StringBuffer script = new StringBuffer();
script.append(REF_COMPONENT).append("['").append(apiFunctionName).append("'].").append("apply").append("(");
// get client api function parameters
List<Object> apiFunctionParams = createSignature(controlBehavior);
script.append(REF_COMPONENT).append(",").append(ScriptUtils.toScript(apiFunctionParams.toArray())).append(");");
callback.addToBody(script);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(PARAM_CALLBACK, callback);
parameters.put(PARAM_TARGET, resolveTargets(facesContext, targetSourceString));
parameters.put(PARAM_SELECTOR, selector);
String onBeforeOperation = controlBehavior.getOnbeforeoperation();
if (null != onBeforeOperation && !onBeforeOperation.isEmpty()) {
JSFunctionDefinition onBeforeOperationFunction = new JSFunctionDefinition(new JSReference(REF_EVENT));
onBeforeOperationFunction.addToBody(onBeforeOperation);
parameters.put(PARAM_ONBEFOREOPERATION, onBeforeOperationFunction);
}