-
Notifications
You must be signed in to change notification settings - Fork 88
/
component.js
394 lines (352 loc) · 11.2 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
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
// 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 gmfRasterRasterService from 'gmf/raster/RasterService.js';
import ngeoMiscDebounce from 'ngeo/misc/debounce.js';
import {listen, unlistenByKey} from 'ol/events.js';
import MapBrowserEvent from 'ol/MapBrowserEvent.js';
import 'bootstrap/js/src/dropdown.js';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('gmfRasterComponent', [gmfRasterRasterService.name, ngeoMiscDebounce.name]);
myModule.run(
/**
* @ngInject
* @param {angular.ITemplateCacheService} $templateCache
*/
($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('gmf/raster/widgetComponent', require('./widgetComponent.html'));
}
);
myModule.value(
'gmfElevationwidgetTemplateUrl',
/**
* @param {angular.IAttributes} $attrs Attributes.
* @return {string} The template url.
*/
($attrs) => {
const templateUrl = $attrs.gmfElevationwidgetTemplateUrl;
return templateUrl !== undefined ? templateUrl : 'gmf/raster/widgetComponent';
}
);
/**
* @hidden
* @param {angular.IAttributes} $attrs Attributes.
* @param {function(angular.IAttributes): string} gmfElevationwidgetTemplateUrl Template function.
* @return {string} Template URL.
* @ngInject
* @private
* @hidden
*/
function gmfElevationwidgetTemplateUrl($attrs, gmfElevationwidgetTemplateUrl) {
return gmfElevationwidgetTemplateUrl($attrs);
}
/**
* Provide a directive that set a value each 500ms with the elevation under the
* mouse cursor position on the map. The value must come from the elevation
* service of a c2cgeoportal server. The server's URL must be defined as
* config value of the application.
*
* Example:
*
* <span gmf-elevation
* gmf-elevation-active="elevationActive"
* gmf-elevation-elevation="elevationValue"
* gmf-elevation-layer="mainCtrl.elevationLayer"
* gmf-elevation-map="::mainCtrl.map">
* {{elevationValue}}
* </span>
*
* For value in meter `elevationLayersConfig` can be an empty object, complex example:
*
* elevationLayersConfig = {
* '<layer>': {
* 'filter': 'ngeoUnitPrefix',
* 'args': ['m²', 'square'],
* 'postfix': '<notice>',
* 'separator': ''
* }
* };
*
*
* @htmlAttribute {boolean} gmf-elevation-active A boolean to set active or deactivate the component.
* @htmlAttribute {number} gmf-elevation-elevation The value to set with the elevation value.
* @htmlAttribute {string} gmf-elevation-layer Elevation layer to use.
* @htmlAttribute {import("ol/Map.js").default} gmf-elevation-map The map.
* @return {angular.IDirective} Directive Definition Object.
* @ngdoc directive
* @ngname gmfElevation
*/
function rasterComponent() {
return {
restrict: 'A',
controller: 'GmfElevationController as ctrl',
bindToController: true,
scope: {
'active': '<gmfElevationActive',
'elevation': '=gmfElevationElevation',
'loading': '=?gmfElevationLoading',
'layer': '<gmfElevationLayer',
'map': '=gmfElevationMap',
},
link: (scope, element, attr) => {
// @ts-ignore: scope ......
const ctrl = scope.ctrl;
// Watch active or not.
scope.$watch(
() => ctrl.active,
(active) => {
ctrl.toggleActive_(active);
}
);
// Watch current layer.
scope.$watch(
() => ctrl.layer,
(layer) => {
ctrl.layer = layer;
ctrl.elevation = null;
}
);
},
};
}
myModule.directive('gmfElevation', rasterComponent);
/**
* @hidden
* @param {angular.IScope} $scope Scope.
* @param {angular.IFilterService} $filter Angular filter.
* @param {import("ngeo/misc/debounce.js").miscDebounce<function(Event|import('ol/events/Event.js').default): void>} ngeoDebounce Ngeo debounce factory
* @param {import("gmf/raster/RasterService.js").RasterService} gmfRaster Gmf Raster service
* @param {angular.gettext.gettextCatalog} gettextCatalog Gettext catalog.
* @param {import('gmf/options.js').gmfElevationOptions} gmfElevationOptions The options
* @constructor
* @hidden
* @ngInject
* @ngdoc controller
* @ngname gmfElevationController
*/
export function Controller($scope, $filter, ngeoDebounce, gmfRaster, gettextCatalog, gmfElevationOptions) {
/**
* @type {import('gmf/options.js').gmfElevationOptions}
*/
this.options = gmfElevationOptions;
this.filter_ = $filter;
this.ngeoDebounce_ = ngeoDebounce;
this.gmfRaster_ = gmfRaster;
this.gettextCatalog = gettextCatalog;
/**
* @type {boolean}
*/
this.active = false;
/**
* @type {?string}
*/
this.elevation = null;
/**
* @type {string}
*/
this.layer = '';
/**
* @type {?import("ol/Map.js").default}
*/
this.map = null;
/**
* @type {Array<import("ol/events.js").EventsKey>}
*/
this.listenerKeys_ = [];
/**
* @type {angular.IScope}
*/
this.scope_ = $scope;
/**
* @type {boolean}
*/
this.inViewport_ = false;
/**
* @type {boolean}
*/
this.loading = false;
}
/**
* Activate or deactivate the listeners of the component.
* @param {boolean} active true to make requests.
* @private
*/
Controller.prototype.toggleActive_ = function (active) {
this.clear_();
active ? this.activate_() : this.deactivate_();
};
/**
* Activate the listeners of the component.
*/
Controller.prototype.activate_ = function () {
if (this.listenerKeys_.length) {
throw new Error('Unexpexted listenerKeys');
}
if (!this.map) {
throw new Error('Missing map');
}
// Moving the mouse clears previously displayed elevation
this.listenerKeys_.push(listen(this.map, 'pointermove', this.onPointerMove_.bind(this)));
// Launch the raster service request when the user stops moving the mouse for less short delay.
this.listenerKeys_.push(
listen(this.map, 'pointermove', this.ngeoDebounce_(this.afterPointerMove_.bind(this), 500, true))
);
// Clear the elevation and set inViewport_ to false.
this.listenerKeys_.push(listen(this.map.getViewport(), 'mouseout', this.onMouseout_.bind(this)));
};
/**
* Deactivate the listeners of the component
*/
Controller.prototype.deactivate_ = function () {
this.listenerKeys_.forEach(unlistenByKey);
this.listenerKeys_.length = 0;
};
/**
* Clears previously displayed raster info and set inViewport_ to true.
*/
Controller.prototype.onPointerMove_ = function () {
this.scope_.$apply(() => {
this.clear_();
this.inViewport_ = true;
this.loading = true;
});
};
/**
* Set the state of the component and query the raster service with the coordinate.
* @param {Event|import('ol/events/Event.js').default} evt Event.
*/
Controller.prototype.afterPointerMove_ = function (evt) {
if (this.inViewport_ && evt instanceof MapBrowserEvent) {
const params = {
'layers': this.layer,
};
this.gmfRaster_
.getRaster(evt.coordinate, params)
.then(this.getRasterSuccess_.bind(this), this.getRasterError_.bind(this));
}
};
/**
* Clears previously displayed raster info and set inViewport_ to false.
*/
Controller.prototype.onMouseout_ = function () {
this.scope_.$apply(() => {
this.clear_();
this.inViewport_ = false;
});
};
/**
* @param {Object<string, number>} resp Response of the get Raster service.
*/
Controller.prototype.getRasterSuccess_ = function (resp) {
if (this.layer === undefined) {
throw new Error('Missing layer');
}
const value = resp[this.layer];
if (value !== undefined && value !== null) {
const options = (this.options.layersConfig || {})[this.layer] || {};
const filter = options.filter || 'number';
const custom_args = options.args || [];
/** @type {string} */
const postfix = options.hasOwnProperty('postfix') ? options.postfix : 'm';
/** @type {string} */
const separator =
postfix.length > 0 ? (options.hasOwnProperty('separator') ? options.separator : '\u00a0') : '';
const args = Array.prototype.concat([value], custom_args);
const elevation = /** @type {string} */ (this.filter_(filter)(...args));
if (typeof elevation != 'string') {
throw new Error('Wrong elevation type');
}
this.elevation = elevation + separator + postfix;
} else {
const gettextCatalog = this.gettextCatalog;
this.elevation = gettextCatalog.getString('No value');
}
this.loading = false;
};
Controller.prototype.getRasterError_ = function () {
console.error('Error on getting the raster.');
this.clear_();
};
/**
* Clear the state.
*/
Controller.prototype.clear_ = function () {
this.elevation = null;
this.loading = false;
};
myModule.controller('GmfElevationController', Controller);
/**
* Provides a component which encapsulates the elevation component (see above)
* in a button with dropdown menu to be included in a application directly.
*
* Example:
* <gmf-elevationwidget
* gmf-elevationwidget-map="::mainCtrl.map"
* gmf-elevationwidget-active="mainCtrl.showInfobar">
* </gmf-elevationwidget>
*
* @htmlAttribute {import("ol/Map.js").default} gmf-elevationwidget-map The map.
* @htmlAttribute {boolean} gmf-elevationwidget-active Whether to activate the elevation component.
*
* @ngdoc component
* @ngname gmfElevationwidget
*/
const rasterWidgetComponent = {
controller: 'gmfElevationwidgetController as ctrl',
bindings: {
'map': '<gmfElevationwidgetMap',
'active': '<gmfElevationwidgetActive',
},
templateUrl: gmfElevationwidgetTemplateUrl,
};
myModule.component('gmfElevationwidget', rasterWidgetComponent);
/**
* @constructor
* @hidden
* @nginject
* @ngdoc controller
* @param {import('gmf/options.js').gmfElevationOptions} gmfElevationOptions The options.
*/
export function WidgetController(gmfElevationOptions) {
this.options = gmfElevationOptions;
/**
* @type {?import("ol/Map.js").default}
*/
this.map = null;
/**
* @type {boolean}
*/
this.active = false;
/**
* @type {boolean}
*/
this.show = this.options.layers && this.options.layers.length > 0;
/**
* @type {string}
*/
this.selectedElevationLayer = this.show ? this.options.layers[0] : '';
}
myModule.controller('gmfElevationwidgetController', WidgetController);
export default myModule;