Author: rgardler
Date: Thu Nov  3 00:29:12 2011
New Revision: 1196887

URL: http://svn.apache.org/viewvc?rev=1196887&view=rev
Log:
refactor callbacks to make them more widely available to widgets built from 
templates. Allow callbacks to be associated with either a class of events or an 
event with a single widget. Add a clickItem event to the browse template

Modified:
    incubator/wookie/trunk/widgets/templates/base/scripts/controller.js
    incubator/wookie/trunk/widgets/templates/browse/readme.txt
    incubator/wookie/trunk/widgets/templates/browse/scripts/browse_controller.js

Modified: incubator/wookie/trunk/widgets/templates/base/scripts/controller.js
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/templates/base/scripts/controller.js?rev=1196887&r1=1196886&r2=1196887&view=diff
==============================================================================
--- incubator/wookie/trunk/widgets/templates/base/scripts/controller.js 
(original)
+++ incubator/wookie/trunk/widgets/templates/base/scripts/controller.js Thu Nov 
 3 00:29:12 2011
@@ -20,6 +20,12 @@
  * This is used to wire up the view and model with actions
  */ 
 var ${widget.shortname}_controller = {
+    /**
+     * A dictionary of callback functions that are called whenever an
+     * event is fired by this widget.
+     */
+    callbacks: {},
+
     init:function() {
         ${widget.shortname}_controller.update();
     },
@@ -54,6 +60,59 @@ var ${widget.shortname}_controller = {
            e = document.documentElement || document.body;
        }
        return { width : e[ a + 'Width' ] , height : e[ a + 'Height' ] }
+    },
+
+    /**
+     * Register a callback function. 
+     *
+     * type: the name of the type of event to respond to 
+     *
+     * widget: [optional] the name of the widget where the event must
+     * occur for the callback to be executed. This allows callbacks to
+     * be register so that they will only respond to events in
+     * specific widgets, or to all events.
+     */
+    register:function(event, callback, widget) {
+       if (widget === undefined) {
+           var cbks = ${widget.shortname}_controller.callbacks[event];
+       } else {
+           var cbks = ${widget.shortname}_controller.callbacks[widget + "." + 
event];
+       }
+       if (cbks === undefined) {
+           cbks = new Array();
+       }
+       cbks.push(callback);
+       ${widget.shortname}_controller.callbacks[event] = cbks;
+    },
+
+    /**
+     * Execute all the callbacks registered for a given event.
+     *
+     * The event object is passed directly to the callback function
+     * and can contani any number of properties. Two properties that
+     * important to the callback functionality are:
+     *
+     * type: the name of the type of event to respond to 
+     *
+     * widget: [optional] the name of the widget where the event
+     * occured. This allows callbacks to be register so that they will
+     * only respond to events in specific widgets, or to all events.
+     */
+    executeCallbacks:function(event) {
+       // Execute all callbacks not restricted to a widget
+       var cbks = ${widget.shortname}_controller.callbacks[event.type];
+       if (cbks === undefined) return;
+       for (var i = 0; i < cbks.length; i++) {
+           cbks[i](event);
+       }
+
+       // Execute all callbacks restricted to a widget
+       if (event.widget === undefined) return;
+       var cbks = ${widget.shortname}_controller.callbacks[event.widget + "." 
+ event.type];
+       if (cbks === undefined) return;
+       for (var i = 0; i < cbks.length; i++) {
+           cbks[i](event);
+       }
     }
 };
 

Modified: incubator/wookie/trunk/widgets/templates/browse/readme.txt
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/templates/browse/readme.txt?rev=1196887&r1=1196886&r2=1196887&view=diff
==============================================================================
--- incubator/wookie/trunk/widgets/templates/browse/readme.txt (original)
+++ incubator/wookie/trunk/widgets/templates/browse/readme.txt Thu Nov  3 
00:29:12 2011
@@ -44,19 +44,47 @@ API.
 ** Events
 
 You can register callbacks for events using the
-WIDGETNAME_browse_controller.register(EVENTNAME, callback function)
-method.
+WIDGETNAME_controller.register(EVENTNAME, callback function, WIDGETNAME)
+method. Callbacks will recieve an event object as a parameter which
+will include the following properties:
+
+  * type: a string identifying the type of event
+  * widget: a string identifying the shortname of the widget generating the 
event
+
+When registering a callback you can either register it to respond to
+all events of a given type or only events fired by a specific
+widget. To respond to all events simply omit the WIDGETNAME in the
+register method call.
 
 Available events are:
 
-*** Expand
+*** ClickItem
+
+Called whenever an item in the browse list is clicked on. 
+
+The event object will have an "itemId" property that contains the ID for the 
item clicked.
+
+**** Examples
+
+#+BEGIN_SRC javascript
+viewItem = function(event) {
+    alert("Item with id " + event.itemId + " was clicked by the user");
+}
+
+${widget.shortname}_browse_controller.register("clickItem", viewItem, 
${widget.shortname});
+#+END_SRC
+
+*** ExpandItem
 
 Called whenever an item in the browse list has been expanded. The
 function is passed an event object (which is generated by JQuery) and
 the ID of the item being expanded.
 
+The event object will have an "itemId" property that contains the ID
+for the item expanded.
+
 #+BEGIN_SRC javascript
-${widget.shortname}_browse_controller.register("expand", function(event, 
itemid) {
-    alert("expanded item with id " + itemId);
+${widget.shortname}_browse_controller.register("expandItem", function(event) {
+    alert("expanded item with id " + event.itemId);
 });
 #+END_SRC

Modified: 
incubator/wookie/trunk/widgets/templates/browse/scripts/browse_controller.js
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/widgets/templates/browse/scripts/browse_controller.js?rev=1196887&r1=1196886&r2=1196887&view=diff
==============================================================================
--- 
incubator/wookie/trunk/widgets/templates/browse/scripts/browse_controller.js 
(original)
+++ 
incubator/wookie/trunk/widgets/templates/browse/scripts/browse_controller.js 
Thu Nov  3 00:29:12 2011
@@ -20,12 +20,6 @@
  * This is used to wire up the view and model with actions
  */ 
 var ${widget.shortname}_browse_controller = {
-    /**
-     * A dictionary of callback function that are called whenver an
-     * event is fired by this widget.
-     */
-    callbacks: {},
-
     init:function() {
         ${widget.shortname}_browse_controller.update();
         ${widget.shortname}_browse_controller.search()
@@ -82,19 +76,7 @@ var ${widget.shortname}_browse_controlle
         });
         $('#content-primary').html(html).trigger("create");
     },
-    
-    /**
-     * Register a callback function for an an event in this widget.
-     */
-    register:function(event, callback) {
-       var cbks = ${widget.shortname}_browse_controller.callbacks[event];
-       if (cbks === undefined) {
-           cbks = new Array();
-       }
-       cbks.push(callback);
-       ${widget.shortname}_browse_controller.callbacks[event] = cbks;
-    },
-    
+        
     /**
      * Retrieve the details of an item and display them in the detail section.
      */
@@ -106,7 +88,11 @@ var ${widget.shortname}_browse_controlle
             xslurl:${browse.detail.xsl.url}
         });
         $(".detail").html(html);    
-    }
+       $('.detail').click(function() {
+           var event = { widget: "${widget.shortname}", type: "clickItem", 
itemId: itemId};        
+           ${widget.shortname}_controller.executeCallbacks(event);
+       });
+    }                    
 }
 
 /**
@@ -121,10 +107,9 @@ $('#home').live('pageshow',function(even
  * Display the content of a result item when it is expanded.
  */
 $('div.result').live('expand', function(event) {
-    ${widget.shortname}_browse_controller.displaySummary($(this).attr("wid"));
-    var cbks = ${widget.shortname}_browse_controller.callbacks["expand"];
-    if (cbks === undefined) return;
-    for (var i = 0; i < cbks.length; i++) {
-       cbks[i](event, $(this).attr("wid"));
-    }
+    var wid = $(this).attr("wid");
+    ${widget.shortname}_browse_controller.displaySummary(wid);
+    var itemId = wid.substr(wid.indexOf("id=")+3);
+    var event = { widget: "${widget.shortname}", type: "expandItem", itemId: 
itemId};      
+    ${widget.shortname}_controller.executeCallbacks(event );
 });
\ No newline at end of file


Reply via email to