@nextgis/ngw-map
Version:
3 lines (2 loc) • 11.8 kB
JavaScript
import{getIcon as e}from"@nextgis/icons";import t from"@nextgis/ngw-connector";import{NgwKit as r,fetchNgwLayerItem as o,fetchNgwLayerItems as s,fetchNgwLayerFeature as i,fetchNgwLayerFeatureCollection as n,fetchIdentifyItem as a,fetchIdentifyGeoJson as c,createNgwLayerAdapter as l,fetchNgwExtent as h,sendIdentifyRequest as u,getCompanyLogo as d,getIdentifyItems as y,createIdentifyItem as p}from"@nextgis/ngw-kit";import{deepmerge as m,deprecatedWarn as f,defined as g,isObject as w,getIdentifyRadius as b,getCirclePolygonCoordinates as L}from"@nextgis/utils";import{getDefaultControls as I,WebMap as v}from"@nextgis/webmap";export*from"@nextgis/webmap";import{EventEmitter as _}from"events";import{QmsKit as O}from"@nextgis/qms-kit";var N=Object.defineProperty,P=Object.defineProperties,F=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertySymbols,j=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable,x=(e,t,r)=>t in e?N(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,C=(e,t)=>{for(var r in t||(t={}))j.call(t,r)&&x(e,r,t[r]);if($)for(var r of $(t))S.call(t,r)&&x(e,r,t[r]);return e},R=(e,t)=>P(e,F(t));function E(e,t,r,o){if("number"==typeof t||"string"==typeof t)t=Number(t),e.push(R(C({},r),{resource:t}));else if(Array.isArray(t)){const[s,i]=t;e.push(C(R(C({},r),{resource:s,id:i}),o))}else"object"==typeof t&&e.push(C(C(C({},r),t),o))}var A=Object.defineProperty,B=Object.defineProperties,V=Object.getOwnPropertyDescriptors,k=Object.getOwnPropertySymbols,q=Object.prototype.hasOwnProperty,T=Object.prototype.propertyIsEnumerable,D=(e,t,r)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;const G={target:"map",baseUrl:"",whitlabel:!1,controls:I(),controlsOptions:{ZOOM:{position:"top-left"},ATTRIBUTION:{position:"bottom-right",customAttribution:['<a href="https://nextgis.com" target="_blank">©NextGIS</a>']}},pixelRadius:10};function M(e){const o=[new O];var s;return e.starterKits&&e.starterKits.forEach(e=>{o.push(e)}),e.connector?e.connector&&(e.baseUrl=e.connector.options.baseUrl):e.connector=new t({baseUrl:e.baseUrl||"",auth:e.auth,withCredentials:e.withCredentials}),(e=m(G,e)).center||e.bounds||(e.bounds=[-179,-90,180,90]),e.connector&&o.push(new r({connector:e.connector,auth:e.auth})),s=((e,t)=>{for(var r in t||(t={}))q.call(t,r)&&D(e,r,t[r]);if(k)for(var r of k(t))T.call(t,r)&&D(e,r,t[r]);return e})({},e),e=B(s,V({starterKits:o,create:!1}))}var U=Object.defineProperty,J=Object.defineProperties,W=Object.getOwnPropertyDescriptors,z=Object.getOwnPropertySymbols,K=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,H=Reflect.get,X=(e,t,r)=>t in e?U(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,Y=(e,t)=>{for(var r in t||(t={}))Q.call(t,r)&&X(e,r,t[r]);if(z)for(var r of z(t))Z.call(t,r)&&X(e,r,t[r]);return e},ee=(e,t)=>J(e,W(t)),te=(e,t,r)=>X(e,"symbol"!=typeof t?t+"":t,r),re=(e,t,r)=>H(K(e),r,t),oe=(e,t,r)=>new Promise((o,s)=>{var i=e=>{try{a(r.next(e))}catch(t){s(t)}},n=e=>{try{a(r.throw(e))}catch(t){s(t)}},a=e=>e.done?o(e.value):Promise.resolve(e.value).then(i,n);a((r=r.apply(e,t)).next())});const se=class e extends v{constructor(e){super(M(e)),te(this,"emitter",new _),te(this,"connector"),te(this,"_ngwLayers",{}),te(this,"$$selectFromNgwRaster"),te(this,"$$selectFromNgwVector"),te(this,"_promises",{select:[],identify:[]}),e.connector&&(this.connector=e.connector),this._createWebMap().then(()=>{const e=this.getContainer();e&&e.classList.add("ngw-map-container"),this.options.whitlabel&&this._whiteLabel()})}addControl(t,r,o){return oe(this,null,function*(){return yield this.onLoad("controls:create"),re(e.prototype,this,"addControl").call(this,t,r,o)})}addNgwLayer(e){return oe(this,null,function*(){yield this.onMapLoad();const{keyname:t,resourceId:r}=e;(t||void 0!==r)&&f("set `resource` options instead of `keyname` or `resourceId`");const o=e.resource;if(!t&&!r&&!o)throw new Error("resource, resourceId or keyname is required parameter to add NGW layer");if(g(this.options.baseUrl))try{g(this.options.setViewDelay)&&(e.adapterOptions=e.adapterOptions||{},g(e.adapterOptions.setViewDelay)||(e.adapterOptions.setViewDelay=this.options.setViewDelay));const t=l(e,this,this.connector),r=Y(Y({visibility:!0},e),e.adapterOptions),o=yield this.addLayer(t,r),s=o&&this.getLayerId(o);if(o&&s&&(this._ngwLayers[s]={layer:o,resourceId:o.resourceId},o.options.name=o.options.name||o.item&&o.item.resource.display_name,o.options.baselayer)){if(this.getActiveBaseLayer())return o}return o}catch(s){const e=w(o)&&"id"in o?o.id:t||r||o;console.error(`Can't add NGW layer ${e}.`,s)}})}fit(){const{center:e,zoom:t,bounds:r}=this.options;e?(this.setCenter(e),t&&this.setZoom(t)):r&&this.fitBounds(r)}fetchNgwLayerItem(e){return o(Y({connector:this.connector},e))}fetchNgwLayerItems(e){return s(Y({connector:this.connector},e))}fetchNgwLayerFeature(e){return i(Y({connector:this.connector},e))}fetchNgwLayerFeatures(e){return n(Y({connector:this.connector},e))}fetchIdentifyItem(e,t){const r=new AbortController,o=r.signal;(null==t?void 0:t.signal)&&t.signal.addEventListener("abort",r.abort),(t=t||{}).signal=o;const s=a({identify:e,connector:this.connector,requestOptions:t});return this._addPromise("identify",s,r),s}fetchIdentifyGeoJson(e,{multiple:r,signal:o}={}){const s=new AbortController;if(o){if(o.aborted)return Promise.reject(new t.errors.AbortError);o.addEventListener("abort",()=>{s.abort("AbortError")})}const i=c({identify:e,connector:this.connector,multiple:r,requestOptions:{signal:s.signal}});return i&&"then"in i?(this._addPromise("identify",i,s),i):Promise.resolve(i)}getIdentifyGeoJson(e,t=!1){return this.fetchIdentifyGeoJson(e,{multiple:t})}getNgwLayers(){return oe(this,null,function*(){return yield this.onLoad(),this._ngwLayers})}getNgwLayerByResourceId(e){return oe(this,null,function*(){for(const t in this._ngwLayers){const r=this._ngwLayers[t];if(r.resourceId===e)return r&&r.layer;if(r.layer.getIdentificationIds){const t=yield r.layer.getIdentificationIds();if(t&&t.some(t=>t===e))return r.layer}if(r.layer.getDependLayers){const t=r.layer.getDependLayers().find(t=>t.item&&"style_parent_id"in t.item&&void 0!==t.item.style_parent_id&&t.item.style_parent_id===e);if(t)return t.layer}}})}fitLayer(t,r){return oe(this,null,function*(){let o;o="string"==typeof t||"number"==typeof t?String(o):t.id;const s=o&&this._ngwLayers[o];if(s)if(s.layer.getBounds){const e=yield s.layer.getBounds();e&&this.fitBounds(e,r)}else{let e;if(s.layer.item)e=s.layer.item;else{const t=s.resourceId;e=yield this.connector.getResource(t)}e&&this.fitResource(e.resource.id)}else re(e.prototype,this,"fitLayer").call(this,"number"==typeof t?String(t):t,r)})}fitResource(e,t){return oe(this,null,function*(){const r=yield this.connector.resources.getIdOrFail(e),o=yield h({resourceId:r,connector:this.connector});o&&this.fitBounds(o,t)})}zoomToLayer(e){return oe(this,null,function*(){return this.fitLayer(e)})}onLoad(e="ngw-map:create"){return super.onLoad(e)}removeLayer(e){const t=this.getLayer(e);if(t){const e=this.getLayerId(t);e&&delete this._ngwLayers[e],super.removeLayer(t)}}enableSelection(){this.$$selectFromNgwRaster||(this.$$selectFromNgwRaster=e=>{this._getSelectListenersCount()&&this.selectFromNgwRaster(e)},this.$$selectFromNgwVector=e=>{this._getSelectListenersCount()&&this._selectFromNgwVector(e)},this.emitter.on("click",this.$$selectFromNgwRaster),this.emitter.on("layer:click",this.$$selectFromNgwVector))}disableSelection(){this.$$selectFromNgwRaster&&(this.emitter.removeListener("click",this.$$selectFromNgwRaster),this.$$selectFromNgwRaster=void 0),this.$$selectFromNgwVector&&(this.emitter.removeListener("layer:click",this.$$selectFromNgwVector),this.$$selectFromNgwVector=void 0)}getNgwLayerItem(e){return this.fetchNgwLayerItem(e)}getNgwLayerItems(e){return this.fetchNgwLayerItems(e)}getNgwLayerFeature(e){return this.fetchNgwLayerFeature(e)}getNgwLayerFeatures(e){return this.fetchNgwLayerFeatures(e)}cancelPromise(...e){this.cancelPromises(...e)}cancelPromises(...e){e.length||(e=Object.keys(this._promises)),e.forEach(e=>{const t=this._promises[e];t&&(t.forEach(e=>e[1].abort()),this._promises[e]=[])})}selectFromNgwRaster(e){return oe(this,null,function*(){var t;this._emitStatusEvent("ngw:preselect");const r=[],o=Object.values(this._ngwLayers);o.sort((e,t)=>e.layer.order&&t.layer.order?t.layer.order-e.layer.order:1);for(const e of o){const o=e.layer,s="function"==typeof o.getIdentificationIds&&o.getIdentificationIds,i=null==(t=o.options.interactive)||t;if(s&&o.options.selectable&&i&&this.isLayerVisible(o)){const e=s.call(o);r.push(e)}}const s=Promise.all(r),i=yield s,n=[];for(const e of i)e&&n.push(...e);if(!n.length)return void this._emitStatusEvent("ngw:select",null);const a=this.options.pixelRadius||10,c=this.getCenter();let l=this.getZoom();if(l=void 0!==l?l:20,!c||!l)return void this._emitStatusEvent("ngw:select",null);const h=b(c,l,a);let d;if(this.options.highlightIdentification){const t=this.options.highlightIdentification,r="number"==typeof t?t:1e3,[o,s]=e.lngLat;d={type:"Polygon",coordinates:[L(o,s,h)]};const i=yield this.addGeoJsonLayer({data:{type:"Feature",geometry:d}});r!==1/0&&setTimeout(()=>{this.removeLayer(i)},r)}const y=new AbortController,p=u(e,{layers:n,connector:this.connector,radius:h,geom:d,signal:y.signal}).then(t=>{const r=ee(Y({},t),{resources:n,sourceType:"raster",event:e}),o=this._prepareToIdentify(r);return this._emitStatusEvent("ngw:select",o),o});return this._addPromise("select",p,y),p})}_addPromise(e,t,r){const o=this._promises[e];if(o&&-1===o.findIndex(e=>e[0]===t)){const e=()=>{const e=o.findIndex(e=>e[0]===t);-1!==e&&o.splice(e,1)};t.then(e,e),o.push([t,r])}}_isFitFromResource(){const e=this._initMapState;return!e.zoom||!e.center}_createWebMap(){return oe(this,null,function*(){yield this.create(),this.options.qmsId&&this._addQmsBaseLayer(),this.options.osm&&this._addOsmBaseLayer();const e=[],t=this._isFitFromResource();if(this.options.webmapId&&E(e,this.options.webmapId,{fit:t}),this.options.resources&&Array.isArray(this.options.resources))for(const o of this.options.resources){const r={};t||(r.fit=!1),E(e,o,{},r)}for(const o of e)try{yield this.addNgwLayer(o)}catch(r){console.warn(r)}this._emitStatusEvent("ngw-map:create",this),this.enableSelection()})}_addOsmBaseLayer(){this.addBaseLayer("OSM")}_addQmsBaseLayer(){let e,t;Array.isArray(this.options.qmsId)?(e=this.options.qmsId[0],t=this.options.qmsId[1]):e=Number(this.options.qmsId);const r={qmsId:e};t&&(r.id=t),this.addBaseLayer("QMS",r)}_selectFromNgwVector(e){const t=e.layer;if(!(t.options.selectable&&this.isLayerVisible(t)))return;const r=t.item&&t.item.resource.id,o=e.feature;if(void 0!==r&&o){const e=o.id;if(e){const t={featureCount:1,features:[{id:Number(e),fields:o.properties||{},label:`#${r}`,layerId:Number(r),parent:"",geom:o.geometry}]},s={featureCount:1,[r]:t};return this._emitStatusEvent("ngw:select",this._prepareToIdentify(ee(Y({},s),{resources:[r],sourceType:"vector"}))),s}}}_prepareToIdentify(e){return ee(Y({},e),{getIdentifyItems:()=>y(e,!0).map(e=>p({feature:e.feature,connector:this.connector}))})}_getSelectListenersCount(){return this.emitter.listenerCount("ngw:select")}_whiteLabel(){return oe(this,null,function*(){const e=this.getContainer();if(e){const t=yield d(this.connector,this.options.companyLogoOptions);t&&e.appendChild(t)}})}};te(se,"getIcon",e);let ie=se;function ne(e){return t=this,r=null,o=function*(){return new ie(e).onLoad()},new Promise((e,s)=>{var i=e=>{try{a(o.next(e))}catch(t){s(t)}},n=e=>{try{a(o.throw(e))}catch(t){s(t)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,n);a((o=o.apply(t,r)).next())});var t,r,o}export{ie as NgwMap,ne as createNgwMap};
//# sourceMappingURL=ngw-map.esm-bundler.prod.js.map