001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.corelib.mixins;
014
015import org.apache.tapestry5.*;
016import org.apache.tapestry5.annotations.AfterRender;
017import org.apache.tapestry5.annotations.Events;
018import org.apache.tapestry5.annotations.InjectContainer;
019import org.apache.tapestry5.annotations.Parameter;
020import org.apache.tapestry5.corelib.components.Zone;
021import org.apache.tapestry5.internal.util.CaptureResultCallback;
022import org.apache.tapestry5.ioc.annotations.Inject;
023import org.apache.tapestry5.services.javascript.JavaScriptSupport;
024
025/**
026 *
027 * This mixin periodically refreshs a {@link org.apache.tapestry5.corelib.components.Zone zone}
028 * by triggering an event on the server using ajax requests.
029 *
030 *
031 * Server-side, the mixin triggers the "refresh" event with the mixin's context. A container may but
032 * does not need to handle the event. If the event is handled and a value is returned, that value is
033 * used to render the response. Otherwise, the Zone's body is re-rendered.
034 *
035 * <b>Note: </b> This mixin is only meant for a {@link org.apache.tapestry5.corelib.components.Zone zone}.
036 *
037 * @tapestrydoc
038 */
039@Events(EventConstants.REFRESH)
040public class ZoneRefresh
041{
042    /**
043     * Period between two consecutive refreshes (in seconds). If a new refresh occurs before the
044     * previous refresh has completed, it will be skipped.
045     */
046    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
047    private int period;
048
049    /**
050     * Context passed to the event
051     */
052    @Parameter
053    private Object[] context;
054
055    @InjectContainer
056    private Zone zone;
057
058    @Inject
059    private JavaScriptSupport javaScriptSupport;
060
061    @Inject
062    private ComponentResources resources;
063
064    //For testing purpose
065    ZoneRefresh(Object[] context, ComponentResources resources, JavaScriptSupport javaScriptSupport, Zone zone)
066    {
067        this.context = context;
068        this.resources = resources;
069        this.javaScriptSupport = javaScriptSupport;
070        this.zone = zone;
071    }
072
073    @AfterRender
074    void addJavaScript()
075    {
076        Link link = resources.createEventLink("zoneRefresh", context);
077
078        javaScriptSupport.require("t5/core/zone-refresh").with(zone.getClientId(), period, link.toString());
079    }
080
081    Object onZoneRefresh(EventContext eventContext)
082    {
083        CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
084        resources.triggerContextEvent(EventConstants.REFRESH, eventContext, callback);
085
086        if (callback.getResult() != null)
087        {
088            return callback.getResult();
089        }
090
091        return zone.getBody();
092    }
093
094}