You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2012/08/03 23:33:10 UTC

[6/10] git commit: Begin working on an SPI (service provider interface) as the core of the framework abstraction layer Begin updating some of the Tapestry libraries to make use of RequireJS require() and define()

Begin working on an SPI (service provider interface) as the core of the framework abstraction layer
Begin updating some of the Tapestry libraries to make use of RequireJS require() and define()


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/75bcc330
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/75bcc330
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/75bcc330

Branch: refs/heads/5.4-js-rewrite
Commit: 75bcc330381c944f0e6db03d817ee689ea461baa
Parents: 997397c
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Aug 3 09:57:25 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Aug 3 09:57:25 2012 -0700

----------------------------------------------------------------------
 tapestry-core/spi.coffee                           |   95 +
 .../services/javascript/CoreJavaScriptStack.java   |  175 +-
 .../main/resources/org/apache/tapestry5/t5-core.js |    2 +-
 .../resources/org/apache/tapestry5/tapestry.js     | 3091 ++++++++-------
 4 files changed, 1734 insertions(+), 1629 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/75bcc330/tapestry-core/spi.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/spi.coffee b/tapestry-core/spi.coffee
new file mode 100644
index 0000000..d93a722
--- /dev/null
+++ b/tapestry-core/spi.coffee
@@ -0,0 +1,95 @@
+# Copyright 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.
+# You may obtain a copy of the License at
+#
+#     http:#www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Service Provider Interface
+# This is the core of the abstraction layer that allows the majority of components to operate
+# without caring whether the underlying infrastructure framework is Prototype, jQuery, or something else.
+# This is the standard SPI, which wraps Prototype ... but does it in a way that makes it relatively
+# easy to swap in jQuery instead.
+
+# TODO: The dependency on prototype is temporary while prototype/scriptaculous is part of the core stack.
+# Either it will be broken out (likely), or something else will change. This will likely not work
+# with JavaScript aggregation enabled.
+
+define ["_", "prototype"], (_) ->
+
+  split = (str) ->
+    _(str.split " ").reject (s) -> s is ""
+
+  # Generic view of an Event that is passed to a handler function.
+  #
+  class Event
+
+    constructor: (@prototypeEvent) ->
+
+    # Stops the event which prevents further propagation of the event,
+    # as well as event bubbling.
+    stop: ->
+      @prototypeEvent.stop()
+
+  # Value returned from on(); an EventHandler is used to stop listening to
+  # events, or even pause listening.
+  class EventHandler
+
+    # Registers the handler as an event listener for matching elements and event names.
+    # elements - array of DOM elements
+    # eventNames - array of event names
+    # match - selector to match bubbled elements, or null
+    # handler - event handler function to invoke; it will be passed an Event instance
+    constructor: (elements, eventNames, match, handler) ->
+
+      wrapped = (prototypeEvent, matchedElement) ->
+        # Set "this" to be the matched element (jQuery style), rather than
+        # the element on which the event is observed.
+        handler.call(matchedElement, new Event prototypeEvent)
+
+      # Prototype Event.Handler instances
+      @protoHandlers = []
+
+      _.each elements, (element) =>
+        _.each eventName, (eventName) =>
+          @protoHandlers.push element.on event, match, wrapped
+
+     # Invoked after stop() to restart event listening. Returns this EventHandler instance.
+     start: ->
+
+       _.each @protoHandlers, (h) -> h.start()
+
+       this
+
+    # Invoked to stop or pause event listening. Returns this EventHandler instance.
+    stop: ->
+
+      _.each @protoHandlers, (h) -> h.stop()
+
+      this
+
+  exports =
+
+    # on() is used to add an event handler
+    # selector - CSS selector used to select elements to attach handler to
+    # events - one or more event names, separated by spaces
+    # match - optional: filters the descendents of the matched elements; the event is
+    # only triggered for matching element
+    # handler - function invoked; the function is passed an Event object.
+    # Returns an EventHandler object, with methods start() and stop().
+    on: (selector, events, match, handler) ->
+
+      if handler is null
+        handler = match
+        match = null
+
+      elements = $$ selector
+
+      return new EventHandler(elements, split events, match, handler)

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/75bcc330/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/CoreJavaScriptStack.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/CoreJavaScriptStack.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/CoreJavaScriptStack.java
index 803ce51..fa7ab06 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/CoreJavaScriptStack.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/CoreJavaScriptStack.java
@@ -36,135 +36,140 @@ import java.util.Locale;
  *
  * @since 5.2.0
  */
-public class CoreJavaScriptStack implements JavaScriptStack {
-  private final boolean productionMode;
+public class CoreJavaScriptStack implements JavaScriptStack
+{
+    private final boolean productionMode;
 
-  private final SymbolSource symbolSource;
+    private final SymbolSource symbolSource;
 
-  private final AssetSource assetSource;
+    private final AssetSource assetSource;
 
-  private final ThreadLocale threadLocale;
+    private final ThreadLocale threadLocale;
 
-  private final List<Asset> javaScriptStack, stylesheetStack;
+    private final List<Asset> javaScriptStack, stylesheetStack;
 
-  private static final String ROOT = "org/apache/tapestry5";
+    private static final String ROOT = "org/apache/tapestry5";
 
-  private static final String[] CORE_JAVASCRIPT = new String[]
-          {
-                  // Core scripts added to any page that uses scripting
+    private static final String[] CORE_JAVASCRIPT = new String[]
+            {
+                    // Core scripts added to any page that uses scripting
 
-                  // TEMPORARY: For the moment, exposing Underscore as a module
-                  // and as a library; this is because statically loaded libraries (t5-core.js)
-                  // has no way to wait for module '_' to be loaded.
-                  "classpath:org/apache/tapestry5/underscore_1_3_3.js",
+                    "${tapestry.scriptaculous}/prototype.js",
 
-                  "${tapestry.scriptaculous}/prototype.js",
+                    "${tapestry.scriptaculous}/scriptaculous.js",
 
-                  "${tapestry.scriptaculous}/scriptaculous.js",
+                    "${tapestry.scriptaculous}/effects.js",
 
-                  "${tapestry.scriptaculous}/effects.js",
+                    // Below uses functions defined by the prior three.
 
-                  // Below uses functions defined by the prior three.
+                    // Order is important, there are some dependencies
+                    // going on here. Switching over to a more managed module system
+                    // is starting to look like a really nice idea!
 
-                  // Order is important, there are some dependencies
-                  // going on here. Switching over to a more managed module system
-                  // is starting to look like a really nice idea!
+                    ROOT + "/t5-core.js",
 
-                  ROOT + "/t5-core.js",
+                    ROOT + "/t5-spi.js",
 
-                  ROOT + "/t5-spi.js",
+                    ROOT + "/t5-prototype.js",
 
-                  ROOT + "/t5-prototype.js",
+                    ROOT + "/t5-init.js",
 
-                  ROOT + "/t5-init.js",
+                    ROOT + "/t5-pubsub.js",
 
-                  ROOT + "/t5-pubsub.js",
+                    ROOT + "/t5-events.js",
 
-                  ROOT + "/t5-events.js",
+                    ROOT + "/t5-dom.js",
 
-                  ROOT + "/t5-dom.js",
+                    ROOT + "/t5-console.js",
 
-                  ROOT + "/t5-console.js",
+                    ROOT + "/t5-ajax.js",
 
-                  ROOT + "/t5-ajax.js",
+                    ROOT + "/t5-formfragment.js",
 
-                  ROOT + "/t5-formfragment.js",
+                    ROOT + "/t5-alerts.js",
 
-                  ROOT + "/t5-alerts.js",
+                    ROOT + "/tapestry.js",
 
-                  ROOT + "/tapestry.js",
+                    ROOT + "/tapestry-console.js",
 
-                  ROOT + "/tapestry-console.js",
+                    ROOT + "/tree.js",
+            };
 
-                  ROOT + "/tree.js",
-          };
+    // Because of changes to the logic of how stylesheets get incorporated, the default stylesheet
+    // was removed, the logic for it is now in TapestryModule.contributeMarkupRenderer().
 
-  // Because of changes to the logic of how stylesheets get incorporated, the default stylesheet
-  // was removed, the logic for it is now in TapestryModule.contributeMarkupRenderer().
+    private static final String[] CORE_STYLESHEET = new String[]
+            {
+                    ROOT + "/tapestry-console.css",
 
-  private static final String[] CORE_STYLESHEET = new String[]
-          {
-                  ROOT + "/tapestry-console.css",
+                    ROOT + "/t5-alerts.css",
 
-                  ROOT + "/t5-alerts.css",
+                    ROOT + "/tree.css"
+            };
 
-                  ROOT + "/tree.css"
-          };
+    public CoreJavaScriptStack(
+            @Symbol(SymbolConstants.PRODUCTION_MODE)
+            boolean productionMode,
 
-  public CoreJavaScriptStack(
-          @Symbol(SymbolConstants.PRODUCTION_MODE)
-          boolean productionMode,
+            SymbolSource symbolSource,
 
-          SymbolSource symbolSource,
+            AssetSource assetSource,
 
-          AssetSource assetSource,
+            ThreadLocale threadLocale)
+    {
+        this.symbolSource = symbolSource;
+        this.productionMode = productionMode;
+        this.assetSource = assetSource;
+        this.threadLocale = threadLocale;
 
-          ThreadLocale threadLocale) {
-    this.symbolSource = symbolSource;
-    this.productionMode = productionMode;
-    this.assetSource = assetSource;
-    this.threadLocale = threadLocale;
+        javaScriptStack = convertToAssets(CORE_JAVASCRIPT);
+        stylesheetStack = convertToAssets(CORE_STYLESHEET);
+    }
 
-    javaScriptStack = convertToAssets(CORE_JAVASCRIPT);
-    stylesheetStack = convertToAssets(CORE_STYLESHEET);
-  }
+    public String getInitialization()
+    {
+        return productionMode ? null : "Tapestry.DEBUG_ENABLED = true;";
+    }
 
-  public String getInitialization() {
-    return productionMode ? null : "Tapestry.DEBUG_ENABLED = true;";
-  }
+    public List<String> getStacks()
+    {
+        return Collections.emptyList();
+    }
 
-  public List<String> getStacks() {
-    return Collections.emptyList();
-  }
+    private List<Asset> convertToAssets(String[] paths)
+    {
+        List<Asset> assets = CollectionFactory.newList();
 
-  private List<Asset> convertToAssets(String[] paths) {
-    List<Asset> assets = CollectionFactory.newList();
+        for (String path : paths)
+        {
+            assets.add(expand(path, null));
+        }
 
-    for (String path : paths) {
-      assets.add(expand(path, null));
+        return Collections.unmodifiableList(assets);
     }
 
-    return Collections.unmodifiableList(assets);
-  }
-
-  private Asset expand(String path, Locale locale) {
-    String expanded = symbolSource.expandSymbols(path);
+    private Asset expand(String path, Locale locale)
+    {
+        String expanded = symbolSource.expandSymbols(path);
 
-    return assetSource.getAsset(null, expanded, locale);
-  }
+        return assetSource.getAsset(null, expanded, locale);
+    }
 
-  public List<Asset> getJavaScriptLibraries() {
-    Asset messages = assetSource.getAsset(null, ROOT + "/tapestry-messages.js", threadLocale.getLocale());
+    public List<Asset> getJavaScriptLibraries()
+    {
+        Asset messages = assetSource.getAsset(null, ROOT + "/tapestry-messages.js", threadLocale.getLocale());
 
-    return createStack(javaScriptStack, messages).toList();
-  }
+        return createStack(javaScriptStack, messages).toList();
+    }
 
-  public List<StylesheetLink> getStylesheets() {
-    return createStack(stylesheetStack).map(TapestryInternalUtils.assetToStylesheetLink)
-            .toList();
-  }
+    public List<StylesheetLink> getStylesheets()
+    {
+        return createStack(stylesheetStack).map(TapestryInternalUtils.assetToStylesheetLink)
+                .toList();
+    }
 
-  private Flow<Asset> createStack(List<Asset> stack, Asset... assets) {
-    return F.flow(stack).append(assets);
-  }
+    private Flow<Asset> createStack(List<Asset> stack, Asset... assets)
+    {
+        return F.flow(stack).append(assets);
+    }
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/75bcc330/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js b/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
index afc88e1..2419e14 100644
--- a/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
+++ b/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
@@ -89,7 +89,7 @@ T5.proxyFunctionsToModule = function (moduleName) {
     // later.
     require(deps, function () { });
 
-   _.each(functionNames, function (name) {
+    _.each(functionNames, function (name) {
         result[name] = function () {
             var capturedArguments = slice.call(arguments, 0);