@nextgis/ngw-map 1.19.2 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,669 +1,644 @@
1
- /** Bundle of @nextgis/ngw-map; version: 1.19.2; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-map; version: 2.0.0-alpha.0; author: NextGIS */
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
6
  var events = require('events');
7
7
  var CancelablePromise = require('@nextgis/cancelable-promise');
8
+ var icons = require('@nextgis/icons');
9
+ var ngwKit = require('@nextgis/ngw-kit');
8
10
  var utils = require('@nextgis/utils');
9
11
  var webmap = require('@nextgis/webmap');
10
- var ngwKit = require('@nextgis/ngw-kit');
11
- var icons = require('@nextgis/icons');
12
12
  var NgwConnector = require('@nextgis/ngw-connector');
13
13
  var qmsKit = require('@nextgis/qms-kit');
14
14
 
15
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
16
-
17
- var CancelablePromise__default = /*#__PURE__*/_interopDefaultLegacy(CancelablePromise);
18
- var NgwConnector__default = /*#__PURE__*/_interopDefaultLegacy(NgwConnector);
19
-
20
15
  function appendNgwResources(options, resource, defOptions, overwriteOptions) {
21
- if (typeof resource === 'number' || typeof resource === 'string') {
22
- resource = Number(resource);
23
- options.push({
24
- ...defOptions,
25
- resource,
26
- });
27
- }
28
- else if (Array.isArray(resource)) {
29
- const [resourceId, id] = resource;
30
- options.push({
31
- ...defOptions,
32
- resource: resourceId,
33
- id,
34
- ...overwriteOptions,
35
- });
36
- }
37
- else if (typeof resource === 'object') {
38
- options.push({ ...defOptions, ...resource, ...overwriteOptions });
39
- }
16
+ if (typeof resource === "number" || typeof resource === "string") {
17
+ resource = Number(resource);
18
+ options.push({
19
+ ...defOptions,
20
+ resource
21
+ });
22
+ } else if (Array.isArray(resource)) {
23
+ const [resourceId, id] = resource;
24
+ options.push({
25
+ ...defOptions,
26
+ resource: resourceId,
27
+ id,
28
+ ...overwriteOptions
29
+ });
30
+ } else if (typeof resource === "object") {
31
+ options.push({ ...defOptions, ...resource, ...overwriteOptions });
32
+ }
40
33
  }
41
34
 
42
35
  const OPTIONS = {
43
- target: 'map',
44
- baseUrl: '',
45
- whitlabel: false,
46
- controls: webmap.getDefaultControls(),
47
- controlsOptions: {
48
- ZOOM: { position: 'top-left' },
49
- ATTRIBUTION: {
50
- position: 'bottom-right',
51
- customAttribution: [
52
- '<a href="https://nextgis.com" target="_blank">©NextGIS</a>',
53
- ],
54
- },
55
- },
56
- pixelRadius: 10,
36
+ target: "map",
37
+ baseUrl: "",
38
+ whitlabel: false,
39
+ controls: webmap.getDefaultControls(),
40
+ controlsOptions: {
41
+ ZOOM: { position: "top-left" },
42
+ ATTRIBUTION: {
43
+ position: "bottom-right",
44
+ customAttribution: [
45
+ '<a href="https://nextgis.com" target="_blank">\xA9NextGIS</a>'
46
+ ]
47
+ }
48
+ },
49
+ pixelRadius: 10
57
50
  };
58
51
  function prepareWebMapOptions(options) {
59
- const kits = [new qmsKit.QmsKit()];
60
- if (options.starterKits) {
61
- options.starterKits.forEach((x) => {
62
- kits.push(x);
63
- });
64
- }
65
- if (!options.connector) {
66
- options.connector = new NgwConnector__default({
67
- baseUrl: options.baseUrl || '',
68
- auth: options.auth,
69
- });
70
- }
71
- else if (options.connector) {
72
- options.baseUrl = options.connector.options.baseUrl;
73
- }
74
- options = utils.deepmerge(OPTIONS, options);
75
- if (!options.center && !options.bounds) {
76
- options.bounds = [-179, -90, 180, 90];
77
- // options.maxBounds = options.bounds;
78
- }
79
- if (options.connector) {
80
- kits.push(new ngwKit.NgwKit({
81
- connector: options.connector,
82
- auth: options.auth,
83
- }));
84
- }
85
- options = {
86
- ...options,
87
- starterKits: kits,
88
- create: false,
89
- };
90
- return options;
52
+ const kits = [new qmsKit.QmsKit()];
53
+ if (options.starterKits) {
54
+ options.starterKits.forEach((x) => {
55
+ kits.push(x);
56
+ });
57
+ }
58
+ if (!options.connector) {
59
+ options.connector = new NgwConnector({
60
+ baseUrl: options.baseUrl || "",
61
+ auth: options.auth
62
+ });
63
+ } else if (options.connector) {
64
+ options.baseUrl = options.connector.options.baseUrl;
65
+ }
66
+ options = utils.deepmerge(OPTIONS, options);
67
+ if (!options.center && !options.bounds) {
68
+ options.bounds = [-179, -90, 180, 90];
69
+ }
70
+ if (options.connector) {
71
+ kits.push(
72
+ new ngwKit.NgwKit({
73
+ connector: options.connector,
74
+ auth: options.auth
75
+ })
76
+ );
77
+ }
78
+ options = {
79
+ ...options,
80
+ starterKits: kits,
81
+ create: false
82
+ };
83
+ return options;
91
84
  }
92
85
 
93
- /**
94
- * Base class containing the logic of interaction WebMap with NextGIS services.
95
- *
96
- * @example
97
- * ```javascript
98
- * import { NgwMap } from '@nextgis/ngw-map';
99
- * import MapAdapter from '@nextgis/leaflet-map-adapter';
100
- * // styles are not included in the leaflet-map-adapter
101
- * import 'leaflet/dist/leaflet.css';
102
- *
103
- * const ngwMap = new NgwMap({
104
- * mapAdapter: new MapAdapter(),
105
- * target: 'map',
106
- * qmsId: 448,
107
- * baseUrl: 'https://demo.nextgis.com',
108
- * webmapId: 3985
109
- * });
110
- * ```
111
- */
86
+ var __defProp = Object.defineProperty;
87
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
88
+ var __publicField = (obj, key, value) => {
89
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
90
+ return value;
91
+ };
112
92
  class NgwMap extends webmap.WebMap {
113
- constructor(options) {
114
- super(prepareWebMapOptions(options));
115
- this.emitter = new events.EventEmitter();
116
- this._ngwLayers = {};
117
- this._promises = {
118
- select: [],
119
- identify: [],
93
+ constructor(options) {
94
+ super(prepareWebMapOptions(options));
95
+ __publicField(this, "emitter", new events.EventEmitter());
96
+ __publicField(this, "connector");
97
+ __publicField(this, "_ngwLayers", {});
98
+ __publicField(this, "$$selectFromNgwRaster");
99
+ __publicField(this, "$$selectFromNgwVector");
100
+ __publicField(this, "_promises", {
101
+ select: [],
102
+ identify: []
103
+ });
104
+ if (options.connector) {
105
+ this.connector = options.connector;
106
+ }
107
+ this._createWebMap().then(() => {
108
+ const container = this.getContainer();
109
+ if (container) {
110
+ container.classList.add("ngw-map-container");
111
+ }
112
+ if (this.options.whitlabel) {
113
+ this._whiteLabel();
114
+ }
115
+ });
116
+ }
117
+ /**
118
+ * Organized addition to the map design and controls elements,
119
+ * calling `control.onAdd(this.webMap.mapAdapter)`
120
+ * @param control - object with onAdd and onRemove methods
121
+ * or a string value indicating the name of the control installed in the map adapter
122
+ * @param position - position relative to the map angles
123
+ * @param options - initialization parameters if the control is set as a string value
124
+ *
125
+ * @example
126
+ * ```javascript
127
+ * ngwMap.addControl(new CustomControl(), 'bottom-left');
128
+ * ngwMap.addControl('ZOOM', 'top-right')
129
+ * ```
130
+ */
131
+ async addControl(controlDef, position, options) {
132
+ await this.onLoad("controls:create");
133
+ return super.addControl(controlDef, position, options);
134
+ }
135
+ /**
136
+ * Add any (style, vector, webmap) NGW layer by resource definition.
137
+ * @param options - set layer identification parameters and render method.
138
+ *
139
+ * @example
140
+ * ```javascript
141
+ * // Add raster layer resourceId is the style of 4004 layer
142
+ * ngwMap.addNgwLayer({ resource: 4005 });
143
+ * // Add vector data from layer GEOJSON source
144
+ * ngwMap.addNgwLayer({
145
+ * resource: 4038,
146
+ * adapter: 'GEOJSON',
147
+ * adapterOptions: { paint: { color: 'red' } }
148
+ * });
149
+ * ```
150
+ */
151
+ async addNgwLayer(options) {
152
+ await this.onMapLoad();
153
+ const { keyname, resourceId } = options;
154
+ if (keyname || resourceId !== void 0) {
155
+ utils.deprecatedWarn(
156
+ "set `resource` options instead of `keyname` or `resourceId`"
157
+ );
158
+ }
159
+ const resource = options.resource;
160
+ if (!keyname && !resourceId && !resource) {
161
+ throw new Error(
162
+ "resource, resourceId or keyname is required parameter to add NGW layer"
163
+ );
164
+ }
165
+ if (utils.defined(this.options.baseUrl)) {
166
+ try {
167
+ if (utils.defined(this.options.setViewDelay)) {
168
+ options.adapterOptions = options.adapterOptions || {};
169
+ if (!utils.defined(options.adapterOptions.setViewDelay)) {
170
+ options.adapterOptions.setViewDelay = this.options.setViewDelay;
171
+ }
172
+ }
173
+ const adapter = ngwKit.createNgwLayerAdapter(options, this, this.connector);
174
+ const adapterOpts = {
175
+ visibility: true,
176
+ // TODO: do not merge options, use only `adapterOptions`
177
+ ...options,
178
+ ...options.adapterOptions
120
179
  };
121
- if (options.connector) {
122
- this.connector = options.connector;
123
- }
124
- this._createWebMap().then(() => {
125
- const container = this.getContainer();
126
- if (container) {
127
- container.classList.add('ngw-map-container');
128
- }
129
- if (this.options.whitlabel) {
130
- this._whiteLabel();
131
- }
132
- });
133
- }
134
- /**
135
- * Organized addition to the map design and controls elements,
136
- * calling `control.onAdd(this.webMap.mapAdapter)`
137
- * @param control - object with onAdd and onRemove methods
138
- * or a string value indicating the name of the control installed in the map adapter
139
- * @param position - position relative to the map angles
140
- * @param options - initialization parameters if the control is set as a string value
141
- *
142
- * @example
143
- * ```javascript
144
- * ngwMap.addControl(new CustomControl(), 'bottom-left');
145
- * ngwMap.addControl('ZOOM', 'top-right')
146
- * ```
147
- */
148
- async addControl(controlDef, position, options) {
149
- await this.onLoad('controls:create');
150
- return super.addControl(controlDef, position, options);
151
- }
152
- /**
153
- * Add any (style, vector, webmap) NGW layer by resource definition.
154
- * @param options - set layer identification parameters and render method.
155
- *
156
- * @example
157
- * ```javascript
158
- * // Add raster layer resourceId is the style of 4004 layer
159
- * ngwMap.addNgwLayer({ resource: 4005 });
160
- * // Add vector data from layer GEOJSON source
161
- * ngwMap.addNgwLayer({
162
- * resource: 4038,
163
- * adapter: 'GEOJSON',
164
- * adapterOptions: { paint: { color: 'red' } }
165
- * });
166
- * ```
167
- */
168
- async addNgwLayer(options) {
169
- await this.onMapLoad();
170
- // @ts-ignore for backward compatibility
171
- const { keyname, resourceId } = options;
172
- if (keyname || resourceId !== undefined) {
173
- utils.deprecatedWarn('set `resource` options instead of `keyname` or `resourceId`');
174
- }
175
- const resource = options.resource;
176
- if (!keyname && !resourceId && !resource) {
177
- throw new Error('resource, resourceId or keyname is required parameter to add NGW layer');
178
- }
179
- if (utils.defined(this.options.baseUrl)) {
180
- try {
181
- if (utils.defined(this.options.setViewDelay)) {
182
- options.adapterOptions = options.adapterOptions || {};
183
- if (!utils.defined(options.adapterOptions.setViewDelay)) {
184
- options.adapterOptions.setViewDelay = this.options.setViewDelay;
185
- }
186
- }
187
- const adapter = ngwKit.createNgwLayerAdapter(options, this, this.connector);
188
- const adapterOpts = {
189
- visibility: true,
190
- // TODO: do not merge options, use only `adapterOptions`
191
- ...options,
192
- ...options.adapterOptions,
193
- };
194
- const layer = (await this.addLayer(adapter, adapterOpts));
195
- const id = layer && this.getLayerId(layer);
196
- if (layer && id) {
197
- this._ngwLayers[id] = { layer, resourceId: layer.resourceId };
198
- layer.options.name =
199
- layer.options.name ||
200
- (layer.item && layer.item.resource.display_name);
201
- if (layer.options.baselayer) {
202
- const visibleLayerBaseLayer = this.getActiveBaseLayer();
203
- if (visibleLayerBaseLayer) {
204
- return layer;
205
- }
206
- }
207
- }
208
- return layer;
209
- }
210
- catch (er) {
211
- const resId = utils.isObject(resource) && 'id' in resource
212
- ? resource.id
213
- : keyname || resourceId || resource;
214
- console.error(`Can't add NGW layer ${resId}.`, er);
215
- }
216
- }
217
- }
218
- /**
219
- * Pans and zooms the map to the initial position specified in the options
220
- */
221
- fit() {
222
- const { center, zoom, bounds } = this.options;
223
- if (center) {
224
- this.setCenter(center);
225
- if (zoom) {
226
- this.setZoom(zoom);
227
- }
228
- }
229
- else if (bounds) {
230
- this.fitBounds(bounds);
231
- }
232
- }
233
- fetchNgwLayerItem(options) {
234
- return ngwKit.fetchNgwLayerItem({
235
- connector: this.connector,
236
- ...options,
237
- });
238
- }
239
- fetchNgwLayerItems(options) {
240
- return ngwKit.fetchNgwLayerItems({
241
- connector: this.connector,
242
- ...options,
243
- });
244
- }
245
- fetchNgwLayerFeature(options) {
246
- return ngwKit.fetchNgwLayerFeature({
247
- connector: this.connector,
248
- ...options,
249
- });
250
- }
251
- fetchNgwLayerFeatures(options) {
252
- return ngwKit.fetchNgwLayerFeatureCollection({
253
- connector: this.connector,
254
- ...options,
255
- });
256
- }
257
- fetchIdentifyItem(identify, requestOptions) {
258
- const promise = ngwKit.fetchIdentifyItem({
259
- identify,
260
- connector: this.connector,
261
- requestOptions,
262
- // multiple,
263
- });
264
- this._addPromise('identify', promise);
265
- return promise;
266
- }
267
- fetchIdentifyGeoJson(identify, multiple = false) {
268
- const promise = ngwKit.fetchIdentifyGeoJson({
269
- identify,
270
- connector: this.connector,
271
- multiple,
272
- });
273
- if (promise && 'then' in promise) {
274
- this._addPromise('identify', promise);
275
- return promise;
276
- }
277
- else {
278
- return CancelablePromise__default.resolve(promise);
279
- }
280
- }
281
- /**
282
- * @deprecated use {@link fetchIdentifyGeoJson} instead
283
- */
284
- getIdentifyGeoJson(identify, multiple = false) {
285
- return this.fetchIdentifyGeoJson(identify, multiple);
286
- }
287
- async getNgwLayers() {
288
- await this.onLoad();
289
- return this._ngwLayers;
290
- }
291
- async getNgwLayerByResourceId(id) {
292
- for (const n in this._ngwLayers) {
293
- const mem = this._ngwLayers[n];
294
- if (mem.resourceId === id) {
295
- return mem && mem.layer;
296
- }
297
- else if (mem.layer.getIdentificationIds) {
298
- const ids = await mem.layer.getIdentificationIds();
299
- if (ids && ids.some((x) => x === id)) {
300
- return mem.layer;
301
- }
302
- }
303
- if (mem.layer.getDependLayers) {
304
- const dependLayers = mem.layer.getDependLayers();
305
- const dependFit = dependLayers.find((x) => {
306
- return x.item && x.item.parentId === id;
307
- });
308
- if (dependFit) {
309
- return dependFit.layer;
310
- }
311
- }
312
- }
313
- }
314
- /**
315
- * Move map to layer. If the layer is NGW resource, extent will be received from the server
316
- *
317
- * @example
318
- * ```javascript
319
- * const ngwLayer = ngwMap.addNgwLayer({ id: 'ngw_layer_name', resource: 4005 });
320
- * ngwMap.fitLayer(ngwLayer);
321
- * ngwMap.fitLayer('ngw_layer_name');
322
- * ```
323
- */
324
- async fitLayer(layerDef, options) {
325
- let id;
326
- if (typeof layerDef === 'string' || typeof layerDef === 'number') {
327
- id = String(id);
328
- }
329
- else {
330
- id = layerDef.id;
331
- }
332
- const ngwLayer = id && this._ngwLayers[id];
333
- if (ngwLayer) {
334
- if (ngwLayer.layer.getBounds) {
335
- const bounds = await ngwLayer.layer.getBounds();
336
- if (bounds) {
337
- this.fitBounds(bounds, options);
338
- }
339
- }
340
- else {
341
- let item;
342
- if (ngwLayer.layer.item) {
343
- item = ngwLayer.layer.item;
344
- }
345
- else {
346
- const resourceId = ngwLayer.resourceId;
347
- item = await this.connector.getResource(resourceId);
348
- }
349
- if (item) {
350
- ngwKit.fetchNgwExtent({
351
- resourceId: item.resource.id,
352
- connector: this.connector,
353
- }).then((extent) => {
354
- if (extent) {
355
- this.fitBounds(extent, options);
356
- }
357
- });
358
- }
359
- }
360
- }
361
- else {
362
- super.fitLayer(typeof layerDef === 'number' ? String(layerDef) : layerDef, options);
363
- }
364
- }
365
- /** @deprecated use {@link fitLayer} instead */
366
- async zoomToLayer(layerDef) {
367
- return this.fitLayer(layerDef);
368
- }
369
- onLoad(event = 'ngw-map:create') {
370
- return super.onLoad(event);
371
- }
372
- removeLayer(layerDef) {
373
- const layer = this.getLayer(layerDef);
374
- if (layer) {
375
- const layerId = this.getLayerId(layer);
376
- if (layerId) {
377
- delete this._ngwLayers[layerId];
378
- }
379
- super.removeLayer(layer);
380
- }
381
- }
382
- enableSelection() {
383
- if (!this.$$selectFromNgwRaster) {
384
- this.$$selectFromNgwRaster = (ev) => {
385
- const count = this._getSelectListenersCount();
386
- if (count) {
387
- this.selectFromNgwRaster(ev);
388
- }
389
- };
390
- this.$$selectFromNgwVector = (ev) => {
391
- const count = this._getSelectListenersCount();
392
- if (count) {
393
- this._selectFromNgwVector(ev);
394
- }
395
- };
396
- this.emitter.on('click', this.$$selectFromNgwRaster);
397
- this.emitter.on('layer:click', this.$$selectFromNgwVector);
398
- }
399
- }
400
- disableSelection() {
401
- if (this.$$selectFromNgwRaster) {
402
- this.emitter.removeListener('click', this.$$selectFromNgwRaster);
403
- this.$$selectFromNgwRaster = undefined;
404
- }
405
- if (this.$$selectFromNgwVector) {
406
- this.emitter.removeListener('layer:click', this.$$selectFromNgwVector);
407
- this.$$selectFromNgwVector = undefined;
408
- }
409
- }
410
- /**
411
- * @deprecated use {@link fetchNgwLayerItem} instead
412
- */
413
- getNgwLayerItem(options) {
414
- return this.fetchNgwLayerItem(options);
415
- }
416
- /**
417
- * @deprecated use {@link fetchNgwLayerItems} instead
418
- */
419
- getNgwLayerItems(options) {
420
- return this.fetchNgwLayerItems(options);
421
- }
422
- /**
423
- * @deprecated use {@link fetchNgwLayerFeature} instead
424
- */
425
- getNgwLayerFeature(options) {
426
- return this.fetchNgwLayerFeature(options);
427
- }
428
- /**
429
- * @deprecated use {@link fetchNgwLayerFeatures} instead
430
- */
431
- getNgwLayerFeatures(options) {
432
- return this.fetchNgwLayerFeatures(options);
433
- }
434
- /** @deprecated use {@link cancelPromises} instead */
435
- cancelPromise(...args) {
436
- this.cancelPromises(...args);
437
- }
438
- cancelPromises(...args) {
439
- if (!args.length) {
440
- args = Object.keys(this._promises);
441
- }
442
- args.forEach((name) => {
443
- const group = this._promises[name];
444
- if (group) {
445
- group.forEach((x) => x.cancel());
446
- this._promises[name] = [];
447
- }
448
- });
449
- }
450
- async selectFromNgwRaster(ev) {
451
- var _a;
452
- this._emitStatusEvent('ngw:preselect');
453
- const promises = [];
454
- const layers = Object.values(this._ngwLayers);
455
- layers.sort((a, b) => {
456
- if (a.layer.order && b.layer.order) {
457
- return b.layer.order - a.layer.order;
180
+ const layer = await this.addLayer(
181
+ adapter,
182
+ adapterOpts
183
+ );
184
+ const id = layer && this.getLayerId(layer);
185
+ if (layer && id) {
186
+ this._ngwLayers[id] = { layer, resourceId: layer.resourceId };
187
+ layer.options.name = layer.options.name || layer.item && layer.item.resource.display_name;
188
+ if (layer.options.baselayer) {
189
+ const visibleLayerBaseLayer = this.getActiveBaseLayer();
190
+ if (visibleLayerBaseLayer) {
191
+ return layer;
458
192
  }
459
- return 1;
193
+ }
194
+ }
195
+ return layer;
196
+ } catch (er) {
197
+ const resId = utils.isObject(resource) && "id" in resource ? resource.id : keyname || resourceId || resource;
198
+ console.error(`Can't add NGW layer ${resId}.`, er);
199
+ }
200
+ }
201
+ }
202
+ /**
203
+ * Pans and zooms the map to the initial position specified in the options
204
+ */
205
+ fit() {
206
+ const { center, zoom, bounds } = this.options;
207
+ if (center) {
208
+ this.setCenter(center);
209
+ if (zoom) {
210
+ this.setZoom(zoom);
211
+ }
212
+ } else if (bounds) {
213
+ this.fitBounds(bounds);
214
+ }
215
+ }
216
+ fetchNgwLayerItem(options) {
217
+ return ngwKit.fetchNgwLayerItem({
218
+ connector: this.connector,
219
+ ...options
220
+ });
221
+ }
222
+ fetchNgwLayerItems(options) {
223
+ return ngwKit.fetchNgwLayerItems({
224
+ connector: this.connector,
225
+ ...options
226
+ });
227
+ }
228
+ fetchNgwLayerFeature(options) {
229
+ return ngwKit.fetchNgwLayerFeature({
230
+ connector: this.connector,
231
+ ...options
232
+ });
233
+ }
234
+ fetchNgwLayerFeatures(options) {
235
+ return ngwKit.fetchNgwLayerFeatureCollection({
236
+ connector: this.connector,
237
+ ...options
238
+ });
239
+ }
240
+ fetchIdentifyItem(identify, requestOptions) {
241
+ const promise = ngwKit.fetchIdentifyItem({
242
+ identify,
243
+ connector: this.connector,
244
+ requestOptions
245
+ // multiple,
246
+ });
247
+ this._addPromise("identify", promise);
248
+ return promise;
249
+ }
250
+ fetchIdentifyGeoJson(identify, multiple = false) {
251
+ const promise = ngwKit.fetchIdentifyGeoJson({
252
+ identify,
253
+ connector: this.connector,
254
+ multiple
255
+ });
256
+ if (promise && "then" in promise) {
257
+ this._addPromise("identify", promise);
258
+ return promise;
259
+ } else {
260
+ return CancelablePromise.resolve(promise);
261
+ }
262
+ }
263
+ /**
264
+ * @deprecated use {@link fetchIdentifyGeoJson} instead
265
+ */
266
+ getIdentifyGeoJson(identify, multiple = false) {
267
+ return this.fetchIdentifyGeoJson(identify, multiple);
268
+ }
269
+ async getNgwLayers() {
270
+ await this.onLoad();
271
+ return this._ngwLayers;
272
+ }
273
+ async getNgwLayerByResourceId(id) {
274
+ for (const n in this._ngwLayers) {
275
+ const mem = this._ngwLayers[n];
276
+ if (mem.resourceId === id) {
277
+ return mem && mem.layer;
278
+ } else if (mem.layer.getIdentificationIds) {
279
+ const ids = await mem.layer.getIdentificationIds();
280
+ if (ids && ids.some((x) => x === id)) {
281
+ return mem.layer;
282
+ }
283
+ }
284
+ if (mem.layer.getDependLayers) {
285
+ const dependLayers = mem.layer.getDependLayers();
286
+ const dependFit = dependLayers.find((x) => {
287
+ return x.item && x.item.parentId === id;
460
288
  });
461
- for (const l of layers) {
462
- const layer = l.layer;
463
- const identFunc = typeof layer.getIdentificationIds === 'function'
464
- ? layer.getIdentificationIds
465
- : false;
466
- const interactive = (_a = layer.options.interactive) !== null && _a !== void 0 ? _a : true;
467
- if (identFunc &&
468
- layer.options.selectable &&
469
- interactive &&
470
- this.isLayerVisible(layer)) {
471
- promises.push(identFunc.call(layer));
472
- }
473
- }
474
- const getIdsPromise = Promise.all(promises);
475
- const getIds = await getIdsPromise;
476
- const ids = [];
477
- for (const x of getIds) {
478
- if (x) {
479
- ids.push(...x);
289
+ if (dependFit) {
290
+ return dependFit.layer;
291
+ }
292
+ }
293
+ }
294
+ }
295
+ /**
296
+ * Move map to layer. If the layer is NGW resource, extent will be received from the server
297
+ *
298
+ * @example
299
+ * ```javascript
300
+ * const ngwLayer = ngwMap.addNgwLayer({ id: 'ngw_layer_name', resource: 4005 });
301
+ * ngwMap.fitLayer(ngwLayer);
302
+ * ngwMap.fitLayer('ngw_layer_name');
303
+ * ```
304
+ */
305
+ async fitLayer(layerDef, options) {
306
+ let id;
307
+ if (typeof layerDef === "string" || typeof layerDef === "number") {
308
+ id = String(id);
309
+ } else {
310
+ id = layerDef.id;
311
+ }
312
+ const ngwLayer = id && this._ngwLayers[id];
313
+ if (ngwLayer) {
314
+ if (ngwLayer.layer.getBounds) {
315
+ const bounds = await ngwLayer.layer.getBounds();
316
+ if (bounds) {
317
+ this.fitBounds(bounds, options);
318
+ }
319
+ } else {
320
+ let item;
321
+ if (ngwLayer.layer.item) {
322
+ item = ngwLayer.layer.item;
323
+ } else {
324
+ const resourceId = ngwLayer.resourceId;
325
+ item = await this.connector.getResource(resourceId);
326
+ }
327
+ if (item) {
328
+ ngwKit.fetchNgwExtent({
329
+ resourceId: item.resource.id,
330
+ connector: this.connector
331
+ }).then((extent) => {
332
+ if (extent) {
333
+ this.fitBounds(extent, options);
480
334
  }
481
- }
482
- if (!ids.length) {
483
- this._emitStatusEvent('ngw:select', null);
484
- return;
485
- }
486
- const pixelRadius = this.options.pixelRadius || 10;
487
- const center = this.getCenter();
488
- let zoom = this.getZoom();
489
- zoom = zoom !== undefined ? zoom : 20;
490
- if (!center || !zoom) {
491
- this._emitStatusEvent('ngw:select', null);
492
- return;
493
- }
494
- const radius = utils.getIdentifyRadius(center, zoom, pixelRadius);
495
- const selectPromise = ngwKit.sendIdentifyRequest(ev, {
496
- layers: ids,
497
- connector: this.connector,
498
- radius,
499
- }).then((resp) => {
500
- const identify = {
501
- ...resp,
502
- resources: ids,
503
- sourceType: 'raster',
504
- event: ev,
505
- };
506
- const identifyEvent = this._prepareToIdentify(identify);
507
- this._emitStatusEvent('ngw:select', identifyEvent);
508
- return identifyEvent;
509
- });
510
- this._addPromise('select', selectPromise);
511
- return selectPromise;
512
- }
513
- _addPromise(groupName, promise) {
514
- const group = this._promises[groupName];
515
- if (group && group.indexOf(promise) === -1) {
516
- const removeFromGroup = () => {
517
- const index = group.indexOf(promise);
518
- if (index !== -1) {
519
- group.splice(index, 1);
520
- }
521
- };
522
- promise.then(removeFromGroup);
523
- promise.catch(removeFromGroup);
524
- group.push(promise);
525
- }
526
- }
527
- _isFitFromResource() {
528
- const params = this._initMapState;
529
- if (params.zoom && params.center) {
530
- return false;
531
- }
532
- return true;
533
- }
534
- async _createWebMap() {
535
- await this.create();
536
- if (this.options.qmsId) {
537
- this._addQmsBaseLayer();
538
- }
539
- if (this.options.osm) {
540
- this._addOsmBaseLayer();
541
- }
542
- const resources = [];
543
- const layerFitAllowed = this._isFitFromResource();
544
- if (this.options.webmapId) {
545
- appendNgwResources(resources, this.options.webmapId, {
546
- fit: layerFitAllowed,
547
- });
548
- }
549
- if (this.options.resources && Array.isArray(this.options.resources)) {
550
- for (const x of this.options.resources) {
551
- const overwriteOptions = {};
552
- if (!layerFitAllowed) {
553
- overwriteOptions.fit = false;
554
- }
555
- appendNgwResources(resources, x, {}, overwriteOptions);
556
- }
557
- }
558
- for (const r of resources) {
559
- try {
560
- await this.addNgwLayer(r);
561
- }
562
- catch (er) {
563
- console.warn(er);
564
- }
565
- }
566
- this._emitStatusEvent('ngw-map:create', this);
567
- this.enableSelection();
568
- }
569
- _addOsmBaseLayer() {
570
- this.addBaseLayer('OSM');
571
- }
572
- _addQmsBaseLayer() {
573
- let qmsId;
574
- let qmsLayerName;
575
- if (Array.isArray(this.options.qmsId)) {
576
- qmsId = this.options.qmsId[0];
577
- qmsLayerName = this.options.qmsId[1];
578
- }
579
- else {
580
- qmsId = Number(this.options.qmsId);
581
- }
582
- const qmsLayerOptions = {
583
- qmsId,
335
+ });
336
+ }
337
+ }
338
+ } else {
339
+ super.fitLayer(
340
+ typeof layerDef === "number" ? String(layerDef) : layerDef,
341
+ options
342
+ );
343
+ }
344
+ }
345
+ /** @deprecated use {@link fitLayer} instead */
346
+ async zoomToLayer(layerDef) {
347
+ return this.fitLayer(layerDef);
348
+ }
349
+ onLoad(event = "ngw-map:create") {
350
+ return super.onLoad(event);
351
+ }
352
+ removeLayer(layerDef) {
353
+ const layer = this.getLayer(layerDef);
354
+ if (layer) {
355
+ const layerId = this.getLayerId(layer);
356
+ if (layerId) {
357
+ delete this._ngwLayers[layerId];
358
+ }
359
+ super.removeLayer(layer);
360
+ }
361
+ }
362
+ enableSelection() {
363
+ if (!this.$$selectFromNgwRaster) {
364
+ this.$$selectFromNgwRaster = (ev) => {
365
+ const count = this._getSelectListenersCount();
366
+ if (count) {
367
+ this.selectFromNgwRaster(ev);
368
+ }
369
+ };
370
+ this.$$selectFromNgwVector = (ev) => {
371
+ const count = this._getSelectListenersCount();
372
+ if (count) {
373
+ this._selectFromNgwVector(ev);
374
+ }
375
+ };
376
+ this.emitter.on("click", this.$$selectFromNgwRaster);
377
+ this.emitter.on("layer:click", this.$$selectFromNgwVector);
378
+ }
379
+ }
380
+ disableSelection() {
381
+ if (this.$$selectFromNgwRaster) {
382
+ this.emitter.removeListener("click", this.$$selectFromNgwRaster);
383
+ this.$$selectFromNgwRaster = void 0;
384
+ }
385
+ if (this.$$selectFromNgwVector) {
386
+ this.emitter.removeListener("layer:click", this.$$selectFromNgwVector);
387
+ this.$$selectFromNgwVector = void 0;
388
+ }
389
+ }
390
+ /**
391
+ * @deprecated use {@link fetchNgwLayerItem} instead
392
+ */
393
+ getNgwLayerItem(options) {
394
+ return this.fetchNgwLayerItem(options);
395
+ }
396
+ /**
397
+ * @deprecated use {@link fetchNgwLayerItems} instead
398
+ */
399
+ getNgwLayerItems(options) {
400
+ return this.fetchNgwLayerItems(options);
401
+ }
402
+ /**
403
+ * @deprecated use {@link fetchNgwLayerFeature} instead
404
+ */
405
+ getNgwLayerFeature(options) {
406
+ return this.fetchNgwLayerFeature(options);
407
+ }
408
+ /**
409
+ * @deprecated use {@link fetchNgwLayerFeatures} instead
410
+ */
411
+ getNgwLayerFeatures(options) {
412
+ return this.fetchNgwLayerFeatures(options);
413
+ }
414
+ /** @deprecated use {@link cancelPromises} instead */
415
+ cancelPromise(...args) {
416
+ this.cancelPromises(...args);
417
+ }
418
+ cancelPromises(...args) {
419
+ if (!args.length) {
420
+ args = Object.keys(this._promises);
421
+ }
422
+ args.forEach((name) => {
423
+ const group = this._promises[name];
424
+ if (group) {
425
+ group.forEach((x) => x.cancel());
426
+ this._promises[name] = [];
427
+ }
428
+ });
429
+ }
430
+ async selectFromNgwRaster(ev) {
431
+ var _a;
432
+ this._emitStatusEvent("ngw:preselect");
433
+ const promises = [];
434
+ const layers = Object.values(this._ngwLayers);
435
+ layers.sort((a, b) => {
436
+ if (a.layer.order && b.layer.order) {
437
+ return b.layer.order - a.layer.order;
438
+ }
439
+ return 1;
440
+ });
441
+ for (const l of layers) {
442
+ const layer = l.layer;
443
+ const identFunc = typeof layer.getIdentificationIds === "function" ? layer.getIdentificationIds : false;
444
+ const interactive = (_a = layer.options.interactive) != null ? _a : true;
445
+ if (identFunc && layer.options.selectable && interactive && this.isLayerVisible(layer)) {
446
+ promises.push(identFunc.call(layer));
447
+ }
448
+ }
449
+ const getIdsPromise = Promise.all(promises);
450
+ const getIds = await getIdsPromise;
451
+ const ids = [];
452
+ for (const x of getIds) {
453
+ if (x) {
454
+ ids.push(...x);
455
+ }
456
+ }
457
+ if (!ids.length) {
458
+ this._emitStatusEvent("ngw:select", null);
459
+ return;
460
+ }
461
+ const pixelRadius = this.options.pixelRadius || 10;
462
+ const center = this.getCenter();
463
+ let zoom = this.getZoom();
464
+ zoom = zoom !== void 0 ? zoom : 20;
465
+ if (!center || !zoom) {
466
+ this._emitStatusEvent("ngw:select", null);
467
+ return;
468
+ }
469
+ const radius = utils.getIdentifyRadius(center, zoom, pixelRadius);
470
+ const selectPromise = ngwKit.sendIdentifyRequest(ev, {
471
+ layers: ids,
472
+ connector: this.connector,
473
+ radius
474
+ }).then((resp) => {
475
+ const identify = {
476
+ ...resp,
477
+ resources: ids,
478
+ sourceType: "raster",
479
+ event: ev
480
+ };
481
+ const identifyEvent = this._prepareToIdentify(identify);
482
+ this._emitStatusEvent("ngw:select", identifyEvent);
483
+ return identifyEvent;
484
+ });
485
+ this._addPromise("select", selectPromise);
486
+ return selectPromise;
487
+ }
488
+ _addPromise(groupName, promise) {
489
+ const group = this._promises[groupName];
490
+ if (group && group.indexOf(promise) === -1) {
491
+ const removeFromGroup = () => {
492
+ const index = group.indexOf(promise);
493
+ if (index !== -1) {
494
+ group.splice(index, 1);
495
+ }
496
+ };
497
+ promise.then(removeFromGroup);
498
+ promise.catch(removeFromGroup);
499
+ group.push(promise);
500
+ }
501
+ }
502
+ _isFitFromResource() {
503
+ const params = this._initMapState;
504
+ if (params.zoom && params.center) {
505
+ return false;
506
+ }
507
+ return true;
508
+ }
509
+ async _createWebMap() {
510
+ await this.create();
511
+ if (this.options.qmsId) {
512
+ this._addQmsBaseLayer();
513
+ }
514
+ if (this.options.osm) {
515
+ this._addOsmBaseLayer();
516
+ }
517
+ const resources = [];
518
+ const layerFitAllowed = this._isFitFromResource();
519
+ if (this.options.webmapId) {
520
+ appendNgwResources(resources, this.options.webmapId, {
521
+ fit: layerFitAllowed
522
+ });
523
+ }
524
+ if (this.options.resources && Array.isArray(this.options.resources)) {
525
+ for (const x of this.options.resources) {
526
+ const overwriteOptions = {};
527
+ if (!layerFitAllowed) {
528
+ overwriteOptions.fit = false;
529
+ }
530
+ appendNgwResources(resources, x, {}, overwriteOptions);
531
+ }
532
+ }
533
+ for (const r of resources) {
534
+ try {
535
+ await this.addNgwLayer(r);
536
+ } catch (er) {
537
+ console.warn(er);
538
+ }
539
+ }
540
+ this._emitStatusEvent("ngw-map:create", this);
541
+ this.enableSelection();
542
+ }
543
+ _addOsmBaseLayer() {
544
+ this.addBaseLayer("OSM");
545
+ }
546
+ _addQmsBaseLayer() {
547
+ let qmsId;
548
+ let qmsLayerName;
549
+ if (Array.isArray(this.options.qmsId)) {
550
+ qmsId = this.options.qmsId[0];
551
+ qmsLayerName = this.options.qmsId[1];
552
+ } else {
553
+ qmsId = Number(this.options.qmsId);
554
+ }
555
+ const qmsLayerOptions = {
556
+ qmsId
557
+ };
558
+ if (qmsLayerName) {
559
+ qmsLayerOptions.id = qmsLayerName;
560
+ }
561
+ this.addBaseLayer("QMS", qmsLayerOptions);
562
+ }
563
+ _selectFromNgwVector(ev) {
564
+ const layer = ev.layer;
565
+ const selectable = layer.options.selectable && this.isLayerVisible(layer);
566
+ if (!selectable) {
567
+ return void 0;
568
+ }
569
+ const id = layer.item && layer.item.resource.id;
570
+ const feature = ev.feature;
571
+ if (id !== void 0 && feature) {
572
+ const featureId = feature.id;
573
+ if (featureId) {
574
+ const identifyFeature = {
575
+ id: Number(featureId),
576
+ fields: feature.properties || {},
577
+ label: `#${id}`,
578
+ layerId: Number(id),
579
+ parent: "",
580
+ geom: feature.geometry
584
581
  };
585
- if (qmsLayerName) {
586
- qmsLayerOptions.id = qmsLayerName;
587
- }
588
- this.addBaseLayer('QMS', qmsLayerOptions);
589
- }
590
- _selectFromNgwVector(ev) {
591
- const layer = ev.layer;
592
- const selectable = layer.options.selectable && this.isLayerVisible(layer);
593
- if (!selectable) {
594
- return undefined;
595
- }
596
- // Item property means layer is NgwResource
597
- const id = layer.item && layer.item.resource.id;
598
- const feature = ev.feature;
599
- if (id !== undefined && feature) {
600
- const featureId = feature.id;
601
- if (featureId) {
602
- const identifyFeature = {
603
- id: Number(featureId),
604
- fields: feature.properties || {},
605
- label: `#${id}`,
606
- layerId: Number(id),
607
- parent: '',
608
- geom: feature.geometry,
609
- };
610
- const items = {
611
- featureCount: 1,
612
- features: [identifyFeature],
613
- };
614
- const identify = {
615
- featureCount: 1,
616
- [id]: items,
617
- };
618
- this._emitStatusEvent('ngw:select', this._prepareToIdentify({
619
- ...identify,
620
- resources: [id],
621
- sourceType: 'vector',
622
- }));
623
- return identify;
624
- }
625
- }
626
- }
627
- _prepareToIdentify(identify) {
628
- const getIdentifyItems_ = () => {
629
- return ngwKit.getIdentifyItems(identify, true).map((x) => {
630
- return ngwKit.createIdentifyItem({
631
- feature: x.feature,
632
- connector: this.connector,
633
- });
634
- });
582
+ const items = {
583
+ featureCount: 1,
584
+ features: [identifyFeature]
635
585
  };
636
- return {
637
- ...identify,
638
- getIdentifyItems: getIdentifyItems_,
586
+ const identify = {
587
+ featureCount: 1,
588
+ [id]: items
639
589
  };
640
- }
641
- _getSelectListenersCount() {
642
- return this.emitter.listenerCount('ngw:select');
643
- }
644
- async _whiteLabel() {
645
- const container = this.getContainer();
646
- if (container) {
647
- const logo = await ngwKit.getCompanyLogo(this.connector, this.options.companyLogoOptions);
648
- if (logo) {
649
- container.appendChild(logo);
650
- }
651
- }
652
- }
590
+ this._emitStatusEvent(
591
+ "ngw:select",
592
+ this._prepareToIdentify({
593
+ ...identify,
594
+ resources: [id],
595
+ sourceType: "vector"
596
+ })
597
+ );
598
+ return identify;
599
+ }
600
+ }
601
+ }
602
+ _prepareToIdentify(identify) {
603
+ const getIdentifyItems_ = () => {
604
+ return ngwKit.getIdentifyItems(identify, true).map((x) => {
605
+ return ngwKit.createIdentifyItem({
606
+ feature: x.feature,
607
+ connector: this.connector
608
+ });
609
+ });
610
+ };
611
+ return {
612
+ ...identify,
613
+ getIdentifyItems: getIdentifyItems_
614
+ };
615
+ }
616
+ _getSelectListenersCount() {
617
+ return this.emitter.listenerCount("ngw:select");
618
+ }
619
+ async _whiteLabel() {
620
+ const container = this.getContainer();
621
+ if (container) {
622
+ const logo = await ngwKit.getCompanyLogo(
623
+ this.connector,
624
+ this.options.companyLogoOptions
625
+ );
626
+ if (logo) {
627
+ container.appendChild(logo);
628
+ }
629
+ }
630
+ }
653
631
  }
654
- NgwMap.getIcon = icons.getIcon;
632
+ __publicField(NgwMap, "getIcon", icons.getIcon);
655
633
 
656
- /**
657
- * @module ngw-map
658
- */
659
634
  async function createNgwMap(options) {
660
- const ngwMap = new NgwMap(options);
661
- return ngwMap.onLoad();
635
+ const ngwMap = new NgwMap(options);
636
+ return ngwMap.onLoad();
662
637
  }
663
638
 
664
639
  exports.NgwMap = NgwMap;
665
640
  exports.createNgwMap = createNgwMap;
666
- for (var k in webmap) {
667
- if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = webmap[k];
668
- }
641
+ Object.keys(webmap).forEach(function (k) {
642
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = webmap[k];
643
+ });
669
644
  //# sourceMappingURL=ngw-map.cjs.js.map