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.internal.services;
014
015import org.apache.tapestry5.json.JSONArray;
016import org.apache.tapestry5.services.javascript.InitializationPriority;
017import org.apache.tapestry5.services.javascript.ModuleConfigurationCallback;
018import org.apache.tapestry5.services.javascript.StylesheetLink;
019
020/**
021 * Responsible for injecting script and style links into the <head> and <body> element of the rendered HTML
022 * document.
023 *
024 * @see org.apache.tapestry5.services.javascript.ModuleManager#writeInitialization(org.apache.tapestry5.dom.Element, java.util.List, java.util.List)
025 * @since 5.4
026 */
027public interface DocumentLinker
028{
029
030    /**
031     * Adds a link to load a non-core JavaScript library. These libraries are loaded, sequentially, only once
032     * the core libraries have loaded and initialized. Thus difference between core libraries and other libraries
033     * is new in 5.4, and represents a conflict between asynchronous loading of modules (introduced in 5.4) and
034     * sequential loading of libraries (in 5.3 and earlier).
035     */
036    void addLibrary(String libraryURL);
037
038    /**
039     * A special case used only for the libraries that are part of the core stack, which itself contains RequireJS
040     * and is used to bootstrap up to adding non-core libraries.
041     *
042     * @since 5.4
043     */
044    void addCoreLibrary(String libraryURL);
045
046    /**
047     * Adds a link to load a CSS stylesheet.
048     */
049    void addStylesheetLink(StylesheetLink stylesheet);
050    
051    /**
052     * Adds a module configuration callback for this request.
053     * 
054     * @param callback a {@link ModuleConfigurationCallback}. It cannot be null.
055     * @since 5.4
056     */
057    void addModuleConfigurationCallback(ModuleConfigurationCallback callback);
058
059    /**
060     * Adds JavaScript code. The code is collected into a single block that is injected just before the close body tag
061     * of the page (in a full page render) and collected as the "script" property of the partial page render response.
062     * The JavaScript is executed after the page loads (or in an Ajax update, after external JavaScript libraries are
063     * loaded and the DOM is updated).
064     *
065     * This method may be called multiple times for the same priority and the script will be accumulated.
066     *
067     * @param priority
068     *         when to execute the provided script
069     * @param script
070     *         statement to add to the block (a newline will be appended as well)
071     */
072    void addScript(InitializationPriority priority, String script);
073
074    /**
075     * Adds initialization, based on invoking functions exported by JavaScript modules.
076     *
077     * @param priority
078     *         priority at which to perform initialization
079     * @param moduleName
080     *         name of module; the module exports a single function, or a map of functions
081     * @param functionName
082     *         name of function exported by module, or null (if the module exports a single function)
083     * @param arguments
084     *         arguments to pass to the function, or null if no arguments
085     * @since 5.4
086     */
087    void addInitialization(InitializationPriority priority,
088                           String moduleName,
089                           String functionName,
090                           JSONArray arguments);
091}