Updated Branches: refs/heads/master d9d8bd99e -> 330e655e6
WICKET-3367 Rewrite all JavaScript inline event handlers to be proper attached event handlers Implement throttling of Ajax calls with the new AjaxRequestAttributes Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/330e655e Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/330e655e Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/330e655e Branch: refs/heads/master Commit: 330e655e6e04872c4516b797af6410b1f962fff4 Parents: d9d8bd9 Author: martin-g <[email protected]> Authored: Tue Jan 3 16:59:42 2012 +0200 Committer: martin-g <[email protected]> Committed: Tue Jan 3 16:59:42 2012 +0200 ---------------------------------------------------------------------- .../wicket/ajax/AbstractDefaultAjaxBehavior.java | 46 +++----- .../org/apache/wicket/ajax/AjaxEventBehavior.java | 83 +-------------- .../ajax/attributes/AjaxRequestAttributes.java | 45 ++++++- .../wicket/ajax/attributes/ThrottlingSettings.java | 73 ++++++++++++ .../calldecorator/AjaxCallThrottlingDecorator.java | 88 --------------- .../wicket/ajax/res/js/wicket-ajax-jquery.js | 39 ++++--- wicket-core/src/test/js/ajax.js | 42 +++++++ 7 files changed, 193 insertions(+), 223 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java index 832248a..12a32d3 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java @@ -22,6 +22,7 @@ import org.apache.wicket.ajax.attributes.AjaxRequestAttributes; import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method; import org.apache.wicket.ajax.attributes.IAjaxCallListener; import org.apache.wicket.ajax.attributes.JavaScriptPrecondition; +import org.apache.wicket.ajax.attributes.ThrottlingSettings; import org.apache.wicket.ajax.json.JSONException; import org.apache.wicket.ajax.json.JSONObject; import org.apache.wicket.behavior.AbstractAjaxBehavior; @@ -352,6 +353,19 @@ public abstract class AbstractDefaultAjaxBehavior extends AbstractAjaxBehavior attributesJson.put("dt", dataType); } + ThrottlingSettings throttlingSettings = attributes.getThrottlingSettings(); + if (throttlingSettings != null) + { + JSONObject throttlingSettingsJson = new JSONObject(); + throttlingSettingsJson.put("id", throttlingSettings.getId()); + throttlingSettingsJson.put("d", throttlingSettings.getDelay().getMilliseconds()); + if (throttlingSettings.getPostponeTimerOnUpdate()) + { + throttlingSettingsJson.put("p", true); + } + attributesJson.put("tr", throttlingSettingsJson); + } + postprocessConfiguration(attributesJson, component); } catch (JSONException e) @@ -590,36 +604,4 @@ public abstract class AbstractDefaultAjaxBehavior extends AbstractAjaxBehavior // in the RequestCycle on the AjaxRequestTarget.. protected abstract void respond(AjaxRequestTarget target); - /** - * Wraps the provided javascript with a throttled block. Throttled behaviors only execute once - * within the given delay even though they are triggered multiple times. - * <p> - * For example, this is useful when attaching an event behavior to the onkeypress event. It is - * not desirable to have an ajax call made every time the user types so we throttle that call to - * a desirable delay, such as once per second. This gives us a near real time ability to provide - * feedback without overloading the server with ajax calls. - * - * @param script - * javascript to be throttled - * @param throttleId - * the id of the throttle to be used. Usually this should remain constant for the - * same javascript block. - * @param throttleDelay - * time span within which the javascript block will only execute once - * @return wrapped javascript - */ - public static CharSequence throttleScript(CharSequence script, String throttleId, - Duration throttleDelay) - { - Args.notEmpty(script, "script"); - Args.notEmpty(throttleId, "throttleId"); - Args.notNull(throttleDelay, "throttleDelay"); - - return new AppendingStringBuffer("Wicket.throttler.throttle( '").append(throttleId) - .append("', ") - .append(throttleDelay.getMilliseconds()) - .append(", Wicket.bind(function() { ") - .append(script) - .append("}, this));"); - } } http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java index 2a7c4ff..eaa88e7 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java @@ -19,6 +19,7 @@ package org.apache.wicket.ajax; import org.apache.wicket.Component; import org.apache.wicket.IClusterable; import org.apache.wicket.ajax.attributes.AjaxRequestAttributes; +import org.apache.wicket.ajax.attributes.ThrottlingSettings; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.markup.head.IHeaderResponse; import org.apache.wicket.markup.head.OnDomReadyHeaderItem; @@ -53,12 +54,8 @@ public abstract class AjaxEventBehavior extends AbstractDefaultAjaxBehavior { private static final long serialVersionUID = 1L; - private static long sequence = 0; - private final String event; - private ThrottlingSettings throttlingSettings; - /** * Construct. * @@ -80,26 +77,6 @@ public abstract class AjaxEventBehavior extends AbstractDefaultAjaxBehavior this.event = event; } - /** - * Sets the throttle delay for this behavior. Throttled behaviors only execute once within the - * given delay even though they are triggered multiple times. - * <p> - * For example, this is useful when attaching this behavior to the keypress event. It is not - * desirable to have an ajax call made every time the user types so we throttle that call to a - * desirable delay, such as once per second. This gives us a near real time ability to provide - * feedback without overloading the server with ajax calls. - * - * - * @param throttleDelay - * throttle delay - * @return this for chaining - */ - public final AjaxEventBehavior setThrottleDelay(Duration throttleDelay) - { - throttlingSettings = new ThrottlingSettings("th" + (++sequence), throttleDelay); - return this; - } - @Override protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { @@ -163,19 +140,6 @@ public abstract class AjaxEventBehavior extends AbstractDefaultAjaxBehavior return getCallbackScript(); } - @Override - protected CharSequence generateCallbackScript(CharSequence partialCall) - { - CharSequence script = super.generateCallbackScript(partialCall); - final ThrottlingSettings ts = throttlingSettings; - - if (ts != null) - { - script = AbstractDefaultAjaxBehavior.throttleScript(script, ts.getId(), ts.getDelay()); - } - return script; - } - /** * * @param event @@ -209,49 +173,4 @@ public abstract class AjaxEventBehavior extends AbstractDefaultAjaxBehavior * @param target */ protected abstract void onEvent(final AjaxRequestTarget target); - - - /** - * Class to keep track of throttling settings. - * - * @author ivaynberg - */ - private static class ThrottlingSettings implements IClusterable - { - private static final long serialVersionUID = 1L; - - private final Duration delay; - private final String id; - - /** - * Construct. - * - * @param id - * throttle id - * @param delay - * throttle delay - */ - public ThrottlingSettings(final String id, final Duration delay) - { - super(); - this.id = id; - this.delay = delay; - } - - /** - * @return throttle delay - */ - public Duration getDelay() - { - return delay; - } - - /** - * @return throttle id - */ - public String getId() - { - return id; - } - } } http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/AjaxRequestAttributes.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/AjaxRequestAttributes.java b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/AjaxRequestAttributes.java index d9cb625..c34c0be 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/AjaxRequestAttributes.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/AjaxRequestAttributes.java @@ -66,12 +66,12 @@ public final class AjaxRequestAttributes /** * The id of the for that should be submitted */ - private String formId = null; + private String formId; /** * The id of the button/link that submitted the form */ - private String submittingComponentName = null; + private String submittingComponentName; /** * Indicates whether or not this AjaxBehavior will produce <ajax-response>. By default it will @@ -82,11 +82,11 @@ public final class AjaxRequestAttributes private String dataType = XML_DATA_TYPE; - private List<IAjaxCallListener> ajaxCallListeners = null; - private List<JavaScriptPrecondition> preconditions = null; - private Map<String, Object> extraParameters = null; - private List<CharSequence> dynamicExtraParameters = null; - private AjaxChannel channel = null; + private List<IAjaxCallListener> ajaxCallListeners; + private List<JavaScriptPrecondition> preconditions; + private Map<String, Object> extraParameters; + private List<CharSequence> dynamicExtraParameters; + private AjaxChannel channel; /** * Whether or not to use asynchronous XMLHttpRequest @@ -94,6 +94,18 @@ public final class AjaxRequestAttributes private boolean async = true; /** + * The settings to use if the Ajax call should be throttled. + * Throttled behaviors only execute once within the given delay + * even though they are triggered multiple times. + * <p> + * For example, this is useful when attaching a behavior to the keypress event. It is not + * desirable to have an ajax call made every time the user types so we throttle that call to a + * desirable delay, such as once per second. This gives us a near real time ability to provide + * feedback without overloading the server with ajax calls. + */ + private ThrottlingSettings throttlingSettings; + + /** * Returns whether the form submit is multipart. * <p> * Note that for multipart AJAX requests a hidden IFRAME will be used and that can have negative @@ -447,4 +459,23 @@ public final class AjaxRequestAttributes return this; } + /** + * @return the settings to use when throttling is needed. + */ + public ThrottlingSettings getThrottlingSettings() + { + return throttlingSettings; + } + + /** + * @param throttlingSettings + * the settings to use when throttling is needed. Pass {@code null} to disable throttling. + * @return this object + */ + public AjaxRequestAttributes setThrottlingSettings(ThrottlingSettings throttlingSettings) + { + this.throttlingSettings = throttlingSettings; + return this; + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/ThrottlingSettings.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/ThrottlingSettings.java b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/ThrottlingSettings.java new file mode 100644 index 0000000..c9f895a --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/attributes/ThrottlingSettings.java @@ -0,0 +1,73 @@ +package org.apache.wicket.ajax.attributes; + +import org.apache.wicket.IClusterable; +import org.apache.wicket.util.time.Duration; + +/** + * Class to keep track of throttling settings. + * + * @author ivaynberg + */ +public class ThrottlingSettings implements IClusterable +{ + private static final long serialVersionUID = 1L; + + private final Duration delay; + private final String id; + + /** + * If it is set to true, then the timer is reset each time the throttle function + * gets called. Use this behaviour if you want something to happen at X milliseconds + * after the *last* call to throttle. If the parameter is not set, or set to false, + * then the timer is not reset. + */ + private final boolean postponeTimerOnUpdate; + + /** + * Construct. + * + * @param id throttle id + * @param delay throttle delay + */ + public ThrottlingSettings(final String id, final Duration delay) + { + this(id, delay, false); + } + + /** + * Construct. + * + * @param id throttle id + * @param delay throttle delay + * @param postponeTimerOnUpdate postpone timer + */ + public ThrottlingSettings(final String id, final Duration delay, final boolean postponeTimerOnUpdate) + { + this.id = id; + this.delay = delay; + this.postponeTimerOnUpdate = postponeTimerOnUpdate; + } + + /** + * @return throttle delay + */ + public Duration getDelay() + { + return delay; + } + + /** + * @return throttle id + */ + public String getId() + { + return id; + } + + /** + * @return + */ + public boolean getPostponeTimerOnUpdate() { + return postponeTimerOnUpdate; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/main/java/org/apache/wicket/ajax/calldecorator/AjaxCallThrottlingDecorator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/calldecorator/AjaxCallThrottlingDecorator.java b/wicket-core/src/main/java/org/apache/wicket/ajax/calldecorator/AjaxCallThrottlingDecorator.java deleted file mode 100644 index 310ac78..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/calldecorator/AjaxCallThrottlingDecorator.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.ajax.calldecorator; - -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior; -import org.apache.wicket.ajax.IAjaxCallDecorator; -import org.apache.wicket.util.string.Strings; -import org.apache.wicket.util.time.Duration; - -/** - * Adds throttling to the ajax call. Throttled behaviors only execute once within the given delay - * even though they can be triggered multiple times. - * <p> - * For example, this is useful when attaching an event behavior to the onkeypress event. It is not - * desirable to have an ajax call made every time the user types so we can throttle that call to a - * desirable delay, such as once per second. This gives us a near real time ability to provide - * feedback without overloading the server. - * - * @since 1.2 - * - * @author Igor Vaynberg (ivaynberg) - * - */ -@Deprecated -public final class AjaxCallThrottlingDecorator extends AjaxPostprocessingCallDecorator -{ - private static final long serialVersionUID = 1L; - - private final Duration duration; - private final String id; - - /** - * Construct. - * - * @param id - * throttle id - * @param delay - * throttle delay - */ - public AjaxCallThrottlingDecorator(String id, Duration delay) - { - this(null, id, delay); - } - - /** - * Construct. - * - * @param decorator - * wrapped decorator - * @param id - * throttle id - * @param delay - * throttle delay - */ - public AjaxCallThrottlingDecorator(IAjaxCallDecorator decorator, String id, Duration delay) - { - super(decorator); - if (Strings.isEmpty(id)) - { - throw new IllegalArgumentException("id cannot be an empty string"); - } - this.id = id; - duration = delay; - } - - - @Override - public final CharSequence postDecorateScript(Component c, CharSequence script) - { - return AbstractDefaultAjaxBehavior.throttleScript(script, id, duration); - } - -} http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js index 532818e..aa1470c 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js @@ -931,6 +931,10 @@ }; Wicket.Throttler = Wicket.Class.create(); + + // declare it as static so that it can be shared between Throttler instances + Wicket.Throttler.entries = []; + Wicket.Throttler.prototype = { /* "postponeTimerOnUpdate" is an optional parameter. If it is set to true, then the timer is @@ -938,25 +942,20 @@ to happen at X milliseconds after the *last* call to throttle. If the parameter is not set, or set to false, then the timer is not reset. */ initialize: function (postponeTimerOnUpdate) { - this.entries = []; - if (postponeTimerOnUpdate !== undefined) { - this.postponeTimerOnUpdate = postponeTimerOnUpdate; - } - else { - this.postponeTimerOnUpdate = false; - } + this.postponeTimerOnUpdate = postponeTimerOnUpdate; }, throttle: function (id, millis, func) { - var entry = this.entries[id]; + var entries = Wicket.Throttler.entries; + var entry = entries[id]; var me = this; if (typeof(entry) === 'undefined') { entry = new Wicket.ThrottlerEntry(func); entry.setTimeoutVar(window.setTimeout(function() { me.execute(id); }, millis)); - this.entries[id] = entry; + entries[id] = entry; } else { entry.setFunc(func); - if (this.postponeTimerOnUpdate === true) + if (this.postponeTimerOnUpdate) { window.clearTimeout(entry.getTimeoutVar()); entry.setTimeoutVar(window.setTimeout(function() { me.execute(id); }, millis)); @@ -965,11 +964,12 @@ }, execute: function (id) { - var entry = this.entries[id]; + var entries = Wicket.Throttler.entries; + var entry = entries[id]; if (typeof(entry) !== 'undefined') { var func = entry.getFunc(); - this.entries[id] = undefined; - var tmp = func(); + entries[id] = undefined; + return func(); } } }; @@ -1434,7 +1434,18 @@ var call = new Wicket.Ajax.Call(); attrs.event = jqEvent; - return call.ajax(attrs); + var throttlingSettings = attrs.tr; + if (throttlingSettings) { + var postponeTimerOnUpdate = throttlingSettings.p || false; + var throttler = new Wicket.Throttler(postponeTimerOnUpdate); + throttler.throttle(throttlingSettings.id, throttlingSettings.d, + Wicket.bind(function () { + return call.ajax(attrs); + }, this)); + } + else { + return call.ajax(attrs); + } }); }); } http://git-wip-us.apache.org/repos/asf/wicket/blob/330e655e/wicket-core/src/test/js/ajax.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/js/ajax.js b/wicket-core/src/test/js/ajax.js index 6cda7cf..470b858 100644 --- a/wicket-core/src/test/js/ajax.js +++ b/wicket-core/src/test/js/ajax.js @@ -241,5 +241,47 @@ jQuery(document).ready(function() { target.triggerHandler("event2"); target.off("event1 event2"); }); + + + asyncTest('Wicket.Ajax - throttle execution.', function () { + + expect(2); + + var attrs = { + tr: { + id: "someId", + d: 100, // in millis + p: false + }, + u: 'data/ajax/nonWicketResponse.json', + e: 'event1', + dt: 'json', // datatype + wr: false, // not Wicket's <ajax-response> + sh: [ + function(data, textStatus, jqXHR) { + start(); + var expected = { + one: 1, + two: '2', + three: true + }; + deepEqual(data, expected); + equal('success', textStatus); + } + ] + } + + Wicket.Ajax.ajax(attrs); + + var target = jQuery(window); + + // this one will be throttled + target.triggerHandler("event1"); + + // this one will override the previous and will be throttled too + target.triggerHandler("event1"); + + target.off("event1"); + }); } });
