@juun-roh/cesium-utils
Version:
Solve common Cesium.js challenges: combine multiple terrain sources, tag and filter entity collections, and add visual highlights.
2 lines (1 loc) • 6.82 kB
JavaScript
"use strict";var c=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var g=(s,e)=>{for(var t in e)c(s,t,{get:e[t],enumerable:!0})},C=(s,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of y(e))!f.call(s,n)&&n!==t&&c(s,n,{get:()=>e[n],enumerable:!(i=m(e,n))||i.enumerable});return s};var _=s=>C(c({},"__esModule",{value:!0}),s);var T={};g(T,{Collection:()=>p});module.exports=_(T);var a=require("cesium");function h(s,e){let t=!1,i=Object.getOwnPropertyDescriptor(s,e);if(!i){let n=Object.getPrototypeOf(s);for(;n&&!i;)i=Object.getOwnPropertyDescriptor(n,e),n=Object.getPrototypeOf(n)}return i&&i.get&&!i.set&&(t=!0),t}function d(s,e,t){let i=e.split(".");for(let l of i)if(v(l))return{success:!1,reason:"dangerous-property",message:`Property path contains dangerous property name: "${l}"`};let n=s,r=0;for(;r<i.length-1;r++){let l=i[r];if(!n||typeof n!="object"||!Object.prototype.hasOwnProperty.call(n,l))return{success:!1,reason:"invalid-path",message:`Property path "${e}" does not exist or contains inherited properties`};if(n=n[l],!n||typeof n!="object"||n===Object.prototype)return{success:!1,reason:"invalid-path",message:`Cannot traverse path "${e}" - reached non-object or prototype`}}if(r!==i.length-1)return{success:!1,reason:"invalid-path",message:`Failed to traverse property path "${e}"`};let o=i[i.length-1];if(v(o))return{success:!1,reason:"dangerous-property",message:`Cannot set dangerous property "${o}"`};if(!n||typeof n!="object"||n===Object.prototype)return{success:!1,reason:"invalid-path",message:"Cannot set property on invalid target"};if(o in n){if(typeof n[o]=="function")return{success:!1,reason:"function-property",message:`Cannot set function property "${o}"`};if(h(n,o))return{success:!1,reason:"read-only",message:`Cannot set read-only property "${e}"`}}return n[o]=t,{success:!0}}var b=["__proto__","constructor","prototype"];function v(s){return b.includes(s)}var u=class s{static symbol=Symbol("cesium-item-tag");tag;collection;_valuesCache=null;_tagMap=new Map;_eventListeners=new Map;_eventCleanupFunctions=[];constructor({collection:e,tag:t}){this.tag=t||"default",this.collection=e,this._setupCacheInvalidator(e)}[Symbol.iterator](){return this.values[Symbol.iterator]()}get values(){if(this._valuesCache!==null)return this._valuesCache;let e;if(this.collection instanceof a.EntityCollection)e=this.collection.values;else{e=[];for(let t=0;t<this.collection.length;t++)e.push(this.collection.get(t))}return this._valuesCache=e,e}get length(){return this.values?.length||0}get tags(){return Array.from(this._tagMap.keys())}addEventListener(e,t){return this._eventListeners.has(e)||this._eventListeners.set(e,new Set),this._eventListeners.get(e)?.add(t),this}removeEventListener(e,t){return this._eventListeners.get(e)?.delete(t),this}add(e,t=this.tag,i){return Array.isArray(e)?e.forEach(n=>{this.add(n,t)}):(Object.defineProperty(e,s.symbol,{value:t,enumerable:!1,writable:!0,configurable:!0}),this.collection.add(e,i),this._addToTagMap(e,t),this._invalidateCache(),this._emit("add",{items:[e],tag:t})),this}contains(e){if(typeof e=="object")return this.collection.contains(e);let t=this._tagMap.get(e);return!!t&&t.size>0}remove(e){if(typeof e=="object"&&!Array.isArray(e)&&this.collection.remove(e)&&(this._removeFromTagMap(e),this._invalidateCache(),this._emit("remove",{items:[e]})),Array.isArray(e)){if(e.length===0)return this;for(let i of e)this.remove(i)}let t=this.get(e);if(t.length===0)return this;for(let i of t)this.remove(i);return this}removeAll(){this._tagMap.clear(),this.collection.removeAll(),this._invalidateCache(),this._emit("clear")}destroy(){this._eventCleanupFunctions.forEach(e=>e()),this._eventCleanupFunctions=[],this._tagMap.clear(),this._eventListeners.clear(),this._valuesCache=null}get(e){let t=this._tagMap.get(e);return t?Array.from(t):[]}first(e){let t=this._tagMap.get(e);if(t&&t.size>0)return t.values().next().value}update(e,t){let i=this.get(e);for(let n of i)this._removeFromTagMap(n),Object.defineProperty(n,s.symbol,{value:t,enumerable:!1,writable:!0,configurable:!0}),this._addToTagMap(n,t);return i.length>0&&this._emit("update",{items:i,tag:t}),i.length}show(e){let t=this.get(e);for(let i of t)(0,a.defined)(i.show)&&(i.show=!0);return this}hide(e){let t=this.get(e);for(let i of t)(0,a.defined)(i.show)&&(i.show=!1);return this}toggle(e){let t=this.get(e);for(let i of t)(0,a.defined)(i.show)&&(i.show=!i.show);return this}setProperty(e,t,i=this.tag){let n=this.get(i);for(let r of n){let o=d(r,e,t);if(!o.success&&o.reason==="read-only")throw Error(`Cannot set read-only property '${e}' on ${r.constructor.name}`)}return this}filter(e,t){return(t?this.get(t):this.values).filter(e)}forEach(e,t){(t?this.get(t):this.values).forEach((n,r)=>e(n,r))}map(e,t){return(t?this.get(t):this.values).map(e)}find(e,t){return(t?this.get(t):this.values).find(e)}_emit(e,t){let i=this._eventListeners.get(e);if(i){let n={type:e,...t};i.forEach(r=>r(n))}}_addToTagMap(e,t){this._tagMap.has(t)||this._tagMap.set(t,new Set),this._tagMap.get(t)?.add(e)}_removeFromTagMap(e){let t=e[s.symbol],i=this._tagMap.get(t);i&&(i.delete(e),i.size===0&&this._tagMap.delete(t))}_invalidateCache=()=>{this._valuesCache=null};_setupCacheInvalidator(e){e instanceof a.EntityCollection?(e.collectionChanged.addEventListener(this._invalidateCache),this._eventCleanupFunctions.push(()=>e.collectionChanged.removeEventListener(this._invalidateCache))):e instanceof a.PrimitiveCollection?(e.primitiveAdded.addEventListener(this._invalidateCache),e.primitiveRemoved.addEventListener(this._invalidateCache),this._eventCleanupFunctions.push(()=>e.primitiveAdded.removeEventListener(this._invalidateCache),()=>e.primitiveRemoved.removeEventListener(this._invalidateCache))):e instanceof a.DataSourceCollection?(e.dataSourceAdded.addEventListener(this._invalidateCache),e.dataSourceMoved.addEventListener(this._invalidateCache),e.dataSourceRemoved.addEventListener(this._invalidateCache),this._eventCleanupFunctions.push(()=>e.dataSourceAdded.removeEventListener(this._invalidateCache),()=>e.dataSourceMoved.removeEventListener(this._invalidateCache),()=>e.dataSourceRemoved.removeEventListener(this._invalidateCache))):e instanceof a.ImageryLayerCollection&&(e.layerAdded.addEventListener(this._invalidateCache),e.layerMoved.addEventListener(this._invalidateCache),e.layerRemoved.addEventListener(this._invalidateCache),e.layerShownOrHidden.addEventListener(this._invalidateCache),this._eventCleanupFunctions.push(()=>e.layerAdded.removeEventListener(this._invalidateCache),()=>e.layerMoved.removeEventListener(this._invalidateCache),()=>e.layerRemoved.removeEventListener(this._invalidateCache),()=>e.layerShownOrHidden.removeEventListener(this._invalidateCache)))}},p=u;