@nextgis/ngw-map 1.16.9 → 1.18.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,829 +1,651 @@
1
- /** Bundle of @nextgis/ngw-map; version: 1.16.9; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-map; version: 1.18.0; author: NextGIS */
2
2
  import { EventEmitter } from 'events';
3
3
  import CancelablePromise from '@nextgis/cancelable-promise';
4
- import { deepmerge, isObject, defined, deprecatedWarn, getIdentifyRadius } from '@nextgis/utils';
4
+ import { deepmerge, deprecatedWarn, defined, isObject, getIdentifyRadius } from '@nextgis/utils';
5
5
  import { getDefaultControls, WebMap } from '@nextgis/webmap';
6
6
  export * from '@nextgis/webmap';
7
- import { NgwKit, fetchNgwLayerItem, fetchNgwLayerItems, fetchNgwLayerFeature, fetchNgwLayerFeatureCollection, fetchIdentifyItem, fetchIdentifyGeoJson, getIdentifyItems, createIdentifyItem, createNgwLayerAdapter, sendIdentifyRequest, getCompanyLogo, fetchNgwExtent } from '@nextgis/ngw-kit';
7
+ import { NgwKit, createNgwLayerAdapter, fetchNgwLayerItem, fetchNgwLayerItems, fetchNgwLayerFeature, fetchNgwLayerFeatureCollection, fetchIdentifyItem, fetchIdentifyGeoJson, fetchNgwExtent, sendIdentifyRequest, getCompanyLogo, getIdentifyItems, createIdentifyItem } from '@nextgis/ngw-kit';
8
8
  import { getIcon } from '@nextgis/icons';
9
9
  import NgwConnector from '@nextgis/ngw-connector';
10
10
  import { QmsKit } from '@nextgis/qms-kit';
11
11
 
12
- /******************************************************************************
13
- Copyright (c) Microsoft Corporation.
14
-
15
- Permission to use, copy, modify, and/or distribute this software for any
16
- purpose with or without fee is hereby granted.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
19
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
20
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
21
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
22
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
23
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
24
- PERFORMANCE OF THIS SOFTWARE.
25
- ***************************************************************************** */
26
- /* global Reflect, Promise */
27
-
28
- var extendStatics = function(d, b) {
29
- extendStatics = Object.setPrototypeOf ||
30
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
31
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
32
- return extendStatics(d, b);
33
- };
34
-
35
- function __extends(d, b) {
36
- if (typeof b !== "function" && b !== null)
37
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
38
- extendStatics(d, b);
39
- function __() { this.constructor = d; }
40
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
41
- }
42
-
43
- var __assign = function() {
44
- __assign = Object.assign || function __assign(t) {
45
- for (var s, i = 1, n = arguments.length; i < n; i++) {
46
- s = arguments[i];
47
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
48
- }
49
- return t;
50
- };
51
- return __assign.apply(this, arguments);
52
- };
53
-
54
- function __awaiter(thisArg, _arguments, P, generator) {
55
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
56
- return new (P || (P = Promise))(function (resolve, reject) {
57
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
58
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
59
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
60
- step((generator = generator.apply(thisArg, _arguments || [])).next());
61
- });
62
- }
63
-
64
- function __generator(thisArg, body) {
65
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
66
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
67
- function verb(n) { return function (v) { return step([n, v]); }; }
68
- function step(op) {
69
- if (f) throw new TypeError("Generator is already executing.");
70
- while (_) try {
71
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
72
- if (y = 0, t) op = [op[0] & 2, t.value];
73
- switch (op[0]) {
74
- case 0: case 1: t = op; break;
75
- case 4: _.label++; return { value: op[1], done: false };
76
- case 5: _.label++; y = op[1]; op = [0]; continue;
77
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
78
- default:
79
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
80
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
81
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
82
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
83
- if (t[2]) _.ops.pop();
84
- _.trys.pop(); continue;
85
- }
86
- op = body.call(thisArg, _);
87
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
88
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
89
- }
12
+ function appendNgwResources(options, resource, defOptions, overwriteOptions) {
13
+ if (typeof resource === 'number' || typeof resource === 'string') {
14
+ resource = Number(resource);
15
+ options.push({
16
+ ...defOptions,
17
+ resource,
18
+ });
19
+ }
20
+ else if (Array.isArray(resource)) {
21
+ const [resourceId, id] = resource;
22
+ options.push({
23
+ ...defOptions,
24
+ resource: resourceId,
25
+ id,
26
+ ...overwriteOptions,
27
+ });
28
+ }
29
+ else if (typeof resource === 'object') {
30
+ options.push({ ...defOptions, ...resource, ...overwriteOptions });
31
+ }
90
32
  }
91
33
 
92
- function appendNgwResources(options, resource, defOptions, overwriteOptions) {
93
- if (typeof resource === 'number' || typeof resource === 'string') {
94
- resource = Number(resource);
95
- options.push(__assign(__assign({}, defOptions), { resource: resource }));
96
- }
97
- else if (Array.isArray(resource)) {
98
- var resourceId = resource[0], id = resource[1];
99
- options.push(__assign(__assign(__assign({}, defOptions), { resource: resourceId, id: id }), overwriteOptions));
100
- }
101
- else if (typeof resource === 'object') {
102
- options.push(__assign(__assign(__assign({}, defOptions), resource), overwriteOptions));
103
- }
34
+ const OPTIONS = {
35
+ target: 'map',
36
+ baseUrl: '',
37
+ whitlabel: false,
38
+ controls: getDefaultControls(),
39
+ controlsOptions: {
40
+ ZOOM: { position: 'top-left' },
41
+ ATTRIBUTION: {
42
+ position: 'bottom-right',
43
+ customAttribution: [
44
+ '<a href="https://nextgis.com" target="_blank">©NextGIS</a>',
45
+ ],
46
+ },
47
+ },
48
+ pixelRadius: 10,
49
+ };
50
+ function prepareWebMapOptions(options) {
51
+ const kits = [new QmsKit()];
52
+ if (options.starterKits) {
53
+ options.starterKits.forEach((x) => {
54
+ kits.push(x);
55
+ });
56
+ }
57
+ if (!options.connector) {
58
+ options.connector = new NgwConnector({
59
+ baseUrl: options.baseUrl || '',
60
+ auth: options.auth,
61
+ });
62
+ }
63
+ else if (options.connector) {
64
+ options.baseUrl = options.connector.options.baseUrl;
65
+ }
66
+ options = deepmerge(OPTIONS, options);
67
+ if (!options.center && !options.bounds) {
68
+ options.bounds = [-179, -90, 180, 90];
69
+ // options.maxBounds = options.bounds;
70
+ }
71
+ if (options.connector) {
72
+ kits.push(new NgwKit({
73
+ connector: options.connector,
74
+ auth: options.auth,
75
+ }));
76
+ }
77
+ options = {
78
+ ...options,
79
+ starterKits: kits,
80
+ create: false,
81
+ };
82
+ return options;
104
83
  }
105
84
 
106
- var OPTIONS = {
107
- target: 'map',
108
- baseUrl: '',
109
- whitlabel: false,
110
- controls: getDefaultControls(),
111
- controlsOptions: {
112
- ZOOM: { position: 'top-left' },
113
- ATTRIBUTION: {
114
- position: 'bottom-right',
115
- customAttribution: [
116
- '<a href="https://nextgis.com" target="_blank">©NextGIS</a>',
117
- ],
118
- },
119
- },
120
- pixelRadius: 10,
121
- };
122
- function prepareWebMapOptions(options) {
123
- var kits = [new QmsKit()];
124
- if (options.starterKits) {
125
- options.starterKits.forEach(function (x) {
126
- kits.push(x);
127
- });
128
- }
129
- if (!options.connector) {
130
- options.connector = new NgwConnector({
131
- baseUrl: options.baseUrl || '',
132
- auth: options.auth,
133
- });
134
- }
135
- else if (options.connector) {
136
- options.baseUrl = options.connector.options.baseUrl;
137
- }
138
- options = deepmerge(OPTIONS, options);
139
- if (!options.center && !options.bounds) {
140
- options.bounds = [-179, -90, 180, 90];
141
- // options.maxBounds = options.bounds;
142
- }
143
- if (options.connector) {
144
- kits.push(new NgwKit({
145
- connector: options.connector,
146
- auth: options.auth,
147
- }));
148
- }
149
- options = __assign(__assign({}, options), { starterKits: kits, create: false });
150
- return options;
85
+ /**
86
+ * Base class containing the logic of interaction WebMap with NextGIS services.
87
+ *
88
+ * @example
89
+ * ```javascript
90
+ * import { NgwMap } from '@nextgis/ngw-map';
91
+ * import MapAdapter from '@nextgis/leaflet-map-adapter';
92
+ * // styles are not included in the leaflet-map-adapter
93
+ * import 'leaflet/dist/leaflet.css';
94
+ *
95
+ * const ngwMap = new NgwMap({
96
+ * mapAdapter: new MapAdapter(),
97
+ * target: 'map',
98
+ * qmsId: 448,
99
+ * baseUrl: 'https://demo.nextgis.com',
100
+ * webmapId: 3985
101
+ * });
102
+ * ```
103
+ */
104
+ class NgwMap extends WebMap {
105
+ constructor(options) {
106
+ super(prepareWebMapOptions(options));
107
+ this.emitter = new EventEmitter();
108
+ this._ngwLayers = {};
109
+ this._promises = {
110
+ select: [],
111
+ identify: [],
112
+ };
113
+ if (options.connector) {
114
+ this.connector = options.connector;
115
+ }
116
+ this._createWebMap().then(() => {
117
+ const container = this.getContainer();
118
+ if (container) {
119
+ container.classList.add('ngw-map-container');
120
+ }
121
+ if (this.options.whitlabel) {
122
+ this._whiteLabel();
123
+ }
124
+ });
125
+ }
126
+ /**
127
+ * Organized addition to the map design and controls elements,
128
+ * calling `control.onAdd(this.webMap.mapAdapter)`
129
+ * @param control - object with onAdd and onRemove methods
130
+ * or a string value indicating the name of the control installed in the map adapter
131
+ * @param position - position relative to the map angles
132
+ * @param options - initialization parameters if the control is set as a string value
133
+ *
134
+ * @example
135
+ * ```javascript
136
+ * ngwMap.addControl(new CustomControl(), 'bottom-left');
137
+ * ngwMap.addControl('ZOOM', 'top-right')
138
+ * ```
139
+ */
140
+ async addControl(controlDef, position, options) {
141
+ await this.onLoad('controls:create');
142
+ return super.addControl(controlDef, position, options);
143
+ }
144
+ /**
145
+ * Add any (style, vector, webmap) NGW layer by resource definition.
146
+ * @param options - set layer identification parameters and render method.
147
+ *
148
+ * @example
149
+ * ```javascript
150
+ * // Add raster layer resourceId is the style of 4004 layer
151
+ * ngwMap.addNgwLayer({ resource: 4005 });
152
+ * // Add vector data from layer GEOJSON source
153
+ * ngwMap.addNgwLayer({
154
+ * resource: 4038,
155
+ * adapter: 'GEOJSON',
156
+ * adapterOptions: { paint: { color: 'red' } }
157
+ * });
158
+ * ```
159
+ */
160
+ async addNgwLayer(options) {
161
+ await this.onMapLoad();
162
+ // @ts-ignore for backward compatibility
163
+ const { keyname, resourceId } = options;
164
+ if (keyname || resourceId !== undefined) {
165
+ deprecatedWarn('set `resource` options instead of `keyname` or `resourceId`');
166
+ }
167
+ const resource = options.resource;
168
+ if (!keyname && !resourceId && !resource) {
169
+ throw new Error('resource, resourceId or keyname is required parameter to add NGW layer');
170
+ }
171
+ if (defined(this.options.baseUrl)) {
172
+ try {
173
+ if (defined(this.options.setViewDelay)) {
174
+ options.adapterOptions = options.adapterOptions || {};
175
+ if (!defined(options.adapterOptions.setViewDelay)) {
176
+ options.adapterOptions.setViewDelay = this.options.setViewDelay;
177
+ }
178
+ }
179
+ const adapter = createNgwLayerAdapter(options, this, this.connector);
180
+ const adapterOpts = {
181
+ visibility: true,
182
+ // TODO: do not merge options, use only `adapterOptions`
183
+ ...options,
184
+ ...options.adapterOptions,
185
+ };
186
+ const layer = (await this.addLayer(adapter, adapterOpts));
187
+ const id = layer && this.getLayerId(layer);
188
+ if (layer && id) {
189
+ this._ngwLayers[id] = { layer, resourceId: layer.resourceId };
190
+ layer.options.name =
191
+ layer.options.name ||
192
+ (layer.item && layer.item.resource.display_name);
193
+ if (layer.options.baselayer) {
194
+ const visibleLayerBaseLayer = this.getActiveBaseLayer();
195
+ if (visibleLayerBaseLayer) {
196
+ return layer;
197
+ }
198
+ }
199
+ }
200
+ return layer;
201
+ }
202
+ catch (er) {
203
+ const resId = isObject(resource) && 'id' in resource
204
+ ? resource.id
205
+ : keyname || resourceId || resource;
206
+ console.error(`Can't add NGW layer ${resId}.`, er);
207
+ }
208
+ }
209
+ }
210
+ /**
211
+ * Pans and zooms the map to the initial position specified in the options
212
+ */
213
+ fit() {
214
+ const { center, zoom, bounds } = this.options;
215
+ if (center) {
216
+ this.setCenter(center);
217
+ if (zoom) {
218
+ this.setZoom(zoom);
219
+ }
220
+ }
221
+ else if (bounds) {
222
+ this.fitBounds(bounds);
223
+ }
224
+ }
225
+ fetchNgwLayerItem(options) {
226
+ return fetchNgwLayerItem({
227
+ connector: this.connector,
228
+ ...options,
229
+ });
230
+ }
231
+ fetchNgwLayerItems(options) {
232
+ return fetchNgwLayerItems({
233
+ connector: this.connector,
234
+ ...options,
235
+ });
236
+ }
237
+ fetchNgwLayerFeature(options) {
238
+ return fetchNgwLayerFeature({
239
+ connector: this.connector,
240
+ ...options,
241
+ });
242
+ }
243
+ fetchNgwLayerFeatures(options) {
244
+ return fetchNgwLayerFeatureCollection({
245
+ connector: this.connector,
246
+ ...options,
247
+ });
248
+ }
249
+ fetchIdentifyItem(identify, requestOptions) {
250
+ const promise = fetchIdentifyItem({
251
+ identify,
252
+ connector: this.connector,
253
+ requestOptions,
254
+ // multiple,
255
+ });
256
+ this._addPromise('identify', promise);
257
+ return promise;
258
+ }
259
+ fetchIdentifyGeoJson(identify, multiple = false) {
260
+ const promise = fetchIdentifyGeoJson({
261
+ identify,
262
+ connector: this.connector,
263
+ multiple,
264
+ });
265
+ if (promise && 'then' in promise) {
266
+ this._addPromise('identify', promise);
267
+ return promise;
268
+ }
269
+ else {
270
+ return CancelablePromise.resolve(promise);
271
+ }
272
+ }
273
+ /**
274
+ * @deprecated use {@link fetchIdentifyGeoJson} instead
275
+ */
276
+ getIdentifyGeoJson(identify, multiple = false) {
277
+ return this.fetchIdentifyGeoJson(identify, multiple);
278
+ }
279
+ async getNgwLayers() {
280
+ await this.onLoad();
281
+ return this._ngwLayers;
282
+ }
283
+ async getNgwLayerByResourceId(id) {
284
+ for (const n in this._ngwLayers) {
285
+ const mem = this._ngwLayers[n];
286
+ if (mem.resourceId === id) {
287
+ return mem && mem.layer;
288
+ }
289
+ else if (mem.layer.getIdentificationIds) {
290
+ const ids = await mem.layer.getIdentificationIds();
291
+ if (ids && ids.some((x) => x === id)) {
292
+ return mem.layer;
293
+ }
294
+ }
295
+ if (mem.layer.getDependLayers) {
296
+ const dependLayers = mem.layer.getDependLayers();
297
+ const dependFit = dependLayers.find((x) => {
298
+ return x.item && x.item.parentId === id;
299
+ });
300
+ if (dependFit) {
301
+ return dependFit.layer;
302
+ }
303
+ }
304
+ }
305
+ }
306
+ /**
307
+ * Move map to layer. If the layer is NGW resource, extent will be received from the server
308
+ *
309
+ * @example
310
+ * ```javascript
311
+ * const ngwLayer = ngwMap.addNgwLayer({ id: 'ngw_layer_name', resource: 4005 });
312
+ * ngwMap.fitLayer(ngwLayer);
313
+ * ngwMap.fitLayer('ngw_layer_name');
314
+ * ```
315
+ */
316
+ async fitLayer(layerDef, options) {
317
+ let id;
318
+ if (typeof layerDef === 'string' || typeof layerDef === 'number') {
319
+ id = String(id);
320
+ }
321
+ else {
322
+ id = layerDef.id;
323
+ }
324
+ const ngwLayer = id && this._ngwLayers[id];
325
+ if (ngwLayer) {
326
+ if (ngwLayer.layer.getBounds) {
327
+ const bounds = await ngwLayer.layer.getBounds();
328
+ if (bounds) {
329
+ this.fitBounds(bounds, options);
330
+ }
331
+ }
332
+ else {
333
+ let item;
334
+ if (ngwLayer.layer.item) {
335
+ item = ngwLayer.layer.item;
336
+ }
337
+ else {
338
+ const resourceId = ngwLayer.resourceId;
339
+ item = await this.connector.getResource(resourceId);
340
+ }
341
+ if (item) {
342
+ fetchNgwExtent({
343
+ resourceId: item.resource.id,
344
+ connector: this.connector,
345
+ }).then((extent) => {
346
+ if (extent) {
347
+ this.fitBounds(extent, options);
348
+ }
349
+ });
350
+ }
351
+ }
352
+ }
353
+ else {
354
+ super.fitLayer(typeof layerDef === 'number' ? String(layerDef) : layerDef, options);
355
+ }
356
+ }
357
+ /** @deprecated use {@link fitLayer} instead */
358
+ async zoomToLayer(layerDef) {
359
+ return this.fitLayer(layerDef);
360
+ }
361
+ onLoad(event = 'ngw-map:create') {
362
+ return super.onLoad(event);
363
+ }
364
+ removeLayer(layerDef) {
365
+ const layer = this.getLayer(layerDef);
366
+ if (layer) {
367
+ const layerId = this.getLayerId(layer);
368
+ if (layerId) {
369
+ delete this._ngwLayers[layerId];
370
+ }
371
+ super.removeLayer(layer);
372
+ }
373
+ }
374
+ enableSelection() {
375
+ if (!this.$$selectFromNgwRaster) {
376
+ this.$$selectFromNgwRaster = (ev) => {
377
+ const count = this._getSelectListenersCount();
378
+ if (count) {
379
+ this.selectFromNgwRaster(ev);
380
+ }
381
+ };
382
+ this.$$selectFromNgwVector = (ev) => {
383
+ const count = this._getSelectListenersCount();
384
+ if (count) {
385
+ this._selectFromNgwVector(ev);
386
+ }
387
+ };
388
+ this.emitter.on('click', this.$$selectFromNgwRaster);
389
+ this.emitter.on('layer:click', this.$$selectFromNgwVector);
390
+ }
391
+ }
392
+ disableSelection() {
393
+ if (this.$$selectFromNgwRaster) {
394
+ this.emitter.removeListener('click', this.$$selectFromNgwRaster);
395
+ this.$$selectFromNgwRaster = undefined;
396
+ }
397
+ if (this.$$selectFromNgwVector) {
398
+ this.emitter.removeListener('layer:click', this.$$selectFromNgwVector);
399
+ this.$$selectFromNgwVector = undefined;
400
+ }
401
+ }
402
+ /**
403
+ * @deprecated use {@link fetchNgwLayerItem} instead
404
+ */
405
+ getNgwLayerItem(options) {
406
+ return this.fetchNgwLayerItem(options);
407
+ }
408
+ /**
409
+ * @deprecated use {@link fetchNgwLayerItems} instead
410
+ */
411
+ getNgwLayerItems(options) {
412
+ return this.fetchNgwLayerItems(options);
413
+ }
414
+ /**
415
+ * @deprecated use {@link fetchNgwLayerFeature} instead
416
+ */
417
+ getNgwLayerFeature(options) {
418
+ return this.fetchNgwLayerFeature(options);
419
+ }
420
+ /**
421
+ * @deprecated use {@link fetchNgwLayerFeatures} instead
422
+ */
423
+ getNgwLayerFeatures(options) {
424
+ return this.fetchNgwLayerFeatures(options);
425
+ }
426
+ /** @deprecated use {@link cancelPromises} instead */
427
+ cancelPromise(...args) {
428
+ this.cancelPromises(...args);
429
+ }
430
+ cancelPromises(...args) {
431
+ if (!args.length) {
432
+ args = Object.keys(this._promises);
433
+ }
434
+ args.forEach((name) => {
435
+ const group = this._promises[name];
436
+ if (group) {
437
+ group.forEach((x) => x.cancel());
438
+ this._promises[name] = [];
439
+ }
440
+ });
441
+ }
442
+ async selectFromNgwRaster(ev) {
443
+ this._emitStatusEvent('ngw:preselect');
444
+ const promises = [];
445
+ const layers = Object.values(this._ngwLayers);
446
+ layers.sort((a, b) => {
447
+ if (a.layer.order && b.layer.order) {
448
+ return b.layer.order - a.layer.order;
449
+ }
450
+ return 1;
451
+ });
452
+ for (const l of layers) {
453
+ const layer = l.layer;
454
+ const identFunc = typeof layer.getIdentificationIds === 'function'
455
+ ? layer.getIdentificationIds
456
+ : false;
457
+ if (identFunc && layer.options.selectable && this.isLayerVisible(layer)) {
458
+ promises.push(identFunc.call(layer));
459
+ }
460
+ }
461
+ const getIdsPromise = Promise.all(promises);
462
+ const getIds = await getIdsPromise;
463
+ const ids = [];
464
+ for (const x of getIds) {
465
+ if (x) {
466
+ ids.push(...x);
467
+ }
468
+ }
469
+ if (!ids.length) {
470
+ this._emitStatusEvent('ngw:select', null);
471
+ return;
472
+ }
473
+ const pixelRadius = this.options.pixelRadius || 10;
474
+ const center = this.getCenter();
475
+ let zoom = this.getZoom();
476
+ zoom = zoom !== undefined ? zoom : 20;
477
+ if (!center || !zoom) {
478
+ this._emitStatusEvent('ngw:select', null);
479
+ return;
480
+ }
481
+ const radius = getIdentifyRadius(center, zoom, pixelRadius);
482
+ const selectPromise = sendIdentifyRequest(ev, {
483
+ layers: ids,
484
+ connector: this.connector,
485
+ radius,
486
+ }).then((resp) => {
487
+ const identify = {
488
+ ...resp,
489
+ resources: ids,
490
+ sourceType: 'raster',
491
+ event: ev,
492
+ };
493
+ const identifyEvent = this._prepareToIdentify(identify);
494
+ this._emitStatusEvent('ngw:select', identifyEvent);
495
+ return identifyEvent;
496
+ });
497
+ this._addPromise('select', selectPromise);
498
+ return selectPromise;
499
+ }
500
+ _addPromise(groupName, promise) {
501
+ const group = this._promises[groupName];
502
+ if (group && group.indexOf(promise) === -1) {
503
+ const removeFromGroup = () => {
504
+ const index = group.indexOf(promise);
505
+ if (index !== -1) {
506
+ group.splice(index, 1);
507
+ }
508
+ };
509
+ promise.then(removeFromGroup);
510
+ promise.catch(removeFromGroup);
511
+ group.push(promise);
512
+ }
513
+ }
514
+ _isFitFromResource() {
515
+ const params = this._initMapState;
516
+ if (params.zoom && params.center) {
517
+ return false;
518
+ }
519
+ return true;
520
+ }
521
+ async _createWebMap() {
522
+ await this.create();
523
+ if (this.options.qmsId) {
524
+ this._addQmsBaseLayer();
525
+ }
526
+ if (this.options.osm) {
527
+ this._addOsmBaseLayer();
528
+ }
529
+ const resources = [];
530
+ const layerFitAllowed = this._isFitFromResource();
531
+ if (this.options.webmapId) {
532
+ appendNgwResources(resources, this.options.webmapId, {
533
+ fit: layerFitAllowed,
534
+ });
535
+ }
536
+ if (this.options.resources && Array.isArray(this.options.resources)) {
537
+ for (const x of this.options.resources) {
538
+ const overwriteOptions = {};
539
+ if (!layerFitAllowed) {
540
+ overwriteOptions.fit = false;
541
+ }
542
+ appendNgwResources(resources, x, {}, overwriteOptions);
543
+ }
544
+ }
545
+ for (const r of resources) {
546
+ try {
547
+ await this.addNgwLayer(r);
548
+ }
549
+ catch (er) {
550
+ console.warn(er);
551
+ }
552
+ }
553
+ this._emitStatusEvent('ngw-map:create', this);
554
+ this.enableSelection();
555
+ }
556
+ _addOsmBaseLayer() {
557
+ this.addBaseLayer('OSM');
558
+ }
559
+ _addQmsBaseLayer() {
560
+ let qmsId;
561
+ let qmsLayerName;
562
+ if (Array.isArray(this.options.qmsId)) {
563
+ qmsId = this.options.qmsId[0];
564
+ qmsLayerName = this.options.qmsId[1];
565
+ }
566
+ else {
567
+ qmsId = Number(this.options.qmsId);
568
+ }
569
+ const qmsLayerOptions = {
570
+ qmsId,
571
+ };
572
+ if (qmsLayerName) {
573
+ qmsLayerOptions.id = qmsLayerName;
574
+ }
575
+ this.addBaseLayer('QMS', qmsLayerOptions);
576
+ }
577
+ _selectFromNgwVector(ev) {
578
+ const layer = ev.layer;
579
+ const selectable = layer.options.selectable && this.isLayerVisible(layer);
580
+ if (!selectable) {
581
+ return undefined;
582
+ }
583
+ // Item property means layer is NgwResource
584
+ const id = layer.item && layer.item.resource.id;
585
+ const feature = ev.feature;
586
+ if (id !== undefined && feature) {
587
+ const featureId = feature.id;
588
+ if (featureId) {
589
+ const identifyFeature = {
590
+ id: Number(featureId),
591
+ fields: feature.properties || {},
592
+ label: `#${id}`,
593
+ layerId: Number(id),
594
+ parent: '',
595
+ geom: feature.geometry,
596
+ };
597
+ const items = {
598
+ featureCount: 1,
599
+ features: [identifyFeature],
600
+ };
601
+ const identify = {
602
+ featureCount: 1,
603
+ [id]: items,
604
+ };
605
+ this._emitStatusEvent('ngw:select', this._prepareToIdentify({
606
+ ...identify,
607
+ resources: [id],
608
+ sourceType: 'vector',
609
+ }));
610
+ return identify;
611
+ }
612
+ }
613
+ }
614
+ _prepareToIdentify(identify) {
615
+ const getIdentifyItems_ = () => {
616
+ return getIdentifyItems(identify, true).map((x) => {
617
+ return createIdentifyItem({
618
+ feature: x.feature,
619
+ connector: this.connector,
620
+ });
621
+ });
622
+ };
623
+ return {
624
+ ...identify,
625
+ getIdentifyItems: getIdentifyItems_,
626
+ };
627
+ }
628
+ _getSelectListenersCount() {
629
+ return this.emitter.listenerCount('ngw:select');
630
+ }
631
+ async _whiteLabel() {
632
+ const container = this.getContainer();
633
+ if (container) {
634
+ const logo = await getCompanyLogo(this.connector, this.options.companyLogoOptions);
635
+ if (logo) {
636
+ container.appendChild(logo);
637
+ }
638
+ }
639
+ }
151
640
  }
641
+ NgwMap.getIcon = getIcon;
152
642
 
153
- /**
154
- * Base class containing the logic of interaction WebMap with NextGIS services.
155
- *
156
- * @example
157
- * ```javascript
158
- * import { NgwMap } from '@nextgis/ngw-map';
159
- * import MapAdapter from '@nextgis/leaflet-map-adapter';
160
- * // styles are not included in the leaflet-map-adapter
161
- * import 'leaflet/dist/leaflet.css';
162
- *
163
- * const ngwMap = new NgwMap({
164
- * mapAdapter: new MapAdapter(),
165
- * target: 'map',
166
- * qmsId: 448,
167
- * baseUrl: 'https://demo.nextgis.com',
168
- * webmapId: 3985
169
- * });
170
- * ```
171
- */
172
- var NgwMap = /** @class */ (function (_super) {
173
- __extends(NgwMap, _super);
174
- function NgwMap(options) {
175
- var _this = _super.call(this, prepareWebMapOptions(options)) || this;
176
- _this.emitter = new EventEmitter();
177
- _this._ngwLayers = {};
178
- _this._promises = {
179
- select: [],
180
- identify: [],
181
- };
182
- if (options.connector) {
183
- _this.connector = options.connector;
184
- }
185
- _this._createWebMap().then(function () {
186
- var container = _this.getContainer();
187
- if (container) {
188
- container.classList.add('ngw-map-container');
189
- }
190
- if (_this.options.whitlabel) {
191
- _this._whiteLabel();
192
- }
193
- });
194
- return _this;
195
- }
196
- /**
197
- * Organized addition to the map design and controls elements,
198
- * calling `control.onAdd(this.webMap.mapAdapter)`
199
- * @param control - object with onAdd and onRemove methods
200
- * or a string value indicating the name of the control installed in the map adapter
201
- * @param position - position relative to the map angles
202
- * @param options - initialization parameters if the control is set as a string value
203
- *
204
- * @example
205
- * ```javascript
206
- * ngwMap.addControl(new CustomControl(), 'bottom-left');
207
- * ngwMap.addControl('ZOOM', 'top-right')
208
- * ```
209
- */
210
- NgwMap.prototype.addControl = function (controlDef, position, options) {
211
- return __awaiter(this, void 0, void 0, function () {
212
- return __generator(this, function (_a) {
213
- switch (_a.label) {
214
- case 0: return [4 /*yield*/, this.onLoad('controls:create')];
215
- case 1:
216
- _a.sent();
217
- return [2 /*return*/, _super.prototype.addControl.call(this, controlDef, position, options)];
218
- }
219
- });
220
- });
221
- };
222
- /**
223
- * Add any (style, vector, webmap) NGW layer by resource definition.
224
- * @param options - set layer identification parameters and render method.
225
- *
226
- * @example
227
- * ```javascript
228
- * // Add raster layer resourceId is the style of 4004 layer
229
- * ngwMap.addNgwLayer({ resource: 4005 });
230
- * // Add vector data from layer GEOJSON source
231
- * ngwMap.addNgwLayer({
232
- * resource: 4038,
233
- * adapter: 'GEOJSON',
234
- * adapterOptions: { paint: { color: 'red' } }
235
- * });
236
- * ```
237
- */
238
- NgwMap.prototype.addNgwLayer = function (options) {
239
- return __awaiter(this, void 0, void 0, function () {
240
- var keyname, resourceId, resource, adapter, adapterOpts, layer, id, visibleLayerBaseLayer, er_1, resId;
241
- return __generator(this, function (_a) {
242
- switch (_a.label) {
243
- case 0: return [4 /*yield*/, this.onMapLoad()];
244
- case 1:
245
- _a.sent();
246
- keyname = options.keyname, resourceId = options.resourceId;
247
- if (keyname || resourceId !== undefined) {
248
- deprecatedWarn('set `resource` options instead of `keyname` or `resourceId`');
249
- }
250
- resource = options.resource;
251
- if (!keyname && !resourceId && !resource) {
252
- throw new Error('resource, resourceId or keyname is required parameter to add NGW layer');
253
- }
254
- if (!defined(this.options.baseUrl)) return [3 /*break*/, 5];
255
- _a.label = 2;
256
- case 2:
257
- _a.trys.push([2, 4, , 5]);
258
- if (defined(this.options.setViewDelay)) {
259
- options.adapterOptions = options.adapterOptions || {};
260
- if (!defined(options.adapterOptions.setViewDelay)) {
261
- options.adapterOptions.setViewDelay = this.options.setViewDelay;
262
- }
263
- }
264
- adapter = createNgwLayerAdapter(options, this, this.connector);
265
- adapterOpts = __assign(__assign({ visibility: true }, options), options.adapterOptions);
266
- return [4 /*yield*/, this.addLayer(adapter, adapterOpts)];
267
- case 3:
268
- layer = (_a.sent());
269
- id = layer && this.getLayerId(layer);
270
- if (layer && id) {
271
- this._ngwLayers[id] = { layer: layer, resourceId: layer.resourceId };
272
- layer.options.name =
273
- layer.options.name ||
274
- (layer.item && layer.item.resource.display_name);
275
- if (layer.options.baselayer) {
276
- visibleLayerBaseLayer = this.getActiveBaseLayer();
277
- if (visibleLayerBaseLayer) {
278
- return [2 /*return*/, layer];
279
- }
280
- }
281
- }
282
- return [2 /*return*/, layer];
283
- case 4:
284
- er_1 = _a.sent();
285
- resId = isObject(resource) && 'id' in resource
286
- ? resource.id
287
- : keyname || resourceId || resource;
288
- console.error("Can't add NGW layer ".concat(resId, "."), er_1);
289
- return [3 /*break*/, 5];
290
- case 5: return [2 /*return*/];
291
- }
292
- });
293
- });
294
- };
295
- /**
296
- * Pans and zooms the map to the initial position specified in the options
297
- */
298
- NgwMap.prototype.fit = function () {
299
- var _a = this.options, center = _a.center, zoom = _a.zoom, bounds = _a.bounds;
300
- if (center) {
301
- this.setCenter(center);
302
- if (zoom) {
303
- this.setZoom(zoom);
304
- }
305
- }
306
- else if (bounds) {
307
- this.fitBounds(bounds);
308
- }
309
- };
310
- NgwMap.prototype.fetchNgwLayerItem = function (options) {
311
- return fetchNgwLayerItem(__assign({ connector: this.connector }, options));
312
- };
313
- NgwMap.prototype.fetchNgwLayerItems = function (options) {
314
- return fetchNgwLayerItems(__assign({ connector: this.connector }, options));
315
- };
316
- NgwMap.prototype.fetchNgwLayerFeature = function (options) {
317
- return fetchNgwLayerFeature(__assign({ connector: this.connector }, options));
318
- };
319
- NgwMap.prototype.fetchNgwLayerFeatures = function (options) {
320
- return fetchNgwLayerFeatureCollection(__assign({ connector: this.connector }, options));
321
- };
322
- NgwMap.prototype.fetchIdentifyItem = function (identify, requestOptions) {
323
- var promise = fetchIdentifyItem({
324
- identify: identify,
325
- connector: this.connector,
326
- requestOptions: requestOptions,
327
- // multiple,
328
- });
329
- this._addPromise('identify', promise);
330
- return promise;
331
- };
332
- NgwMap.prototype.fetchIdentifyGeoJson = function (identify, multiple) {
333
- if (multiple === void 0) { multiple = false; }
334
- var promise = fetchIdentifyGeoJson({
335
- identify: identify,
336
- connector: this.connector,
337
- multiple: multiple,
338
- });
339
- if (promise && 'then' in promise) {
340
- this._addPromise('identify', promise);
341
- return promise;
342
- }
343
- else {
344
- return CancelablePromise.resolve(promise);
345
- }
346
- };
347
- /**
348
- * @deprecated use {@link fetchIdentifyGeoJson} instead
349
- */
350
- NgwMap.prototype.getIdentifyGeoJson = function (identify, multiple) {
351
- if (multiple === void 0) { multiple = false; }
352
- return this.fetchIdentifyGeoJson(identify, multiple);
353
- };
354
- NgwMap.prototype.getNgwLayers = function () {
355
- return __awaiter(this, void 0, void 0, function () {
356
- return __generator(this, function (_a) {
357
- switch (_a.label) {
358
- case 0: return [4 /*yield*/, this.onLoad()];
359
- case 1:
360
- _a.sent();
361
- return [2 /*return*/, this._ngwLayers];
362
- }
363
- });
364
- });
365
- };
366
- NgwMap.prototype.getNgwLayerByResourceId = function (id) {
367
- return __awaiter(this, void 0, void 0, function () {
368
- var _a, _b, _i, n, mem, ids, dependLayers, dependFit;
369
- return __generator(this, function (_c) {
370
- switch (_c.label) {
371
- case 0:
372
- _a = [];
373
- for (_b in this._ngwLayers)
374
- _a.push(_b);
375
- _i = 0;
376
- _c.label = 1;
377
- case 1:
378
- if (!(_i < _a.length)) return [3 /*break*/, 6];
379
- n = _a[_i];
380
- mem = this._ngwLayers[n];
381
- if (!(mem.resourceId === id)) return [3 /*break*/, 2];
382
- return [2 /*return*/, mem && mem.layer];
383
- case 2:
384
- if (!mem.layer.getIdentificationIds) return [3 /*break*/, 4];
385
- return [4 /*yield*/, mem.layer.getIdentificationIds()];
386
- case 3:
387
- ids = _c.sent();
388
- if (ids && ids.some(function (x) { return x === id; })) {
389
- return [2 /*return*/, mem.layer];
390
- }
391
- _c.label = 4;
392
- case 4:
393
- if (mem.layer.getDependLayers) {
394
- dependLayers = mem.layer.getDependLayers();
395
- dependFit = dependLayers.find(function (x) {
396
- return x.item && x.item.parentId === id;
397
- });
398
- if (dependFit) {
399
- return [2 /*return*/, dependFit.layer];
400
- }
401
- }
402
- _c.label = 5;
403
- case 5:
404
- _i++;
405
- return [3 /*break*/, 1];
406
- case 6: return [2 /*return*/];
407
- }
408
- });
409
- });
410
- };
411
- /**
412
- * Move map to layer. If the layer is NGW resource, extent will be received from the server
413
- *
414
- * @example
415
- * ```javascript
416
- * const ngwLayer = ngwMap.addNgwLayer({ id: 'ngw_layer_name', resource: 4005 });
417
- * ngwMap.fitLayer(ngwLayer);
418
- * ngwMap.fitLayer('ngw_layer_name');
419
- * ```
420
- */
421
- NgwMap.prototype.fitLayer = function (layerDef, options) {
422
- return __awaiter(this, void 0, void 0, function () {
423
- var id, ngwLayer, bounds, item, resourceId;
424
- var _this = this;
425
- return __generator(this, function (_a) {
426
- switch (_a.label) {
427
- case 0:
428
- if (typeof layerDef === 'string' || typeof layerDef === 'number') {
429
- id = String(id);
430
- }
431
- else {
432
- id = layerDef.id;
433
- }
434
- ngwLayer = id && this._ngwLayers[id];
435
- if (!ngwLayer) return [3 /*break*/, 7];
436
- if (!ngwLayer.layer.getBounds) return [3 /*break*/, 2];
437
- return [4 /*yield*/, ngwLayer.layer.getBounds()];
438
- case 1:
439
- bounds = _a.sent();
440
- if (bounds) {
441
- this.fitBounds(bounds, options);
442
- }
443
- return [3 /*break*/, 6];
444
- case 2:
445
- item = void 0;
446
- if (!ngwLayer.layer.item) return [3 /*break*/, 3];
447
- item = ngwLayer.layer.item;
448
- return [3 /*break*/, 5];
449
- case 3:
450
- resourceId = ngwLayer.resourceId;
451
- return [4 /*yield*/, this.connector.getResource(resourceId)];
452
- case 4:
453
- item = _a.sent();
454
- _a.label = 5;
455
- case 5:
456
- if (item) {
457
- fetchNgwExtent({
458
- resourceId: item.resource.id,
459
- connector: this.connector,
460
- }).then(function (extent) {
461
- if (extent) {
462
- _this.fitBounds(extent, options);
463
- }
464
- });
465
- }
466
- _a.label = 6;
467
- case 6: return [3 /*break*/, 8];
468
- case 7:
469
- _super.prototype.fitLayer.call(this, typeof layerDef === 'number' ? String(layerDef) : layerDef, options);
470
- _a.label = 8;
471
- case 8: return [2 /*return*/];
472
- }
473
- });
474
- });
475
- };
476
- /** @deprecated use {@link fitLayer} instead */
477
- NgwMap.prototype.zoomToLayer = function (layerDef) {
478
- return __awaiter(this, void 0, void 0, function () {
479
- return __generator(this, function (_a) {
480
- return [2 /*return*/, this.fitLayer(layerDef)];
481
- });
482
- });
483
- };
484
- NgwMap.prototype.onLoad = function (event) {
485
- if (event === void 0) { event = 'ngw-map:create'; }
486
- return _super.prototype.onLoad.call(this, event);
487
- };
488
- NgwMap.prototype.removeLayer = function (layerDef) {
489
- var layer = this.getLayer(layerDef);
490
- if (layer) {
491
- var layerId = this.getLayerId(layer);
492
- if (layerId) {
493
- delete this._ngwLayers[layerId];
494
- }
495
- _super.prototype.removeLayer.call(this, layer);
496
- }
497
- };
498
- NgwMap.prototype.enableSelection = function () {
499
- var _this = this;
500
- if (!this.$$selectFromNgwRaster) {
501
- this.$$selectFromNgwRaster = function (ev) {
502
- var count = _this._getSelectListenersCount();
503
- if (count) {
504
- _this.selectFromNgwRaster(ev);
505
- }
506
- };
507
- this.$$selectFromNgwVector = function (ev) {
508
- var count = _this._getSelectListenersCount();
509
- if (count) {
510
- _this._selectFromNgwVector(ev);
511
- }
512
- };
513
- this.emitter.on('click', this.$$selectFromNgwRaster);
514
- this.emitter.on('layer:click', this.$$selectFromNgwVector);
515
- }
516
- };
517
- NgwMap.prototype.disableSelection = function () {
518
- if (this.$$selectFromNgwRaster) {
519
- this.emitter.removeListener('click', this.$$selectFromNgwRaster);
520
- this.$$selectFromNgwRaster = undefined;
521
- }
522
- if (this.$$selectFromNgwVector) {
523
- this.emitter.removeListener('layer:click', this.$$selectFromNgwVector);
524
- this.$$selectFromNgwVector = undefined;
525
- }
526
- };
527
- /**
528
- * @deprecated use {@link fetchNgwLayerItem} instead
529
- */
530
- NgwMap.prototype.getNgwLayerItem = function (options) {
531
- return this.fetchNgwLayerItem(options);
532
- };
533
- /**
534
- * @deprecated use {@link fetchNgwLayerItems} instead
535
- */
536
- NgwMap.prototype.getNgwLayerItems = function (options) {
537
- return this.fetchNgwLayerItems(options);
538
- };
539
- /**
540
- * @deprecated use {@link fetchNgwLayerFeature} instead
541
- */
542
- NgwMap.prototype.getNgwLayerFeature = function (options) {
543
- return this.fetchNgwLayerFeature(options);
544
- };
545
- /**
546
- * @deprecated use {@link fetchNgwLayerFeatures} instead
547
- */
548
- NgwMap.prototype.getNgwLayerFeatures = function (options) {
549
- return this.fetchNgwLayerFeatures(options);
550
- };
551
- /** @deprecated use {@link cancelPromises} instead */
552
- NgwMap.prototype.cancelPromise = function () {
553
- var args = [];
554
- for (var _i = 0; _i < arguments.length; _i++) {
555
- args[_i] = arguments[_i];
556
- }
557
- this.cancelPromises.apply(this, args);
558
- };
559
- NgwMap.prototype.cancelPromises = function () {
560
- var _this = this;
561
- var args = [];
562
- for (var _i = 0; _i < arguments.length; _i++) {
563
- args[_i] = arguments[_i];
564
- }
565
- if (!args.length) {
566
- args = Object.keys(this._promises);
567
- }
568
- args.forEach(function (name) {
569
- var group = _this._promises[name];
570
- if (group) {
571
- group.forEach(function (x) { return x.cancel(); });
572
- _this._promises[name] = [];
573
- }
574
- });
575
- };
576
- NgwMap.prototype.selectFromNgwRaster = function (ev) {
577
- return __awaiter(this, void 0, void 0, function () {
578
- var promises, layers, _i, layers_1, l, layer, identFunc, getIdsPromise, getIds, ids, _a, getIds_1, x, pixelRadius, center, zoom, radius, selectPromise;
579
- var _this = this;
580
- return __generator(this, function (_b) {
581
- switch (_b.label) {
582
- case 0:
583
- this._emitStatusEvent('ngw:preselect');
584
- promises = [];
585
- layers = Object.values(this._ngwLayers);
586
- layers.sort(function (a, b) {
587
- if (a.layer.order && b.layer.order) {
588
- return b.layer.order - a.layer.order;
589
- }
590
- return 1;
591
- });
592
- for (_i = 0, layers_1 = layers; _i < layers_1.length; _i++) {
593
- l = layers_1[_i];
594
- layer = l.layer;
595
- identFunc = typeof layer.getIdentificationIds === 'function'
596
- ? layer.getIdentificationIds
597
- : false;
598
- if (identFunc && layer.options.selectable && this.isLayerVisible(layer)) {
599
- promises.push(identFunc.call(layer));
600
- }
601
- }
602
- getIdsPromise = Promise.all(promises);
603
- return [4 /*yield*/, getIdsPromise];
604
- case 1:
605
- getIds = _b.sent();
606
- ids = [];
607
- for (_a = 0, getIds_1 = getIds; _a < getIds_1.length; _a++) {
608
- x = getIds_1[_a];
609
- if (x) {
610
- ids.push.apply(ids, x);
611
- }
612
- }
613
- if (!ids.length) {
614
- this._emitStatusEvent('ngw:select', null);
615
- return [2 /*return*/];
616
- }
617
- pixelRadius = this.options.pixelRadius || 10;
618
- center = this.getCenter();
619
- zoom = this.getZoom();
620
- zoom = zoom !== undefined ? zoom : 20;
621
- if (!center || !zoom) {
622
- this._emitStatusEvent('ngw:select', null);
623
- return [2 /*return*/];
624
- }
625
- radius = getIdentifyRadius(center, zoom, pixelRadius);
626
- selectPromise = sendIdentifyRequest(ev, {
627
- layers: ids,
628
- connector: this.connector,
629
- radius: radius,
630
- }).then(function (resp) {
631
- var identify = __assign(__assign({}, resp), { resources: ids, sourceType: 'raster', event: ev });
632
- var identifyEvent = _this._prepareToIdentify(identify);
633
- _this._emitStatusEvent('ngw:select', identifyEvent);
634
- return identifyEvent;
635
- });
636
- this._addPromise('select', selectPromise);
637
- return [2 /*return*/, selectPromise];
638
- }
639
- });
640
- });
641
- };
642
- NgwMap.prototype._addPromise = function (groupName, promise) {
643
- var group = this._promises[groupName];
644
- if (group && group.indexOf(promise) === -1) {
645
- var removeFromGroup = function () {
646
- var index = group.indexOf(promise);
647
- if (index !== -1) {
648
- group.splice(index, 1);
649
- }
650
- };
651
- promise.then(removeFromGroup);
652
- promise.catch(removeFromGroup);
653
- group.push(promise);
654
- }
655
- };
656
- NgwMap.prototype._isFitFromResource = function () {
657
- var params = this._initMapState;
658
- if (params.zoom && params.center) {
659
- return false;
660
- }
661
- return true;
662
- };
663
- NgwMap.prototype._createWebMap = function () {
664
- return __awaiter(this, void 0, void 0, function () {
665
- var resources, layerFitAllowed, _i, _a, x, overwriteOptions, _b, resources_1, r, er_2;
666
- return __generator(this, function (_c) {
667
- switch (_c.label) {
668
- case 0: return [4 /*yield*/, this.create()];
669
- case 1:
670
- _c.sent();
671
- if (this.options.qmsId) {
672
- this._addQmsBaseLayer();
673
- }
674
- if (this.options.osm) {
675
- this._addOsmBaseLayer();
676
- }
677
- resources = [];
678
- layerFitAllowed = this._isFitFromResource();
679
- if (this.options.webmapId) {
680
- appendNgwResources(resources, this.options.webmapId, {
681
- fit: layerFitAllowed,
682
- });
683
- }
684
- if (this.options.resources && Array.isArray(this.options.resources)) {
685
- for (_i = 0, _a = this.options.resources; _i < _a.length; _i++) {
686
- x = _a[_i];
687
- overwriteOptions = {};
688
- if (!layerFitAllowed) {
689
- overwriteOptions.fit = false;
690
- }
691
- appendNgwResources(resources, x, {}, overwriteOptions);
692
- }
693
- }
694
- _b = 0, resources_1 = resources;
695
- _c.label = 2;
696
- case 2:
697
- if (!(_b < resources_1.length)) return [3 /*break*/, 7];
698
- r = resources_1[_b];
699
- _c.label = 3;
700
- case 3:
701
- _c.trys.push([3, 5, , 6]);
702
- return [4 /*yield*/, this.addNgwLayer(r)];
703
- case 4:
704
- _c.sent();
705
- return [3 /*break*/, 6];
706
- case 5:
707
- er_2 = _c.sent();
708
- console.warn(er_2);
709
- return [3 /*break*/, 6];
710
- case 6:
711
- _b++;
712
- return [3 /*break*/, 2];
713
- case 7:
714
- this._emitStatusEvent('ngw-map:create', this);
715
- this.enableSelection();
716
- return [2 /*return*/];
717
- }
718
- });
719
- });
720
- };
721
- NgwMap.prototype._addOsmBaseLayer = function () {
722
- this.addBaseLayer('OSM');
723
- };
724
- NgwMap.prototype._addQmsBaseLayer = function () {
725
- var qmsId;
726
- var qmsLayerName;
727
- if (Array.isArray(this.options.qmsId)) {
728
- qmsId = this.options.qmsId[0];
729
- qmsLayerName = this.options.qmsId[1];
730
- }
731
- else {
732
- qmsId = Number(this.options.qmsId);
733
- }
734
- var qmsLayerOptions = {
735
- qmsId: qmsId,
736
- };
737
- if (qmsLayerName) {
738
- qmsLayerOptions.id = qmsLayerName;
739
- }
740
- this.addBaseLayer('QMS', qmsLayerOptions);
741
- };
742
- NgwMap.prototype._selectFromNgwVector = function (ev) {
743
- var _a;
744
- var layer = ev.layer;
745
- var selectable = layer.options.selectable && this.isLayerVisible(layer);
746
- if (!selectable) {
747
- return undefined;
748
- }
749
- // Item property means layer is NgwResource
750
- var id = layer.item && layer.item.resource.id;
751
- var feature = ev.feature;
752
- if (id !== undefined && feature) {
753
- var featureId = feature.id;
754
- if (featureId) {
755
- var identifyFeature = {
756
- id: Number(featureId),
757
- fields: feature.properties || {},
758
- label: "#".concat(id),
759
- layerId: Number(id),
760
- parent: '',
761
- geom: feature.geometry,
762
- };
763
- var items = {
764
- featureCount: 1,
765
- features: [identifyFeature],
766
- };
767
- var identify = (_a = {
768
- featureCount: 1
769
- },
770
- _a[id] = items,
771
- _a);
772
- this._emitStatusEvent('ngw:select', this._prepareToIdentify(__assign(__assign({}, identify), { resources: [id], sourceType: 'vector' })));
773
- return identify;
774
- }
775
- }
776
- };
777
- NgwMap.prototype._prepareToIdentify = function (identify) {
778
- var _this = this;
779
- var getIdentifyItems_ = function () {
780
- return getIdentifyItems(identify, true).map(function (x) {
781
- return createIdentifyItem({
782
- feature: x.feature,
783
- connector: _this.connector,
784
- });
785
- });
786
- };
787
- return __assign(__assign({}, identify), { getIdentifyItems: getIdentifyItems_ });
788
- };
789
- NgwMap.prototype._getSelectListenersCount = function () {
790
- return this.emitter.listenerCount('ngw:select');
791
- };
792
- NgwMap.prototype._whiteLabel = function () {
793
- return __awaiter(this, void 0, void 0, function () {
794
- var container, logo;
795
- return __generator(this, function (_a) {
796
- switch (_a.label) {
797
- case 0:
798
- container = this.getContainer();
799
- if (!container) return [3 /*break*/, 2];
800
- return [4 /*yield*/, getCompanyLogo(this.connector, this.options.companyLogoOptions)];
801
- case 1:
802
- logo = _a.sent();
803
- if (logo) {
804
- container.appendChild(logo);
805
- }
806
- _a.label = 2;
807
- case 2: return [2 /*return*/];
808
- }
809
- });
810
- });
811
- };
812
- NgwMap.getIcon = getIcon;
813
- return NgwMap;
814
- }(WebMap));
815
-
816
- /**
817
- * @module ngw-map
818
- */
819
- function createNgwMap(options) {
820
- return __awaiter(this, void 0, void 0, function () {
821
- var ngwMap;
822
- return __generator(this, function (_a) {
823
- ngwMap = new NgwMap(options);
824
- return [2 /*return*/, ngwMap.onLoad()];
825
- });
826
- });
643
+ /**
644
+ * @module ngw-map
645
+ */
646
+ async function createNgwMap(options) {
647
+ const ngwMap = new NgwMap(options);
648
+ return ngwMap.onLoad();
827
649
  }
828
650
 
829
651
  export { NgwMap, createNgwMap };