Author: jmorliaguet
Date: Sat Jan 28 12:45:43 2006
New Revision: 2227

Modified:
   cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js
Log:

- added support for storage adapters in the model.

  storage adapters need to implement getData() and setData(data)

  they are bound to the model's DOM node which is used as a context.

  currently there is only one type of storage, i.e. the "canvas" storage that
  uses the page's canvas to store and retrieve data (client-side storage).

  the "canvas" storage is used by default if not storage is specified.

  TODO:

  - add a "remote data storage" to get and set data from the server.




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 Jan 28 
12:45:43 2006
@@ -277,7 +277,7 @@
 
   getIdentifiable: function(node) {
     node = $(node);
-    if (this.isIdentifiable(node)) { return node; } 
+    if (this.isIdentifiable(node)) { return node; }
     return this.getParent(node);
   },
 
@@ -313,20 +313,23 @@
     return null;
   },
 
+  getModelDef: function(node) {
+    var model_node = this._getModelNode(node);
+    if (model_node) {
+      return CPSSkins.jsonParse(model_node);
+    }
+  },
+
   getNodeData: function(node) {
     node = $(node);
-    var node_id = Identifiable.isIdentifiable(node) ? 
+    var node_id = Identifiable.isIdentifiable(node) ?
                   node.getAttribute("id") : null;
 
     if (node_id && node_id in CPSSkins.Cache) {
        return CPSSkins.Cache[node_id].data;
     }
 
-    var node_info = null;
-    var model_node = this._getModelNode(node);
-    if (model_node) {
-      node_info = CPSSkins.jsonParse(model_node);
-    }
+    var node_info = this.getModelDef(node);
 
     if (node_id && node_info) {
       CPSSkins.Cache[node_id] = node_info;
@@ -336,7 +339,7 @@
 
   setNodeData: function(node, data) {
     node = $(node);
-    var node_id = Identifiable.isIdentifiable(node) ? 
+    var node_id = Identifiable.isIdentifiable(node) ?
                   node.getAttribute("id") : null;
 
     var node_info = {};
@@ -368,13 +371,6 @@
 
   },
 
-  updateNodeData: function(node, data) {
-    node = $(node);
-    var current = this.getNodeData(node) || new Object();
-    Object.extend(current, data);
-    this.setNodeData(node, current);
-  },
-
   createNode: function(options) {
     var node = document.createElement(options.tag);
     Element.addClassName(node, options.classes);
@@ -559,22 +555,81 @@
   initialize: function(node) {
     this.node = $(node);
     CPSSkins.Models[this.node] = this;
+    // set the storage adapter
+    try {
+      this._setStorageAdapter();
+    } catch(e) {
+      /* XXX: this should not occur, but some views (e.g. panels) have no
+         obvious model, hence no obvious storage adapter */
+    }
   },
 
   getData: function() {
-    return Canvas.getNodeData(this.node);
+    return this.storage.getData()
   },
 
   setData: function(data) {
-    Canvas.setNodeData(this.node, data)
+    this.storage.setData(data);
   },
 
   updateData: function(data) {
-    Canvas.updateNodeData(this.node, data)
-  }
+    var current = this.storage.getData() || new Object();
+    Object.extend(current, data);
+    this.storage.setData(data);
+  },
+
+  _setStorageAdapter: function() {
+    var model_def = Canvas.getModelDef(this.node);
+    var storage_def = model_def.storage;
+    if (!storage_def) {
+      storage_def = {"type": "canvas"};
+    }
+    this.storage = Storages[storage_def.type](this.node, model_def);
+  },
 
 }
 
+// Storage adapter base class
+CPSSkins.StorageAdapter = Class.create();
+CPSSkins.StorageAdapter.prototype = {
+
+  initialize: function(node, def) {
+    this.node = $(node);
+    this.def = def;
+  },
+
+  getData: function() {
+    /* to be overriden */
+  },
+
+  setData: function(data) {
+    /* to be overriden */
+  }
+}
+
+if (!window.Storages) { var Storages = new Object(); }
+Object.extend(Storages, {
+
+  canvas: function(node, def) {
+    return new CPSSkins.CanvasStorage(node, def);
+  },
+
+});
+
+CPSSkins.CanvasStorage = Class.create();
+CPSSkins.CanvasStorage.prototype = Object.extend(
+  new CPSSkins.StorageAdapter(), {
+
+  getData: function() {
+    return Canvas.getNodeData(this.node);
+  },
+
+  setData: function(data) {
+    return Canvas.setNodeData(this.node, data);
+  }
+
+});
+
 
 // View
 
@@ -1117,7 +1172,7 @@
     this.entered = true;
   }
 
-}); 
+});
 
 
 // Tooltip
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to