Author: jmorliaguet Date: Sat Jan 28 22:17:23 2006 New Revision: 2229 Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/getData1.txt (contents, props changed) cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/data-source1.txt (contents, props changed) Modified: cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_observer_test.html cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_remote_data_sources.html cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html Log:
- saving work: - some refactoring of CPSSkins.Model to better support storage adapters - began working on remote storages 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 22:17:23 2006 @@ -17,14 +17,12 @@ */ var CPSSkins = { - Version: "0.4", + Version: "0.5", Models: $H({}), Controllers: $H({}), - Observers: $H({}), - Perspectives: $H({}), Cache: $H({}), @@ -36,6 +34,17 @@ CPSSkins.parse(document); }, + getModelById: function(id) { + var model = null; + $A(CPSSkins.Models).each(function(m) { + var def = m[1].def; + if (def.id == id) { + model = m[1] + }; + }); + return model; + }, + jsonParse: function(el) { var res = null; var text = el.innerHTML; @@ -72,7 +81,7 @@ } new Ajax.Request(url, options); } else { - CPSSkins.load(el, classid); + CPSSkins.load(el, classid, index); } }); @@ -80,11 +89,20 @@ load: function(el, classid, index) { + // make the element identifiable + var el_id = classid + index; + el.setAttribute("id", el_id); + + var def = CPSSkins.jsonParse(el); + switch(classid) { - case "view": { - var def = CPSSkins.jsonParse(el); + case "model": { + CPSSkins.Models[el_id] = new CPSSkins.Model(el, def); + break; + } + case "view": { var widget_type = def.widget.type; if (!(widget_type in Widgets)) { return; @@ -104,10 +122,6 @@ CPSSkins.Perspectives[p].push(view); }); - var model_id = def.model; - if (model_id) { - CPSSkins.addObserver(new CPSSkins.Model(model_id), view) - } } break; } @@ -115,24 +129,6 @@ } }, - addObserver: function(model, view) { - if (!(model in CPSSkins.Observers)) { - CPSSkins.Observers[model] = $A([]); - } - CPSSkins.Observers[model].push(view); - view.model = model; - - view.update(); - }, - - removeObserver: function(view) { - var model = view.model; - if (model) { - var observers = CPSSkins.Observers[model]; - observers = observers.without(view); - } - }, - _resolvePerspective: function(perspective) { var path = CPSSkins.CurrentPerspective.split("/"); var base = perspective; @@ -296,7 +292,16 @@ if (!window.Canvas) { var Canvas = new Object(); } Object.extend(Canvas, { - _getModelNode: function(node) { + getModel: function(node) { + var model_node = this.getModelNode(node); + if (model_node) { + var id = model_node.getAttribute("id"); + return CPSSkins.Models[id]; + } + return null; + }, + + getModelNode: function(node) { node = $(node); while(node = node.previousSibling) { if (node.nodeType == 1) { @@ -313,64 +318,6 @@ 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) ? - node.getAttribute("id") : null; - - if (node_id && node_id in CPSSkins.Cache) { - return CPSSkins.Cache[node_id].data; - } - - var node_info = this.getModelDef(node); - - if (node_id && node_info) { - CPSSkins.Cache[node_id] = node_info; - } - if (node_info) { return node_info.data; } - }, - - setNodeData: function(node, data) { - node = $(node); - var node_id = Identifiable.isIdentifiable(node) ? - node.getAttribute("id") : null; - - var node_info = {}; - var model_node = this._getModelNode(node); - - if (model_node) { - node_info = CPSSkins.jsonParse(model_node); - } else { - model_node = document.createElement("ins") - node.parentNode.insertBefore(model_node, node); - } - node_info.data = data; - - if (node_id) { - CPSSkins.Cache[node_id] = node_info; - } else { - model_node.innerHTML = JSON.stringify(node_info); - } - - // Update the views and controllers that are registered as observers - // TODO: use notify() instead - var model = CPSSkins.Models[node]; - var observers = CPSSkins.Observers[model]; - if (observers) { - observers.each(function(o) { - o.update(); - }); - } - - }, - createNode: function(options) { var node = document.createElement(options.tag); Element.addClassName(node, options.classes); @@ -552,16 +499,15 @@ CPSSkins.Model = Class.create(); CPSSkins.Model.prototype = { - initialize: function(node) { - this.node = $(node); - CPSSkins.Models[this.node] = this; + initialize: function(node, def) { + this.node = this.node; + this.def = def; + + // observers + this.observers = $A([]); + // 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 */ - } + this._setStorageAdapter(); }, getData: function() { @@ -570,22 +516,42 @@ setData: function(data) { this.storage.setData(data); + + // Update the observers + var observers = this.observers; + if (observers) { + observers.each(function(o) { + o.update(); + }); + } + }, updateData: function(data) { - var current = this.storage.getData() || new Object(); + var current = this.getData() || new Object(); Object.extend(current, data); - this.storage.setData(data); + this.setData(data); + }, + + addObserver: function(view) { + this.observers.push(view); + // create a back-reference + view.model = this; + // initialize the view + view.update(); + }, + + removeObserver: function(view) { + this.observers = this.observers.without(view); }, _setStorageAdapter: function() { - var model_def = Canvas.getModelDef(this.node); - var storage_def = model_def.storage; + var storage_def = this.def.storage; if (!storage_def) { storage_def = {"type": "canvas"}; } - this.storage = Storages[storage_def.type](this.node, model_def); - }, + this.storage = Storages[storage_def.type](this); + } } @@ -593,11 +559,11 @@ CPSSkins.StorageAdapter = Class.create(); CPSSkins.StorageAdapter.prototype = { - initialize: function(node, def) { - this.node = $(node); - this.def = def; + initialize: function(model) { + this.model = model; }, + /* Public API */ getData: function() { /* to be overriden */ }, @@ -610,10 +576,14 @@ if (!window.Storages) { var Storages = new Object(); } Object.extend(Storages, { - canvas: function(node, def) { - return new CPSSkins.CanvasStorage(node, def); + canvas: function(model) { + return new CPSSkins.CanvasStorage(model); }, + remote: function(model) { + return new CPSSkins.RemoteStorage(model); + } + }); CPSSkins.CanvasStorage = Class.create(); @@ -621,11 +591,36 @@ new CPSSkins.StorageAdapter(), { getData: function() { - return Canvas.getNodeData(this.node); + return this.model.def.data; + }, + + setData: function(data) { + this.model.def.data = data; + } + +}); + +CPSSkins.RemoteStorage = Class.create(); +CPSSkins.RemoteStorage.prototype = Object.extend( + new CPSSkins.StorageAdapter(), { + + getData: function() { + var url = this.model.def.accessors.get; + + options = { + onComplete: function(req) { + var data = JSON.parse(req.responseText); + $("message").innerHTML += data; + this.model.data = data; + } + } + new Ajax.Request(url, options); + return null; }, setData: function(data) { - return Canvas.setNodeData(this.node, data); + $("message").innerHTML += def; + /* to implement */ } }); @@ -649,7 +644,8 @@ var model_id = def.model; if (model_id) { - this.model = new CPSSkins.Model(model_id); + this.model = CPSSkins.getModelById(model_id); + this.observe(this.model); } else { this.model = null; } @@ -683,6 +679,17 @@ /* Private API */ + observe: function(model) { + model.addObserver(this); + this.model = model; + }, + + stopObserving: function() { + if (this.model) { + this.model.removeObserver(this); + } + }, + handleAction: function(object, action, args) { var controller = this.controller; if (!controller) return; @@ -709,8 +716,10 @@ this.prepare(); if (!this.def.model) { - var model = new CPSSkins.Model(this.selected); - CPSSkins.addObserver(model, this); + var model_node = Canvas.getModelNode(this.selected); + if (!model_node) return; + var model = CPSSkins.Models[model_node.getAttribute("id")]; + this.observe(model); } if (this.effect) { @@ -744,7 +753,7 @@ } this.visible = false; - CPSSkins.removeObserver(this); + this.stopObserving(); this.teardown(); }, @@ -1202,7 +1211,9 @@ showEvent: function(e) { var selected = Event.element(e); - var data = Canvas.getNodeData(selected); + var model = Canvas.getModel(selected); + if (!model) return; + var data = model.def.data; if (!data) return; if (!data.hint) return; Modified: cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_observer_test.html ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_observer_test.html (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_observer_test.html Sat Jan 28 22:17:23 2006 @@ -16,7 +16,7 @@ margin-top: 1.5em; } - #data-provider { + #dataProvider { border: 1px solid orange; padding: 0.7em; background-color: #fc0; @@ -55,7 +55,7 @@ Event.observe(window, "load", init); function init() { - model = new CPSSkins.Model("data-provider"); + model = CPSSkins.getModelById("data-provider"); } function start() { @@ -127,14 +127,16 @@ <h2>Model</h2> <ins class="model"> - {"data": { + {"id": "data-provider", + "data": { "size": 1, "sizeinfo": [ {"choice": "", "label": ""} ] }} - </ins> - <div id="data-provider">DATA PROVIDER + </ins> + + <div id="dataProvider">DATA PROVIDER <!-- tooltip view --> <ins class="view"> @@ -208,6 +210,8 @@ <button onclick="stop()">STOP</button> </p> + <div id="message"></div> + </body> </html> Modified: cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_remote_data_sources.html ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_remote_data_sources.html (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_remote_data_sources.html Sat Jan 28 22:17:23 2006 @@ -28,7 +28,7 @@ </head> <body> - <h1>CPSSkins: remote data</h1> + <h1>CPSSkins: remote data sources</h1> <p>The object's definition for both the model and the view can specify a remote location from which the data will be fetched. Modified: cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html ============================================================================== --- cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html (original) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html Sat Jan 28 22:17:23 2006 @@ -39,17 +39,37 @@ <ins class="model"> {"data": { - "hint":"Click here to close the document." + "hint": "" }, "storage": { - "type": "canvas" - } + "type": "remote", + "accessors": { + "get": "getData1.txt", + "set": "setData1.txt" + } } } </ins> <a href="#">Open</a> + <ins class="model"> + {"data": { + "hint":"" + }, + "storage": { + "type": "remote", + "accessors": { + "get": "getData1.txt", + "set": "setData1.txt" + } + } + } + } + </ins> + + <a href="#">Close</a> + <ins class="view"> {"widget": { "type": "tooltip" @@ -57,6 +77,7 @@ } </ins> + </div> <div id="message"></div> Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/getData1.txt ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/getData1.txt Sat Jan 28 22:17:23 2006 @@ -0,0 +1 @@ +{ "hint":"Click here to close the document." } Added: cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/data-source1.txt ============================================================================== --- (empty file) +++ cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/data-source1.txt Sat Jan 28 22:17:23 2006 @@ -0,0 +1 @@ +{"hint":"Click here to close the document."} -- http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins