/
abstract.js
625 lines (533 loc) · 16.7 KB
/
abstract.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
goog.provide('gmf.AbstractController');
goog.require('gmf');
/** @suppress {extraRequire} */
goog.require('gmf.authenticationDirective');
/** @suppress {extraRequire} */
goog.require('gmf.backgroundlayerselectorComponent');
/** @suppress {extraRequire} */
goog.require('gmf.DataSourcesManager');
/** @suppress {extraRequire} */
goog.require('gmf.TreeManager');
/** @suppress {extraRequire} */
goog.require('gmf.Themes');
/** @suppress {extraRequire} */
goog.require('gmf.disclaimerComponent');
/** @suppress {extraRequire} */
goog.require('gmf.displayquerywindowComponent');
/** @suppress {extraRequire} */
goog.require('gmf.layertreeComponent');
/** @suppress {extraRequire} */
goog.require('gmf.mapDirective');
/** @suppress {extraRequire} */
goog.require('gmf.searchDirective');
/** @suppress {extraRequire} */
goog.require('gmf.themeselectorDirective');
/** @suppress {extraRequire} */
goog.require('ngeo.BackgroundLayerMgr');
/** @suppress {extraRequire} */
goog.require('ngeo.filters');
/** @suppress {extraRequire} */
goog.require('ngeo.mapQueryDirective');
goog.require('ngeo.FeatureOverlayMgr');
goog.require('ngeo.GetBrowserLanguage');
/** @suppress {extraRequire} */
goog.require('ngeo.MapQuerent');
goog.require('ngeo.StateManager');
goog.require('ngeo.ToolActivate');
goog.require('ngeo.ToolActivateMgr');
goog.require('ol.style.Circle');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
gmf.module.value('ngeoExportFeatureFormats', [
ngeo.FeatureHelper.FormatType.KML,
ngeo.FeatureHelper.FormatType.GPX
]);
/**
* Application abstract controller.
*
* This file includes `goog.require` for base components/directives used
* by the HTML page and the controller to provide the configuration.
*
* @param {gmfx.Config} config A part of the application config.
* @param {angular.Scope} $scope Scope.
* @param {angular.$injector} $injector Main injector.
* @constructor
* @ngdoc controller
* @ngInject
* @export
*/
gmf.AbstractController = function(config, $scope, $injector) {
/**
* Location service
* @type {ngeo.Location}
*/
this.ngeoLocation = $injector.get('ngeoLocation');
if (this.ngeoLocation.hasParam('debug')) {
// make the injector globally available
window.injector = $injector;
}
goog.asserts.assertInstanceof(this.map, ol.Map);
/**
* Ngeo FeatureHelper service
* @type {ngeo.FeatureHelper}
*/
const ngeoFeatureHelper = $injector.get('ngeoFeatureHelper');
ngeoFeatureHelper.setProjection(goog.asserts.assert(this.map.getView().getProjection()));
/**
* @type {gmf.ThemeManager}
* @export
*/
this.gmfThemeManager = $injector.get('gmfThemeManager');
/**
* @type {gmf.TreeManager}
* @private
*/
this.gmfTreeManager_ = $injector.get('gmfTreeManager');
/**
* Themes service
* @type {gmf.Themes}
* @private
*/
this.gmfThemes_ = $injector.get('gmfThemes');
/**
* Permalink service
* @type {gmf.Permalink}
* @private
*/
this.permalink_ = $injector.get('gmfPermalink');
/**
* Authentication service
* @type {gmf.Authentication}
*/
const gmfAuthentication = $injector.get('gmfAuthentication');
/**
* @type {boolean}
* @export
*/
this.hasEditableLayers = false;
/**
* @private
*/
this.updateHasEditableLayers_ = function() {
this.gmfThemes_.hasEditableLayers().then((hasEditableLayers) => {
this.hasEditableLayers = hasEditableLayers;
});
};
const userChange = (evt) => {
const roleId = (evt.user.username !== null) ? evt.user.role_id : undefined;
// Open filter panel if 'open_panel' is set in functionalities and
// has 'layer_filter' as first value
this.gmfThemes_.getThemesObject().then((themes) => {
const functionalities = this.gmfUser.functionalities;
if (functionalities &&
functionalities.open_panel &&
functionalities.open_panel[0] === 'layer_filter') {
this.filterSelectorActive = true;
}
});
// Reload theme when login status changes.
const previousThemeName = this.gmfThemeManager.getThemeName();
this.gmfThemeManager.setThemeName('', true);
// Reload themes and background layer when login status changes.
this.gmfThemes_.loadThemes(roleId);
if (evt.type !== gmf.AuthenticationEventType.READY) {
this.updateCurrentTheme_(previousThemeName);
}
this.setDefaultBackground_(null);
this.updateHasEditableLayers_();
};
ol.events.listen(gmfAuthentication, gmf.AuthenticationEventType.READY, userChange);
ol.events.listen(gmfAuthentication, gmf.AuthenticationEventType.LOGIN, userChange);
ol.events.listen(gmfAuthentication, gmf.AuthenticationEventType.LOGOUT, userChange);
/**
* @type {Array.<gmfx.SearchDirectiveDatasource>}
* @export
*/
this.searchDatasources = [{
labelKey: 'label',
groupValues: /** @type {Array.<string>} **/ ($injector.get('gmfSearchGroups')),
groupActions: /** @type {Array.<string>} **/ ($injector.get('gmfSearchActions')),
projection: `EPSG:${config.srid || 21781}`,
url: /** @type {string} **/ ($injector.get('fulltextsearchUrl'))
}];
/**
* @type {!Object.<string, string>}
* @export
*/
this.dimensions = {};
// watch any change on dimensions object to refresh the url
this.permalink_.setDimensions(this.dimensions);
// Injecting the gmfDataSourcesManager service creates the data sources
const gmfDataSourcesManager = $injector.get('gmfDataSourcesManager');
// Give the dimensions to the gmfDataSourcesManager
gmfDataSourcesManager.setDimensions(this.dimensions);
if ($injector.has('gmfDefaultDimensions')) {
// Set defaults
const defaultDimensions = $injector.get('gmfDefaultDimensions');
for (const dim in defaultDimensions) {
if (this.dimensions[dim] === undefined) {
this.dimensions[dim] = defaultDimensions[dim];
}
}
}
/**
* @type {ngeo.BackgroundLayerMgr}
* @private
*/
this.backgroundLayerMgr_ = $injector.get('ngeoBackgroundLayerMgr');
// watch any change on dimensions object to refresh the background layer
$scope.$watchCollection(() => this.dimensions, () => {
this.backgroundLayerMgr_.updateDimensions(this.map, this.dimensions);
});
this.backgroundLayerMgr_.on(ngeo.BackgroundEventType.CHANGE, () => {
this.backgroundLayerMgr_.updateDimensions(this.map, this.dimensions);
});
/**
* @type {boolean}
* @export
*/
this.leftNavVisible = false;
/**
* @type {boolean}
* @export
*/
this.rightNavVisible = false;
const queryFill = new ol.style.Fill({color: [255, 170, 0, 0.6]});
const queryStroke = new ol.style.Stroke({color: [255, 170, 0, 1], width: 2});
/**
* FeatureStyle used by the displayquerywindow directive
* @type {ol.style.Style}
* @export
*/
this.queryFeatureStyle = new ol.style.Style({
fill: queryFill,
image: new ol.style.Circle({
fill: queryFill,
radius: 5,
stroke: queryStroke
}),
stroke: queryStroke
});
/**
* @type {boolean}
* @export
*/
this.filterSelectorActive = false;
/**
* The active state of the ngeo query directive.
* @type {boolean}
* @export
*/
this.queryActive = true;
/**
* Set the clearing of the ngeoQuery after the deactivation of the query
* @type {boolean}
* @export
*/
this.queryAutoClear = true;
/**
* @type {boolean}
* @export
*/
this.printPanelActive = false;
/**
* @type {boolean}
* @export
*/
this.printActive = false;
/**
* @type {ngeo.MapQuerent}
* @private
*/
this.ngeoMapQuerent_ = $injector.get('ngeoMapQuerent');
// Don't deactivate ngeoQuery on print activation
$scope.$watch(() => this.printPanelActive, (newVal) => {
// Clear queries if another panel is open but not if user go back to the
// map form the print.
if (!newVal && !this.queryActive) {
this.ngeoMapQuerent_.clear();
}
this.queryAutoClear = !newVal;
this.printActive = newVal;
});
/**
* The active state of the directive responsible of point measurements.
* @type {boolean}
* @export
*/
this.measurePointActive = false;
/**
* The active state of the directive responsible of length measurements.
* @type {boolean}
* @export
*/
this.measureLengthActive = false;
/**
* @type {boolean}
* @export
*/
this.drawFeatureActive = false;
/**
* @type {boolean}
* @export
*/
this.drawProfilePanelActive = false;
/**
* @type {gmfx.User}
* @export
*/
this.gmfUser = $injector.get('gmfUser');
// close right nave on successful login
$scope.$watch(() => this.gmfUser.username, (newVal) => {
if (newVal !== null && this.navIsVisible) {
this.rightNavVisible = false;
}
});
/**
* @type {ngeo.GetBrowserLanguage}
*/
this.getBrowserLanguage = $injector.get('ngeoGetBrowserLanguage');
/**
* @type {ngeo.StateManager}
*/
this.stateManager = $injector.get('ngeoStateManager');
/**
* @type {tmhDynamicLocale}
*/
this.tmhDynamicLocale = $injector.get('tmhDynamicLocale');
/**
* @type {angular.Scope}
*/
this.$scope = $scope;
/**
* Default language
* @type {string}
*/
this.defaultLang = $injector.get('defaultLang');
/**
* Languages URL
* @type {!Object.<string, string>}
*/
this.langUrls = $injector.get('langUrls');
/**
* The gettext catalog
* @type {angularGettext.Catalog}
*/
this.gettextCatalog = $injector.get('gettextCatalog');
this.initLanguage();
const mapTools = 'mapTools';
/**
* @type {string}
* @export
*/
this.mapToolsGroup = mapTools;
/**
* The ngeo feature overlay manager service
* @type {ngeo.FeatureOverlayMgr}
*/
const ngeoFeatureOverlayMgr = $injector.get('ngeoFeatureOverlayMgr');
ngeoFeatureOverlayMgr.init(this.map);
/**
* The ngeo ToolActivate manager service.
* @type {ngeo.ToolActivateMgr}
*/
const ngeoToolActivateMgr = $injector.get('ngeoToolActivateMgr');
const queryToolActivate = new ngeo.ToolActivate(this, 'queryActive');
ngeoToolActivateMgr.registerTool(mapTools, queryToolActivate, true);
const measurePointActivate = new ngeo.ToolActivate(this, 'measurePointActive');
ngeoToolActivateMgr.registerTool(mapTools, measurePointActivate, false);
const measureLengthActivate = new ngeo.ToolActivate(this, 'measureLengthActive');
ngeoToolActivateMgr.registerTool(mapTools, measureLengthActivate, false);
const drawFeatureActivate = new ngeo.ToolActivate(this, 'drawFeatureActive');
ngeoToolActivateMgr.registerTool(mapTools, drawFeatureActivate, false);
const drawProfilePanelActivate = new ngeo.ToolActivate(this, 'drawProfilePanelActive');
ngeoToolActivateMgr.registerTool(mapTools, drawProfilePanelActivate, false);
const printPanelActivate = new ngeo.ToolActivate(this, 'printPanelActive');
ngeoToolActivateMgr.registerTool(mapTools, printPanelActivate, false);
$scope.$root.$on(gmf.ThemeManagerEventType.THEME_NAME_SET, (event, name) => {
this.gmfThemes_.getThemeObject(name).then((theme) => {
this.setDefaultBackground_(theme);
});
});
/**
* Ngeo create popup factory
* @type {ngeo.CreatePopup}
*/
const ngeoCreatePopup = $injector.get('ngeoCreatePopup');
// Static "not used" functions should be in the window because otherwise
// closure remove them. "export" tag doens't work on static function below,
// we "export" them as externs in the gmfx options file.
const gmfx = window.gmfx || {};
/**
* @export
*/
window.gmfx = gmfx;
/**
* Static function to create a popup with an iframe.
* @param {string} url an url.
* @param {string} title (text).
* @param {string=} opt_width CSS width.
* @param {string=} opt_height CSS height.
* @export
*/
gmfx.openIframePopup = function(url, title, opt_width, opt_height) {
const popup = ngeoCreatePopup();
popup.setUrl(`${url}`);
gmfx.openPopup_(popup, title, opt_width, opt_height);
};
/**
* Static function to create a popup with html content.
* @param {string} content (text or html).
* @param {string} title (text).
* @param {string=} opt_width CSS width.
* @param {string=} opt_height CSS height.
* @export
*/
gmfx.openTextPopup = function(content, title, opt_width, opt_height) {
const popup = ngeoCreatePopup();
popup.setContent(`${content}`, true);
gmfx.openPopup_(popup, title, opt_width, opt_height);
};
/**
* @param {ngeo.Popup!} popup a ngeoPopup.
* @param {string} title (text).
* @param {string=} opt_width CSS width.
* @param {string=} opt_height CSS height.
*/
gmfx.openPopup_ = function(popup, title, opt_width, opt_height) {
if (opt_width) {
popup.setWidth(`${opt_width}`);
}
if (opt_height) {
popup.setHeight(`${opt_height}`);
}
popup.setTitle(`${title}`);
popup.setAutoDestroy(true);
popup.setOpen(true);
};
/**
* Whether to update the size of the map on browser window resize.
* @type {boolean}
* @export
*/
this.manageResize = false;
/**
* The duration (milliseconds) of the animation that may occur on the div
* containing the map. Used to smoothly resize the map while the animation
* is in progress.
* @type {number|undefined}
* @export
*/
this.resizeTransition;
const cgxp = window.cgxp || {};
/**
* @export
*/
window.cgxp = cgxp;
/**
* @export
*/
cgxp.tools = window.cgxp.tools || {};
/**
* Static function to create a popup with an iframe.
* @param {string} url an url.
* @param {string} title (text).
* @param {string=} opt_width CSS width.
* @param {string=} opt_height CSS height.
* @export
*/
cgxp.tools.openInfoWindow = function(url, title, opt_width, opt_height) {
gmfx.openIframePopup(url, title, opt_width, opt_height);
};
};
/**
* @param {Array.<ol.layer.Base>} layers Layers list.
* @param {Array.<string>} labels default_basemap list.
* @return {ol.layer.Base} layer or null
*/
gmf.AbstractController.getLayerByLabels = function(layers, labels) {
if (labels && labels.length > 0) {
return ol.array.find(layers, layer => layer.get('label') === labels[0]);
}
return null;
};
/**
* @param {string} lang Language code.
* @export
*/
gmf.AbstractController.prototype.switchLanguage = function(lang) {
goog.asserts.assert(lang in this.langUrls);
this.gettextCatalog.setCurrentLanguage(lang);
this.gettextCatalog.loadRemote(this.langUrls[lang]);
this.tmhDynamicLocale.set(lang);
this['lang'] = lang;
};
/**
*/
gmf.AbstractController.prototype.initLanguage = function() {
this.$scope.$watch(() => this['lang'], (newValue) => {
this.stateManager.updateState({
'lang': newValue
});
});
const browserLanguage = /** @type {string|undefined} */
(this.getBrowserLanguage(Object.keys(this.langUrls)));
const urlLanguage = /** @type {string|undefined} */
(this.stateManager.getInitialValue('lang'));
if (urlLanguage !== undefined && urlLanguage in this.langUrls) {
this.switchLanguage(urlLanguage);
return;
} else if (browserLanguage !== undefined && browserLanguage in this.langUrls) {
this.switchLanguage(browserLanguage);
return;
} else {
// if there is no information about language preference,
// fallback to default language
this.switchLanguage(this.defaultLang);
return;
}
};
/**
* @param {gmfThemes.GmfTheme} theme Theme.
* @private
*/
gmf.AbstractController.prototype.setDefaultBackground_ = function(theme) {
this.gmfThemes_.getBgLayers().then((layers) => {
let layer;
// get the background from the permalink
layer = this.permalink_.getBackgroundLayer(layers);
if (!layer && this.gmfUser.functionalities) {
// get the background from the user settings
layer = gmf.AbstractController.getLayerByLabels(layers, this.gmfUser.functionalities.default_basemap);
}
if (!layer && theme) {
// get the background from the theme
layer = gmf.AbstractController.getLayerByLabels(layers, theme.functionalities.default_basemap);
}
if (!layer) {
// fallback to the layers list, use the second one because the first is the blank layer.
layer = layers[layers.length > 1 ? 1 : 0];
}
goog.asserts.assert(layer);
this.backgroundLayerMgr_.set(this.map, layer);
});
};
/**
* @param {string} fallbackThemeName fallback theme name.
* @private
*/
gmf.AbstractController.prototype.updateCurrentTheme_ = function(fallbackThemeName) {
this.gmfThemes_.getThemesObject().then((themes) => {
const themeName = this.permalink_.defaultThemeNameFromFunctionalities();
if (themeName) {
const theme = gmf.Themes.findThemeByName(themes, /** @type {string} */ (themeName));
if (theme) {
this.gmfThemeManager.addTheme(theme, true);
}
} else {
this.gmfThemeManager.setThemeName(fallbackThemeName);
}
});
};
gmf.module.controller('AbstractController', gmf.AbstractController);