/
getWMSFeatureComponent.js
160 lines (143 loc) · 5.21 KB
/
getWMSFeatureComponent.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
// 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 gmfObjecteditingQuery from 'gmf/objectediting/Query';
import {listen, unlistenByKey} from 'ol/events';
import MapBrowserEvent from 'ol/MapBrowserEvent';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('gmfObjecteditingGetWMSFeatureComponent', [gmfObjecteditingQuery.name]);
/**
* When activated, this directive registers clicks on an OL3 map and use the
* clicked coordinate to fetch a feature using the ObjectEditing query service.
* A feature returned is pushed to a collection.
*
* Example:
*
* <gmf-objecteditinggetwmsfeature
* gmf-objecteditinggetwmsfeature-active="ctrl.active"
* gmf-objecteditinggetwmsfeature-features="ctrl.features"
* gmf-objecteditinggetwmsfeature-layerinfo="ctrl.layerInfo"
* gmf-objecteditinggetwmsfeature-map="::ctrl.map">
* </gmf-objecteditinggetwmsfeature>
*
* @htmlAttribute {boolean} gmf-objecteditinggetwmsfeature-active Whether the
* directive is active or not.
* @htmlAttribute {import('ol/Collection').default} gmf-objecteditinggetwmsfeature-features
* The collection of features where to add those created by this directive.
* @htmlAttribute {import('gmf/objectediting/toolsComponent').ObjectEditingQueryableLayerInfo}
* gmf-objecteditinggetwmsfeature-layerinfo Queryable layer info.
* @htmlAttribute {import('ol/Map').default} gmf-objecteditinggetwmsfeature-map The map.
* @returns {angular.IDirective} The directive specs.
* @ngInject
* @ngdoc directive
* @ngname gmfObjecteditinggetwmsfeature
*/
function objectEditingGetWMSFeatureComponent() {
return {
controller: 'gmfObjecteditinggetwmsfeatureController',
scope: {
'active': '=gmfObjecteditinggetwmsfeatureActive',
'features': '<gmfObjecteditinggetwmsfeatureFeatures',
'layerInfo': '=gmfObjecteditinggetwmsfeatureLayerinfo',
'map': '<gmfObjecteditinggetwmsfeatureMap',
},
bindToController: true,
};
}
myModule.directive('gmfObjecteditinggetwmsfeature', objectEditingGetWMSFeatureComponent);
/**
* @param {angular.IScope} $scope Scope.
* @param {import('gmf/objectediting/Query').ObjectEditingQuery} gmfObjectEditingQuery GMF ObjectEditing
* query service.
* @class
* @hidden
* @ngInject
* @ngdoc controller
* @ngname GmfObjecteditinggetwmsfeatureController
*/
export function Controller($scope, gmfObjectEditingQuery) {
// Scope properties
/**
* @type {boolean}
*/
this.active = false;
$scope.$watch(() => this.active, this.handleActiveChange_.bind(this));
/**
* @type {?import('ol/Collection').default<import('ol/Feature').default<import('ol/geom/Geometry').default>>}
*/
this.features = null;
/**
* @type {?import('gmf/objectediting/toolsComponent').ObjectEditingQueryableLayerInfo}
*/
this.layerInfo = null;
/**
* @type {?import('ol/Map').default}
*/
this.map = null;
// Injected properties
/**
* @type {import('gmf/objectediting/Query').ObjectEditingQuery}
*/
this.gmfObjectEditingQuery_ = gmfObjectEditingQuery;
/**
* @type {import('ol/events').EventsKey[]}
*/
this.listenerKeys_ = [];
}
/**
* @param {boolean} active Active.
*/
Controller.prototype.handleActiveChange_ = function (active) {
if (!this.map) {
throw new Error('Missing map');
}
if (active) {
this.listenerKeys_.push(listen(this.map, 'click', this.handleMapClick_, this));
} else {
this.listenerKeys_.forEach(unlistenByKey);
}
};
/**
* @param {Event|import('ol/events/Event').default} evt Event.
*/
Controller.prototype.handleMapClick_ = function (evt) {
if (evt instanceof MapBrowserEvent) {
if (!this.map) {
throw new Error('Missing map');
}
if (!this.layerInfo) {
throw new Error('Missing layerInfo');
}
this.gmfObjectEditingQuery_.getFeatureInfo(this.layerInfo, evt.coordinate, this.map).then((feature) => {
if (feature) {
if (!this.features) {
throw new Error('Missing features');
}
this.features.push(feature);
}
});
}
};
myModule.controller('gmfObjecteditinggetwmsfeatureController', Controller);
export default myModule;