This repository was archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_stores.js
More file actions
577 lines (504 loc) · 16.5 KB
/
data_stores.js
File metadata and controls
577 lines (504 loc) · 16.5 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*globals Ext, SWorks, console */
/*jslint glovar: true, undef: true, nomen: true */
Ext.namespace('Ext.ux.data', 'SWorks');
Ext.override(Ext.data.Record, {
getKlass: function() {
return this.data.klass || (this.store ? this.store.klass : undefined);
},
setAttributes: function(data) {
this.json = this.json || {};
Ext.apply(this.json, data);
this.beginEdit();
for(var a in data) if (typeof data[a] !== 'function') {
var value = data[a];
if(typeof value == 'object') {
//this.set only takes non-objects
this.data[a] = value;
} else {
this.set(a, value);
}
}
this.endEdit();
}
});
Ext.override(Ext.data.Store, {
// Their load records function isn't very extensible,
// so I had to copy it in here
loadRecords : function(o, options, success){
if(!o || success === false){
if(success !== false){
this.fireEvent("load", this, [], options);
}
if(options.callback){
options.callback.call(options.scope || this, [], options, false);
}
return;
}
var r = o.records, t = o.totalRecords || r.length;
if(!options || options.add !== true){
if(this.pruneModifiedRecords){
this.modified = [];
}
for(var i = 0, len = r.length; i < len; i++){
r[i].join(this);
}
if(this.snapshot){
this.data = this.snapshot;
delete this.snapshot;
}
this.data.clear();
this.data.addAll(r);
this.totalLength = t;
this.onDataChanged(); //This line added
this.fireEvent("datachanged", this);
}else{
this.totalLength = Math.max(t, this.data.length+r.length);
this.add(r);
}
this.fireEvent("load", this, r, options);
if(options.callback){
options.callback.call(options.scope || this, r, options, true);
}
},
onDataChanged: function() {
this.applySort();
},
isLoaded: function() {
// check that a load has been requested and that no
// load is current in progress, otherwise, it might just
// have be requested and not come back yet.
// data.length > 0 isn't a valid check because there may
// actually not be any data for the store (like looking for
// people with the last name 'zzzzz')
return (this.lastOptions !== null && !this.proxy.activeRequest);
},
whenLoaded: function(fn, scope) {
if (this.isLoaded()) {
fn.call(scope);
} else {
this.on('load', fn, scope, {single:true});
if(!this.proxy.activeRequest) {
this.load();
}
}
}
});
Ext.override(Ext.data.SimpleStore, {
isLoaded: function() {
return true;
}
});
// Add cloning and mirroring ability
Ext.override(Ext.data.Store, {
clone: function clone(keepFilters, legacy) {
if(typeof legacy != 'undefined') {
console.error('Legacy usage of Ext.data.Store.clone(), please fix.');
return;
}
// Never mess with a dirty data store
if (this.modified.length > 0) {
console.error('Tried to clone a dirty data store.');
return;
}
var newObj = {};
Ext.apply(newObj, this);
delete newObj.clones;
newObj.resetEvents();
newObj.mirror(this);
if(!keepFilters) {
newObj.filterChain = [];
}
return newObj;
},
mirror: function(source) {
// Mirrored data stores only work for a limited subset of the total
// uses of datastores. Consider them carefully before using them.
if(source.mirrorSource) {
this.mirrorSource = source.mirrorSource;
} else {
this.mirrorSource = source;
}
source.clones = source.clones || [];
source.clones.push(this);
var update = function() {
this.snapshot = source.snapshot || source.data;
this.data = this.snapshot;
this.onDataChanged();
this.fireEvent("datachanged", this);
};
//The index from add/remove won't match with ours and
//we may not want their records anyways, so we have to
//call applyFilters and we have to fire datachanged
//If you try and call this.add(record) with source.on('add'
//you'll get a loop because of the delegates below. The
//delegates are essential to make sure updated data gets
//updated in all the mirrored trees
source.on('add', update, this);
source.on('remove', update, this);
source.on('datachanged', update, this);
source.on('update', function(store, record, type) {
// If it's attributes have changed, it's filter conditions
// might have changed
this.onDataChanged();
// Anytime you call onDataChanged, you MUST fire datachanged
this.fireEvent("datachanged", this);
// Our listeners only care if this record is in our dataset
// right now. Unlike other events, people will be listening
// for changes to this specific record, this will cause a
// minor redundant UI update, but it's not bad and we can't
// avoid it
if(this.indexOf(record) != -1) {
this.fireEvent('update', this, record, type);
}
}, this);
source.on('metachange', function(grid, meta) {
this.onMetaChange(meta, grid.recordType, null);
}, this);
this.relayEvents(source, ['load']);
// By creating a delegate based on the source's method,
// if the source is also a clone, the delegate call
// will have no effect on the scope in which the real
// code executes because a delegate method ignores any
// scope applied to it. In this way, only base stores
// will have their real methods called
this.addSorted = source.addSorted.createDelegate(source);
this.add = source.add.createDelegate(source);
this.remove = source.remove.createDelegate(source);
this.load = source.load.createDelegate(source);
this.reload = source.reload.createDelegate(source);
this.isLoaded = source.isLoaded.createDelegate(source);
this.onMirror(source);
},
onMirror: function(source) {
// For overriding via config options or inheritance
}
});
// Better mask handling
Ext.override(Ext.grid.GridPanel, {
afterRender_without_maskHandling: Ext.grid.GridPanel.prototype.afterRender,
afterRender: function() {
this.afterRender_without_maskHandling();
if( this.store.proxy &&
this.store.proxy.activeRequest &&
!this.store.totalLength &&
this.loadMask) {
this.loadMask.show();
} else if(this.loadMask &&
this.loadMask.removeMask &&
this.store.totalLength){
// If the store is loaded already,
// remove the mask
this.loadMask.destroy();
}
}
});
Ext.ux.data.ReloadingStore = function(store) {
store.mirror_without_reloading = store.mirror;
Ext.apply(store, Ext.ux.data.ReloadingStore.overrides);
store.on('beforeload', store.checkReloadingHooks, store);
};
Ext.ux.data.ReloadingStore.scheduleReload = function(store) {
// This causes the periodic reloads to not hammer the CPU so hard with UI refreshes
// It only reload a store every 5 seconds, although each store only schedules itself
// for reload every 5min. Previously all the stores would reload at the sametime and
// it would peg the cpu for upto 30sec.
if(!this.reloadTask) {
var reloadPeriod = 5000;
this.reloadQueue = [];
this.reloadTask = new Ext.util.DelayedTask(function() {
this.reloadTask.delay(reloadPeriod);
var store = this.reloadQueue[0];
if(!this.disabled && store) {
this.reloadQueue = this.reloadQueue.slice(1);
store.reload();
}
}, this);
this.reloadTask.delay(reloadPeriod);
}
if(this.reloadQueue.indexOf(store) == -1) {
this.reloadQueue.push(store);
}
};
Ext.ux.data.ReloadingStore.overrides = {
refreshPeriod: 360000,
loadIfNeeded: function() {
if(!(this.proxy &&
this.proxy.conn &&
this.proxy.conn.url )) {
return;
}
var data = this.snapshot || this.filteredCache || this.data;
// If there isn't a request in progress, and
// we arn't automatically refreshing the data, or
// the data hasn't been loaded yet
if( !this.proxy.activeRequest &&
(!(this.refreshTask || (this.mirrorSource && this.mirrorSource.refreshTask)) || !this.isLoaded())) {
this.load();
}
},
checkReloadingHooks: function() {
if(!this.refreshTask && !(this.mirrorSource && this.mirrorSource.refreshTask)) {
this.createRefreshTask(this.refreshPeriod);
}
},
mirror: function(source) {
this.mirror_without_reloading(source);
this.createRefreshTask = source.createRefreshTask.createDelegate(source);
this.cancelRefreshTask = source.cancelRefreshTask.createDelegate(source);
},
cancelRefreshTask: function() {
if(this.refreshTask) {
this.refreshTask.cancel();
}
},
createRefreshTask: function(refreshRate) {
if(!this.proxy || refreshRate <= 0) {
return;
}
if(this.refreshTask) {
this.refreshTask.cancel();
}
//TODO maybe there is a way to not reload the table
//if there is nothing new
this.refreshTask = new Ext.util.DelayedTask();
this.on('beforeload',function() {
// Someone might remove it so check first
if(this.refreshTask) {
this.refreshTask.delay(refreshRate);
}
}, this);
this.refreshTask.setRefreshRate = function(newRate) {
refreshRate = newRate;
this.delay(refreshRate);
};
this.refreshTask.delay(refreshRate, function(){
Ext.ux.data.ReloadingStore.scheduleReload(this);
}, this);
return this.refreshTask;
}
};
Ext.ux.data.PersistentFilters = function(store) {
Ext.apply(store, Ext.ux.data.PersistentFilters.overrides);
};
Ext.ux.data.PersistentFilters.overrides = {
onDataChanged: function() {
// Anytime you call onDataChanged, you MUST fire datachanged, otherwise
// your data structures tracking the dom elements will be out of sync with
// your store you'll start to have visual update artifacts
this.applyFilters(false);
this.applySort();
},
clearFilter: function(suppressEvent) {
// Just removes the effects of filterBy
this.filteredCache = this.filteredCache || this.snapshot || this.data;
if(this.filteredCache && this.filteredCache != this.data){
this.data = this.filteredCache;
if(suppressEvent !== true){
this.fireEvent("datachanged", this);
}
}
},
purgeFilters: function() {
this.filterChain = [];
this.applyFilters();
},
filterBy: function(fn, scope) {
this.applyFilters();
this.data = this.data.filterBy(fn, scope||this);
this.fireEvent("datachanged", this);
},
// I'm not sure we want these results filtered too,
// but if so, here is how to do it.
/* queryBy: function(fn, scope) {
var data = this.filterCache || this.snapshot || this.data;
return data.filterBy(fn, scope||this);
}, */
addRegExFilter: function(property, value, anyMatch, caseSensitive) {
var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
if (fn) {
this.addFilter(fn);
}
},
addFilter: function(fn, scope, suppress_filtering) {
this.filterChain = this.filterChain || [];
if (typeof fn != 'function') {
throw "Unable to add filter: function undefined";
}
if(this.findFilter(fn, scope) == -1) {
this.filterChain.push({
fn: fn,
scope: (scope || this)
});
}
if(!suppress_filtering) {
this.applyFilters();
}
return this;
},
removeFilter: function(fn, scope, suppress_filtering) {
var index;
if((index = this.findFilter(fn, scope)) != -1) {
this.filterChain.splice(index, 1);
if(!suppress_filtering) {
this.applyFilters();
}
}
},
findFilter: function(fn, scope) {
//Copied straight from Ext.Event
scope = scope || this;
var fc = this.filterChain || [];
for(var i = 0, len = fc.length; i < len; i++){
var l = fc[i];
if(l.fn == fn && l.scope == scope){
return i;
}
}
return -1;
},
applyFilters: function(fire_event) {
this.snapshot = this.snapshot || this.data;
this.data = this.snapshot;
var fc = this.filterChain || [];
var len = fc.length;
if(len !== 0) {
this.data = this.data.filterBy(function(record, id) {
for(var i=0;i<len;i++) {
var set = fc[i];
if(!set.fn.call((set.scope||this), record, id)) {
return false;
}
}
return true;
});
}
this.filteredCache = this.data;
if(fire_event !== false) {
this.fireEvent("datachanged", this);
}
}
};
SWorks.CustomGroupingStore = function(config, custom) {
// Because we need to hack the constructor a little
if (typeof config == 'string') {
var clone = Ext.StoreMgr.get(config);
custom = custom || {};
config = Ext.applyIf(custom, clone.initialConfig);
}
this.initialConfig = config;
// From JsonStore
SWorks.CustomGroupingStore.superclass.constructor.call(this, Ext.apply({
proxy: !config.data ? new Ext.data.HttpProxy({
method: "GET",
url: config.url
}) : undefined,
reader: new Ext.data.JsonReader(config, config.fields)
}, config));
};
Ext.extend(SWorks.CustomGroupingStore, Ext.data.GroupingStore);
SWorks.CrudStore = function(config, custom) {
SWorks.CrudStore.superclass.constructor.apply(this, arguments);
Ext.ux.data.ReloadingStore(this);
Ext.ux.data.PersistentFilters(this);
};
Ext.extend(SWorks.CrudStore, SWorks.CustomGroupingStore, {
remoteSort: false,
linkToParent: function(p, idCol) {
this.checkParentColumns(idCol);
p.on('load', function(form, record) {
if(!p.form || p.form == form) {
//This deals with polymorphic relations
this.relation_id = p.form.record.id;
// New records don't have a store, so get it off the parent
this.relation_type = record.getKlass() || p.store.klass;
this.addFilter(this.parentFilter, this);
}
}, this);
p.on('save', function() {
if(p.form.record) {
this.relation_id = p.form.record.id;
}
}, this);
},
filterOnRelation: function(record) {
//This deals with polymorphic relations
this.relation_id = record.data.id;
this.relation_type = record.getKlass();
this.addFilter(this.parentFilter, this);
},
findRelations: function(record) {
//Searches everything. Quite the hack.
return this.snapshot.filterBy(this.parentFilter, {
relation_id: record.data.id,
relation_type: record.getKlass(),
parentIdColumn: this.parentIdColumn,
parentTypeColumn: this.parentTypeColumn
});
},
checkParentColumns: function(idCol) {
if(idCol) {
this.parentIdColumn = idCol;
var column = idCol.replace(/id/, "type");
var value = (this.recordType.prototype.fields.keys.indexOf(column) != -1);
if(value) {
this.parentTypeColumn = idCol.replace(/id/, "type");
} else {
this.parentTypeColumn = null;
}
}
},
parentFilter: function(record){
var idMatch = (record.data[this.parentIdColumn] == this.relation_id);
var typeMatch = true;
//Provides automatic filtering on polymophic relations
if(this.parentTypeColumn) {
typeMatch = (record.data[this.parentTypeColumn] == this.relation_type);
}
return (idMatch && typeMatch);
}
});
SWorks.SearchStore = function(config, custom) {
SWorks.SearchStore.superclass.constructor.apply(this, arguments);
this.querySet = {};
};
Ext.extend(SWorks.SearchStore, SWorks.CustomGroupingStore, {
queryParam: 'q',
allQuery: 'id:*',
mode: 'remote',
remoteSort: true,
groupOnSort: true,
groupField: false,
addFilter: function(name, query) {
if (!query) {
query = name;
name = Ext.id();
}
this.querySet[name] = query;
// This is to make it easy to use inline
// with your markup by chaining.
return this;
},
removeFilter: function(name) {
delete this.querySet[name];
},
load: function(options) {
options = options || {};
options.params = options.params || {};
var query = options.params[this.queryParam];
if(typeof query == 'undefined') {
//reload on anything that uses lastOptions needs to bypass this, if you have
//a one off query you'll bypass it also
var qs = this.querySet, queryList = (typeof query == 'undefined') ? [] : [ query ];
for (var i in qs) if (typeof qs[i] == 'string') {
queryList.push(qs[i]);
}
if (queryList.length > 0) {
query = queryList.join(' AND ');
} else {
query = 'id:*';
}
options.params[this.queryParam] = query;
}
return SWorks.SearchStore.superclass.load.call(this, options);
}
});