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