-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
_GridMixin.js
644 lines (549 loc) · 21.6 KB
/
_GridMixin.js
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
define([
'dojo/_base/declare',
'dojo/_base/lang',
'dojo/topic',
'dojo/sniff',
'dojo/_base/array',
'dojo/date/locale',
'dojo/number',
'dstore/Memory',
'dgrid1/Grid', // http://dojofoundation.org/packages/dgrid/
'dgrid1/Selector',
'dgrid1/Keyboard',
'dgrid1/Editor',
'dgrid1/extensions/ColumnHider',
'dgrid1/extensions/ColumnReorder',
'dgrid1/extensions/ColumnResizer',
'dgrid1/extensions/Pagination',
'xstyle/css!dgrid1/css/dgrid.css'
], function (
declare,
lang,
topic,
has,
array,
locale,
number,
Memory,
Grid,
Selector,
Keyboard,
Editor,
ColumnHider,
ColumnReorder,
ColumnResizer,
Pagination
) {
return declare(null, {
gridOptions: {},
defaultGridOptions: {
minWidth: 70,
// no columns, use fields from Query's returned features
columns: [],
// no sort
sort: [],
// populate the grid with field Coded Domain instead of raw value code
useCodedDomainValues: true,
// Allow the user to enable the editor in the grid
editor: true,
// Allow the user to use column sets in grid
columnSet: false,
// Allow the user to hide columns in grid
columnHide: true,
// Allow the user to reorder columns in grid
columnReorder: true,
// Allow the user to resize columns in grid
columnResize: true,
// Use pagination on the results grid
pagination: true,
paginationOptions: {
rowsPerPage: 100,
previousNextArrows: true,
firstLastArrows: true,
pagingLinks: 2,
pagingTextBox: true,
pageSizeOptions: [10, 25, 50, 100, 250, 500, 1000],
showLoadingMessage: true
},
// optional user-defined column formatters
formatters: {},
// optional user-defined column getters
getters: {}
},
// column formatters
formatters: {},
defaultFormatters: {
date: function (value) {
var date = new Date(value);
return locale.format(date, {
selector: 'date',
formatLength: 'medium'
});
},
dateTime: function (value) {
var date = new Date(value);
return locale.format(date, {
formatLength: 'short'
});
},
number: function (value) {
return number.format(value);
},
double: function (value) {
return number.format(value, {
places: 3
});
},
/**
* converts a string to a nice sentence case format
* @url http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript
* @param {string} str The string to convert
* @return {string} The converted string
*/
makeSentenceCase: function (str) {
if (!str.length) {
return '';
}
str = str.toLowerCase().replace(/_/g, ' ').split(' ');
for (var i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + (str[i].substr(1).length ? str[i].substr(1) : '');
}
return (str.length ? str.join(' ') : str);
}
},
// column getters
getters: {},
defaultGetters: {
layerName: function () {
return this.getLayerName();
},
dateTime: function (value) {
if (isNaN(value) || value === 0 || value === null) {
return null;
}
return new Date(value);
}
},
getGridConfiguration: function (options) {
this.gridOptions = this.mixinDeep(lang.clone(this.defaultGridOptions), options);
this.formatters = lang.mixin(lang.clone(this.defaultFormatters), this.gridOptions.formatters);
this.getters = lang.mixin(lang.clone(this.defaultGetters), this.gridOptions.getters);
},
createGrid: function () {
if (!this.grid) {
var gridOptions = {
cellNavigation: false,
showHeader: true,
showFooter: true,
addUiClasses: false,
collection: new Memory(),
columns: [],
sort: []
};
var options = this.gridOptions || {};
// grid and mixins
var req = [Grid, Keyboard];
if (this.featureOptions.selected !== false) {
req.push(Selector);
gridOptions.selectionMode = has('touch') ? 'toggle' : 'extended';
gridOptions.allowFeatureSelectionAll = true;
}
// hack to show all records when there is no pagination
if (options.pagination !== true) {
options.paginationOptions.rowsPerPage = 999999;
}
req.push(Pagination);
lang.mixin(gridOptions, options.paginationOptions);
if (options.editor !== false) {
req.push(Editor);
}
// grid extensions
if (options.columnHide !== false) {
req.push(ColumnHider);
}
if (options.columnReorder !== false) {
req.push(ColumnReorder);
}
if (options.columnResize !== false) {
req.push(ColumnResizer);
}
var AttributeGrid = declare(req);
this.grid = new AttributeGrid(gridOptions, this.attributesTableGridDijit.domNode);
this.grid.startup();
// don't show the footer when there is no pagination
if (options.pagination !== true) {
this.grid.set('showFooter', false);
}
if (this.featureOptions.selected) {
this.grid.on('dgrid-select', lang.hitch(this, 'selectFeaturesFromGrid'));
this.grid.on('dgrid-deselect', lang.hitch(this, 'selectFeaturesFromGrid'));
}
}
},
populateGrid: function (options) {
var features = null,
results = options;
if (options.results) {
results = options.results;
} else {
options = null; // no options when it is also the results
}
if (!this.results) {
this.results = results;
features = this.getFeaturesFromResults();
} else {
features = this.getFeatures();
}
if (!this.idProperty) {
this.getIdProperty(results);
}
/* apparently not used
var delim = '', linkField = this.linkField;
var filteredFields = array.filter(results.fields, function (field) {
return (field.name === linkField);
});
if (filteredFields.length > 0) {
if (filteredFields[0].type === 'esriFieldTypeString') {
delim = '\'';
}
}
*/
var rows = [];
array.forEach(features, lang.hitch(this, function (feature) {
// relationship query
if (feature.relatedRecords) {
rows = rows.concat(this.getRelatedRecords(feature));
// spatial or table query
} else if (feature.attributes) {
rows = rows.concat(this.getRecordFromFeature(feature));
}
}));
if (this.toolbarOptions.zoom.show) {
this.zoomToFeatureGraphics();
}
this.getColumnsAndSort(results, options);
if (rows && rows.length > 0) {
var store = new Memory({
idProperty: this.idProperty,
data: rows
});
this.grid.set('collection', store);
}
// refresh only needs with IE?
if (has('ie')) {
this.grid.refresh();
}
this.setToolbarButtons();
},
getRecordFromFeature: function (feature) {
var rows = [], delim = '';
var lq = null;
if (this.hasLinkedQuery()) {
lq = this.linkedQuery;
}
var showFeatures = this.featureOptions.features;
if (!lq || lq.type !== 'table') {
var row = feature.attributes;
if (this.gridOptions.useCodedDomainValues) {
row = this.getCodedDomainValues(row);
}
if (this.getColumn('layerName') && !row.layerName) {
row.layerName = this.getLayerName();
}
// add reference to the feature if there is geometry
if (showFeatures && feature.geometry) {
row.feature = lang.clone(feature);
}
if (lq && lq.linkIDs) {
lq.linkIDs.push(delim + feature.attributes[this.linkField] + delim);
}
rows.push(row);
if (showFeatures && feature.geometry) {
this.addFeatureGraphic(feature);
}
}
return rows;
},
getRelatedRecords: function (feature) {
var rows = [], delim = '', objectID = feature.objectId;
var lq = null;
if (this.hasLinkedQuery()) {
lq = this.linkedQuery;
}
var showFeatures = this.featureOptions.features;
// multiple related records for a feature
array.forEach(feature.relatedRecords, lang.hitch(this, function (record) {
if (record.attributes) {
var row = record.attributes;
row.RelatedObjectID = objectID;
rows.push(row);
}
if (lq && lq.linkIDs) {
lq.linkIDs.push(delim + feature.attributes[this.linkField] + delim);
}
if (showFeatures && record.geometry) {
this.addFeatureGraphic(feature);
}
}));
return rows;
},
getCodedDomainValues: function (attributes) {
var k = null, len = null;
var layer = this.getQueryTaskLayerJSON();
if (layer && layer.fields) {
for (var fieldName in attributes) {
if (attributes.hasOwnProperty(fieldName)) {
var field = null, fields = layer.fields;
len = fields.length;
for (k = 0; k < len; k++) {
if (fieldName === fields[k].name) {
field = fields[k];
if (field) {
var codedValueDomain = field.domain;
if (codedValueDomain && codedValueDomain.type === 'codedValue') {
var codedValues = codedValueDomain.codedValues;
len = codedValues.length;
for (k = 0; k < len; k++) {
var codedValue = codedValues[k];
if (attributes[fieldName] === codedValue.code) {
attributes[fieldName] = codedValue.name;
}
}
}
}
}
}
}
}
}
return attributes;
},
getLayerName: function () {
var layer = this.getQueryTaskLayerJSON();
if (layer) {
return layer.name;
}
return '';
},
getColumnsAndSort: function (results, options) {
if (options) {
// reset the columns?
if (options.columns) {
this.gridOptions.columns = options.columns;
}
// reset the sort?
if (options.sort) {
this.gridOptions.sort = options.sort;
}
}
// set the columns
var columns = lang.clone(this.gridOptions.columns) || [];
// no columns? get them from the fields
if (!columns || columns.length < 1) {
columns = this.buildColumns(results);
}
if (columns) {
columns = this.setFormattersAndGetters(columns);
this.setColumnStyles(columns);
this.grid.set('columns', columns);
} else if (this.gridOptions.subRows) {
this.grid.set('subRows', this.gridOptions.subRows);
}
// set the sort
var sort = this.gridOptions.sort || [];
// sort === 'inherit'? use query result order
if (typeof sort === 'string' && sort.toLowerCase() === 'inherit') {
return;
}
// no sort? use the first column
if (sort.length < 1 && columns && columns.length > 0) {
sort = [
{
property: columns[0].field,
descending: false
}
];
} else {
// replace 'attribute' with 'property'.
// needed to handle old configurations with new dgrid 1.x
array.forEach(sort, function (item) {
if (item.attribute && !item.property) {
item.property = item.attribute;
delete item.attribute;
}
});
}
this.grid.set('sort', sort);
},
buildColumns: function (results) {
var excludedFields = ['objectid', 'esri_oid', 'shape', 'shape.len', 'shape.area', 'shape.starea()', 'shape.stlength()', 'st_area(shape)', 'st_length(shape)'];
var columns = [],
col = null,
nameLC = null;
if (results.fields) {
array.forEach(results.fields, lang.hitch(this, function (field) {
nameLC = field.name.toLowerCase();
if (array.indexOf(excludedFields, nameLC) < 0) {
var alias = field.alias;
if (alias === field.name) {
alias = this.formatters.makeSentenceCase(alias);
}
col = {
id: field.name,
field: field.name,
label: alias,
style: 'white-space:nowrap;overflow:hidden;text-overflow:ellipsis;',
width: 100
};
switch (field.type) {
case 'esriFieldTypeString':
col.width = 150;
break;
case 'esriFieldTypeSmallInteger':
case 'esriFieldTypeInteger':
col.formatter = 'number';
col.style += 'text-align:right;';
break;
case 'esriFieldTypeSingle':
case 'esriFieldTypeDouble':
col.formatter = 'double';
col.style += 'text-align:right;';
break;
case 'esriFieldTypeDate':
col.width = 150;
col.formatter = 'dateTime';
break;
default:
break;
}
columns.push(col);
}
}));
} else if (this.getFeatureCount() > 0) {
var feature = this.features[0];
if (feature) {
var attributes = feature.attributes;
for (var key in attributes) {
if (attributes.hasOwnProperty(key)) {
columns.push({
id: key,
field: key,
label: this.formatters.makeSentenceCase(key),
width: 100
});
}
}
}
}
return columns;
},
setFormattersAndGetters: function (columns) {
array.forEach(columns, lang.hitch(this, function (column) {
if (typeof column.formatter === 'string') {
if (this.formatters && this.formatters[column.formatter]) {
column.formatter = lang.hitch(this, this.formatters[column.formatter]);
} else {
delete column.formatter;
}
}
if (typeof column.get === 'string') {
if (this.getters && this.getters[column.get]) {
column.get = lang.hitch(this, this.getters[column.get]);
} else {
delete column.get;
}
}
}));
return columns;
},
setColumnStyles: function (columns) {
//style the grid columns from the config object
var gr = this.grid;
array.forEach(columns, function (column) {
if (column.style) {
gr.styleColumn(column.id, column.style);
}
});
},
selectFeaturesFromGrid: function () {
var selection = this.grid.get('selection'),
feature = null;
this.selectedFeatures = [];
this.selectedGraphics.clear();
for (var key in selection) {
if (selection.hasOwnProperty(key) && selection[key] === true) {
feature = this.getFeatureFromStore(key);
if (feature && feature.geometry) {
this.addSelectedGraphic(feature);
}
}
}
this.doneSelectingFeatures(true);
},
getFeatureFromStore: function (key) {
var collection = this.grid.get('collection'),
rec = null,
feature = null;
rec = collection.getSync(key);
if (rec) {
feature = rec.feature;
}
return feature;
},
getColumn: function (columnID) {
var columns = lang.clone(this.gridOptions.columns) || [];
var columnFound = array.filter(columns, function (column) {
return (column.field === columnID);
});
return (columnFound.length > 0) ? columnFound[0] : null;
},
clearGrid: function () {
if (this.grid) {
if (this.grid.clearSelection) {
this.grid.clearSelection();
}
this.grid.set('columns', []);
this.grid.set('collection', new Memory());
this.grid.refresh();
}
this.setToolbarButtons();
topic.publish(this.attributesContainerID + '/tableUpdated', this);
},
clearSelectedGridRows: function () {
if (!this.grid) {
return null;
}
var collection = this.grid.get('collection');
var selection = lang.clone(this.grid.get('selection'));
if (!selection || !collection || !collection.data) {
return null;
}
for (var key in selection) {
if (selection.hasOwnProperty(key) && selection[key] === true) {
collection.removeSync(key);
}
}
this.grid.refresh();
return {selection: selection, idProperty: this.idProperty};
},
showOnlySelectedGridRows: function () {
if (!this.grid) {
return null;
}
var collection = this.grid.get('collection');
var selection = lang.clone(this.grid.get('selection'));
var toKeep = [];
if (!selection || !collection || !collection.data) {
return null;
}
for (var key in selection) {
if (selection.hasOwnProperty(key) && selection[key] === true) {
toKeep.push(lang.clone(collection.getSync(key)));
}
}
collection.setData(toKeep);
this.grid.refresh();
return {selection: selection, idProperty: this.idProperty};
}
});
});