/** * @param updateInterval * Duration between AJAX callbacks * @return JS script */ protected final String getJsTimeoutCall(final Duration updateInterval) { // this might look strange, but it is necessary for IE not to leak :( return "setTimeout(\"" + getCallbackScript() + "\", " + updateInterval.getMilliseconds() + ");"; }
@Override protected CharSequence getCallbackScript() { return generateCallbackScript("wicketAjaxGet('" + getCallbackUrl(onlyTargetActivePage()) + "'"); }
/** * * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget) */ @Override protected final void respond(final AjaxRequestTarget target) { onTimer(target); if (!stopped && isEnabled(getComponent())) { target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval)); } }
/** * 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 */ protected boolean shouldTrigger() { return isStopped() == false && isEnabled(getComponent()) && (getComponent() instanceof Page || getComponent().findParent(Page.class) != null); }
/** * * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(AjaxRequestTarget) */ @Override protected final void respond(final AjaxRequestTarget target) { // timerId is no longer valid after timer has triggered timerId = null; if (shouldTrigger()) { onTimer(target); if (shouldTrigger()) { setTimeout(target.getHeaderResponse()); } } }
@Override public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); response.render(JavaScriptHeaderItem.forReference(BrowserInfoForm.JS)); }
/** * * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget) */ protected final void respond(final AjaxRequestTarget target) { onTimer(target); if (!stopped) { target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval)); } }
/** * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getPreconditionScript() */ protected CharSequence getPreconditionScript() { String precondition = null; if (!(getComponent() instanceof Page)) { String componentId = getComponent().getMarkupId(); precondition = "var c = Wicket.$('" + componentId + "'); return typeof(c) != 'undefined' && c != null"; } return precondition; }
@Override protected void onUnbind() { Component component = getComponent(); component.getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target -> clearTimeout(target.getHeaderResponse())); }
/** * 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) { 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()))); }
@Override public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); if (isStopped() == false) { setTimeout(response); } }
private void internalExecuteAllTimerBehaviors(final Component component) { List<AbstractAjaxTimerBehavior> behaviors = component.getBehaviors(AbstractAjaxTimerBehavior.class); for (AbstractAjaxTimerBehavior timer : behaviors) { checkUsability(component, true); if (!timer.isStopped()) { if (log.isDebugEnabled()) { log.debug("Triggering AjaxSelfUpdatingTimerBehavior: {}", component.getClassRelativePath()); } executeBehavior(timer); } } }
protected void waitOnImportProgressPage() { do { // manually run the ajax timer behavior attached to the "info" markup container try { List<IBehavior> behaviors = tester.getComponentFromLastRenderedPage("info").getBehaviors(); for (IBehavior behavior: behaviors) { if(behavior instanceof AbstractAjaxTimerBehavior) { AbstractAjaxTimerBehavior tb = (AbstractAjaxTimerBehavior) behavior; CharSequence url = tb.getCallbackUrl(false); WebRequestCycle cycle = tester.setupRequestAndResponse(true); tester.getServletRequest().setRequestToRedirectString(url.toString()); tester.processRequestCycle(cycle); } } } catch(WicketRuntimeException e) { // this happens a lot if the page switched while we where looping } } while(tester.getLastRenderedPage().getClass().equals(ImportProgressPage.class)); }
/** * @see org.apache.wicket.behavior.AbstractAjaxBehavior#renderHead(org.apache.wicket.markup.html.IHeaderResponse) */ public void renderHead(IHeaderResponse response) { super.renderHead(response); WebRequest request = (WebRequest)RequestCycle.get().getRequest(); if (!stopped && (!headRendered || !request.isAjax())) { headRendered = true; response.renderOnLoadJavascript(getJsTimeoutCall(updateInterval)); } }
/** * 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 */ protected boolean shouldTrigger() { return isStopped() == false && isEnabled(getComponent()) && (getComponent() instanceof Page || getComponent().findParent(Page.class) != null); }
@Override public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); response.render(JavaScriptHeaderItem.forReference(BrowserInfoForm.JS)); }
/** * * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(AjaxRequestTarget) */ @Override protected final void respond(final AjaxRequestTarget target) { // timerId is no longer valid after timer has triggered timerId = null; if (shouldTrigger()) { onTimer(target); if (shouldTrigger()) { setTimeout(target.getHeaderResponse()); } } }
/** * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getPreconditionScript() */ @Override protected CharSequence getPreconditionScript() { String precondition = null; if (!(getComponent() instanceof Page)) { String componentId = getComponent().getMarkupId(); precondition = "var c = Wicket.$('" + componentId + "'); return typeof(c) != 'undefined' && c != null"; } return precondition; }
@Override protected void onUnbind() { Component component = getComponent(); component.getRequestCycle().find(IPartialPageRequestHandler.class).ifPresent(target -> clearTimeout(target.getHeaderResponse())); }
/** * 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) { 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()))); }