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;
016
017import org.apache.tapestry5.annotations.Environmental;
018import org.apache.tapestry5.ioc.services.SymbolSource;
019import org.apache.tapestry5.json.JSONArray;
020import org.apache.tapestry5.json.JSONObject;
021import org.apache.tapestry5.services.AssetSource;
022import org.apache.tapestry5.services.EnvironmentalShadowBuilder;
023import org.apache.tapestry5.services.javascript.JavaScriptSupport;
024
025/**
026 * Provides support to all components that render. This is primarily about generating unique client-side ids (very
027 * important for JavaScript generation) as well as accumulating JavaScript to be sent to the client. PageRenderSupport
028 * also allows for the incremental addition of stylesheets.
029 * <p/>
030 * When rendering, a &lt;script&gt; block will be added to the bottom of the page (just before the &lt;/body&gt; tag).
031 * The scripting statements added to this block will be executed, on the client, only once the page has fully loaded.
032 * <p>
033 * The methods in this interface are largely being replaced with a new environmental interface,
034 * {@link JavaScriptSupport}.
035 * <p>
036 * RenderSupport is normally accessed within a component by using the {@link Environmental} annotation on a component
037 * field. In addition, RenderSupport may also be accessed as a service (the service
038 * {@linkplain EnvironmentalShadowBuilder internally delegates to the current environmental instance}), which is useful
039 * for service-layer objects.
040 * 
041 * @deprecated RenderSupport has been replaced by {@link JavaScriptSupport} and may be removed after Tapestry 5.3.
042 */
043public interface RenderSupport
044{
045    /**
046     * Allocates a unique id based on the component's id. In some cases, the return value will not precisely match the
047     * input value (an underscore and a unique index value may be appended).
048     * 
049     * @param id
050     *            the component id from which a unique id will be generated
051     * @return a unique id for this rendering of the page
052     * @see org.apache.tapestry5.ioc.util.IdAllocator
053     * @see JavaScriptSupport#allocateClientId(ComponentResources)
054     * @deprecated Use {@link JavaScriptSupport#allocateClientId(String)} instead
055     */
056    String allocateClientId(String id);
057
058    /**
059     * As with {@link #allocateClientId(String)} but uses the id of the component extracted from the resources.
060     * 
061     * @param resources
062     *            of the component which requires an id
063     * @return a unique id for this rendering of the page
064     * @deprecated Use {@link JavaScriptSupport#allocateClientId(ComponentResources)} instead
065     */
066    String allocateClientId(ComponentResources resources);
067
068    /**
069     * Adds one or more new script assets to the page. Assets are added uniquely, and appear as &lt;script&gt; elements
070     * inside the &lt;head&gt; element of the rendered page. Duplicate requests to add the same script are quietly
071     * ignored.
072     * 
073     * @param scriptAssets
074     *            asset to the script to add
075     * @deprecated Use {@link JavaScriptSupport#importJavaScriptLibrary(Asset)} instead
076     */
077    void addScriptLink(Asset... scriptAssets);
078
079    /**
080     * Adds some number of script links as strings representations of URLs. The scripts are passed down to the client
081     * as-is. Typically, this is used to reference a script stored outside the web application entirely.
082     * 
083     * @param scriptURLs
084     *            URL strings of scripts
085     * @deprecated Use {@link JavaScriptSupport#importJavaScriptLibrary(String)} instead
086     * @throws RuntimeException
087     *             <strong>always</strong> as of 5.2.0
088     */
089    void addScriptLink(String... scriptURLs);
090
091    /**
092     * Used to add scripts that are stored on the classpath. Each element has {@linkplain SymbolSource symbols
093     * expanded}, then is {@linkplain AssetSource converted to an asset} and added as a script link.
094     * 
095     * @param classpaths
096     *            array of paths. Symbols in the paths are expanded, then the paths are each converted into an
097     *            asset.
098     * @deprecated Use {@link JavaScriptSupport#importJavaScriptLibrary(Asset)} instead
099     */
100    void addClasspathScriptLink(String... classpaths);
101
102    /**
103     * Adds a link to a CSS stylesheet. As with JavaScript libraries, each stylesheet is added at most once. Stylesheets
104     * added this way will be ordered before any other content, in the &lt;head&gt; element of the document. The
105     * &lt;head&gt; element will be created, if necessary.
106     * 
107     * @param stylesheet
108     *            the asset referencing the stylesheet
109     * @param media
110     *            the media value for the stylesheet, or null to not specify a specific media type
111     */
112
113    void addStylesheetLink(Asset stylesheet, String media);
114
115    /**
116     * Adds a stylesheet as a URL. See notes in {@link #addScriptLink(String[])}.
117     * 
118     * @param stylesheetURL
119     *            URL string of stylesheet
120     * @param media
121     *            media value for the stylesheet, or null to not specify a specific media type
122     */
123    void addStylesheetLink(String stylesheetURL, String media);
124
125    /**
126     * Adds a script statement to the page's script block. A newline will be added after the script statement.
127     * 
128     * @param script
129     *            text to be added to the script block
130     * @deprecated Use {@link JavaScriptSupport#addScript(String, Object...)} instead
131     */
132    void addScript(String script);
133
134    /**
135     * Adds a script statement to the page's script block. The parameters are passed to
136     * {@link String#format(String, Object[])} before being added to the script block. A newline will be added after the
137     * formatted statement.
138     * 
139     * @param format
140     *            base string format, to be passed through String.format
141     * @param arguments
142     *            additional arguments formatted to form the final script
143     * @deprecated Use {@link JavaScriptSupport#addScript(String, Object...)} instead
144     */
145    void addScript(String format, Object... arguments);
146
147    /**
148     * Add an initialization call. This method is deprecated and, although it still works, it now generates
149     * very verbose, inefficient client-side JavaScript.
150     * 
151     * @param functionName
152     *            the name of the function (on the client-side Tapestry.Initializer object) to invoke.
153     * @param parameterList
154     *            list of parameters for the method invocation.
155     * @see #addScript(String, Object[])
156     * @deprecated Use {@link JavaScriptSupport#addInitializerCall(String, JSONObject)} instead (which may require
157     *             changes to your JavaScript initializer function)
158     */
159    void addInit(String functionName, JSONArray parameterList);
160
161    /**
162     * Alternate version of {@link #addInit(String, org.apache.tapestry5.json.JSONArray)} where just a single object is
163     * passed.
164     * 
165     * @param functionName
166     *            the name of the function (on the client-side Tapestry object) to invoke.
167     * @param parameter
168     *            the object to pass to the function
169     * @deprecated Use {@link JavaScriptSupport#addInitializerCall(String, JSONObject)} instead
170     */
171    void addInit(String functionName, JSONObject parameter);
172
173    /**
174     * Alternate version of {@link #addInit(String, org.apache.tapestry5.json.JSONArray)} where one or more strings are
175     * passed. A single string is added to the initialization call as itself; otherwise, the parameters are combined to
176     * form a {@link JSONArray}. This method is deprecated and, although it still works, it now generates
177     * very verbose, inefficient client-side JavaScript.
178     * 
179     * @param functionName
180     *            the name of the function (on the client-side Tapestry object) to invoke.
181     * @param parameters
182     * @deprecated Use {@link JavaScriptSupport#addInitializerCall(String, JSONObject)} instead (which may require
183     *             changes to your JavaScript initializer function), or (for a single parameter)
184     *             {@link JavaScriptSupport#addInitializerCall(String, String)}
185     */
186    void addInit(String functionName, String... parameters);
187
188    /**
189     * Invoked to set focus on a rendered field. Takes into account priority, meaning that a field with errors will take
190     * precedence over a merely required field, and over a field that is optional. The value
191     * {@link org.apache.tapestry5.FieldFocusPriority#OVERRIDE} can be used to force a particular field to receive
192     * focus.
193     * 
194     * @param priority
195     *            focus is set only if the provided priority is greater than the current priority
196     * @param fieldId
197     *            id of client-side element to take focus
198     */
199    void autofocus(FieldFocusPriority priority, String fieldId);
200}