Add @Import.modules(), to support module initializations
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/1222f492 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/1222f492 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/1222f492 Branch: refs/heads/5.4-js-rewrite Commit: 1222f4928799a27f440e0afda92e8e2cb7179c1a Parents: 0a3e456 Author: Howard M. Lewis Ship <[email protected]> Authored: Mon Aug 13 15:33:08 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Mon Aug 13 15:33:08 2012 -0700 ---------------------------------------------------------------------- .../META-INF/modules/core/pageinit.coffee | 11 +++ .../org/apache/tapestry5/annotations/Import.java | 17 ++++- .../tapestry5/internal/transform/ImportWorker.java | 66 ++++++++++++++- 3 files changed, 92 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1222f492/tapestry-core/src/main/coffeescript/META-INF/modules/core/pageinit.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/pageinit.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/pageinit.coffee index 89ba343..923fdbe 100644 --- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/pageinit.coffee +++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/pageinit.coffee @@ -81,10 +81,21 @@ define ["_", "core/console", "core/spi", "core/events"], [moduleName, functionName] = qualifiedName.split ':' require [moduleName], (moduleLib) -> + + # Some modules export nothing but do some full-page initialization, such as adding + # event handlers to the body. + if not functionName and + initArguments.length is 0 and + not _.isFunction moduleLib + tracker() + return + fn = if functionName? then moduleLib[functionName] else moduleLib + console.debug "Invoking #{qualifiedName} with " + JSON.stringify(initArguments) fn.apply null, initArguments + tracker() exports = http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1222f492/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Import.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Import.java b/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Import.java index 1d6c177..d78b53f 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Import.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Import.java @@ -1,4 +1,4 @@ -// Copyright 2010, 2011 The Apache Software Foundation +// Copyright 2010-2012 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -67,4 +67,19 @@ public @interface Import * @see JavaScriptSupport#importStylesheet(org.apache.tapestry5.Asset) */ String[] stylesheet() default {}; + + /** + * Names of modules to import. A module name consists of a path, with the terms seperated by a slash character. The first + * term is the library name (or "app" for the application), e.g. <code>flash/gordon</code> would map to the file + * <code>META-INF/modules/flash/gordon.js</code>. Alternately a function name can be included, after a colon seperator. + * e.g., <code>flash/gordon:setup</code>. + * <p/> + * Module initializations specified this way may not have an parameters, so they are typically doing single-use + * setup. + * + * @see org.apache.tapestry5.services.javascript.ModuleManager + * @see JavaScriptSupport#require(String) + * @since 5.4 + */ + String[] modules() default {}; } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1222f492/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ImportWorker.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ImportWorker.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ImportWorker.java index 839ac20..2c48d3f 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ImportWorker.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ImportWorker.java @@ -1,4 +1,4 @@ -// Copyright 2010, 2011 The Apache Software Foundation +// Copyright 2010-2012 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,10 +26,14 @@ import org.apache.tapestry5.model.MutableComponentModel; import org.apache.tapestry5.plastic.*; import org.apache.tapestry5.services.AssetSource; import org.apache.tapestry5.services.TransformConstants; +import org.apache.tapestry5.services.javascript.Initialization; import org.apache.tapestry5.services.javascript.JavaScriptSupport; import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2; import org.apache.tapestry5.services.transform.TransformationSupport; +import java.util.ArrayList; +import java.util.List; + /** * Implements the {@link Import} annotation, both at the class and at the method level. * @@ -113,6 +117,8 @@ public class ImportWorker implements ComponentClassTransformWorker2 importLibraries(componentClass, model, method, annotation.library()); importStylesheets(componentClass, model, method, annotation.stylesheet()); + + importModules(method, annotation.modules()); } private void importStacks(PlasticMethod method, String[] stacks) @@ -139,6 +145,64 @@ public class ImportWorker implements ComponentClassTransformWorker2 }; } + private void importModules(PlasticMethod method, String[] moduleNames) + { + if (moduleNames.length != 0) + { + method.addAdvice(createImportModulesAdvice(moduleNames)); + } + } + + class ModuleImport + { + final String moduleName, functionName; + + ModuleImport(String moduleName, String functionName) + { + this.moduleName = moduleName; + this.functionName = functionName; + } + + void apply(JavaScriptSupport javaScriptSupport) + { + Initialization initialization = javaScriptSupport.require(moduleName); + + if (functionName != null) + { + initialization.invoke(functionName); + } + } + } + + private MethodAdvice createImportModulesAdvice(final String[] moduleNames) + { + final List<ModuleImport> moduleImports = new ArrayList<ModuleImport>(moduleNames.length); + + for (String name : moduleNames) + { + int colonx = name.indexOf(':'); + + String moduleName = colonx < 0 ? name : name.substring(0, colonx); + String functionName = colonx < 0 ? null : name.substring(colonx + 1); + + moduleImports.add(new ModuleImport(moduleName, functionName)); + } + + return new MethodAdvice() + { + @Override + public void advise(MethodInvocation invocation) + { + for (ModuleImport moduleImport : moduleImports) + { + moduleImport.apply(javascriptSupport); + } + + invocation.proceed(); + } + }; + } + private void importLibraries(PlasticClass plasticClass, MutableComponentModel model, PlasticMethod method, String[] paths) {
