Author: jmorliaguet
Date: Sat Dec 31 13:38:17 2005
New Revision: 2102

Modified:
   cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js
   cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html
Log:

- the node data structure can now be set, and updated



Modified: cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js
==============================================================================
--- cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js (original)
+++ cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js Sat Dec 31 
13:38:17 2005
@@ -135,29 +135,58 @@
 if (!window.Canvas) { var Canvas = new Object(); }
 Object.extend(Canvas, {
 
-  getNodeData: function(element) {
+  json_regexp: new RegExp(/^\sjson-data:/),
+
+  _getCommentNode: function(element) {
     var node = $(element);
-    var regExp = new RegExp(/^\sjson-data:/);
-    while(true) {
-      node = node.previousSibling;
-      if (!node) return null;
+    while(node = node.previousSibling) {
       var type = node.nodeType;
       if (type == 1) return null;
-      if (type == 8 || type == 4) {
+      if (type == 8) {
         var comment = node.nodeValue;
-        if (comment && comment.match(regExp)) {
-          comment = comment.replace(regExp, '');
-          return JSON.parse(comment);
+        if (comment && comment.match(this.json_regexp)) {
+          return comment;
         }
       }
     }
     return null;
   },
 
+  getNodeData: function(element) {
+    var comment = this._getCommentNode(element);
+    if (comment) {
+      comment = comment.replace(this.json_regexp, '');
+      try {
+        return JSON.parse(comment);
+      } catch(e) {
+        /* catch the parse exception */
+      }
+    }
+    return null;
+  },
+
+  setNodeData: function(element, data) {
+    var node = $(element);
+    var comment = ' json-data: ' + JSON.stringify(data);
+    comment = document.createComment(comment);
+    var old = this._getCommentNode(element);
+    if (old) {
+      //node.parentNode.removeChild(old);
+    }
+    node.parentNode.insertBefore(comment, node);
+  },
+
+  updateNodeData: function(element, data) {
+    var node = $(element);
+    var current = this.getNodeData(element) || new Object();
+    Object.extend(current, data);
+    this.setNodeData(element, current);
+  },
+
   addWidget: function(element){
     body = document.getElementsByTagName('body');
     if (!body.length) return;
-    body[0].appendChild(element);
+    body.item(0).appendChild(element);
   },
 
   setOpacity: function(element, opacity) {

Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html   
    (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html   
    Sat Dec 31 13:38:17 2005
@@ -121,13 +121,37 @@
       assertEqual(Identifiable.isEmpty(e6), true);
     }},
 
-    testGetData: function() { with(this) {
+    testGetNodeData: function() { with(this) {
       assertEqual(Canvas.getNodeData(e1), null);
       assertEqual($H(Canvas.getNodeData(e2)).inspect(),
                   $H({a:"1", b:"2"}).inspect());
       assertEqual(Canvas.getNodeData(e5), null);
       assertEqual($H(Canvas.getNodeData(e6)).inspect(),
         $H({"a":["1","2","3"],"b":{"c":1,"d":2},"c":[[1],[2],[3]]}).inspect());
+    }},
+
+    testSetNodeData: function() { with(this) {
+      Canvas.setNodeData(e1, {"new1":true});
+      assertEqual($H(Canvas.getNodeData(e1)).inspect(),
+                  $H({"new1":true}).inspect());
+
+      Canvas.setNodeData(e2, {"new2":true});
+      assertEqual($H(Canvas.getNodeData(e2)).inspect(),
+                  $H({"new2":true}).inspect());
+    }},
+
+    testUpdateNodeData: function() { with(this) {
+      Canvas.updateNodeData(e1, {"new3":false});
+      assertEqual($H(Canvas.getNodeData(e1)).inspect(),
+                  $H({"new1":true,"new3":false}).inspect());
+
+      Canvas.updateNodeData(e2, {"new2":false});
+      assertEqual($H(Canvas.getNodeData(e2)).inspect(),
+                  $H({"new2":false}).inspect());
+
+      Canvas.updateNodeData(e5, {"new5":false});
+      assertEqual($H(Canvas.getNodeData(e5)).inspect(),
+                  $H({"new5":false}).inspect());
     }}
 
   });
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to