Search in sources :

Example 1 with CallbackParameter

use of org.apache.wicket.ajax.attributes.CallbackParameter in project wicket by apache.

the class AbstractDefaultAjaxBehavior method getCallbackFunctionBody.

/**
 * Generates the body the {@linkplain #getCallbackFunction(CallbackParameter...) callback
 * function}. To embed this code directly into a piece of javascript, make sure any context
 * parameters are available as local variables, global variables or within the closure.
 *
 * @param extraParameters
 * @return The body of the {@linkplain #getCallbackFunction(CallbackParameter...) callback
 *         function}.
 */
public CharSequence getCallbackFunctionBody(CallbackParameter... extraParameters) {
    AjaxRequestAttributes attributes = getAttributes();
    attributes.setEventNames();
    CharSequence attrsJson = renderAjaxAttributes(getComponent(), attributes);
    StringBuilder sb = new StringBuilder();
    sb.append("var attrs = ");
    sb.append(attrsJson);
    sb.append(";\n");
    JSONArray jsonArray = new JSONArray();
    for (CallbackParameter curExtraParameter : extraParameters) {
        if (curExtraParameter.getAjaxParameterName() != null) {
            try {
                JSONObject object = new JSONObject();
                object.put("name", curExtraParameter.getAjaxParameterName());
                object.put("value", new JSONFunction(curExtraParameter.getAjaxParameterCode()));
                jsonArray.put(object);
            } catch (JSONException e) {
                throw new WicketRuntimeException(e);
            }
        }
    }
    sb.append("var params = ").append(jsonArray).append(";\n");
    sb.append("attrs.").append(AjaxAttributeName.EXTRA_PARAMETERS).append(" = params.concat(attrs.").append(AjaxAttributeName.EXTRA_PARAMETERS).append(" || []);\n");
    sb.append("Wicket.Ajax.ajax(attrs);\n");
    return sb;
}
Also used : AjaxRequestAttributes(org.apache.wicket.ajax.attributes.AjaxRequestAttributes) JSONObject(com.github.openjson.JSONObject) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) JSONArray(com.github.openjson.JSONArray) JSONException(com.github.openjson.JSONException) CallbackParameter(org.apache.wicket.ajax.attributes.CallbackParameter) JSONFunction(org.apache.wicket.ajax.json.JSONFunction)

Example 2 with CallbackParameter

use of org.apache.wicket.ajax.attributes.CallbackParameter in project wicket by apache.

the class AbstractDefaultAjaxBehavior method getCallbackFunction.

/**
 * Generates a javascript function that can take parameters and performs an AJAX call which
 * includes these parameters. The generated code looks like this:
 *
 * <pre>
 * function(param1, param2) {
 *    var attrs = attrsJson;
 *    var params = {'param1': param1, 'param2': param2};
 *    attrs.ep = jQuery.extend(attrs.ep, params);
 *    Wicket.Ajax.ajax(attrs);
 * }
 * </pre>
 *
 * @param extraParameters
 * @return A function that can be used as a callback function in javascript
 */
public CharSequence getCallbackFunction(CallbackParameter... extraParameters) {
    StringBuilder sb = new StringBuilder();
    sb.append("function (");
    boolean first = true;
    for (CallbackParameter curExtraParameter : extraParameters) {
        if (curExtraParameter.getFunctionParameterName() != null) {
            if (!first)
                sb.append(',');
            else
                first = false;
            sb.append(curExtraParameter.getFunctionParameterName());
        }
    }
    sb.append(") {\n");
    sb.append(getCallbackFunctionBody(extraParameters));
    sb.append("}\n");
    return sb;
}
Also used : CallbackParameter(org.apache.wicket.ajax.attributes.CallbackParameter)

Example 3 with CallbackParameter

use of org.apache.wicket.ajax.attributes.CallbackParameter in project openmeetings by apache.

the class CallbackFunctionHelper method getNamedFunctionStr.

private static StringBuilder getNamedFunctionStr(String name, AbstractDefaultAjaxBehavior b, CallbackParameter... extraParameters) {
    StringBuilder sb = new StringBuilder();
    sb.append("function ").append(name).append("(");
    boolean first = true;
    for (CallbackParameter curExtraParameter : extraParameters) {
        if (curExtraParameter.getFunctionParameterName() != null) {
            if (first) {
                first = false;
            } else {
                sb.append(',');
            }
            sb.append(curExtraParameter.getFunctionParameterName());
        }
    }
    sb.append(") {\n");
    sb.append(b.getCallbackFunctionBody(extraParameters));
    sb.append("}\n");
    return sb;
}
Also used : CallbackParameter(org.apache.wicket.ajax.attributes.CallbackParameter)

Example 4 with CallbackParameter

use of org.apache.wicket.ajax.attributes.CallbackParameter in project openmeetings by apache.

the class FileTreePanel method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    download.setDefaultModelObject(newDownloadMenuList());
    Droppable<BaseFileItem> trashToolbar = new Droppable<BaseFileItem>("trash-toolbar") {

        private static final long serialVersionUID = 1L;

        @Override
        public void onConfigure(JQueryBehavior behavior) {
            super.onConfigure(behavior);
            behavior.setOption("hoverClass", Options.asString("ui-state-hover trash-toolbar-hover"));
            behavior.setOption("accept", Options.asString(".recorditem, .fileitem"));
        }

        @Override
        public JQueryBehavior newWidgetBehavior(String selector) {
            return new DroppableBehavior(selector, this) {

                private static final long serialVersionUID = 1L;

                @Override
                protected JQueryAjaxBehavior newOnDropAjaxBehavior(IJQueryAjaxAware source) {
                    return new OnDropAjaxBehavior(source) {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public CharSequence getCallbackFunctionBody(CallbackParameter... parameters) {
                            String dialogId = UUID.randomUUID().toString();
                            String statement = "var $drop = $(this);";
                            statement += "$('body').append('<div id=" + dialogId + ">" + getString("713") + "</div>');";
                            statement += "$( '#" + dialogId + "' ).dialog({ title: '" + escapeEcmaScript(getString("80")) + "', classes: {'ui-dialog-titlebar': 'ui-corner-all no-close'}, buttons: [";
                            statement += "	{ text: '" + escapeEcmaScript(getString("54")) + "', click: function() { $drop.append(ui.draggable); $(this).dialog('close'); " + super.getCallbackFunctionBody(parameters) + " } },";
                            statement += "	{ text: '" + escapeEcmaScript(getString("lbl.cancel")) + "', click: function() { $(this).dialog('close'); } } ";
                            statement += "],";
                            statement += "close: function(event, ui) { $(this).dialog('destroy').remove(); }";
                            statement += "});";
                            return statement;
                        }
                    };
                }
            };
        }

        @Override
        public void onDrop(AjaxRequestTarget target, Component component) {
            Object o = component.getDefaultModelObject();
            if (o instanceof BaseFileItem) {
                BaseFileItem f = (BaseFileItem) o;
                if (isSelected(f)) {
                    deleteAll(target);
                } else {
                    delete(f, target);
                }
            }
        }
    };
    form.add(trashToolbar);
    trashToolbar.add(getUpload());
    trashToolbar.add(createDir);
    trashToolbar.add(new WebMarkupContainer("refresh").add(new AjaxEventBehavior(EVT_CLICK) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            update(target);
        }
    }));
    trashToolbar.add(trashBorder = new ConfirmableAjaxBorder("trash", getString("80"), getString("713"), trashConfirm) {

        private static final long serialVersionUID = 1L;

        @Override
        protected boolean isClickable() {
            return !readOnly && !selected.isEmpty();
        }

        @Override
        protected void onSubmit(AjaxRequestTarget target) {
            deleteAll(target);
        }
    });
    form.add(trees.add(tree).setOutputMarkupId(true));
    updateSizes();
    form.add(sizes.add(new Label("homeSize", homeSize), new Label("publicSize", publicSize)).setOutputMarkupId(true));
    form.add(errorsDialog);
    setReadOnly(false, null);
}
Also used : JQueryBehavior(com.googlecode.wicket.jquery.core.JQueryBehavior) AjaxEventBehavior(org.apache.wicket.ajax.AjaxEventBehavior) Droppable(com.googlecode.wicket.jquery.ui.interaction.droppable.Droppable) ConfirmableAjaxBorder(org.apache.openmeetings.web.common.ConfirmableAjaxBorder) Label(org.apache.wicket.markup.html.basic.Label) CallbackParameter(org.apache.wicket.ajax.attributes.CallbackParameter) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) BaseFileItem(org.apache.openmeetings.db.entity.file.BaseFileItem) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) IJQueryAjaxAware(com.googlecode.wicket.jquery.core.ajax.IJQueryAjaxAware) Component(org.apache.wicket.Component) DroppableBehavior(com.googlecode.wicket.jquery.ui.interaction.droppable.DroppableBehavior)

Aggregations

CallbackParameter (org.apache.wicket.ajax.attributes.CallbackParameter)4 JSONArray (com.github.openjson.JSONArray)1 JSONException (com.github.openjson.JSONException)1 JSONObject (com.github.openjson.JSONObject)1 JQueryBehavior (com.googlecode.wicket.jquery.core.JQueryBehavior)1 IJQueryAjaxAware (com.googlecode.wicket.jquery.core.ajax.IJQueryAjaxAware)1 Droppable (com.googlecode.wicket.jquery.ui.interaction.droppable.Droppable)1 DroppableBehavior (com.googlecode.wicket.jquery.ui.interaction.droppable.DroppableBehavior)1 BaseFileItem (org.apache.openmeetings.db.entity.file.BaseFileItem)1 ConfirmableAjaxBorder (org.apache.openmeetings.web.common.ConfirmableAjaxBorder)1 Component (org.apache.wicket.Component)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 AjaxEventBehavior (org.apache.wicket.ajax.AjaxEventBehavior)1 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)1 AjaxRequestAttributes (org.apache.wicket.ajax.attributes.AjaxRequestAttributes)1 JSONFunction (org.apache.wicket.ajax.json.JSONFunction)1 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)1 Label (org.apache.wicket.markup.html.basic.Label)1