001// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.Asset;
018import org.apache.tapestry5.ComponentResources;
019import org.apache.tapestry5.FieldFocusPriority;
020import org.apache.tapestry5.RenderSupport;
021import org.apache.tapestry5.ioc.internal.util.InternalUtils;
022import org.apache.tapestry5.ioc.services.SymbolSource;
023import org.apache.tapestry5.json.JSONArray;
024import org.apache.tapestry5.json.JSONObject;
025import org.apache.tapestry5.services.AssetSource;
026import org.apache.tapestry5.services.javascript.JavaScriptSupport;
027import org.apache.tapestry5.services.javascript.StylesheetLink;
028import org.apache.tapestry5.services.javascript.StylesheetOptions;
029
030public class RenderSupportImpl implements RenderSupport
031{
032    private final SymbolSource symbolSource;
033
034    private final AssetSource assetSource;
035
036    private final JavaScriptSupport javascriptSupport;
037
038    // As of 5.2.1, RenderSupportImpl doesn't have any internal mutable state and could be converted
039    // to a service (using the service proxy to the JSS)
040    // instead of an Environmental. But we'll just delete it in 5.3.
041
042    /**
043     * @param symbolSource
044     *            Used to expand symbols (in {@linkplain #addClasspathScriptLink(String...)}
045     * @param assetSource
046     *            Used to convert classpath scripts to {@link org.apache.tapestry5.Asset}s
047     * @param javascriptSupport
048     *            Used to add JavaScript libraries and blocks of initialization JavaScript to the rendered page
049     */
050    public RenderSupportImpl(SymbolSource symbolSource, AssetSource assetSource, JavaScriptSupport javascriptSupport)
051    {
052        this.symbolSource = symbolSource;
053        this.assetSource = assetSource;
054        this.javascriptSupport = javascriptSupport;
055    }
056
057    public String allocateClientId(String id)
058    {
059        return javascriptSupport.allocateClientId(id);
060    }
061
062    public String allocateClientId(ComponentResources resources)
063    {
064        return javascriptSupport.allocateClientId(resources);
065    }
066
067    public void addScriptLink(Asset... scriptAssets)
068    {
069        for (Asset asset : scriptAssets)
070        {
071            assert asset != null;
072
073            javascriptSupport.importJavaScriptLibrary(asset);
074        }
075    }
076
077    public void addScriptLink(String... scriptURLs)
078    {
079        for (String url : scriptURLs)
080            javascriptSupport.importJavaScriptLibrary(url);
081    }
082
083    public void addClasspathScriptLink(String... classpaths)
084    {
085        for (String path : classpaths)
086            addScriptLinkFromClasspath(path);
087    }
088
089    private void addScriptLinkFromClasspath(String path)
090    {
091        String expanded = symbolSource.expandSymbols(path);
092
093        Asset asset = assetSource.getAsset(null, expanded, null);
094
095        addScriptLink(asset);
096    }
097
098    public void addScript(String script)
099    {
100        javascriptSupport.addScript(script);
101    }
102
103    public void addScript(String format, Object... arguments)
104    {
105        javascriptSupport.addScript(format, arguments);
106    }
107
108    public void addInit(String functionName, JSONArray parameterList)
109    {
110        javascriptSupport.addInitializerCall(functionName, parameterList);
111    }
112
113    public void addInit(String functionName, JSONObject parameter)
114    {
115        javascriptSupport.addInitializerCall(functionName, parameter);
116    }
117
118    public void addInit(String functionName, String... parameters)
119    {
120        JSONArray array = new JSONArray();
121
122        for (String parameter : parameters)
123        {
124            array.put(parameter);
125        }
126
127        addInit(functionName, array);
128    }
129
130    public void autofocus(FieldFocusPriority priority, String fieldId)
131    {
132        javascriptSupport.autofocus(priority, fieldId);
133    }
134
135    public void addStylesheetLink(Asset stylesheet, String media)
136    {
137        javascriptSupport.importStylesheet(new StylesheetLink(stylesheet, new StylesheetOptions(media)));
138    }
139
140    public void addStylesheetLink(String stylesheetURL, String media)
141    {
142        javascriptSupport.importStylesheet(new StylesheetLink(stylesheetURL, new StylesheetOptions(media)));
143    }
144}