Author: hlship
Date: Wed Jun 29 00:17:31 2011
New Revision: 1140916

URL: http://svn.apache.org/viewvc?rev=1140916&view=rev
Log:
TAP5-999: Make more use of nested namespaces under T5

Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-init.js
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
    
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js?rev=1140916&r1=1140915&r2=1140916&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js
 Wed Jun 29 00:17:31 2011
@@ -16,7 +16,7 @@
 /**
  * Extends T5 with new utility functions.
  */
-T5.extend(T5, function() {
+T5.define("arrays", function() {
 
        function isNonEmpty(array) {
                if (array === null || array === undefined)
@@ -25,6 +25,12 @@ T5.extend(T5, function() {
                return array.length > 0;
        }
 
+       function isEmpty(array) {
+               return !isNonEmpty(array);
+       }
+
+       var concat = Array.prototype.concat;
+
        /**
         * Iterates over an array, invoking a function for each array element.
         * 
@@ -88,8 +94,6 @@ T5.extend(T5, function() {
                return accumulator;
        }
 
-       var concat = Array.prototype.concat;
-
        /**
         * A variation of map, where the mapperfn is expected to return an 
array of
         * values (not a single value). The result arrays are concatenated, to
@@ -133,11 +137,88 @@ T5.extend(T5, function() {
                return array;
        }
 
+       /**
+        * Filters the array, returning a new array containing just those 
elements
+        * for which the filterfn returns true.
+        * 
+        * @param filterfn
+        *            function of one or two parameters: element and index. 
Returns
+        *            true to include element in result.
+        * @param array
+        *            to filter
+        */
+       function filter(filterfn, array) {
+               var result = [];
+
+               each(function(element, index) {
+                       if (filterfn(element, index)) {
+                               result.push(element);
+                       }
+               }, array);
+
+               return result;
+       }
+
+       /**
+        * Filters the array, returning a new array containing just those 
elements
+        * for which the filterfn returns false.
+        * 
+        * @param filterfn
+        *            function of one or two parameters: element and index. 
Returns
+        *            true to include element in result.
+        * @param array
+        *            to filter
+        */
+       function remove(filterfn, array) {
+               return filter(function(element, index) {
+                       return !filterfn(element, index);
+               }, array);
+       }
+
+       /**
+        * Returns the first element that passes the filterfn, or null if not 
found.
+        * 
+        * @param filterfn
+        *            function of one or two parameters: element and index. 
Returns
+        *            true if the element is a match.
+        * @param arary
+        *            to scan
+        * @return first element for which the function indicates a match, or 
null
+        * 
+        */
+       function first(filterfn, array) {
+               if (isNonEmpty(array)) {
+                       for ( var index = 0; index < array.length; index++) {
+                               if (filterfn(array[index], index)) {
+                                       return array[index];
+                               }
+                       }
+               }
+
+               return null;
+       }
+
+       /**
+        * Returns a function for use with map() or filter(), that extracts a
+        * specific property, by name, from the elements.
+        */
+       function extractProperty(name) {
+               return function(element) {
+                       return element[name];
+               }
+       }
+
        return {
                each : each,
+               extractProperty : extractProperty,
+               filter : filter,
+               first : first,
+               isEmpty : isEmpty,
+               isNonEmpty : isNonEmpty,
                map : map,
                mapcat : mapcat,
                reduce : reduce,
+               remove : remove,
                without : without
        };
 });
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js?rev=1140916&r1=1140915&r2=1140916&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-core.js
 Wed Jun 29 00:17:31 2011
@@ -1,4 +1,4 @@
-/* Copyright  2011 The Apache Software Foundation
+/* Copyright 2011 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.
@@ -30,11 +30,31 @@ var T5 = {
         * @param source
         *            source object for properties, or function returning source
         *            object
+        * @returns the destination object
         */
        extend : function(destination, source) {
                if (Object.isFunction(source))
                        source = source();
 
-               Object.extend(destination, source);
+               // Prototype:
+               return Object.extend(destination, source);
+       },
+
+       /**
+        * Defines a new namespace under the T5 object.
+        * 
+        * @param name
+        *            string name of the namespace
+        * @param source
+        *            source object for properties (or function returning source
+        *            object)
+        * @return the namespace object
+        */
+       define : function(name, source) {
+               var namespace = {};
+               T5[name] = namespace;
+
+               return this.extend(namespace, source);
        }
+
 }
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-init.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-init.js?rev=1140916&r1=1140915&r2=1140916&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-init.js
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-init.js
 Wed Jun 29 00:17:31 2011
@@ -1,4 +1,4 @@
-/* Copyright  2011 The Apache Software Foundation
+/* Copyright 2011 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.
@@ -24,7 +24,7 @@ T5.extend(T5, function() {
                Initializer : {},
 
                /**
-                * A convienience method for extending the T5.Initializer 
namespace.
+                * A convenience method for extending the T5.Initializer 
namespace.
                 * 
                 * @param source
                 *            object or function used to extend T5.Initializer

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js?rev=1140916&r1=1140915&r2=1140916&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
 Wed Jun 29 00:17:31 2011
@@ -13,10 +13,13 @@
  * limitations under the License.
  */
 
-T5.extend(T5, function() {
+T5.define("pubsub", function() {
 
-       var map = T5.map;
-       var mapcat = T5.mapcat;
+       var arrays = T5.arrays;
+
+       var map = arrays.map;
+       var mapcat = arrays.mapcat;
+       var without = arrays.without;
 
        var subscribersVersion = 0;
 
@@ -47,7 +50,7 @@ T5.extend(T5, function() {
        function doPublish(listeners, message) {
 
                return map(function(fn) {
-                       fn(message);
+                       return fn(message);
                }, listeners);
        }
 
@@ -110,7 +113,7 @@ T5.extend(T5, function() {
        function unsubscribe(selector, listenerfn) {
                var listeners = subscribers[selector];
 
-               var editted = T5.without(listenerfn, listeners);
+               var editted = without(listenerfn, listeners);
 
                if (editted !== listeners) {
                        subscribers[selector] = editted;
@@ -151,8 +154,17 @@ T5.extend(T5, function() {
        }
 
        return {
-               createPublisher : createPublisher,
-               pub : publish,
-               sub : subscribe
+               create : createPublisher,
+               publish : publish,
+               subscribe : subscribe
        };
+});
+
+/**
+ * Create aliases on T5 directly: pub -&gt; pubsub.publish and sub -&gt;
+ * pubsub.subscribe.
+ */
+T5.extend(T5, {
+       pub : T5.pubsub.publish,
+       sub : T5.pubsub.subscribe
 });
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml?rev=1140916&r1=1140915&r2=1140916&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
 Wed Jun 29 00:17:31 2011
@@ -224,10 +224,10 @@
 
   <script>
 <![CDATA[
-    map = T5.map
-    mapcat = T5.mapcat
-    reduce = T5.reduce
-    without = T5.without
+    map = T5.arrays.map
+    mapcat = T5.arrays.mapcat
+    reduce = T5.arrays.reduce
+    without = T5.arrays.without
 
     pub = T5.pub
     sub = T5.sub


Reply via email to