001// Copyright 2012-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.services.javascript;
016
017import org.apache.tapestry5.dom.Element;
018import org.apache.tapestry5.ioc.Resource;
019import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
020
021import java.util.List;
022
023/**
024 * Responsible for managing access to the JavaScript modules.
025 * <p/>
026 * The configuration of the service allows overrides of the default search path; the configuration keys
027 * are module names, and the configuration values are the {@link JavaScriptModuleConfiguration} definitions for those module names.
028 * This is primarily used to wrap non-AMD compliant libraries for use with RequireJS (via contributed {@link JavaScriptModuleConfiguration}s).
029 *
030 * @since 5.4
031 * @see ModuleConfigurationCallback
032 * @see AMDWrapper
033 */
034@UsesMappedConfiguration(JavaScriptModuleConfiguration.class)
035public interface ModuleManager
036{
037    /**
038     * Invoked by the internal {@link org.apache.tapestry5.internal.services.DocumentLinker} service to write the configuration
039     * for the module system into the page.
040     *
041     * @param body
042     *         {@code <body>} element of the page, to which new {@code <script>} element(s) will be added.
043     * @param moduleConfigurationCallbacks
044     *         a list of {@link org.apache.tapestry5.services.javascript.ModuleConfigurationCallback}s, which
045     *         is used to customize the configuration before it is written.
046     */
047    void writeConfiguration(Element body,
048                            List<ModuleConfigurationCallback> moduleConfigurationCallbacks);
049
050    /**
051     * Invoked by the internal {@link org.apache.tapestry5.internal.services.DocumentLinker} service to write the initializations
052     * (as per {@link JavaScriptSupport#require(String)} into the page; this occurs after the module infrastructure
053     * has been written into the page, along with the core libraries.
054     *
055     * @param body
056     *         {@code <body>} element of the page, to which new {@code <script>} element(s) will be added.
057     * @param libraryURLs
058     *         additional libraries that should be dynamically loaded before evaluating the inits
059     * @param inits
060     *         specify initialization on the page, based on loading modules, extacting functions from modules, and invoking those functions
061     */
062    void writeInitialization(Element body, List<String> libraryURLs, List<?> inits);
063
064    /**
065     * Given a module name (which may be a path of names separated by slashes), locates the corresponding {@link Resource}.
066     * First checks for {@linkplain JavaScriptModuleConfiguration contributed shim modules}, then searches for possible matches among the
067     * {@linkplain org.apache.tapestry5.services.ComponentClassResolver#getLibraryNames() defined library names}.  As a special
068     * case, the folder name "app" is mapped to the application's package.
069     *
070     * @param moduleName
071     *         name of module to locate
072     * @return corresponding resource, or null if not found
073     */
074    Resource findResourceForModule(String moduleName);
075
076}