001// Copyright 2007-2014 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.MarkupWriter;
018import org.apache.tapestry5.dom.Element;
019import org.apache.tapestry5.internal.services.ajax.AjaxFormUpdateController;
020import org.apache.tapestry5.json.JSONObject;
021import org.apache.tapestry5.runtime.RenderCommand;
022import org.apache.tapestry5.services.ComponentEventResultProcessor;
023import org.apache.tapestry5.services.PartialMarkupRenderer;
024import org.apache.tapestry5.services.PartialMarkupRendererFilter;
025
026import java.io.IOException;
027
028/**
029 * Processor for objects that implement {@link RenderCommand} (such as
030 * {@link org.apache.tapestry5.internal.structure.BlockImpl}), used with an Ajax component event.
031 *
032 * @see AjaxPartialResponseRenderer#renderPartialPageMarkup()
033 */
034public class RenderCommandComponentEventResultProcessor implements ComponentEventResultProcessor<RenderCommand>,
035        PartialMarkupRendererFilter
036{
037    private final PageRenderQueue pageRenderQueue;
038
039    private final AjaxFormUpdateController ajaxFormUpdateController;
040
041    private final AjaxPartialResponseRenderer partialRenderer;
042
043    public RenderCommandComponentEventResultProcessor(PageRenderQueue pageRenderQueue,
044                                                      AjaxFormUpdateController ajaxFormUpdateController, AjaxPartialResponseRenderer partialRenderer)
045    {
046        this.pageRenderQueue = pageRenderQueue;
047        this.ajaxFormUpdateController = ajaxFormUpdateController;
048        this.partialRenderer = partialRenderer;
049    }
050
051    public void processResultValue(RenderCommand value) throws IOException
052    {
053        pageRenderQueue.addPartialMarkupRendererFilter(this);
054        pageRenderQueue.addPartialRenderer(value);
055
056        // And setup to render the content deferred.
057
058        partialRenderer.renderPartialPageMarkup();
059    }
060
061    /**
062     * As a filter, this class does three things:
063     * <ul>
064     * <li>It creates an outer element to capture the partial page content that will be rendered</li>
065     * <li>It does setup and cleanup with the {@link AjaxFormUpdateController}</li>
066     * <li>It extracts the child markup and stuffs it into the reply's "content" property.</li>
067     * </ul>
068     */
069    public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
070    {
071        // The partial will quite often contain multiple elements (or just a block of plain text),
072        // so those must be enclosed in a root element.
073
074        Element root = writer.element("ajax-partial");
075
076        ajaxFormUpdateController.setupBeforePartialZoneRender(writer);
077
078        renderer.renderMarkup(writer, reply);
079
080        ajaxFormUpdateController.cleanupAfterPartialZoneRender();
081
082        writer.end();
083
084        String content = root.getChildMarkup().trim();
085
086        reply.put("content", content);
087    }
088}