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.modules;
014
015import org.apache.tapestry5.BooleanHook;
016import org.apache.tapestry5.MarkupWriter;
017import org.apache.tapestry5.SymbolConstants;
018import org.apache.tapestry5.annotations.Path;
019import org.apache.tapestry5.internal.InternalConstants;
020import org.apache.tapestry5.internal.services.DocumentLinker;
021import org.apache.tapestry5.internal.services.ResourceStreamer;
022import org.apache.tapestry5.internal.services.ajax.JavaScriptSupportImpl;
023import org.apache.tapestry5.internal.services.assets.ResourceChangeTracker;
024import org.apache.tapestry5.internal.services.javascript.*;
025import org.apache.tapestry5.internal.util.MessageCatalogResource;
026import org.apache.tapestry5.ioc.*;
027import org.apache.tapestry5.ioc.annotations.Contribute;
028import org.apache.tapestry5.ioc.annotations.Primary;
029import org.apache.tapestry5.ioc.annotations.Symbol;
030import org.apache.tapestry5.ioc.services.FactoryDefaults;
031import org.apache.tapestry5.ioc.services.SymbolProvider;
032import org.apache.tapestry5.ioc.util.IdAllocator;
033import org.apache.tapestry5.json.JSONObject;
034import org.apache.tapestry5.services.*;
035import org.apache.tapestry5.services.compatibility.Compatibility;
036import org.apache.tapestry5.services.compatibility.Trait;
037import org.apache.tapestry5.services.javascript.*;
038import org.apache.tapestry5.services.messages.ComponentMessagesSource;
039
040import java.util.Locale;
041
042/**
043 * Defines the services related to JavaScript and {@link org.apache.tapestry5.services.javascript.JavaScriptStack}s.
044 *
045 * @since 5.4
046 */
047public class JavaScriptModule
048{
049    private final static String ROOT = "${tapestry.asset.root}";
050
051    private final Environment environment;
052
053    private final EnvironmentalShadowBuilder environmentalBuilder;
054
055    public JavaScriptModule(Environment environment, EnvironmentalShadowBuilder environmentalBuilder)
056    {
057        this.environment = environment;
058        this.environmentalBuilder = environmentalBuilder;
059    }
060
061    public static void bind(ServiceBinder binder)
062    {
063        binder.bind(ModuleManager.class, ModuleManagerImpl.class);
064        binder.bind(JavaScriptStackSource.class, JavaScriptStackSourceImpl.class);
065        binder.bind(JavaScriptStack.class, ExtensibleJavaScriptStack.class).withMarker(Core.class).withId("CoreJavaScriptStack");
066        binder.bind(JavaScriptStack.class, ExtensibleJavaScriptStack.class).withMarker(Internal.class).withId("InternalJavaScriptStack");
067    }
068
069    /**
070     * Contributes the "core" and "internal" {@link JavaScriptStack}s
071     *
072     * @since 5.2.0
073     */
074    @Contribute(JavaScriptStackSource.class)
075    public static void provideBuiltinJavaScriptStacks(MappedConfiguration<String, JavaScriptStack> configuration,
076                                                      @Core JavaScriptStack coreStack,
077                                                      @Internal JavaScriptStack internalStack)
078    {
079        configuration.add(InternalConstants.CORE_STACK_NAME, coreStack);
080        configuration.add("internal", internalStack);
081    }
082
083    // These are automatically bundles with the core JavaScript stack; some applications may want to add a few
084    // additional ones, such as t5/core/zone.
085    private static final String[] bundledModules = new String[]{
086            "alert", "ajax", "bootstrap", "console", "dom", "events", "exception-frame", "fields", "forms",
087            "pageinit", "messages", "utils", "validation"
088    };
089
090    /**
091     * The core JavaScriptStack has a number of entries:
092     * <dl>
093     * <dt>requirejs</dt> <dd>The RequireJS AMD JavaScript library</dd>
094     * <dt>scriptaculous.js, effects.js</dt> <dd>Optional JavaScript libraries in compatibility mode (see {@link Trait#SCRIPTACULOUS})</dd>
095     * <dt>t53-compatibility.js</dt> <dd>Optional JavaScript library (see {@link Trait#INITIALIZERS})</dd>
096     * <dt>underscore-library, underscore-module</dt>
097     * <dt>The Underscore JavaScript library, and the shim that allows underscore to be injected</dt>
098     * <dt>t5/core/init</dt> <dd>Optional module related to t53-compatibility.js</dd>
099     * <dt>jquery-library</dt> <dd>The jQuery library</dd>
100     * <dt>jquery-noconflict</dt> <dd>Switches jQuery to no-conflict mode (only present when the infrastructure is "prototype").</dd>
101     * <dt>jquery</dt> <dd>A module shim that allows jQuery to be injected (and also switches jQuery to no-conflict mode)</dd>
102     * <dt>bootstrap.css, tapestry.css, exception-frame.css, tapestry-console.css, tree.css</dt>
103     * <dd>CSS files</dd>
104     * <dt>t5/core/[...]</dt>
105     * <dd>Additional JavaScript modules</dd>
106     * <dt>jquery</dt>
107     * <dd>Added if the infrastructure provider is "jquery".</dd>
108     * </dl>
109     *
110     * User modules may replace or extend this list.
111     */
112    @Contribute(JavaScriptStack.class)
113    @Core
114    public static void setupCoreJavaScriptStack(OrderedConfiguration<StackExtension> configuration,
115                                                Compatibility compatibility,
116                                                @Symbol(SymbolConstants.JAVASCRIPT_INFRASTRUCTURE_PROVIDER)
117                                                String provider)
118    {
119        configuration.add("requirejs", StackExtension.library(ROOT + "/require.js"));
120        configuration.add("underscore-library", StackExtension.library(ROOT + "/underscore-1.8.3.js"));
121
122        if (provider.equals("prototype"))
123        {
124            final String SCRIPTY = "${tapestry.scriptaculous}";
125
126            add(configuration, StackExtensionType.LIBRARY, SCRIPTY + "/prototype.js");
127
128            if (compatibility.enabled(Trait.SCRIPTACULOUS))
129            {
130                add(configuration, StackExtensionType.LIBRARY,
131                        SCRIPTY + "/scriptaculous.js",
132                        SCRIPTY + "/effects.js");
133            }
134        }
135
136        if (compatibility.enabled(Trait.INITIALIZERS))
137        {
138            add(configuration, StackExtensionType.LIBRARY, ROOT + "/t53-compatibility.js");
139            configuration.add("t5/core/init", new StackExtension(StackExtensionType.MODULE, "t5/core/init"));
140        }
141
142        configuration.add("jquery-library", StackExtension.library(ROOT + "/jquery.js"));
143
144        if (provider.equals("prototype"))
145        {
146            configuration.add("jquery-noconflict", StackExtension.library(ROOT + "/jquery-noconflict.js"));
147        }
148
149        add(configuration, StackExtensionType.MODULE, "jquery");
150
151        addCoreStylesheets(configuration, "${" + SymbolConstants.BOOTSTRAP_ROOT + "}/css/bootstrap.css");
152
153        for (String name : bundledModules)
154        {
155            String full = "t5/core/" + name;
156            configuration.add(full, StackExtension.module(full));
157        }
158
159        configuration.add("underscore-module", StackExtension.module("underscore"));
160    }
161
162    @Contribute(JavaScriptStack.class)
163    @Internal
164    public static void setupInternalJavaScriptStack(OrderedConfiguration<StackExtension> configuration)
165    {
166
167        // For the internal stack, ignore the configuration and just use the Bootstrap CSS shipped with the
168        // framework. This is part of a hack to make internal pages (such as ExceptionReport and T5Dashboard)
169        // render correctly even when the Bootstrap CSS has been replaced by the application.
170
171        addCoreStylesheets(configuration, ROOT + "/bootstrap/css/bootstrap.css");
172    }
173
174    private static void addCoreStylesheets(OrderedConfiguration<StackExtension> configuration, String bootstrapPath)
175    {
176        add(configuration, StackExtensionType.STYLESHEET,
177                bootstrapPath,
178
179                ROOT + "/tapestry.css",
180
181                ROOT + "/exception-frame.css",
182
183                ROOT + "/tapestry-console.css",
184
185                ROOT + "/tree.css");
186    }
187
188    private static void add(OrderedConfiguration<StackExtension> configuration, StackExtensionType type, String... paths)
189    {
190        for (String path : paths)
191        {
192            int slashx = path.lastIndexOf('/');
193            String id = path.substring(slashx + 1);
194
195            configuration.add(id, new StackExtension(type, path));
196        }
197    }
198
199
200    /**
201     * Builds a proxy to the current {@link JavaScriptSupport} inside this thread's {@link org.apache.tapestry5.services.Environment}.
202     *
203     * @since 5.2.0
204     */
205    public JavaScriptSupport buildJavaScriptSupport()
206    {
207        return environmentalBuilder.build(JavaScriptSupport.class);
208    }
209
210    @Contribute(Dispatcher.class)
211    @Primary
212    public static void setupModuleDispatchers(OrderedConfiguration<Dispatcher> configuration,
213                                              ModuleManager moduleManager,
214                                              OperationTracker tracker,
215                                              ResourceStreamer resourceStreamer,
216                                              PathConstructor pathConstructor,
217                                              JavaScriptStackSource javaScriptStackSource,
218                                              JavaScriptStackPathConstructor javaScriptStackPathConstructor,
219                                              @Symbol(SymbolConstants.MODULE_PATH_PREFIX)
220                                              String modulePathPrefix,
221                                              @Symbol(SymbolConstants.ASSET_PATH_PREFIX)
222                                              String assetPathPrefix)
223    {
224        configuration.add("Modules",
225                new ModuleDispatcher(moduleManager, resourceStreamer, tracker, pathConstructor,
226                    javaScriptStackSource, javaScriptStackPathConstructor, modulePathPrefix, assetPathPrefix, false),
227                "after:Asset", "before:ComponentEvent");
228
229        configuration.add("ComnpressedModules",
230                new ModuleDispatcher(moduleManager, resourceStreamer, tracker, pathConstructor,
231                    javaScriptStackSource, javaScriptStackPathConstructor, modulePathPrefix, assetPathPrefix, true),
232                "after:Modules", "before:ComponentEvent");
233    }
234
235    /**
236     * Adds page render filters, each of which provides an {@link org.apache.tapestry5.annotations.Environmental}
237     * service. Filters
238     * often provide {@link org.apache.tapestry5.annotations.Environmental} services needed by
239     * components as they render.
240     * <dl>
241     * <dt>JavascriptSupport</dt>
242     * <dd>Provides {@link JavaScriptSupport}</dd>
243     * </dl>
244     */
245    @Contribute(MarkupRenderer.class)
246    public void exposeJavaScriptSupportForFullPageRenders(OrderedConfiguration<MarkupRendererFilter> configuration,
247                                                          final JavaScriptStackSource javascriptStackSource,
248                                                          final JavaScriptStackPathConstructor javascriptStackPathConstructor,
249                                                          final Request request)
250    {
251
252        final BooleanHook suppressCoreStylesheetsHook = createSuppressCoreStylesheetHook(request);
253
254        MarkupRendererFilter javaScriptSupport = new MarkupRendererFilter()
255        {
256            public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer)
257            {
258                DocumentLinker linker = environment.peekRequired(DocumentLinker.class);
259
260                JavaScriptSupportImpl support = new JavaScriptSupportImpl(linker, javascriptStackSource,
261                        javascriptStackPathConstructor, suppressCoreStylesheetsHook);
262
263                environment.push(JavaScriptSupport.class, support);
264
265                renderer.renderMarkup(writer);
266
267                environment.pop(JavaScriptSupport.class);
268
269                support.commit();
270            }
271        };
272
273        configuration.add("JavaScriptSupport", javaScriptSupport, "after:DocumentLinker");
274    }
275
276    /**
277     * Contributes {@link PartialMarkupRendererFilter}s used when rendering a
278     * partial Ajax response.
279     * <dl>
280     * <dt>JavaScriptSupport
281     * <dd>Provides {@link JavaScriptSupport}</dd>
282     * </dl>
283     */
284    @Contribute(PartialMarkupRenderer.class)
285    public void exposeJavaScriptSupportForPartialPageRender(OrderedConfiguration<PartialMarkupRendererFilter> configuration,
286                                                            final JavaScriptStackSource javascriptStackSource,
287
288                                                            final JavaScriptStackPathConstructor javascriptStackPathConstructor,
289
290                                                            final Request request)
291    {
292        final BooleanHook suppressCoreStylesheetsHook = createSuppressCoreStylesheetHook(request);
293
294        PartialMarkupRendererFilter javascriptSupport = new PartialMarkupRendererFilter()
295        {
296            public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
297            {
298                IdAllocator idAllocator;
299
300                if (request.getParameter(InternalConstants.SUPPRESS_NAMESPACED_IDS) == null)
301                {
302                    String uid = Long.toHexString(System.nanoTime());
303
304                    String namespace = "_" + uid;
305
306                    idAllocator = new IdAllocator(namespace);
307                } else
308                {
309                    // When suppressed, work just like normal rendering.
310                    idAllocator = new IdAllocator();
311                }
312
313                DocumentLinker linker = environment.peekRequired(DocumentLinker.class);
314
315                JavaScriptSupportImpl support = new JavaScriptSupportImpl(linker, javascriptStackSource,
316                        javascriptStackPathConstructor, idAllocator, true, suppressCoreStylesheetsHook);
317
318                environment.push(JavaScriptSupport.class, support);
319
320                renderer.renderMarkup(writer, reply);
321
322                environment.pop(JavaScriptSupport.class);
323
324                support.commit();
325            }
326        };
327
328        configuration.add("JavaScriptSupport", javascriptSupport, "after:DocumentLinker");
329    }
330
331    private BooleanHook createSuppressCoreStylesheetHook(final Request request)
332    {
333        return new BooleanHook()
334        {
335            @Override
336            public boolean checkHook()
337            {
338                return request.getAttribute(InternalConstants.SUPPRESS_CORE_STYLESHEETS) != null;
339            }
340        };
341    }
342
343
344    @Contribute(ModuleManager.class)
345    public static void setupBaseModules(MappedConfiguration<String, Object> configuration,
346                                        @Path("${tapestry.asset.root}/underscore-shim.js")
347                                        Resource underscoreShim,
348
349                                        @Path("${tapestry.asset.root}/jquery-shim.js")
350                                        Resource jqueryShim,
351
352                                        @Path("${tapestry.asset.root}/typeahead.js")
353                                        Resource typeahead,
354
355                                        @Path("${tapestry.asset.root}/moment-2.10.6.js")
356                                        Resource moment,
357
358                                        @Path("${" + SymbolConstants.BOOTSTRAP_ROOT + "}/js/transition.js")
359                                        Resource transition)
360    {
361        // The underscore shim module allows Underscore to be injected
362        configuration.add("underscore", new JavaScriptModuleConfiguration(underscoreShim));
363        configuration.add("jquery", new JavaScriptModuleConfiguration(jqueryShim));
364
365        configuration.add("bootstrap/transition", new AMDWrapper(transition).require("jquery", "$").asJavaScriptModuleConfiguration());
366
367        for (String name : new String[]{"affix", "alert", "button", "carousel", "collapse", "dropdown", "modal",
368                "scrollspy", "tab", "tooltip"})
369        {
370            Resource lib = transition.forFile(name + ".js");
371
372            configuration.add("bootstrap/" + name, new AMDWrapper(lib).require("bootstrap/transition").asJavaScriptModuleConfiguration());
373        }
374
375        Resource popover = transition.forFile("popover.js");
376
377        configuration.add("bootstrap/popover", new AMDWrapper(popover).require("bootstrap/tooltip").asJavaScriptModuleConfiguration());
378
379        configuration.add("t5/core/typeahead", new JavaScriptModuleConfiguration(typeahead).dependsOn("jquery"));
380
381        configuration.add("moment", new JavaScriptModuleConfiguration(moment));
382
383    }
384
385    @Contribute(SymbolProvider.class)
386    @FactoryDefaults
387    public static void setupFactoryDefaults(MappedConfiguration<String, Object> configuration)
388    {
389        configuration.add(SymbolConstants.JAVASCRIPT_INFRASTRUCTURE_PROVIDER, "prototype");
390        configuration.add(SymbolConstants.MODULE_PATH_PREFIX, "modules");
391    }
392
393    @Contribute(ModuleManager.class)
394    public static void setupFoundationFramework(MappedConfiguration<String, Object> configuration,
395                                                @Symbol(SymbolConstants.JAVASCRIPT_INFRASTRUCTURE_PROVIDER)
396                                                String provider,
397                                                @Path("classpath:org/apache/tapestry5/t5-core-dom-prototype.js")
398                                                Resource domPrototype,
399                                                @Path("classpath:org/apache/tapestry5/t5-core-dom-jquery.js")
400                                                Resource domJQuery)
401    {
402        if (provider.equals("prototype"))
403        {
404            configuration.add("t5/core/dom", new JavaScriptModuleConfiguration(domPrototype));
405        }
406
407        if (provider.equals("jquery"))
408        {
409            configuration.add("t5/core/dom", new JavaScriptModuleConfiguration(domJQuery));
410        }
411
412        // If someone wants to support a different infrastructure, they should set the provider symbol to some other value
413        // and contribute their own version of the t5/core/dom module.
414    }
415
416    @Contribute(ModuleManager.class)
417    public static void setupApplicationCatalogModules(MappedConfiguration<String, Object> configuration,
418                                                      LocalizationSetter localizationSetter,
419                                                      ComponentMessagesSource messagesSource,
420                                                      ResourceChangeTracker resourceChangeTracker,
421                                                      @Symbol(SymbolConstants.COMPACT_JSON) boolean compactJSON)
422    {
423        for (Locale locale : localizationSetter.getSupportedLocales())
424        {
425            MessageCatalogResource resource = new MessageCatalogResource(locale, messagesSource, resourceChangeTracker, compactJSON);
426
427            configuration.add("t5/core/messages/" + locale.toString(), new JavaScriptModuleConfiguration(resource));
428        }
429    }
430
431    /**
432     * Contributes 'ConfigureHTMLElement', which writes the attributes into the HTML tag to describe locale, etc.
433     * Contributes 'AddBrowserCompatibilityStyles', which writes {@code <style/>} elements into the {@code <head/>}
434     * element that modifies the page loading mask to work on IE 8 and IE 9.
435     */
436    @Contribute(MarkupRenderer.class)
437    public static void prepareHTMLPageOnRender(OrderedConfiguration<MarkupRendererFilter> configuration)
438    {
439        configuration.addInstance("ConfigureHTMLElement", ConfigureHTMLElementFilter.class);
440        configuration.add("AddBrowserCompatibilityStyles", new AddBrowserCompatibilityStyles());
441    }
442
443}