-
Notifications
You must be signed in to change notification settings - Fork 88
/
featureStyleComponent.js
333 lines (288 loc) · 10.1 KB
/
featureStyleComponent.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
// 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 {getUid as olUtilGetUid} from 'ol/util';
import {listen, unlistenByKey} from 'ol/events';
import ngeoFormatFeatureProperties from 'ngeo/format/FeatureProperties';
import ngeoMiscColorpickerComponent from 'ngeo/misc/colorpickerComponent';
import ngeoMiscFeatureHelper, {ArrowDirections, ArrowPositions} from 'ngeo/misc/FeatureHelper';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('gmfDrawingFeatureStyle', [
ngeoMiscColorpickerComponent.name,
ngeoMiscFeatureHelper.name,
]);
myModule.run(
/**
* @ngInject
* @param {angular.ITemplateCacheService} $templateCache
*/
($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('gmf/drawing/featureStyleComponent', require('./featureStyleComponent.html'));
}
);
/**
* Directive used to set the style of a vector feature. The options depend
* on the type of geometry.
* Example:
*
* <gmf-featurestyle
* gmf-featurestyle-feature="ctrl.selectedFeature">
* </gmf-featurestyle>
*
* @htmlAttribute {import('ol/Feature').default<import('ol/geom/Geometry').default>} gmf-featurestyle-feature The feature.
* @returns {angular.IDirective} The directive specs.
* @ngInject
* @ngdoc directive
* @ngname gmfFeaturestyle
*/
function drawingDrawFeatureComponent() {
return {
controller: 'GmfFeaturestyleController as fsCtrl',
scope: {
'feature': '=gmfFeaturestyleFeature',
},
bindToController: true,
templateUrl: 'gmf/drawing/featureStyleComponent',
};
}
myModule.directive('gmfFeaturestyle', drawingDrawFeatureComponent);
/**
* @param {angular.IScope} $scope Angular scope.
* @param {import('ngeo/misc/FeatureHelper').FeatureHelper} ngeoFeatureHelper Gmf feature helper service.
* @class
* @hidden
* @ngInject
* @ngdoc controller
* @ngname GmfFeaturestyleController
*/
export function Controller($scope, ngeoFeatureHelper) {
/**
* @type {string}
*/
this.uid = olUtilGetUid(this);
/**
* @type {?import('ol/Feature').default<import('ol/geom/Geometry').default>}
*/
this.feature = null;
/**
* @type {angular.IScope}
*/
this.scope_ = $scope;
/**
* @type {import('ngeo/misc/FeatureHelper').FeatureHelper}
*/
this.featureHelper_ = ngeoFeatureHelper;
/**
* @type {string|undefined}
*/
this.color = undefined;
/**
* @type {string|undefined}
*/
this.label = undefined;
/**
* Value of the measure (line length, area, ...).
*
* @type {string|undefined}
*/
this.measure = undefined;
/**
* Arrow possible direction on segments of lines.
*
* @enum {string}
*/
this.arrowDirections = ArrowDirections;
/**
* Arrow possible position on lines.
*
* @enum {string}
*/
this.arrowPositions = ArrowPositions;
$scope.$watch(() => this.color, this.handleColorSet_.bind(this));
/**
* @type {import('ol/events').EventsKey[]}
*/
this.featureListenerKeys_ = [];
/**
* Type of the geometry (text, point, circle, line, multiline, ...).
*
* @type {string|undefined}
*/
this.type;
$scope.$watch(() => this.feature, this.handleFeatureSet_.bind(this));
}
/**
* Called when a new feature is set, which can also be null.
*
* @param {?import('ol/Feature').default<import('ol/geom/Geometry').default>} newFeature New feature or null value.
* @param {?import('ol/Feature').default<import('ol/geom/Geometry').default>} previousFeature Previous feature or null value.
*/
Controller.prototype.handleFeatureSet_ = function (newFeature, previousFeature) {
const keys = this.featureListenerKeys_;
if (previousFeature) {
keys.forEach(unlistenByKey);
keys.length = 0;
this.type = undefined;
this.color = undefined;
this.measure = undefined;
this.label = undefined;
}
if (newFeature) {
[
ngeoFormatFeatureProperties.ANGLE,
ngeoFormatFeatureProperties.COLOR,
ngeoFormatFeatureProperties.NAME,
ngeoFormatFeatureProperties.SHOW_LABEL,
ngeoFormatFeatureProperties.OPACITY,
ngeoFormatFeatureProperties.SHOW_MEASURE,
ngeoFormatFeatureProperties.SIZE,
ngeoFormatFeatureProperties.STROKE,
ngeoFormatFeatureProperties.ARROW_DIRECTION,
ngeoFormatFeatureProperties.ARROW_POSITION,
].forEach((propName) => {
keys.push(listen(newFeature, `change:${propName}`, this.handleFeatureChange_, this));
});
const geometry = newFeature.getGeometry();
if (!geometry) {
throw new Error('Missing geometry');
}
keys.push(listen(geometry, 'change', this.handleGeometryChange_, this));
this.type = this.featureHelper_.getType(newFeature);
this.color = this.featureHelper_.getColorProperty(newFeature);
this.measure = this.featureHelper_.getMeasure(newFeature);
}
};
/**
* @param {string|undefined} newColor Color.
*/
Controller.prototype.handleColorSet_ = function (newColor) {
if (this.feature && newColor) {
const currentColor = this.feature.get(ngeoFormatFeatureProperties.COLOR);
if (currentColor !== newColor) {
this.feature.set(ngeoFormatFeatureProperties.COLOR, newColor);
}
}
};
/**
* @param {number|undefined} value A name value to set or undefined to get.
* @returns {number} The angle of the feature.
*/
Controller.prototype.getSetAngle = function (value) {
return /** @type {number} */ (this.getSetProperty_(ngeoFormatFeatureProperties.ANGLE, value));
};
/**
* @param {string|undefined} value A name value to set or undefined to get.
* @returns {string} The name of the feature.
*/
Controller.prototype.getSetName = function (value) {
return /** @type {string} */ (this.getSetProperty_(ngeoFormatFeatureProperties.NAME, value));
};
/**
* @param {boolean|undefined} value A value to set or undefined for the
* purpose of showing the attribute labels or not.
* @returns {boolean} Whether to show the labels or not.
*/
Controller.prototype.getSetShowLabel = function (value) {
return /** @type {boolean} */ (this.getSetProperty_(ngeoFormatFeatureProperties.SHOW_LABEL, value));
};
/**
* @param {number|undefined} value A stroke value to set or undefined to get.
* @returns {number} The stroke of the feature.
*/
Controller.prototype.getSetOpacity = function (value) {
return /** @type {number} */ (this.getSetProperty_(ngeoFormatFeatureProperties.OPACITY, value));
};
/**
* @param {boolean|undefined} value A value to set or undefined to get for the
* purpose of showing the geometry measurements or not.
* @returns {boolean} Whether to show the measurements or not.
*/
Controller.prototype.getSetShowMeasure = function (value) {
return /** @type {boolean} */ (this.getSetProperty_(ngeoFormatFeatureProperties.SHOW_MEASURE, value));
};
/**
* @param {number|undefined} value A size value to set or undefined to get.
* @returns {number} The size of the feature.
*/
Controller.prototype.getSetSize = function (value) {
return /** @type {number} */ (this.getSetProperty_(ngeoFormatFeatureProperties.SIZE, value));
};
/**
* @param {number|undefined} value A stroke value to set or undefined to get.
* @returns {number} The stroke of the feature.
*/
Controller.prototype.getSetStroke = function (value) {
return /** @type {number} */ (this.getSetProperty_(ngeoFormatFeatureProperties.STROKE, value));
};
/**
* @param {number|undefined} value A arrow-direction value to set or undefined to get.
* @returns {number} The arrow-direction of the feature.
*/
Controller.prototype.getSetArrowDirection = function (value) {
return /** @type {number} */ (this.getSetProperty_(ngeoFormatFeatureProperties.ARROW_DIRECTION, value));
};
/**
* @param {number|undefined} value A arrow-position value to set or undefined to get.
* @returns {number} The arrow-position of the feature.
*/
Controller.prototype.getSetArrowPosition = function (value) {
return /** @type {number} */ (this.getSetProperty_(ngeoFormatFeatureProperties.ARROW_POSITION, value));
};
/**
* @param {string} key The property name.
* @param {boolean|number|string|undefined} value A value to set or undefined
* to get.
* @returns {boolean|number|string} The property value of the feature.
*/
Controller.prototype.getSetProperty_ = function (key, value) {
if (!this.feature) {
throw new Error('Missing feature');
}
if (value !== undefined) {
this.feature.set(key, value);
}
return /** @type {boolean|number|string} */ (this.feature.get(key));
};
Controller.prototype.handleFeatureChange_ = function () {
const feature = this.feature;
if (!feature) {
return;
}
this.featureHelper_.setStyle(feature, true);
};
Controller.prototype.handleGeometryChange_ = function () {
if (!this.feature) {
throw new Error('Missing feature');
}
this.measure = this.featureHelper_.getMeasure(this.feature);
const showMeasure = this.featureHelper_.getShowMeasureProperty(this.feature);
const showArrow = this.featureHelper_.getShowArrowsProperty(this.feature);
if (showMeasure || showArrow) {
this.handleFeatureChange_();
}
this.scope_.$apply();
};
myModule.controller('GmfFeaturestyleController', Controller);
export default myModule;