Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.widget.Iterator"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.widget.Iterator"] = true;
3
dojo.provide("dojox.widget.Iterator");
4
dojo.require("dijit.Declaration");
5
 
6
dojo.experimental("dojox.widget.Iterator"); // level: prototype, designed for dijit.chat.demo
7
 
8
/*
9
	example:
10
		from markup:
11
	|	<span dojoType="dojo.data.ItemFileReadStore"
12
	|		jsId="cstore" url="countries.json"></span>
13
	|
14
	|	<div>
15
	|		<div dojoType="dojox.widget.Iterator" store="cstore"
16
	|			query="{ name: 'A*'}">
17
	|			${name} is a ${type}
18
	|		</div>
19
	|	</div>
20
 
21
	example:
22
		programmatic:
23
	|	var store = new dojo.data.ItemFileReadStore({ url: "countries.json" });
24
	|
25
	|	var iter = new dojox.widget.Iterator({
26
	|		store: store,
27
	|		template: ""
28
	|	});
29
	|
30
 
31
	example:
32
		programmatic from an array of objects:
33
	|	var dataArr = [
34
	|		{ name: "foo", valueAttr: "bar" },
35
	|		{ name: "thinger", valueAttr: "blah" }
36
	|	];
37
	|
38
	|	var iter = new dojox.widget.Iterator({
39
	|		data: dataArr,
40
	|		template: ""
41
	|	});
42
 
43
	example:
44
		programmatic from an array of strings:
45
	|	var dataArr = [
46
	|		{ name: "foo", valueAttr: "bar" },
47
	|		{ name: "thinger", valueAttr: "blah" }
48
	|	];
49
	|
50
	|	var iter = new dojox.widget.Iterator({
51
	|		data: dataArr,
52
	|		template: ""
53
	|	});
54
 
55
*/
56
 
57
 
58
dojo.declare("dojox.widget.Iterator",
59
	[ dijit.Declaration ],
60
	{
61
 
62
	constructor: (function(){
63
		var ctr = 0;
64
		return function(){
65
			this.attrs = [];
66
			this.children = [];
67
			this.widgetClass = "dojox.widget.Iterator._classes._"+(ctr++);
68
		}
69
	})(),
70
 
71
	start: 0,
72
	fetchMax: 1000,
73
	query: { name: "*" },
74
	attrs: [],
75
	defaultValue: "",
76
	widgetCtor: null,
77
	dataValues: [], // an array of strings
78
	data: null, // should be a reference to an Array
79
	store: null,
80
	_srcIndex: 0,
81
	_srcParent: null,
82
 
83
	_setSrcIndex: function(s){
84
		this._srcIndex = 0;
85
		this._srcParent = s.parentNode;
86
		var ts = s;
87
		while(ts.previousSibling){
88
			this._srcIndex++;
89
			ts = ts.previousSibling;
90
		};
91
	},
92
 
93
	postscript: function(p, s){
94
		// figure out the position of the source node in it's parent
95
		this._setSrcIndex(s);
96
		// this._srcIndex = dojo.query(">", this._srcParent).indexOf(s);
97
 
98
		this.inherited("postscript", arguments);
99
		var wc = this.widgetCtor = dojo.getObject(this.widgetClass);
100
 
101
		this.attrs = dojo.map(
102
			wc.prototype.templateString.match(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g),
103
			function(s){ return s.slice(2, -1); }
104
		);
105
		dojo.forEach(
106
			this.attrs,
107
			function(m){ wc.prototype[m] = ""; }
108
		);
109
		this.update();
110
	},
111
 
112
	clear: function(){
113
		if(this.children.length){
114
			this._setSrcIndex(this.children[0].domNode);
115
		}
116
		dojo.forEach(this.children, "item.destroy();");
117
		this.children = [];
118
	},
119
 
120
	update: function(){
121
		if(this.store){
122
			// we're executing a query
123
			this.fetch();
124
		}else{
125
			// we came from an array of objects. Easier!
126
			this.onDataAvailable(this.data||this.dataValues);
127
		}
128
	},
129
 
130
	_addItem: function(/*Object*/config, idx){
131
		if(dojo.isString(config)){
132
			config = { value: config };
133
		}
134
		var widget = new this.widgetCtor(config);
135
		this.children.push(widget);
136
		dojo.place(widget.domNode, this._srcParent, this._srcIndex+idx);
137
	},
138
 
139
	getAttrValuesObj: function(item){
140
		var obj = {};
141
		if(dojo.isString(item)){
142
			dojo.forEach(this.attrs, function(attr){
143
				obj[attr] = (attr == "value") ? item : this.defaultValue;
144
			}, this);
145
		}else{
146
			dojo.forEach(this.attrs, function(attr){
147
				if(this.store){
148
					obj[attr] = this.store.getValue(item, attr)||this.defaultValue;
149
				}else{
150
					obj[attr] = item[attr]||this.defaultValue;
151
				}
152
			}, this);
153
		}
154
		return obj;
155
	},
156
 
157
	onDataAvailable: function(data){
158
		this.clear();
159
		// console.debug(data);
160
		dojo.forEach(data, function(item, idx){
161
			this._addItem(this.getAttrValuesObj(item), idx);
162
		}, this);
163
	},
164
 
165
	fetch: function(query, start, end){
166
		this.store.fetch({
167
			query: query||this.query,
168
			start: start||this.start,
169
			count: end||this.fetchMax,
170
			onComplete: dojo.hitch(this,"onDataAvailable"),
171
		});
172
	}
173
});
174
 
175
dojox.widget.Iterator._classes = {};
176
 
177
}