001    package org.apache.tapestry5.internal.services.ajax;
002    
003    import org.apache.tapestry5.ClientBodyElement;
004    import org.apache.tapestry5.MarkupWriter;
005    import org.apache.tapestry5.internal.services.PageRenderQueue;
006    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
007    import org.apache.tapestry5.ioc.services.TypeCoercer;
008    import org.apache.tapestry5.json.JSONObject;
009    import org.apache.tapestry5.runtime.RenderCommand;
010    import org.apache.tapestry5.services.PartialMarkupRenderer;
011    import org.apache.tapestry5.services.PartialMarkupRendererFilter;
012    import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
013    import org.apache.tapestry5.services.ajax.JSONCallback;
014    import org.apache.tapestry5.services.ajax.JavaScriptCallback;
015    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
016    
017    
018    public class AjaxResponseRendererImpl implements AjaxResponseRenderer
019    {
020        private final PageRenderQueue queue;
021    
022        private final AjaxFormUpdateController ajaxFormUpdateController;
023    
024        private final TypeCoercer typeCoercer;
025    
026        private final JavaScriptSupport javaScriptSupport;
027    
028        public AjaxResponseRendererImpl(PageRenderQueue queue, AjaxFormUpdateController ajaxFormUpdateController, TypeCoercer typeCoercer, JavaScriptSupport javaScriptSupport)
029        {
030            this.queue = queue;
031            this.ajaxFormUpdateController = ajaxFormUpdateController;
032            this.typeCoercer = typeCoercer;
033            this.javaScriptSupport = javaScriptSupport;
034        }
035    
036        public AjaxResponseRenderer addRender(String clientId, Object renderer)
037        {
038            assert InternalUtils.isNonBlank(clientId);
039            assert renderer != null;
040    
041            RenderCommand command = typeCoercer.coerce(renderer, RenderCommand.class);
042    
043            addFilter(new SingleZonePartialRendererFilter(clientId, command, queue, ajaxFormUpdateController));
044    
045            return this;
046        }
047    
048        public AjaxResponseRenderer addRender(ClientBodyElement zone)
049        {
050            assert zone != null;
051    
052            addRender(zone.getClientId(), zone.getBody());
053    
054            return this;
055        }
056    
057        public AjaxResponseRenderer addCallback(final JavaScriptCallback callback)
058        {
059            assert callback != null;
060    
061            addFilter(new PartialMarkupRendererFilter()
062            {
063                public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
064                {
065                    callback.run(javaScriptSupport);
066    
067                    renderer.renderMarkup(writer, reply);
068                }
069            });
070    
071            return this;
072        }
073    
074        public AjaxResponseRenderer addCallback(final Runnable callback)
075        {
076            assert callback != null;
077    
078            addFilter(new PartialMarkupRendererFilter()
079            {
080                public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
081                {
082                    callback.run();
083    
084                    renderer.renderMarkup(writer, reply);
085                }
086            });
087    
088    
089            return this;
090        }
091    
092        public AjaxResponseRenderer addFilter(PartialMarkupRendererFilter filter)
093        {
094            assert filter != null;
095    
096            queue.addPartialMarkupRendererFilter(filter);
097    
098            return this;
099        }
100    
101        public AjaxResponseRenderer addCallback(final JSONCallback callback)
102        {
103            assert callback != null;
104    
105            addFilter(new PartialMarkupRendererFilter()
106            {
107                public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
108                {
109                    callback.run(reply);
110    
111                    renderer.renderMarkup(writer, reply);
112                }
113            });
114    
115            return this;
116        }
117    }