-
Notifications
You must be signed in to change notification settings - Fork 88
/
attributesComponent.js
257 lines (233 loc) · 7.42 KB
/
attributesComponent.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
// The MIT License (MIT)
//
// Copyright (c) 2016-2022 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} from 'ol/events';
import ngeoMiscEventHelper from 'ngeo/misc/EventHelper';
import ngeoMiscDatetimepickerComponent from 'ngeo/misc/datetimepickerComponent';
import {ObjectEvent} from 'ol/Object';
import './editing.css';
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoAttributes', [
ngeoMiscDatetimepickerComponent.name,
ngeoMiscEventHelper.name,
]);
myModule.run(
/**
* @ngInject
* @param {angular.ITemplateCacheService} $templateCache
*/
($templateCache) => {
// @ts-ignore: webpack
$templateCache.put('ngeo/editing/attributescomponent', require('./attributescomponent.html'));
}
);
myModule.value(
'ngeoAttributesTemplateUrl',
/**
* @param {angular.IAttributes} $attrs Attributes.
* @returns {string} The template url.
*/
($attrs) => {
const templateUrl = $attrs.ngeoAttributesTemplateUrl;
return templateUrl !== undefined ? templateUrl : 'ngeo/editing/attributescomponent';
}
);
/**
* @param {angular.IAttributes} $attrs Attributes.
* @param {function(angular.IAttributes): string} ngeoAttributesTemplateUrl Template function.
* @returns {string} Template URL.
* @ngInject
* @private
* @hidden
*/
function ngeoAttributesTemplateUrl($attrs, ngeoAttributesTemplateUrl) {
return ngeoAttributesTemplateUrl($attrs);
}
/**
* Component used to render the attributes of a feature into a form.
* Example:
*
* <ngeo-attributes
* ngeo-attributes-attributes="::ctrl.attributes"
* ngeo-attributes-disabled="ctrl.attributesDisabled"
* ngeo-attributes-feature="::ctrl.feature">
* </ngeo-attributes>
*
* @htmlAttribute {import('ngeo/format/Attribute').Attribute[]} ngeo-attributes-attributes The
* list of attributes to use.
* @htmlAttribute {boolean} ngeo-attributes-disabled Whether the fieldset should
* be disabled or not.
* @htmlAttribute {import('ol/Feature').default<import('ol/geom/Geometry').default>} ngeo-attributes-feature The feature.
* @ngdoc component
* @ngname ngeoAttributes
*/
const editingAttributeComponent = {
controller: 'ngeoAttributesController as attrCtrl',
bindings: {
'attributes': '=ngeoAttributesAttributes',
'disabled': '<ngeoAttributesDisabled',
'feature': '=ngeoAttributesFeature',
},
require: {
'form': '^',
},
templateUrl: ngeoAttributesTemplateUrl,
};
myModule.component('ngeoAttributes', editingAttributeComponent);
/**
* @param {angular.IScope} $scope Angular scope.
* @param {import('ngeo/misc/EventHelper').EventHelper} ngeoEventHelper Ngeo event helper service
* @class
* @hidden
* @ngInject
* @ngdoc controller
* @ngname ngeoAttributesController
*/
export function Controller($scope, ngeoEventHelper) {
/**
* The list of attributes to create the form with.
*
* @type {import('ngeo/format/Attribute').Attribute[]}
*/
this.attributes = [];
/**
* Whether the fieldset should be disabled or not.
*
* @type {boolean}
*/
this.disabled = false;
/**
* The feature containing the values.
*
* @type {?import('ol/Feature').default<import('ol/geom/Geometry').default>}
*/
this.feature = null;
/**
* The properties bound to the form, initialized with the inner properties
* of the feature.
*
* @type {Object<string, *>}
*/
this.properties = {};
/**
* @type {angular.IScope}
*/
this.scope_ = $scope;
/**
* @type {import('ngeo/misc/EventHelper').EventHelper}
*/
this.ngeoEventHelper_ = ngeoEventHelper;
/**
* While changes happen from the form (from the template), they are applied
* to the feature inner properties. The 'propertychange' event registered
* above does the opposite, i.e. it listens to the feature inner properties
* changes and apply them to the form. To prevent circular issues, while
* applying changes coming from the form, this flag is set. While set, changes
* from the feature inner properties are ignored.
*
* @type {boolean}
*/
this.updating_ = false;
}
/**
* Initialize the component.
*/
Controller.prototype.$onInit = function () {
if (!this.feature) {
throw new Error('Missing feature');
}
this.properties = this.feature.getProperties();
// Listen to the feature inner properties change and apply them to the form
const uid = olUtilGetUid(this);
// Set properties before starting to listen to changes to avoid multi AngularJS apply error.
this.attributes.forEach((attribute) => {
this.sanitize_(attribute);
});
this.ngeoEventHelper_.addListenerKey(
uid,
listen(this.feature, 'propertychange', this.handleFeaturePropertyChange_, this)
);
};
/**
* Called when an input node value changes
*
* @param {string} name Attribute name
*/
Controller.prototype.handleInputChange = function (name) {
if (!this.properties) {
throw new Error('Missing properties');
}
if (!this.feature) {
throw new Error('Missing feature');
}
this.updating_ = true;
const attribute = this.attributes.find((attr) => attr.name === name);
this.sanitize_(attribute);
this.updating_ = false;
};
/**
* Never keep a undefined values, use null.
* On boolean, replace null by false.
* On date, datetime and time replace empty string by null.
*
* @param {import('ngeo/format/Attribute.js').Attribute} attribute
*/
Controller.prototype.sanitize_ = function (attribute) {
let value = this.properties[attribute.name];
if (value === undefined) {
value = null;
}
if (attribute.type === 'boolean' && value === null) {
value = false;
} else if (attribute.format && !value) {
// Case of date, datetime or time.
// Shouldn't be set to an empty string
value = null;
}
this.properties[attribute.name] = value;
this.feature.set(attribute.name, value);
};
/**
* Cleanup event listeners.
*/
Controller.prototype.$onDestroy = function () {
const uid = olUtilGetUid(this);
this.ngeoEventHelper_.clearListenerKey(uid);
};
/**
* @param {Event|import('ol/events/Event').default} evt Event.
*/
Controller.prototype.handleFeaturePropertyChange_ = function (evt) {
if (evt instanceof ObjectEvent) {
if (this.updating_) {
return;
}
// @ts-ignore
this.properties[evt.key] = evt.target.get(evt.key);
this.scope_.$apply();
}
};
myModule.controller('ngeoAttributesController', Controller);
export default myModule;