-
Notifications
You must be signed in to change notification settings - Fork 87
/
base-layer.js
290 lines (244 loc) · 7.13 KB
/
base-layer.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
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import Component from '@glimmer/component';
import { scheduleOnce } from '@ember/runloop';
import { classify } from '@ember/string';
/* global L */
const leaf = typeof L === 'undefined' ? {} : L;
/**
* The base class for all ember-leaflet layer components. It contains common
* abstractions used on all layers, including setting event listeners,
* gathering init options, etc.
*
* It is meant to be subclassed with the `createLayer` method being
* mandatory to implement.
*
* @class BaseLayer
* @uses Leaflet
*/
export default class BaseLayer extends Component {
L = leaf;
@service fastboot;
leafletOptions = [
/**
* By default the layer will be added to the map's overlay pane. Overriding this option will
* cause the layer to be placed on another pane by default.
*
* @argument pane
* @type {String}
*/
'pane',
/**
* String to be shown in the attribution control, e.g. "© OpenStreetMap contributors". It describes the layer
* data and is often a legal obligation towards copyright holders and tile providers.
*
* @argument attribution
* @type {String}
*/
'attribution'
];
leafletEvents = [
/**
* Fired after the layer is added to a map.
*
* @argument onAdd
* @type {Function}
*/
'add',
/**
* Fired after the layer is removed from a map.
*
* @argument onRemove
* @type {Function}
*/
'remove',
/**
* Fired when a popup bound to this layer is opened.
*
* @argument onPopupopen
* @type {Function}
*/
'popupopen',
/**
* Fired when a popup bound to this layer is closed.
*
* @argument onPopupclose
* @type {Function}
*/
'popupclose',
/**
* Fired when a tooltip bound to this layer is opened.
*
* @argument onTooltipopen
* @type {Function}
*/
'tooltipopen',
/**
* Fired when a tooltip bound to this layer is closed.
*
* @argument onTooltipclose
* @type {Function}
*/
'tooltipclose'
];
leafletRequiredOptions = [];
leafletStyleProperties = [];
// This is an array that describes how a component's arguments
// relate to leaflet methods and their parameters.
// E.g: Changing the tile layer's `@url` should invoke the layer's `setUrl` method
// with the new `@url` value
leafletDescriptors = [];
// This array allows subclasses to avoid declaring a template
// if the sole purpose is to yield additional components, typical in addons
componentsToYield = [];
@action
mergeComponents(obj) {
if (!this.mergedComponents) {
this.mergedComponents = obj;
} else {
Object.assign(this.mergedComponents, obj);
}
}
createLayer() {
assert("BaseLayer's `createLayer` should be overriden.");
}
didCreateLayer() {}
willDestroyLayer() {}
/*
* Method called by parent when the layer needs to setup
*/
@action
didInsertParent(element) {
// Check for fastBoot
if (this.fastboot?.isFastBoot) {
return;
}
this._layer = this.createLayer(element);
this._addEventListeners();
if (this.args.parent) {
this.addToContainer();
}
this.didCreateLayer();
}
/*
* Default logic for adding the layer to the container
*/
addToContainer() {
this.args.parent._layer.addLayer(this._layer);
}
/*
* Method called by parent when the layer needs to teardown
*/
@action
willDestroyParent() {
// Check for fastBoot
if (this.fastboot?.isFastBoot) {
return;
}
this.willDestroyLayer();
this._removeEventListeners();
if (this.args.parent && this._layer) {
this.removeFromContainer();
}
delete this._layer;
}
/*
* Default logic for removing the layer from the container
*/
removeFromContainer() {
this.args.parent._layer.removeLayer(this._layer);
}
get options() {
let options = {};
for (let optionName of this.leafletOptions) {
if (this.args[optionName] !== undefined) {
options[optionName] = this.args[optionName];
}
}
return options;
}
get requiredOptions() {
let options = [];
for (let optionName of this.leafletRequiredOptions) {
let value = this.args[optionName] || this[optionName];
assert(`\`${optionName}\` is a required option but its value was \`${value}\``, value);
options.push(value);
}
return options;
}
get usedLeafletEvents() {
return this.leafletEvents.filter((eventName) => {
let methodName = `_${eventName}`;
let actionName = `on${classify(eventName)}`;
return this[methodName] !== undefined || this.args[actionName] !== undefined;
});
}
_addEventListeners() {
this._eventHandlers = {};
for (let eventName of this.usedLeafletEvents) {
let actionName = `on${classify(eventName)}`;
let methodName = `_${eventName}`;
// create an event handler that runs the function inside an event loop.
this._eventHandlers[eventName] = function (e) {
let fn = () => {
// try to invoke/send an action for this event
if (typeof this.args[actionName] === 'function') {
this.args[actionName](e);
}
// allow classes to add custom logic on events as well
if (typeof this[methodName] === 'function') {
this[methodName](e);
}
};
scheduleOnce('actions', this, fn);
};
this._layer.addEventListener(eventName, this._eventHandlers[eventName], this);
}
}
_removeEventListeners() {
if (this._eventHandlers) {
for (let eventName of this.usedLeafletEvents) {
this._layer.removeEventListener(eventName, this._eventHandlers[eventName], this);
delete this._eventHandlers[eventName];
}
}
}
get leafletDescriptorsProps() {
return this.leafletDescriptors.map((d) => {
return typeof d === 'string' ? d.split(':')[0] : d.arg;
});
}
@action
updateOption(arg, [value]) {
// find the corresponding leaflet descriptor
let descriptor = this.leafletDescriptors.find((d) => {
let descArg = typeof d === 'string' ? d.split(':')[0] : d.arg;
return descArg === arg;
});
if (!descriptor) {
return;
}
if (typeof descriptor === 'string') {
let [property, method, ...params] = descriptor.split(':');
if (!method) {
method = `set${classify(property)}`;
}
assert(`Leaflet layer must have a ${method} function.`, !!this._layer[method]);
let methodParams = params.map((p) => this.args[p] || this[p]);
this._layer[method].call(this._layer, value, ...methodParams);
} else {
let { updateFn, params = [] } = descriptor;
let methodParams = params.map((p) => this.args[p] || this[p]);
updateFn(this._layer, value, ...methodParams);
}
let methodName = `${classify(arg)}_did_change`;
if (typeof this[methodName] === 'function') {
this[methodName](value);
}
}
@action
updateStyleProperty(arg, [value]) {
this._layer.setStyle({ [arg]: value });
}
}