use of org.apache.wicket.ajax.attributes.AjaxCallListener in project ocvn by devgateway.
the class BootstrapDeleteButton method updateAjaxAttributes.
@Override
protected void updateAjaxAttributes(final AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
AjaxCallListener ajaxCallListener = new AjaxCallListener();
ajaxCallListener.onPrecondition("return confirm('Confirm Delete?');");
attributes.getAjaxCallListeners().add(ajaxCallListener);
}
use of org.apache.wicket.ajax.attributes.AjaxCallListener in project wicket by apache.
the class AbstractDefaultAjaxBehaviorTest method renderAjaxAttributes.
/**
* Checks the generated JSON for Ajax's attributes
*/
@Test
public void renderAjaxAttributes() {
AjaxRequestAttributes attributes = new AjaxRequestAttributes();
attributes.getExtraParameters().put("param1", 123);
attributes.getExtraParameters().put("param2", Locale.CANADA_FRENCH);
AjaxCallListener listener = new AjaxCallListener();
listener.onPrecondition("return somePrecondition();");
listener.onBefore("alert('Before!');");
listener.onAfter("alert('After!');");
listener.onSuccess("alert('Success!');");
listener.onFailure("alert('Failure!');");
listener.onComplete("alert('Complete!');");
attributes.getAjaxCallListeners().add(listener);
Component component = Mockito.mock(Component.class);
AbstractDefaultAjaxBehavior behavior = new AbstractDefaultAjaxBehavior() {
@Override
protected void respond(AjaxRequestTarget target) {
}
@Override
public CharSequence getCallbackUrl() {
return "some/url";
}
};
behavior.bind(component);
CharSequence json = behavior.renderAjaxAttributes(component, attributes);
String expected = "{\"" + AjaxAttributeName.URL + "\":\"some/url\",\"" + AjaxAttributeName.BEFORE_HANDLER + "\":[function(attrs){alert('Before!');}],\"" + AjaxAttributeName.AFTER_HANDLER + "\":[function(attrs){alert('After!');}],\"" + AjaxAttributeName.SUCCESS_HANDLER + "\":[function(attrs, jqXHR, data, textStatus){alert('Success!');}],\"" + AjaxAttributeName.FAILURE_HANDLER + "\":[function(attrs, jqXHR, errorMessage, textStatus){alert('Failure!');}],\"" + AjaxAttributeName.COMPLETE_HANDLER + "\":[function(attrs, jqXHR, textStatus){alert('Complete!');}],\"" + AjaxAttributeName.PRECONDITION + "\":[function(attrs){return somePrecondition();}],\"" + AjaxAttributeName.EXTRA_PARAMETERS + "\":[{\"name\":\"param1\",\"value\":123},{\"name\":\"param2\",\"value\":\"fr_CA\"}]" + "}";
assertEquals(expected, json);
}
use of org.apache.wicket.ajax.attributes.AjaxCallListener in project wicket by apache.
the class AjaxFileDropBehavior method updateAjaxAttributes.
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.setMultipart(true);
attributes.setMethod(Method.POST);
// default must be prevented, otherwise browser will consume the dataTransfer
attributes.setPreventDefault(true);
attributes.getAjaxCallListeners().add(new AjaxCallListener() {
@Override
public CharSequence getPrecondition(Component component) {
String css = getComponent().getString(DRAG_OVER_CLASS_KEY);
return String.format("jQuery('#' + attrs.c).toggleClass('%s', attrs.event.type === 'dragover'); return (attrs.event.type === 'drop');", css);
}
});
attributes.getDynamicExtraParameters().add(String.format("return Wicket.DataTransfer.getFilesAsParamArray(attrs.event.originalEvent, '%s');", parameterName));
}
use of org.apache.wicket.ajax.attributes.AjaxCallListener in project wicket by apache.
the class AjaxEditableChoiceLabel method newEditor.
/**
* {@inheritDoc}
*/
@Override
protected FormComponent<T> newEditor(final MarkupContainer parent, final String componentId, final IModel<T> model) {
IModel<List<? extends T>> choiceModel = new IModel<List<? extends T>>() {
private static final long serialVersionUID = 1L;
@Override
public List<? extends T> getObject() {
return choices.getObject();
}
};
DropDownChoice<T> editor = new DropDownChoice<T>(componentId, model, choiceModel, renderer) {
private static final long serialVersionUID = 1L;
@Override
protected void onModelChanged() {
AjaxEditableChoiceLabel.this.onModelChanged();
}
@Override
protected void onModelChanging() {
AjaxEditableChoiceLabel.this.onModelChanging();
}
};
editor.setOutputMarkupId(true);
editor.setVisible(false);
editor.add(new EditorAjaxBehavior() {
private static final long serialVersionUID = 1L;
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.setEventNames("change", "blur", "keyup");
CharSequence dynamicExtraParameters = "var result = [], " + "kc=Wicket.Event.keyCode(attrs.event)," + "evtType=attrs.event.type;" + "if (evtType === 'blur' || (evtType === 'keyup' && kc===27)) {" + " result.push( { name: 'save', value: false } );" + "}" + "else {" + " result = Wicket.Form.serializeElement(attrs.c);" + " result.push( { name: 'save', value: true } );" + "}" + "return result;";
attributes.getDynamicExtraParameters().add(dynamicExtraParameters);
CharSequence precondition = "var kc=Wicket.Event.keyCode(attrs.event)," + "evtType=attrs.event.type," + "ret=false;" + "if(evtType==='blur' || evtType==='change' || (evtType==='keyup' && kc===27)) ret = true;" + "return ret;";
AjaxCallListener ajaxCallListener = new AjaxCallListener();
ajaxCallListener.onPrecondition(precondition);
attributes.getAjaxCallListeners().add(ajaxCallListener);
}
});
return editor;
}
use of org.apache.wicket.ajax.attributes.AjaxCallListener in project wicket by apache.
the class AjaxEditableLabel method newEditor.
/**
* Create a new form component instance to serve as editor.
*
* @param parent
* The parent component
* @param componentId
* Id that should be used by the component
* @param model
* The model
* @return The editor
*/
protected FormComponent<T> newEditor(final MarkupContainer parent, final String componentId, final IModel<T> model) {
TextField<T> editor = new TextField<T>(componentId, model) {
private static final long serialVersionUID = 1L;
@Override
public <C> IConverter<C> getConverter(final Class<C> type) {
IConverter<C> c = AjaxEditableLabel.this.getConverter(type);
return c != null ? c : super.getConverter(type);
}
@Override
protected void onModelChanged() {
super.onModelChanged();
AjaxEditableLabel.this.onModelChanged();
}
@Override
protected void onModelChanging() {
super.onModelChanging();
AjaxEditableLabel.this.onModelChanging();
}
};
editor.setOutputMarkupId(true);
editor.setVisible(false);
editor.add(new EditorAjaxBehavior() {
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
attributes.setEventNames("blur", "keyup", "keydown");
// Note: preventDefault is handled selectively below
attributes.setPreventDefault(false);
// Note: escape can be detected on keyup, enter can be detected on keyup
CharSequence precondition = "var kc=Wicket.Event.keyCode(attrs.event)," + "evtType=attrs.event.type," + "ret=false;" + "if (evtType==='blur' || (evtType==='keyup' && kc===27) || (evtType==='keydown' && kc===13)) {attrs.event.preventDefault(); ret = true;}" + "return ret;";
AjaxCallListener ajaxCallListener = new AjaxCallListener();
ajaxCallListener.onPrecondition(precondition);
CharSequence dynamicExtraParameters = "var result," + "evtType=attrs.event.type;" + "if (evtType === 'keyup') { result = { 'save': false }; }" + "else { result = { 'save': true }; }" + "return result;";
attributes.getDynamicExtraParameters().add(dynamicExtraParameters);
attributes.getAjaxCallListeners().add(ajaxCallListener);
}
});
return editor;
}
Aggregations