[rhmessaging-commits] rhmessaging commits: r3222 - mgmt/trunk/wooly/resources.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Thu Mar 26 16:14:46 EDT 2009


Author: eallen
Date: 2009-03-26 16:14:46 -0400 (Thu, 26 Mar 2009)
New Revision: 3222

Modified:
   mgmt/trunk/wooly/resources/wooly.js
Log:
Adding wooly.session object.
Adding some methods to wooly to allow the background update timer to be cleared and restarted.

Modified: mgmt/trunk/wooly/resources/wooly.js
===================================================================
--- mgmt/trunk/wooly/resources/wooly.js	2009-03-26 20:13:12 UTC (rev 3221)
+++ mgmt/trunk/wooly/resources/wooly.js	2009-03-26 20:14:46 UTC (rev 3222)
@@ -307,6 +307,7 @@
         this.updatePage = updatePage
 
         this.updaterIDs = [];
+        this.session = new Session();
   
         // pass the responseXML directly back to function 
         this.directUpdate = function(url, callback, interval, passback) {
@@ -336,11 +337,6 @@
             }
         }
 
-        this.getNewRequest = getNewRequest
-        this.updatePage = updatePage
-
-        this.updaterIDs = [];
-
         this.deferredUpdate = function(url, callback, passback) {
             var req = this.getNewRequest();
 
@@ -392,17 +388,32 @@
             }
         }
 
+        this.intervalUpdateInfo = {
+            timerId: 0,
+            url: "",
+            callback: "",
+            interval: 0
+        };
         this.newSetIntervalUpdate = function(url, callback, interval) {
+            
             var req = this.getNewRequest();
 
+            // save the arguments in case we need to modify the url
+            // and restart the interval
+            this.intervalUpdateInfo.url = url;
+            this.intervalUpdateInfo.callback = callback;
+            this.intervalUpdateInfo.interval = interval;
+
             function fetch() {
                 req.open("get", url, true);
                 req.onreadystatechange = update;
                 req.send(null);
             }
 
+            fetch();
+
             var id = window.setInterval(fetch, interval);
-
+            this.intervalUpdateInfo.timerId = id;
             this.updaterIDs.push(id);
 
             function update() {
@@ -426,6 +437,23 @@
             }
         }
 
+        this.cancelIntervalUpdate = function () {
+            var id = this.intervalUpdateInfo.timerId;
+            if (this.updaterIDs.contains(id)) {
+                window.clearInterval(id);
+                this.updaterIDs.erase(id);
+            }
+        }
+
+        this.restartIntervalUpdate = function (url) {
+            this.cancelIntervalUpdate();
+            this.newSetIntervalUpdate(url, this.intervalUpdateInfo.callback, this.intervalUpdateInfo.interval);
+        }
+        
+        this.branchIntervalUpdate = function () {
+            return this.session.branch(this.intervalUpdateInfo.url);
+        }
+
         this._doc = new WoolyDocument(document);
 
         this.doc = function(doc) {
@@ -662,4 +690,94 @@
             return this;
         }
     }
+    
+    function Session() {
+        this.branch = function (url) {
+            if (arguments.length == 0) url = window.location;
+            return new Branch(url);
+        }
+
+        /* convert string query into cumin session object */
+        function Branch (s) {
+    
+            /* convert this branch object to string */
+            this.marshal = function () {
+                var nvs = [];
+                for (var key in this) {
+                    if (this[key] instanceof Array) {
+                        // there were multiple parameters with the same name
+                        for (var i=0; i < this[key].length; i++) {
+                            nvs.push(key + "=" + encodeURIComponent(this[key][i]));
+                        }
+                    } else if (key == "__page") {
+                        //pass;
+                    } else if (key == "session") {
+                        var sess = this[key].marshal();
+                        nvs.push("session="+encodeURIComponent(sess));
+                    } else if (this[key] == null) {
+                        nvs.push(key);
+                    } else if (!(this[key] instanceof Function)) {
+                        nvs.push(key + "=" + encodeURIComponent(this[key] + ""));
+                    }
+                }
+
+                return (nvs.length) ? ((this.__page) ? this.__page + "?" : "") + nvs.join(";") : (this.__page) ? this.__page : "";
+            }
+
+            this.set = function (key, value) {
+                this[key] = value;
+            }
+
+            // find the page
+            var query = s.split("?");
+            if (query.length > 1) {
+                this.__page = query[0];
+            } else if (s.indexOf("=") == -1) {
+                this.__page = s;
+                return;
+            } else {
+                this.__page = null;
+            }
+            // find the other nv pairs
+            var q = (query.length > 1) ? query[1] : s;
+            $extend(this, parseQuery(q));
+            
+            // background updates have another session object under the key "session"
+            if (this.session) {
+                var sess = decodeURIComponent(this.session);
+                this.session = wooly.session.branch(sess);
+            }
+        }
+        
+        /* convert ; separated string to and object. 
+           supports names with no values and
+           puts duplicate names into an array */
+        function parseQuery(s) {
+            var vars = s.split(";");
+            var obj = {};
+            for (var i=0; i < vars.length; i++) {
+                var val = vars[i];
+                var keys = val.split('=');
+                if (keys.length == 2) {
+                    var value = keys[1];
+                    if (typeof obj[keys[0]] != "undefined") {
+                        var oldval = obj[keys[0]];
+                        if (oldval.push) {
+                            oldval.push(value); // already added at least two values
+                        } else {
+                            obj[keys[0]] = [oldval, value]; // adding second value
+                        }
+                    } else {
+                        obj[keys[0]] = value;  // adding first value
+                    }
+                } else {
+                    obj[keys[0]] = null;  // there was no value
+                }
+            }
+            return obj;
+        }
+    }
+    
 }())
+
+




More information about the rhmessaging-commits mailing list