summaryrefslogtreecommitdiff
path: root/includes/js/dojox/grid/tests/databaseModel.js
blob: 3c879ebde57a58b32347a2f1db894452a3aaf39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
if(!dojo._hasResource["dojox.grid.tests.databaseModel"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.grid.tests.databaseModel"] = true;
dojo.provide("dojox.grid.tests.databaseModel");
dojo.require("dojox.grid._data.model");

// Provides a sparse array that is also traversable inorder 
// with basic Array:
//   - iterating by index is slow for large sparse arrays
//   - for...in iteration is in order of element creation 
// maintains a secondary index for interating
// over sparse elements inorder
dojo.declare("dojox.grid.Sparse", null, {
	constructor: function() {
		this.clear();
	},
	clear: function() {
		this.indices = [];
		this.values = [];
	},
	length: function() {
		return this.indices.length;
	},
	set: function(inIndex, inValue) {
		for (var i=0,l=this.indices.length; i<l; i++) {
			if (this.indices[i] >= inIndex) 
				break;
		}
		if (this.indices[i] != inIndex) 
			this.indices.splice(i, 0, inIndex);
		this.values[inIndex] = inValue;
	},
	get: function(inIndex) {
		return this.values[inIndex];
	},
	remove: function(inIndex) {
		for (var i=0,l=this.indices.length; i<l; i++) 
			if (this.indices[i] == inIndex) {
				this.indices.splice(i, 1);
				break;
			}
		delete this.values[inIndex];
	},
	inorder: function(inFor) {
		for (var i=0,l=this.indices.length, ix; i<l; i++) {
			ix = this.indices[i];
			if (inFor(this.values[ix], ix) === false)
				break;
		}
	}
});

// sample custom model implementation that works with mysql server.
dojo.declare("dojox.grid.data.DbTable", dojox.grid.data.Dynamic, {
	delayedInsertCommit: true,
	constructor: function(inFields, inData, inServer, inDatabase, inTable) {
		this.server = inServer;
		this.database = inDatabase;
		this.table = inTable;
		this.stateNames = ['inflight', 'inserting', 'removing', 'error'];
		this.clearStates();
		this.clearSort();
	},
	clearData: function() {
		this.cache = [ ];
		this.clearStates();
		this.inherited(arguments);
	},
	clearStates: function() {
		this.states = {};
		for (var i=0, s; (s=this.stateNames[i]); i++) {
			delete this.states[s];
			this.states[s] = new dojox.grid.Sparse();
		}
	},
	// row state information
	getState: function(inRowIndex) {
		for (var i=0, r={}, s; (s=this.stateNames[i]); i++)
			r[s] = this.states[s].get(inRowIndex);
		return r;
	},
	setState: function(inRowIndex, inState, inValue) {
		this.states[inState].set(inRowIndex, inValue||true);
	},
	clearState: function(inRowIndex, inState) {
		if (arguments.length == 1) {
			for (var i=0, s; (s=this.stateNames[i]); i++)
				this.states[s].remove(inRowIndex);
		}	else {
			for (var i=1, l=arguments.length, arg; (i<l) &&((arg=arguments[i])!=undefined); i++)
				this.states[arg].remove(inRowIndex);
		}
	},
	setStateForIndexes: function(inRowIndexes, inState, inValue) {
		for (var i=inRowIndexes.length-1, k; (i>=0) && ((k=inRowIndexes[i])!=undefined); i--)
			this.setState(k, inState, inValue);
	},
	clearStateForIndexes: function(inRowIndexes, inState) {
		for (var i=inRowIndexes.length-1, k; (i>=0) && ((k=inRowIndexes[i])!=undefined); i--)
			this.clearState(k, inState);
	},
	//$ Return boolean stating whether or not an operation is in progress that may change row indexing.
	isAddRemoving: function() {
		return Boolean(this.states['inserting'].length() || this.states['removing'].length());
	},
	isInflight: function() {
		return Boolean(this.states['inflight'].length());
	},
	//$ Return boolean stating if the model is currently undergoing any type of edit.
	isEditing: function() {
		for (var i=0, r={}, s; (s=this.stateNames[i]); i++)
			if (this.states[s].length())
				return true;
	},
	//$ Return true if ok to modify the given row. Override as needed, using model editing state information.
	canModify: function(inRowIndex) {
		return !this.getState(inRowIndex).inflight && !(this.isInflight() && this.isAddRemoving());
	},
	// server send / receive
	getSendParams: function(inParams) {
		var p = {
			database: this.database || '',
			table: this.table || ''
		}
		return dojo.mixin(p, inParams || {});
	},
	send: function(inAsync, inParams, inCallbacks) {
		//console.log('send', inParams.command);
		var p = this.getSendParams(inParams);
		var d = dojo.xhrPost({
			url: this.server,
			content: p,
			handleAs: 'json-comment-filtered',
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			sync: !inAsync
		});
		d.addCallbacks(dojo.hitch(this, "receive", inCallbacks), dojo.hitch(this, "receiveError", inCallbacks));
		return d;
	},
	_callback: function(cb, eb, data) {
		try{ cb && cb(data); } 
		catch(e){ eb && eb(data, e); }
	},
	receive: function(inCallbacks, inData) {
		inCallbacks && this._callback(inCallbacks.callback, inCallbacks.errback, inData);
	},
	receiveError: function(inCallbacks, inErr) {
		this._callback(inCallbacks.errback, null, inErr)
	},
	encodeRow: function(inParams, inRow, inPrefix) {
		for (var i=0, l=inRow.length; i < l; i++)
			inParams['_' + (inPrefix ? inPrefix : '') + i] = (inRow[i] ? inRow[i] : '');
	},
	measure: function() {
		this.send(true, { command: 'info' }, { callback: dojo.hitch(this, this.callbacks.info) });
	},
	fetchRowCount: function(inCallbacks) {
		this.send(true, { command: 'count' }, inCallbacks);
	},
	// server commits
	commitEdit: function(inOldData, inNewData, inRowIndex, inCallbacks) {
		this.setState(inRowIndex, "inflight", true);
		var params = {command: 'update'};
		this.encodeRow(params, inOldData, 'o');
		this.encodeRow(params, inNewData);
		this.send(true, params, inCallbacks);
	},
	commitInsert: function(inRowIndex, inNewData, inCallbacks) {
		this.setState(inRowIndex, "inflight", true);
		var params = {command: 'insert'};
		this.encodeRow(params, inNewData);
		this.send(true, params, inCallbacks);
	},
	// NOTE: supported only in tables with pk
	commitDelete: function(inRows, inCallbacks) {
		var params = { 
			command: 'delete',
			count: inRows.length
		}	
		var pk = this.getPkIndex();
		if (pk < 0)
			return;
		for (var i=0; i < inRows.length; i++)	{
			params['_' + i] = inRows[i][pk];
		}	
		this.send(true, params, inCallbacks);
	},
	getUpdateCallbacks: function(inRowIndex) {
		return {
			callback: dojo.hitch(this, this.callbacks.update, inRowIndex), 
			errback: dojo.hitch(this, this.callbacks.updateError, inRowIndex)
		};
	},
	// primary key from fields
	getPkIndex: function() {
		for (var i=0, l=this.fields.count(), f; (i<l) && (f=this.fields.get(i)); i++)
			if (f.Key = 'PRI')
				return i;
		return -1;		
	},
	// model implementations
	update: function(inOldData, inNewData, inRowIndex) {
		var cbs = this.getUpdateCallbacks(inRowIndex);
		if (this.getState(inRowIndex).inserting)
			this.commitInsert(inRowIndex, inNewData, cbs);
		else
			this.commitEdit(this.cache[inRowIndex] || inOldData, inNewData, inRowIndex, cbs);
		// set push data immediately to model	so reflectd while committing
		this.setRow(inNewData, inRowIndex);
	},
	insert: function(inData, inRowIndex) {
		this.setState(inRowIndex, 'inserting', true);
		if (!this.delayedInsertCommit)
			this.commitInsert(inRowIndex, inData, this.getUpdateCallbacks(inRowIndex));
		return this.inherited(arguments);
	},
	remove: function(inRowIndexes) {
		var rows = [];
		for (var i=0, r=0, indexes=[]; (r=inRowIndexes[i]) !== undefined; i++)
			if (!this.getState(r).inserting) {
				rows.push(this.getRow(r));
				indexes.push(r);
				this.setState(r, 'removing');
			}
		var cbs = {
			callback: dojo.hitch(this, this.callbacks.remove, indexes),
			errback: dojo.hitch(this, this.callbacks.removeError, indexes)
		};
		this.commitDelete(rows, cbs);
		dojox.grid.data.Dynamic.prototype.remove.apply(this, arguments);
	},
	cancelModifyRow: function(inRowIndex) {
		if (this.isDelayedInsert(inRowIndex)) {
			this.removeInsert(inRowIndex);
		} else
			this.finishUpdate(inRowIndex);
	},	
	finishUpdate: function(inRowIndex, inData) {
		this.clearState(inRowIndex);
		var d = (inData&&inData[0]) || this.cache[inRowIndex];
		if (d)
			this.setRow(d, inRowIndex);
		delete this.cache[inRowIndex];
	},
	isDelayedInsert: function(inRowIndex) {
		return (this.delayedInsertCommit && this.getState(inRowIndex).inserting);
	},
	removeInsert: function(inRowIndex) {
		this.clearState(inRowIndex);
		dojox.grid.data.Dynamic.prototype.remove.call(this, [inRowIndex]);
	},
	// request data 
	requestRows: function(inRowIndex, inCount)	{
		var params = { 
			command: 'select',
			orderby: this.sortField, 
			desc: (this.sortDesc ? "true" : ''),
			offset: inRowIndex, 
			limit: inCount
		}
		this.send(true, params, {callback: dojo.hitch(this, this.callbacks.rows, inRowIndex)});
	},
	// sorting
	canSort: function () { 
		return true; 
	},
	setSort: function(inSortIndex) {
		this.sortField = this.fields.get(Math.abs(inSortIndex) - 1).name || inSortIndex;
		this.sortDesc = (inSortIndex < 0);
	},
	sort: function(inSortIndex) {
		this.setSort(inSortIndex);
		this.clearData();
	},
	clearSort: function(){
		this.sortField = '';
		this.sortDesc = false;
	},
	endModifyRow: function(inRowIndex){
		var cache = this.cache[inRowIndex];
		var m = false;
		if(cache){
			var data = this.getRow(inRowIndex);
			if(!dojox.grid.arrayCompare(cache, data)){
				m = true;
				this.update(cache, data, inRowIndex);
			}	
		}
		if (!m)
			this.cancelModifyRow(inRowIndex);
	},
	// server callbacks (called with this == model)
	callbacks: {
		update: function(inRowIndex, inData) {
			console.log('received update', arguments);
			if (inData.error)
				this.updateError(inData)
			else
				this.finishUpdate(inRowIndex, inData);
		},
		updateError: function(inRowIndex) {
			this.clearState(inRowIndex, 'inflight');
			this.setState(inRowIndex, "error", "update failed: " + inRowIndex);
			this.rowChange(this.getRow(inRowIndex), inRowIndex);
		},
		remove: function(inRowIndexes) {
			this.clearStateForIndexes(inRowIndexes);
		},
		removeError: function(inRowIndexes) {
			this.clearStateForIndexes(inRowIndexes);
			alert('Removal error. Please refresh.');
		},
		rows: function(inRowIndex, inData) {
			//this.beginUpdate();
			for (var i=0, l=inData.length; i<l; i++)
				this.setRow(inData[i], inRowIndex + i);
			//this.endUpdate();
			//this.allChange();
		},
		count: function(inRowCount) {
			this.count = Number(inRowCount);
			this.clearData();
		},
		info: function(inInfo) {
			this.fields.clear();
			for (var i=0, c; (c=inInfo.columns[i]); i++) {
				c.name = c.Field;
				this.fields.set(i, c);
			}
			this.table = inInfo.table;
			this.database = inInfo.database;
			this.notify("MetaData", arguments);
			this.callbacks.count.call(this, inInfo.count);
		}
	}
});

}