public String getScriptContribution(FacesContext context, UIComponent column, String dropTargetScriptId, JSFunction preSendAjaxRequestFunction, JSFunctionDefinition onAjaxCompleteFunction) {
StringBuffer result = new StringBuffer();
result.append(".drop = ");
JSFunctionDefinition definition = new JSFunctionDefinition();
definition.addParameter("event");
definition.addParameter("drag");
Map<String, Object> requestOpts = AjaxRendererUtils.buildEventOptions(context, column);
//replace parameters
String clientId = column.getClientId(context);
@SuppressWarnings("unchecked")
Map<String, Object> parameters = (Map<String, Object>) requestOpts.get("parameters");
if (parameters != null){
if (parameters.containsKey(clientId)){
parameters.remove(clientId);
parameters.put(dropTargetScriptId, dropTargetScriptId);
}
}
definition.addToBody("var dragParams = drag.getParameters();");
String dragSourceScriptId = column.getClientId(context) + ":"+ TableDragDropRenderer.DRAG_SOURCE_SCRIPT_ID;
definition.addToBody(
"var source = dragParams['"+ DraggableRendererContributor.DRAG_SOURCE_ID +"'];" +
"if (source != \"" + dragSourceScriptId + "\"){"//send request only if drag column in not equals to drop column
);
definition.addToBody("var options = ").addToBody(ScriptUtils.toScript(requestOpts)).addToBody(";");
definition.addToBody("options.parameters['" + DropzoneRendererContributor.DROP_TARGET_ID + "'] = '" + dropTargetScriptId + "';");
if (onAjaxCompleteFunction != null)
definition.addToBody("options['" + AjaxRendererUtils.ONCOMPLETE_ATTR_NAME + "'] = " + onAjaxCompleteFunction.toScript() + ";");
//TODO remove as legacy
definition.addToBody("Object.extend(options.parameters, dragParams);");
if (preSendAjaxRequestFunction != null){
definition.addToBody(preSendAjaxRequestFunction.toScript()).addToBody(";");
}
definition.addToBody("var dzOptions = this.getDropzoneOptions(); if (dzOptions.ondrop) { if (!dzOptions.ondrop.call(this, event)) return; };");
JSFunction dropFunction = AjaxRendererUtils.buildAjaxFunction(column, context);
dropFunction.addParameter(new JSReference("options"));
definition.addToBody(dropFunction.toScript()).addToBody(";");
definition.addToBody("};");
definition.appendScript(result);
result.append(";");
return result.toString();
}