-
Notifications
You must be signed in to change notification settings - Fork 88
/
component.js
299 lines (266 loc) · 8.35 KB
/
component.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
// The MIT License (MIT)
//
// Copyright (c) 2016-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
import {isPlatformModifierKeyOnly, isShiftKeyOnly} from 'ngeo/utils.js';
import ngeoMiscFilters from 'ngeo/misc/filters.js';
import {getRowUid} from 'ngeo/grid/Config.js';
import 'floatthead';
import 'angular-float-thead';
import 'ngeo/sass/font.scss';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoGrid', [ngeoMiscFilters.name, 'floatThead']);
myModule.run(
/**
* @ngInject
* @param {angular.ITemplateCacheService} $templateCache
*/
($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('ngeo/grid', require('./component.html'));
}
);
myModule.value(
'ngeoGridTemplateUrl',
/**
* @param {angular.IAttributes} $attrs Attributes.
* @return {string} Template URL.
*/
($attrs) => {
const templateUrl = $attrs.ngeoGridTemplateurl;
return templateUrl !== undefined ? templateUrl : 'ngeo/grid';
}
);
/**
* @param {angular.IAttributes} $attrs Attributes.
* @param {function(angular.IAttributes): string} ngeoGridTemplateUrl Template function.
* @return {string} Template URL.
* @ngInject
* @private
* @hidden
*/
function ngeoGridTemplateUrl($attrs, ngeoGridTemplateUrl) {
return ngeoGridTemplateUrl($attrs);
}
/**
* A grid component for displaying tabular data. The columns of the grid
* are sortable, rows can be selected with a single click (also in combination
* with SHIFT and CTRL/Meta).
*
* Example:
*
* <ngeo-grid
* ngeo-grid-configuration="::ctrl.gridConfiguration"
* </ngeo-grid>
*
* @htmlAttribute {import("ngeo/grid/Config.js").default} ngeo-grid-configuration The
* configuration to use.
*
* @ngdoc component
* @ngname ngeoGrid
*/
const gridComponent = {
controller: GridController,
bindings: {
'configuration': '=ngeoGridConfiguration',
},
templateUrl: ngeoGridTemplateUrl,
};
myModule.component('ngeoGrid', gridComponent);
/**
* @param {angular.IScope} $scope Angular scope.
* @constructor
* @private
* @hidden
* @ngInject
* @ngdoc controller
* @ngname ngeoGridController
*/
export function GridController($scope) {
/**
* @type {angular.IScope}
*/
this.scope_ = $scope;
/**
* @type {?import("ngeo/grid/Config.js").default}
*/
this.configuration = null;
/**
* @type {Object<string, Object>}
*/
this.selectedRows = {};
/**
* The name of the column used to sort the grid.
* @type {string}
*/
this.sortedBy = '';
/**
* @type {boolean}
*/
this.sortAscending = true;
/**
* Configuration object for float-thead.
* @type {unknown}
*/
this.floatTheadConfig = {
/**
* @param {JQuery} $table
*/
scrollContainer: function ($table) {
return $table.closest('.ngeo-grid-table-container');
},
};
}
/**
* Init the controller
*/
GridController.prototype.$onInit = function () {
if (!this.configuration) {
throw new Error('Missing configuration');
}
this.selectedRows = this.configuration.selectedRows;
};
/**
* Sort function that always puts undefined values to the bottom of the grid.
* A new call will sort ascending. A next one will sort descending (and so
* on).
* @param {string} columnName The name of the column that should be used to
* sort the data.
*/
GridController.prototype.sort = function (columnName) {
if (!this.configuration) {
throw new Error('Missing configuration');
}
if (!this.configuration.data) {
throw new Error('Missing configuration.data');
}
this.sortAscending = this.sortedBy === columnName ? !this.sortAscending : true;
this.sortedBy = columnName;
const asc = this.sortAscending ? 1 : -1;
this.configuration.data.sort((attributes1, attributes2) => {
if (!attributes1[columnName]) {
return 1;
}
if (!attributes2[columnName]) {
return -1;
}
return attributes1[columnName] > attributes2[columnName] ? asc : -asc;
});
};
/**
* Handler for clicks on a row.
* @param {unknown} attributes An entry/row.
* @param {JQueryEventObject} event Event.
*/
GridController.prototype.clickRow = function (attributes, event) {
const shiftKey = isShiftKeyOnly(event);
const platformModifierKey = isPlatformModifierKeyOnly(event);
this.clickRow_(attributes, shiftKey, platformModifierKey);
};
/**
* @param {unknown} attributes An entry/row.
* @param {boolean} shiftKey Shift pressed?
* @param {boolean} platformModifierKey CTRL/Meta pressed?
*/
GridController.prototype.clickRow_ = function (attributes, shiftKey, platformModifierKey) {
if (!this.configuration) {
throw new Error('Missing configuration');
}
if (shiftKey && !platformModifierKey) {
this.selectRange_(attributes);
} else if (!shiftKey && platformModifierKey) {
this.configuration.toggleRow(attributes);
} else {
const isSelected = this.configuration.isRowSelected(attributes);
this.configuration.unselectAll();
if (!isSelected) {
this.configuration.selectRow(attributes);
}
}
};
/**
* Selects all rows between the given row and the closest already selected row.
* @param {unknown} attributes An entry/row.
*/
GridController.prototype.selectRange_ = function (attributes) {
if (!this.configuration) {
throw new Error('Missing configuration');
}
if (!this.configuration.data) {
throw new Error('Missing configuration.data');
}
const targetUid = getRowUid(attributes);
const data = this.configuration.data;
if (this.configuration.isRowSelected(attributes)) {
return;
}
// get the position of the clicked and all already selected rows
/** @type {number} */
let posClickedRow = 0;
const posSelectedRows = [];
for (let i = 0; i < data.length; i++) {
const currentRow = data[i];
const currentUid = getRowUid(currentRow);
if (targetUid === currentUid) {
posClickedRow = i;
} else if (this.configuration.isRowSelected(currentRow)) {
posSelectedRows.push(i);
}
}
if (posSelectedRows.length == 0) {
// if no other row is selected, select the clicked one and stop
this.configuration.selectRow(attributes);
}
// find the selected row which is the closest to the clicked row
let distance = Infinity;
let posClosestRow = posSelectedRows[0];
for (const posSelectedRow of posSelectedRows) {
const currentPos = posSelectedRow;
const currentDistance = Math.abs(currentPos - posClickedRow);
if (distance > currentDistance) {
distance = currentDistance;
posClosestRow = currentPos;
}
// note: this could be optimized because `posSelectedRows` is ordered.
}
// then select all rows between the clicked one and the closest
const rangeStart = posClickedRow < posClosestRow ? posClickedRow : posClosestRow;
const rangeEnd = posClickedRow > posClosestRow ? posClickedRow : posClosestRow;
for (let l = rangeStart; l <= rangeEnd; l++) {
this.configuration.selectRow(data[l]);
}
};
/**
* Prevent the default browser behaviour of selecting text
* when selecting multiple rows with SHIFT or CTRL/Meta.
* @param {JQueryEventObject} event Event.
*/
GridController.prototype.preventTextSelection = function (event) {
const shiftKey = isShiftKeyOnly(event);
const platformModifierKey = isPlatformModifierKeyOnly(event);
if (shiftKey || platformModifierKey) {
event.preventDefault();
}
};
myModule.controller('ngeoGridController', GridController);
export default myModule;