-
Notifications
You must be signed in to change notification settings - Fork 88
/
windowComponent.js
599 lines (538 loc) · 17.6 KB
/
windowComponent.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
// 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 ngeoDownloadCsv from 'ngeo/download/Csv.js';
import ngeoDownloadService from 'ngeo/download/service.js';
import ngeoMapFeatureOverlayMgr from 'ngeo/map/FeatureOverlayMgr.js';
import ngeoMiscFeatureHelper, {getFilteredFeatureValues} from 'ngeo/misc/FeatureHelper.js';
import ngeoMiscSwipe from 'ngeo/misc/swipe.js';
import ngeoQueryMapQuerent from 'ngeo/query/MapQuerent.js';
import olCollection from 'ol/Collection.js';
import {isEmpty} from 'ol/obj.js';
import {buildStyle} from 'ngeo/options.js';
import 'jquery-ui/ui/widgets/resizable.js';
import 'ngeo/sass/jquery-ui.scss';
import 'angular-animate';
import 'angular-touch';
import 'bootstrap/js/src/collapse.js';
import 'bootstrap/js/src/dropdown.js';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('gmfQueryWindowComponent', [
ngeoDownloadCsv.name,
ngeoDownloadService.name,
ngeoMapFeatureOverlayMgr.name,
ngeoMiscFeatureHelper.name,
ngeoMiscSwipe.name,
ngeoQueryMapQuerent.name,
'ngAnimate',
'ngTouch',
]);
myModule.config([
'$animateProvider',
/**
* For performance reason, only perform animation on elements that have the
* `gmf-animatable` css class.
* @param {angular.animate.IAnimateProvider} $animateProvider animate provider.
*/
function ($animateProvider) {
$animateProvider.classNameFilter(/gmf-animatable/);
},
]);
myModule.value(
'gmfDisplayquerywindowTemplateUrl',
/**
* @param {JQuery} $element Element.
* @param {angular.IAttributes} $attrs Attributes.
* @return {string} Template.
*/
($element, $attrs) => {
const templateUrl = $attrs.gmfDisplayquerywindowTemplateurl;
return templateUrl !== undefined ? templateUrl : 'gmf/query/windowComponent';
}
);
myModule.run(
/**
* @ngInject
* @param {angular.ITemplateCacheService} $templateCache
*/
($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('gmf/query/windowComponent', require('./windowComponent.html'));
}
);
/**
* @param {JQuery} $element Element.
* @param {angular.IAttributes} $attrs Attributes.
* @param {function(JQuery, angular.IAttributes): string} gmfDisplayquerywindowTemplateUrl Template
* function.
* @return {string} Template URL.
* @ngInject
* @private
* @hidden
*/
function gmfDisplayquerywindowTemplateUrl($element, $attrs, gmfDisplayquerywindowTemplateUrl) {
return gmfDisplayquerywindowTemplateUrl($element, $attrs);
}
/**
* Provide a component to display results of the {@link import("ngeo/queryResult.js").default}
* and shows related features on the map using the
* {@link import("ngeo/map/FeatureOverlayMgr.js").FeatureOverlayMgr}.
*
* You can override the default component's template by setting the
* value `gmfDisplayquerywindowTemplateUrl`.
*
* Features displayed on the map use a default style but you can override these
* styles by passing ol.style.Style objects as attributes of this component.
*
* Example:
*
* <gmf-displayquerywindow></gmf-displayquerywindow>
*
* @htmlAttribute {boolean} desktop If the component is used in the desktop
* application.
* @ngdoc component
* @ngname gmfDisplayquerywindow
*/
const queryWindowComponent = {
controller: 'GmfDisplayquerywindowController as ctrl',
bindings: {
'draggableContainment': '<?gmfDisplayquerywindowDraggableContainment',
'desktop': '=gmfDisplayquerywindowDesktop',
},
templateUrl: gmfDisplayquerywindowTemplateUrl,
};
myModule.component('gmfDisplayquerywindow', queryWindowComponent);
/**
* @param {JQuery} $element Element.
* @param {angular.IScope} $scope Angular scope.
* @param {import('ngeo/query/MapQuerent.js').QueryResult} ngeoQueryResult ngeo query result.
* @param {import("ngeo/query/MapQuerent.js").MapQuerent} ngeoMapQuerent ngeo map querent service.
* @param {import("ngeo/download/Csv.js").DownloadCsvService} ngeoCsvDownload CSV download service.
* @param {import("ngeo/map/FeatureOverlayMgr.js").FeatureOverlayMgr} ngeoFeatureOverlayMgr The ngeo feature
* overlay manager service.
* @param {import('gmf/options.js').gmfCsvFilename} gmfCsvFilename The CSV file name.
* @param {import('gmf/options.js').gmfDisplayQueryWindowOptions} gmfDisplayQueryWindowOptions The options.
* @constructor
* @hidden
* @ngInject
* @ngdoc controller
* @ngname GmfDisplayquerywindowController
*/
export function QueryWindowController(
$element,
$scope,
ngeoQueryResult,
ngeoMapQuerent,
ngeoCsvDownload,
ngeoFeatureOverlayMgr,
gmfCsvFilename,
gmfDisplayQueryWindowOptions
) {
/**
* @type {import('gmf/options.js').gmfDisplayQueryWindowOptions}
*/
this.options = gmfDisplayQueryWindowOptions;
/**
* @type {Element|string}
*/
this.draggableContainment = '';
/**
* @type {boolean}
*/
this.desktop = false;
/**
* Is the window currently collapsed?
* When used for Desktop, it is shown non-collapsed.
* @type {boolean}
*/
this.collapsed = false;
/**
* @type {import('ngeo/query/MapQuerent.js').QueryResult}
*/
this.ngeoQueryResult = {
sources: [],
total: 0,
pending: false,
};
/**
* @type {import("ngeo/query/MapQuerent.js").MapQuerent}
*/
this.ngeoMapQuerent_ = ngeoMapQuerent;
/**
* @type {import("ngeo/download/Csv.js").DownloadCsvService}
*/
this.ngeoCsvDownload_ = ngeoCsvDownload;
/**
* Filename
* @type {import('gmf/options.js').gmfCsvFilename}
*/
this.filename_ = gmfCsvFilename;
/**
* @type {?import('ngeo/statemanager/WfsPermalink.js').QueryResultSource}
*/
this.selectedSource = null;
/**
* @type {import("ol/Collection.js").default<import('ol/Feature.js').default<import("ol/geom/Geometry.js").default>>}
*/
this.features_ = new olCollection();
/**
* @type {import("ngeo/map/FeatureOverlayMgr.js").FeatureOverlayMgr}
*/
this.ngeoFeatureOverlayMgr_ = ngeoFeatureOverlayMgr;
/**
* @type {import("ol/Collection.js").default<import('ol/Feature.js').default<import("ol/geom/Geometry.js").default>>}
*/
this.highlightFeatures_ = new olCollection();
/**
* @type {?import('ngeo/statemanager/WfsPermalink.js').QueryResultSource}
*/
this.source = null;
/**
* @type {?import('ol/Feature.js').default<import("ol/geom/Geometry.js").default>}
*/
this.feature = null;
/**
* @type {number}
*/
this.currentResult = -1;
/**
* @type {boolean}
*/
this.isNext = true;
/**
* @type {number}
*/
this.animate = 0;
/**
* @type {boolean}
*/
this.open = false;
/**
* @const {JQuery}
*/
this.element_ = $element;
/**
* @type {boolean}
*/
this.isLoading = false;
$scope.$watchCollection(
() => ngeoQueryResult,
(newQueryResult, oldQueryResult) => {
this.isLoading = newQueryResult.pending;
this.updateQueryResult_(newQueryResult);
if (newQueryResult.total > 0) {
this.show();
} else if (oldQueryResult !== newQueryResult) {
this.open = false;
this.clear();
}
}
);
}
/**
* Initialise the controller.
*/
QueryWindowController.prototype.$onInit = function () {
if (this.options.collapsed !== undefined) {
this.collapsed = this.options.collapsed;
}
this.draggableContainment = this.draggableContainment || 'document';
const featuresOverlay = this.ngeoFeatureOverlayMgr_.getFeatureOverlay();
featuresOverlay.setFeatures(this.features_);
featuresOverlay.setStyle(buildStyle(this.options.featuresStyle));
const highlightFeaturesOverlay = this.ngeoFeatureOverlayMgr_.getFeatureOverlay();
highlightFeaturesOverlay.setFeatures(this.highlightFeatures_);
highlightFeaturesOverlay.setStyle(buildStyle(this.options.selectedFeatureStyle));
const windowContainer = this.element_.find('.gmf-displayquerywindow .windowcontainer');
if (this.desktop) {
windowContainer.draggable({
handle: '.header',
containment: this.draggableContainment,
});
windowContainer.resizable({
handles: 'all',
minHeight: 240,
minWidth: 260,
});
}
};
/**
* Remove current displayed results then get new results from the
* ngeoQueryResult service. Display all results on the map and display,
* highlight the first feature.
*/
QueryWindowController.prototype.show = function () {
this.clear();
this.updateFeatures_();
};
QueryWindowController.prototype.updateFeatures_ = function () {
this.setCurrentResult_(0, false);
if (this.source !== null) {
this.collectFeatures_();
this.highlightCurrentFeature_();
this.open = true;
}
};
/**
* Select a source and a feature depending of the given position.
* @param {number} position The index of the feature. If the position is bigger
* than the length of the first source, get it in the next source. Etc.
* @param {boolean} setHighlight True to set the highlight automatically.
* @return {boolean} True if result has changed. False else.
*/
QueryWindowController.prototype.setCurrentResult_ = function (position, setHighlight) {
let hasChanged = false;
if (position !== this.currentResult) {
let i, source, features;
const lastFeature = this.feature;
const sources = this.ngeoQueryResult.sources;
this.currentResult = position;
for (i = 0; i < sources.length; i++) {
source = sources[i];
if (this.selectedSource !== null && this.selectedSource !== source) {
// when filtering on a source, only consider features of the selected source
continue;
}
features = source.features;
if (position >= features.length) {
position -= features.length;
} else {
this.source = source;
this.feature = source.features[position];
hasChanged = true;
break;
}
}
if (setHighlight) {
this.highlightCurrentFeature_(lastFeature || undefined);
}
}
return hasChanged;
};
/**
* Select the logical previous source and feature then highlight this feature on
* the map.
*/
QueryWindowController.prototype.previous = function () {
let position = this.currentResult - 1;
if (position < 0) {
position = this.getResultLength() - 1;
}
const hasChanged = this.setCurrentResult_(position, true);
if (hasChanged) {
this.animate_(false);
}
};
/**
* Select the logical next source and feature then highlight this feature on
* the map.
*/
QueryWindowController.prototype.next = function () {
let position = this.currentResult + 1;
const positionMax = this.getResultLength() - 1;
if (position > positionMax) {
position = 0;
}
const hasChanged = this.setCurrentResult_(position, true);
if (hasChanged) {
this.animate_(true);
}
};
/**
* Remove features without properties from the query result.
* @param {import('ngeo/query/MapQuerent.js').QueryResult} queryResult ngeo query result.
*/
QueryWindowController.prototype.updateQueryResult_ = function (queryResult) {
this.ngeoQueryResult.total = 0;
this.ngeoQueryResult.sources.length = 0;
for (const source of queryResult.sources) {
source.features = source.features.filter((feature) => {
if (!feature) {
throw new Error('Missing feature');
}
return !isEmpty(getFilteredFeatureValues(feature));
});
this.ngeoQueryResult.sources.push(source);
this.ngeoQueryResult.total += source.features.length;
}
};
/**
* Get the total count of features in the result of the query. If a source
* has been select, only the number of features of that source are returned.
* @return {number} Total number of features.
*/
QueryWindowController.prototype.getResultLength = function () {
if (this.selectedSource === null) {
return this.ngeoQueryResult.total;
} else {
return this.selectedSource.features.length;
}
};
/**
* @return {boolean} If the first result is active.
*/
QueryWindowController.prototype.isFirst = function () {
return this.currentResult == 0;
};
/**
* @return {boolean} If the last result is active.
*/
QueryWindowController.prototype.isLast = function () {
return this.currentResult == this.getResultLength() - 1;
};
/**
* Delete the unwanted ol3 properties from the current feature then return the
* properties.
* @return {Object<string, string|number|boolean>?} Filtered properties of the current feature or null.
*/
QueryWindowController.prototype.getFeatureValues = function () {
if (!this.feature) {
return null;
}
return getFilteredFeatureValues(this.feature);
};
/**
* Special function that's used to set the "animation" value after to set the
* "isNext" value. The aim is to wait on Angular to add a class (corresponding
* to "isNext") on the DOM before to set the "animation" value and do the
* animation.
* @param {boolean} isNext used to indicate if the user wants to see the next
* or the previous result.
*/
QueryWindowController.prototype.animate_ = function (isNext) {
this.isNext = isNext;
this.animate++;
};
/**
* Collect all features in the queryResult object.
*/
QueryWindowController.prototype.collectFeatures_ = function () {
const sources = this.ngeoQueryResult.sources;
this.features_.clear();
for (const source of sources) {
if (this.selectedSource !== null && this.selectedSource !== source) {
// when filtering on a source, only add features of the selected source
continue;
}
const features = source.features;
for (const feature of features) {
this.features_.push(feature);
}
}
};
/**
* Highlight the current displayed feature.
* @param {import('ol/Feature.js').default<import("ol/geom/Geometry.js").default>=} opt_lastFeature last highlighted feature. Require if
* it exists because it must be added to the 'non-selected' features collection.
*/
QueryWindowController.prototype.highlightCurrentFeature_ = function (opt_lastFeature) {
if (!this.feature) {
throw new Error('Missing feature');
}
this.highlightFeatures_.clear();
this.features_.remove(this.feature);
this.highlightFeatures_.push(this.feature);
if (opt_lastFeature !== undefined) {
this.features_.push(opt_lastFeature);
}
};
/**
* Remove the current selected feature and source and remove all features
* from the map.
*/
QueryWindowController.prototype.close = function () {
this.open = false;
this.clear();
this.ngeoMapQuerent_.clear();
};
/**
* Remove the current selected feature and source and remove all features
* from the map.
*/
QueryWindowController.prototype.clear = function () {
this.feature = null;
this.source = null;
this.currentResult = -1;
this.features_.clear();
this.highlightFeatures_.clear();
this.selectedSource = null;
};
/**
* @param {import('ngeo/statemanager/WfsPermalink.js').QueryResultSource} source The source to select.
*/
QueryWindowController.prototype.setSelectedSource = function (source) {
if (source !== null && source.features.length <= 0) {
// sources with no results can not be selected
return;
}
this.clear();
this.selectedSource = source;
this.updateFeatures_();
};
/**
* Download a CSV with features of the given source.
* @param {import('ngeo/statemanager/WfsPermalink.js').QueryResultSource} source The source to export as csv.
*/
QueryWindowController.prototype.downloadCSV = function (source) {
const data = this.getCSVData_(source);
const columnDefs = this.getCSVHeaderDefinition_(data);
if (columnDefs) {
this.ngeoCsvDownload_.startDownload(data, columnDefs, this.filename_);
}
};
/**
* @param {import('ngeo/statemanager/WfsPermalink.js').QueryResultSource} source The source to export as csv.
* @return {Object<string, string|number|boolean>[]} data.
*/
QueryWindowController.prototype.getCSVData_ = function (source) {
if (!source || source.features.length <= 0) {
// Without source, or with sources with no results, export can't be done
return;
}
// Get properties (name - value) without unwanted ol properties.
return source.features.map((feature) => getFilteredFeatureValues(feature));
};
/**
* @param {Object<string, string|number|boolean>[]} data where keys with at least one defined value will be used as csv column header.
* @return {import('ngeo/download/Csv.js').GridColumnDef[]} columns definitions for the CSV.
*/
QueryWindowController.prototype.getCSVHeaderDefinition_ = function (data) {
if (!data) {
return;
}
const distinctKeys = new Set();
data.forEach((datum) => {
// keep property name for not undefined values.
Object.keys(datum)
.filter((key) => datum[key] !== undefined)
.forEach((key) => distinctKeys.add(key));
});
// From Set (distinct values) to array.
/** @type {import('ngeo/download/Csv.js').GridColumnDef[]} */
const columnDefs = [];
distinctKeys.forEach((key) => columnDefs.push({name: key}));
return columnDefs;
};
myModule.controller('GmfDisplayquerywindowController', QueryWindowController);
export default myModule;