WICKET-6523 removed getJsTimeoutCall

since it needs the timerId, added getTimerId() instead; improved javadoc;
this closes #261


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4ec7c626
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4ec7c626
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4ec7c626

Branch: refs/heads/master
Commit: 4ec7c626058a0fde4163ba1f5f81e6176e27478e
Parents: 2da5505
Author: Sven Meier <[email protected]>
Authored: Tue Feb 6 15:18:37 2018 +0100
Committer: Sven Meier <[email protected]>
Committed: Tue Feb 6 17:29:15 2018 +0100

----------------------------------------------------------------------
 .../wicket/ajax/AbstractAjaxTimerBehavior.java  | 75 +++++++++++++-------
 .../wicket/ajax/res/js/wicket-ajax-jquery.js    |  7 +-
 2 files changed, 53 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/4ec7c626/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
 
b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
index 94407ad..da947ee 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractAjaxTimerBehavior.java
@@ -98,30 +98,13 @@ public abstract class AbstractAjaxTimerBehavior extends 
AbstractDefaultAjaxBehav
        }
 
        /**
-        * @param updateInterval
-        *            Duration between AJAX callbacks
-        * @return JS script
-        */
-       protected final String getJsTimeoutCall(final Duration updateInterval)
-       {
-               CharSequence js = getCallbackScript();
-               
-               Component component = getComponent();
-               // remember id for timer
-               timerId = component.getMarkupId() + "." + 
component.getBehaviorId(this);
-
-               return String.format("Wicket.Timer.set('%s', function(){%s}, 
%d);",
-                       timerId, js, updateInterval.getMilliseconds());
-       }
-
-       /**
         * 
         * @see 
org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(AjaxRequestTarget)
         */
        @Override
        protected final void respond(final AjaxRequestTarget target)
        {
-               // timerId is no longer valid after Ajax request
+               // timerId is no longer valid after timer has triggered
                timerId = null;
                
                if (shouldTrigger())
@@ -136,11 +119,11 @@ public abstract class AbstractAjaxTimerBehavior extends 
AbstractDefaultAjaxBehav
        }
 
        /**
-        * Decides whether the timer behavior should render its JavaScript to 
re-trigger
-        * it after the update interval.
+        * Decides whether the timer behavior should render its JavaScript to 
re-trigger it after the
+        * update interval.
         *
-        * @return {@code true} if the behavior is not stopped, it is enabled 
and still attached to
-        *      any component in the page or to the page itself
+        * @return {@code true} if the behavior is not stopped, it is enabled 
and still attached to any
+        *         component in the page or to the page itself
         */
        protected boolean shouldTrigger()
        {
@@ -181,17 +164,55 @@ public abstract class AbstractAjaxTimerBehavior extends 
AbstractDefaultAjaxBehav
                }
        }
 
+       /**
+        * Create an identifier for the JavaScript timer.
+        * <p>
+        * Note: The identifier must not change as long as this behavior is 
attached to a component!
+        * 
+        * @return creates an id based on {@link Component#getMarkupId()} and
+        *         {@link Component#getBehaviorById(int)} by default
+        */
+       protected String getTimerId()
+       {
+               Component component = getComponent();
+
+               return component.getMarkupId() + "." + 
component.getBehaviorId(this);
+       }
+
+       /**
+        * Set the timeout on the given {@link IHeaderResponse}. Implementation 
note:
+        * <p>
+        * {@link #respond(AjaxRequestTarget)} might set the timer once and
+        * {@link #renderHead(Component, IHeaderResponse)} a second time 
successively, if the attached
+        * component is re-rendered on the same {@link AjaxRequestTarget}.
+        * <p>
+        * But rendering of the component might <em>not</em> actually happen on 
the same {@link AjaxRequestTarget},
+        * e.g. when a redirect to a full page-render is scheduled. Thus this 
method <em>always</em> sets the timeout 
+        * and in the former case {@link AjaxRequestTarget} will take care of 
executing one of the
+        * two {@link OnLoadHeaderItem}s only.
+        * 
+        * @param headerResponse
+        */
        private void setTimeout(IHeaderResponse headerResponse)
        {
-               
headerResponse.render(OnLoadHeaderItem.forScript(getJsTimeoutCall(updateInterval)));
+               CharSequence js = getCallbackScript();
+
+               // remember id to be able to clear it later
+               timerId = getTimerId();
+
+               headerResponse.render(
+                       
OnLoadHeaderItem.forScript(String.format("Wicket.Timer.set('%s', 
function(){%s}, %d);",
+                               timerId, js, 
updateInterval.getMilliseconds())));
        }
 
        private void clearTimeout(IHeaderResponse headerResponse)
        {
-               if (timerId != null) {
-                       
headerResponse.render(OnLoadHeaderItem.forScript("Wicket.Timer.clear('" + 
timerId + "');"));
-                                               
-                       timerId = null; 
+               if (timerId != null)
+               {
+                       headerResponse
+                               
.render(OnLoadHeaderItem.forScript("Wicket.Timer.clear('" + timerId + "');"));
+
+                       timerId = null;
                }
        }
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/4ec7c626/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 00d08ec..33addaf 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
@@ -2557,7 +2557,7 @@
                        },
 
                        /**
-                        * Un-schedules a timer by its id
+                        * Clears a timer by its id
                         * @param {string} timerId - the identifier of the timer
                         */
                        clear: function(timerId) {
@@ -2567,7 +2567,10 @@
                                }
                        },
                        
-                       clearAll: function(timerId) {
+                       /**
+                        * Clear all remaining timers.
+                        */
+                       clearAll: function() {
                                var WTH = Wicket.TimerHandles;
                                if (WTH) {
                                        for (var th in WTH) {

Reply via email to