[Dojo-checkins] toonetown - r17709 - in dojox/trunk/grid: . cells tests tests/support

dojo-checkins-admin at dojotoolkit.org dojo-checkins-admin at dojotoolkit.org
Wed Jun 3 17:53:39 EDT 2009


Author: toonetown
Date: Wed Jun  3 14:53:33 2009
New Revision: 17709

Added:
   dojox/trunk/grid/tests/support/countryStore.json
   dojox/trunk/grid/tests/test_grid_formatters.html   (contents, props changed)
Modified:
   dojox/trunk/grid/DataGrid.js
   dojox/trunk/grid/_Grid.js
   dojox/trunk/grid/_View.js
   dojox/trunk/grid/cells/_base.js
   dojox/trunk/grid/cells/tree.js
Log:
Fixes #9373 improve grid formatting functionality and add test case !strict

Modified: dojox/trunk/grid/DataGrid.js
==============================================================================
--- dojox/trunk/grid/DataGrid.js	(original)
+++ dojox/trunk/grid/DataGrid.js	Wed Jun  3 14:53:33 2009
@@ -8,6 +8,8 @@
 	constructor: function(){
 		//	field: String?
 		//		The attribute to read from the dojo.data item for the row.
+		//  fields: String[]?
+		//		An array of fields to grab the values of and pass as an array to the grid
 		//	get: Function?
 		//		function(rowIndex, item?){} rowIndex is of type Integer, item is of type
 		//		Object.  This function will be called when a cell requests data.  Returns
@@ -78,7 +80,17 @@
 	},
 
 	get: function(inRowIndex, inItem){
-		return (!inItem ? this.defaultValue : (!this.field ? this.value : this.grid.store.getValue(inItem, this.field)));
+		if(inItem && this.field == "_item" && !this.fields){
+			return inItem;
+		}else if(inItem && this.fields){
+			var ret = [];
+			var s = this.grid.store;
+			dojo.forEach(this.fields, function(f){
+				ret = ret.concat(s.getValues(inItem, f));
+			});
+			return ret;
+		}
+		return (!inItem ? this.defaultValue : (!this.field ? this.value : (this.field == "_item" ? inItem : this.grid.store.getValue(inItem, this.field))));
 	},
 
 	_onSet: function(item, attribute, oldValue, newValue){
@@ -538,6 +550,10 @@
 			cellDef.field = field;
 		}
 		cellDef.field = cellDef.field||cellDef.name;
+		var fields = dojo.trim(dojo.attr(node, "fields")||"");
+		if(fields){
+			cellDef.fields = fields.split(",");
+		}
 		if(cellFunc){
 			cellFunc(node, cellDef);
 		}

Modified: dojox/trunk/grid/_Grid.js
==============================================================================
--- dojox/trunk/grid/_Grid.js	(original)
+++ dojox/trunk/grid/_Grid.js	Wed Jun  3 14:53:33 2009
@@ -298,6 +298,12 @@
 		// 		formatter functions is not filtered, even with escapeHTMLInData set to true).
 		escapeHTMLInData: true,	
 		
+		// formatterScope: Object
+		//		An object to execute format functions within.  If not set, the
+		//		format functions will execute within the scope of the cell that
+		//		has a format function.
+		formatterScope: null,
+		
 		// private
 		sortInfo: 0,
 		themeable: true,

Modified: dojox/trunk/grid/_View.js
==============================================================================
--- dojox/trunk/grid/_View.js	(original)
+++ dojox/trunk/grid/_View.js	Wed Jun  3 14:53:33 2009
@@ -106,12 +106,48 @@
 			this.updateStructure();
 		},
 		
+		_cleanupRowWidgets: function(inRowNode){
+			// Summary:
+			//		Cleans up the widgets for the given row node so that
+			//		we can reattach them if needed
+			if(inRowNode){
+				dojo.forEach(dojo.query("[widgetId]", inRowNode).map(dijit.byNode), function(w){
+					if(w._destroyOnRemove){
+						w.destroy();
+						delete w;
+					}else if(w.domNode && w.domNode.parentNode){
+						w.domNode.parentNode.removeChild(w.domNode);
+					}
+				});
+			}
+		},
+		
 		onBeforeRow: function(inRowIndex, cells){
 			this._onBeforeRow(inRowIndex, cells);
+			if(inRowIndex >= 0){
+				this._cleanupRowWidgets(this.getRowNode(inRowIndex));
+			}
 		},
 		
 		onAfterRow: function(inRowIndex, cells, inRowNode){
 			this._onAfterRow(inRowIndex, cells, inRowNode);
+			var g = this.grid;
+			dojo.forEach(dojo.query(".dojoxGridStubNode", inRowNode), function(n){
+				if(n && n.parentNode){
+					var lw = n.getAttribute("linkWidget");
+					var cellIdx = window.parseInt(dojo.attr(n, "cellIdx"), 10);
+					var cellDef = g.getCell(cellIdx);
+					var w = dijit.byId(lw);
+					if(w){
+						n.parentNode.replaceChild(w.domNode, n);
+						if(!w._started){
+							w.startup();
+						}
+					}else{
+						n.innerHTML = "";
+					}
+				}
+			}, this);
 		},
 
 		testFlexCells: function(){
@@ -571,6 +607,9 @@
 		},
 
 		rowRemoved:function(inRowIndex){
+			if(inRowIndex >= 0){
+				this._cleanupRowWidgets(this.getRowNode(inRowIndex));
+			}
 			this.grid.edit.save(this, inRowIndex);
 			delete this.rowNodes[inRowIndex];
 		},

Modified: dojox/trunk/grid/cells/_base.js
==============================================================================
--- dojox/trunk/grid/cells/_base.js	(original)
+++ dojox/trunk/grid/cells/_base.js	Wed Jun  3 14:53:33 2009
@@ -1,6 +1,21 @@
 dojo.provide("dojox.grid.cells._base");
 
 dojo.require("dojox.grid.util");
+dojo.require("dijit._Widget");
+
+dojo.declare("dojox.grid._DeferredTextWidget", dijit._Widget, {
+	deferred: null,
+	_destroyOnRemove: true,
+	postCreate: function(){
+		if(this.deferred){
+			this.deferred.addBoth(dojo.hitch(this, function(text){
+				if(this.domNode){
+					this.domNode.innerHTML = text;
+				}
+			}));
+		}
+	}
+});
 
 (function(){
 	var focusSelectNode = function(inNode){
@@ -45,6 +60,33 @@
 			}
 		},
 
+		_defaultFormat: function(inValue, callArgs){
+			var s = this.grid.formatterScope || this;
+			var f = this.formatter;
+			if(f && s && typeof f == "string"){
+				f = this.formatter = s[f];
+			}
+			var v = (inValue != this.defaultValue && f) ? f.apply(s, callArgs) : inValue;
+			if(typeof v == "undefined"){
+				return this.defaultValue;
+			}
+			if(v && v.addBoth){
+				// Check if it's a deferred
+				v = new dojox.grid._DeferredTextWidget({deferred: v},
+									dojo.create("span", {innerHTML: this.defaultValue}));
+			}
+			if(v && v.declaredClass){
+				return "<div class='dojoxGridStubNode' linkWidget='" +
+						v.id +
+						"' cellIdx='" +
+						this.index +
+						"'>" +
+						this.defaultValue +
+						"</div>";
+			}
+			return v;
+		},
+		
 		// data source
 		format: function(inRowIndex, inItem){
 			// summary:
@@ -57,8 +99,7 @@
 			if(this.editable && (this.alwaysEditing || (i.rowIndex==inRowIndex && i.cell==this))){
 				return this.formatEditing(d, inRowIndex);
 			}else{
-				var v = (d != this.defaultValue && (f = this.formatter)) ? f.call(this, d, inRowIndex) : d;
-				return (typeof v == "undefined" ? this.defaultValue : v);
+				return this._defaultFormat(d, [d, inRowIndex]);
 			}
 		},
 		formatEditing: function(inDatum, inRowIndex){
@@ -231,7 +272,7 @@
 		var d = dojo;
 		var formatter = d.trim(d.attr(node, "formatter")||"");
 		if(formatter){
-			cellDef.formatter = dojo.getObject(formatter);
+			cellDef.formatter = dojo.getObject(formatter)||formatter;
 		}
 		var get = d.trim(d.attr(node, "get")||"");
 		if(get){

Modified: dojox/trunk/grid/cells/tree.js
==============================================================================
--- dojox/trunk/grid/cells/tree.js	(original)
+++ dojox/trunk/grid/cells/tree.js	Wed Jun  3 14:53:33 2009
@@ -6,8 +6,7 @@
 	formatAggregate: function(inItem, level, inRowIndexes){
 		var f, g=this.grid, i=g.edit.info, 
 			d=g.aggregator ? g.aggregator.getForCell(this, level, inItem, level === this.level ? "cnt" : this.parentCell.aggregate) : (this.value || this.defaultValue);
-		var v = (d != this.defaultValue && (f = this.formatter)) ? f.call(this, d, level - this.level, inRowIndexes) : d;
-		return (typeof v == "undefined" ? this.defaultValue : v);
+		return this._defaultFormat(d, [d, level - this.level, inRowIndexes]);
 	},
 	formatIndexes: function(inRowIndexes, inItem){
 		var f, g=this.grid, i=g.edit.info, 
@@ -15,8 +14,7 @@
 		if(this.editable && (this.alwaysEditing || (i.rowIndex==inRowIndexes[0] && i.cell==this))){
 			return this.formatEditing(d, inRowIndexes[0], inRowIndexes);
 		}else{
-			var v = (d != this.defaultValue && (f = this.formatter)) ? f.call(this, d, inRowIndexes[0], inRowIndexes) : d;
-			return (typeof v == "undefined" ? this.defaultValue : v);
+			return this._defaultFormat(d, [d, inRowIndexes[0], inRowIndexes]);
 		}
 	},
 	getOpenState: function(itemId){

Added: dojox/trunk/grid/tests/support/countryStore.json
==============================================================================
--- (empty file)
+++ dojox/trunk/grid/tests/support/countryStore.json	Wed Jun  3 14:53:33 2009
@@ -0,0 +1,45 @@
+{ identifier: 'name',
+  label: 'name',
+  items: [
+	{ name:'Africa', type:'continent', children:[
+		{ name:'Egypt', type:'country' }, 
+		{ name:'Kenya', type:'country', children:[
+			{ name:'Nairobi', type:'city', adults: 70400, popnum: 2940911 },
+			{ name:'Mombasa', type:'city', adults: 294091, popnum: 707400 } ]
+		},
+		{ name:'Sudan', type:'country', children:
+			{ name:'Khartoum', type:'city', adults: 480293, popnum: 1200394 } 
+		} ]
+	},
+	{ name:'Asia', type:'continent', children:[
+		{ name:'China', type:'country' },
+		{ name:'India', type:'country' },
+		{ name:'Russia', type:'country' },
+		{ name:'Mongolia', type:'country' } ]
+	},
+	{ name:'Australia', type:'continent', population:'21 million', children:
+		{ name:'Commonwealth of Australia', type:'country', population:'21 million'}
+	},
+	{ name:'Europe', type:'continent', children:[
+		{ name:'Germany', type:'country' },
+		{ name:'France', type:'country' },
+		{ name:'Spain', type:'country' },
+		{ name:'Italy', type:'country' } ]
+	},
+	{ name:'North America', type:'continent', children:[
+		{ name:'Mexico', type:'country',  population:'108 million', area:'1,972,550 sq km', children:[
+			{ name:'Mexico City', type:'city', adults: 120394, popnum: 19394839, population:'19 million', timezone:'-6 UTC'},
+			{ name:'Guadalajara', type:'city', adults: 1934839, popnum: 4830293, population:'4 million', timezone:'-6 UTC' } ]
+		},
+		{ name:'Canada', type:'country',  population:'33 million', area:'9,984,670 sq km', children:[
+			{ name:'Ottawa', type:'city', adults: 230493, popnum: 9382019, population:'0.9 million', timezone:'-5 UTC'},
+			{ name:'Toronto', type:'city', adults: 932019, popnum: 2530493, population:'2.5 million', timezone:'-5 UTC' }]
+		},
+		{ name:'United States of America', type:'country' } ]
+	},
+	{ name:'South America', type:'continent', children:[
+		{ name:'Brazil', type:'country', population:'186 million' },
+		{ name:'Argentina', type:'country', population:'40 million' } ]
+	} ]
+}
+

Added: dojox/trunk/grid/tests/test_grid_formatters.html
==============================================================================
--- (empty file)
+++ dojox/trunk/grid/tests/test_grid_formatters.html	Wed Jun  3 14:53:33 2009
@@ -0,0 +1,142 @@
+<html>
+	<head>
+	<title>Test dojox.grid.DataGrid Basic</title>
+	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
+
+    <style type="text/css">
+        @import "../../../dojo/resources/dojo.css";
+		@import "../../../dijit/themes/tundra/tundra.css";
+		@import "../resources/Grid.css";
+		@import "../resources/tundraGrid.css";
+        @import "../../../dijit/tests/css/dijitTests.css";
+		table { border: none; }
+        
+        body {
+			font-size: 0.9em;
+			font-family: Geneva, Arial, Helvetica, sans-serif;
+		}
+		.heading {
+			font-weight: bold;
+			padding-bottom: 0.25em;
+		}
+				
+		#grid { 
+			border: 1px solid #333;
+			width: 400px;
+			height: 500px;
+		}
+	</style>
+	<script type="text/javascript" 
+		src="../../../dojo/dojo.js"
+		djConfig="isDebug: true, parseOnLoad: true">
+	</script>
+	<script type="text/javascript">
+		dojo.require("doh.runner");
+		
+		dojo.require("dojo.data.ItemFileReadStore");
+		dojo.require("dojox.grid.DataGrid");
+		dojo.require("dijit.form.TextBox");
+
+		dojo.declare("dojox.grid.formatterScopeObj", null, {
+			store: null,
+			_widgets: [],
+			constructor: function(kwArgs){
+				this.store = kwArgs.store;
+				this._widgets = [];
+			},
+			invalidateCache: function(){
+				dojo.forEach(this._widgets, function(w){
+					if(w && w.destroy){
+						w.destroy();
+					}
+				});
+				this._widgets = [];
+			},
+			fmtABBR: function(value, idx){
+				if(value == "continent"){
+					return "BIG";
+				}else if(value == "country"){
+					return "MID";
+				}else if(value == "city"){
+					return "SML";
+				}
+				return "???";
+			},
+			fmtWidget: function(value, idx){
+				if(!this._widgets[idx]){
+					this._widgets[idx] = new dijit.form.TextBox({value: value});
+				}
+				return this._widgets[idx];
+			},
+			fmtValue: function(value, idx){
+				return value;
+			},
+			fmtBool: function(value, idx){
+				return (value == "country");
+			},
+			fmtInt: function(value, idx){
+				return dojo.filter(["continent", "country", "city"], function(i){ return i == value; })[0]||null;
+			},
+			fmtChildren: function(value, idx){
+				return dojo.map(value, function(i){
+					return this.store.getLabel(i);
+				}, this).join(", ") || "&nbsp;";
+			},
+			fmtItem: function(value, idx){
+				if(!this.store.isItem(value)){
+					return "&nbsp;";
+				}
+				var d = new dojo.Deferred();
+				var fx = function(items){
+					if(items.length){
+						d.callback(this.store.getLabel(items[0]))
+					}else{
+						d.callback("&nbsp;");
+					}
+				};
+				window.setTimeout(dojo.hitch(this, function(){
+					this.store.fetch({query: {children: value}, onComplete: fx, onError: function(e){d.errback(e)}, scope: this});
+				}), 5000);
+				return d;
+			}
+		});
+			
+		dojo.addOnLoad(function(){							
+			doh.register("tests",
+				[
+					function test_gridFormatters(t){
+						t.t(true);
+					}
+				]
+			);
+			doh.run();
+		});
+	</script>
+	<style>
+		.dijitTextBox {
+			color: #000;
+			width: 90px;
+		}
+	</style>
+	</head>	
+	<body class="tundra">
+		<h1 class="testTitle">Test: dojox.tests.grid.Grid Formatters</h1>
+		<div dojoType="dojo.data.ItemFileReadStore" jsId="readStore" url="support/countryStore.json?"></div>
+		<div dojoType="dojox.grid.formatterScopeObj" store="readStore" jsId="myObject"></div>
+		<table dojoType="dojox.grid.DataGrid" formatterScope="myObject" style="width: 65em;height: 25em;" store="readStore" queryOptions="{deep:true}" rowsPerPage="100">
+			<thead>
+				<tr>
+					<th field="name" relWidth="2">Name</th>
+					<th field="type" relWidth="2">Type</th>
+					<th field="type" formatter="fmtABBR" relWidth="1">Abbr</th>
+					<th field="type" formatter="fmtValue" relWidth="2">Value</th>
+					<th field="type" formatter="fmtBool" relWidth="1">IsCountry</th>
+					<th field="type" formatter="fmtInt" relWidth="1">Int</th>
+					<th fields="children" formatter="fmtChildren" relWidth="3">Children</th>
+					<th field="_item" formatter="fmtItem" relWidth="2">Parent</th>
+					<th field="name" formatter="fmtWidget" relWidth="3">Widget</th>
+				</tr>
+			</thead>
+		</table>
+	</body>
+</html>
\ No newline at end of file


More information about the Dojo-checkins mailing list