@juun-roh/cesium-utils 0.0.3 → 0.0.5

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.
@@ -0,0 +1,363 @@
1
+ import { BillboardCollection, DataSourceCollection, EntityCollection, ImageryLayerCollection, LabelCollection, PointPrimitiveCollection, PolylineCollection, PrimitiveCollection, Billboard, DataSource, Entity, ImageryLayer, Label, PointPrimitive, Polyline, Primitive, Cesium3DTileset, GroundPrimitive } from 'cesium';
2
+
3
+ type CesiumCollection = BillboardCollection | DataSourceCollection | EntityCollection | ImageryLayerCollection | LabelCollection | PointPrimitiveCollection | PolylineCollection | PrimitiveCollection;
4
+ type Primitives = Primitive | Cesium3DTileset | GroundPrimitive;
5
+ type CesiumCollectionItem = Billboard | DataSource | Entity | ImageryLayer | Label | PointPrimitive | Polyline | Primitives;
6
+ type Tag = string | number;
7
+ interface WithTag {
8
+ [key: symbol]: Tag;
9
+ }
10
+ /**
11
+ * Collection event types
12
+ */
13
+ type CollectionEventType = 'add' | 'remove' | 'update' | 'clear';
14
+ /**
15
+ * Event handler function type
16
+ */
17
+ type EventHandler<I> = (event: {
18
+ type: CollectionEventType;
19
+ items?: I[];
20
+ tag?: Tag;
21
+ }) => void;
22
+
23
+ /**
24
+ * @class
25
+ * Abstract class that enhances Cesium collection objects with tagging functionality.
26
+ * This class provides a consistent API for working with different types of Cesium collections
27
+ * and allows grouping and manipulating collection items by custom tags.
28
+ *
29
+ * @template C - The type of Cesium collection (e.g., EntityCollection, PrimitiveCollection)
30
+ * @template I - The type of items in the collection (e.g., Entity, Primitive)
31
+ *
32
+ * @example
33
+ * // Creating a specialized collection for entities
34
+ * class MyEntities extends Collection<EntityCollection, Entity> {
35
+ * constructor(viewer) {
36
+ * super({ collection: viewer.entities, tag: 'myEntities' });
37
+ * }
38
+ *
39
+ * get values() {
40
+ * return this.collection.values;
41
+ * }
42
+ * }
43
+ *
44
+ * const entities = new MyEntities(viewer);
45
+ * entities.add(new Entity({ ... }), 'buildings');
46
+ * entities.add(new Entity({ ... }), 'roads');
47
+ *
48
+ * // Later, show only buildings
49
+ * entities.show('buildings');
50
+ * entities.hide('roads');
51
+ */
52
+ declare class Collection<C extends CesiumCollection, I extends CesiumCollectionItem> {
53
+ /**
54
+ * Symbol used as a property key to store tags on collection items.
55
+ * Using a Symbol ensures no property naming conflicts with the item's own properties.
56
+ * @readonly
57
+ */
58
+ static readonly symbol: unique symbol;
59
+ /**
60
+ * Default tag used when adding items without specifying a tag.
61
+ * @protected
62
+ */
63
+ protected tag: Tag;
64
+ /**
65
+ * The underlying Cesium collection being wrapped.
66
+ * @protected
67
+ */
68
+ protected collection: C;
69
+ /**
70
+ * Cache for values array to improve performance
71
+ * @private
72
+ */
73
+ private _valuesCache;
74
+ /**
75
+ * Tag to items map for faster lookups
76
+ * @private
77
+ */
78
+ private _tagMap;
79
+ /**
80
+ * Event listeners
81
+ * @private
82
+ */
83
+ private _eventListeners;
84
+ /**
85
+ * Creates a new Collection instance.
86
+ *
87
+ * @param options - Configuration options
88
+ * @param options.collection - The Cesium collection to wrap
89
+ * @param options.tag - The default tag to use for items (defaults to 'default')
90
+ */
91
+ constructor({ collection, tag }: {
92
+ collection: C;
93
+ tag?: Tag;
94
+ });
95
+ /**
96
+ * Emits an event to all registered listeners.
97
+ *
98
+ * @private
99
+ * @param type - The event type
100
+ * @param data - Additional event data
101
+ */
102
+ private _emit;
103
+ /**
104
+ * Adds an item to the internal tag map for quick lookups.
105
+ *
106
+ * @private
107
+ * @param item - The item to add
108
+ * @param tag - The tag to associate with the item
109
+ */
110
+ private _addToTagMap;
111
+ /**
112
+ * Removes an item from the internal tag map.
113
+ *
114
+ * @private
115
+ * @param item - The item to remove
116
+ */
117
+ private _removeFromTagMap;
118
+ /**
119
+ * Invalidates the values cache when collection changes.
120
+ *
121
+ * @private
122
+ */
123
+ private _invalidateCache;
124
+ /**
125
+ * Registers an event listener for collection events.
126
+ *
127
+ * @param type - The event type to listen for
128
+ * @param handler - The callback function
129
+ * @returns The collection instance for method chaining
130
+ */
131
+ addEventListener(type: CollectionEventType, handler: EventHandler<I>): this;
132
+ /**
133
+ * Removes an event listener.
134
+ *
135
+ * @param type - The event type
136
+ * @param handler - The callback function to remove
137
+ * @returns The collection instance for method chaining
138
+ */
139
+ removeEventListener(type: CollectionEventType, handler: EventHandler<I>): this;
140
+ /**
141
+ * Adds a single item with a tag to the collection.
142
+ *
143
+ * @param item - The item to add to the collection
144
+ * @param tag - Tag to associate with this item (defaults to the collection's default tag)
145
+ * @param index - The index to add the item at (if supported by the collection)
146
+ * @returns The added item for chaining
147
+ *
148
+ * @example
149
+ * const entity = collection.add(new Entity({ ... }), 'landmarks');
150
+ */
151
+ add(item: I, tag?: Tag, index?: number): I;
152
+ /**
153
+ * Adds multiple items with the same tag to the collection.
154
+ *
155
+ * @param items - The array of items to add to the collection
156
+ * @param tag - Tag to associate with this item (defaults to the collection's default tag)
157
+ * @returns The array of added items
158
+ *
159
+ * @example
160
+ * // Add multiple entities with the same tag
161
+ * const entities = [new Entity({ ... }), new Entity({ ... })];
162
+ * const addedEntities = collection.add(entities, 'buildings');
163
+ */
164
+ add(items: I[], tag?: Tag): I[];
165
+ /**
166
+ * Returns true if the provided item is in this collection, false otherwise.
167
+ *
168
+ * @param item - The item to check for
169
+ * @returns True if the item is in the collection, false otherwise
170
+ */
171
+ contains(item: I): boolean;
172
+ /**
173
+ * Removes an item from the collection.
174
+ *
175
+ * @param item - The item to remove
176
+ * @returns True if the item was removed, false if it wasn't found
177
+ */
178
+ remove(item: I): boolean;
179
+ /**
180
+ * Removes all items from the collection.
181
+ */
182
+ removeAll(): void;
183
+ /**
184
+ * Gets all item instances in the collection.
185
+ * This array should not be modified directly.
186
+ *
187
+ * @returns An array of all items in the collection
188
+ */
189
+ get values(): I[];
190
+ /**
191
+ * Gets the number of items in the collection.
192
+ *
193
+ * @returns The item count
194
+ */
195
+ get length(): number;
196
+ /**
197
+ * Gets all items with the specified tag from the collection.
198
+ * Uses an optimized internal map for faster lookups.
199
+ *
200
+ * @param tag - The tag to filter by
201
+ * @returns An array of items with the specified tag, or an empty array if none found
202
+ *
203
+ * @example
204
+ * // Get all buildings
205
+ * const buildings = collection.getByTag('buildings');
206
+ */
207
+ getByTag(tag: Tag): I[];
208
+ /**
209
+ * Gets the first item matching the tag. More efficient than getByTag when
210
+ * you only need one item, especially for large collections.
211
+ *
212
+ * @param tag - The tag to search for
213
+ * @returns The first matching item or undefined if none found
214
+ *
215
+ * @example
216
+ * // Get the first building
217
+ * const firstBuilding = collection.getFirstByTag('buildings');
218
+ */
219
+ getFirstByTag(tag: Tag): I | undefined;
220
+ /**
221
+ * Gets all unique tags currently in use in the collection.
222
+ *
223
+ * @returns An array of all unique tags
224
+ *
225
+ * @example
226
+ * // Get all tags
227
+ * const tags = collection.getTags();
228
+ * console.log(`Collection has these tags: ${tags.join(', ')}`);
229
+ */
230
+ getTags(): Tag[];
231
+ /**
232
+ * Checks if the collection has any items with the specified tag.
233
+ *
234
+ * @param tag - The tag to check for
235
+ * @returns True if items with the tag exist, false otherwise
236
+ *
237
+ * @example
238
+ * if (collection.hasTag('temporary')) {
239
+ * console.log('Temporary items exist');
240
+ * }
241
+ */
242
+ hasTag(tag: Tag): boolean;
243
+ /**
244
+ * Updates the tag for all items with the specified tag.
245
+ *
246
+ * @param oldTag - The tag to replace
247
+ * @param newTag - The new tag to assign
248
+ * @returns The number of items updated
249
+ *
250
+ * @example
251
+ * // Rename a tag
252
+ * const count = collection.updateTag('temp', 'temporary');
253
+ * console.log(`Updated ${count} items`);
254
+ */
255
+ updateTag(oldTag: Tag, newTag: Tag): number;
256
+ /**
257
+ * Removes all items with the specified tag from the collection.
258
+ *
259
+ * @param tag - The tag identifying which items to remove
260
+ * @returns The number of items removed
261
+ *
262
+ * @example
263
+ * // Remove all temporary markers
264
+ * const count = collection.removeByTag('temporary');
265
+ * console.log(`Removed ${count} items`);
266
+ */
267
+ removeByTag(tag: Tag): number;
268
+ /**
269
+ * Removes all items with the array of tags from the collection.
270
+ *
271
+ * @param tag - The tags identifying which items to remove
272
+ * @returns The number of items removed
273
+ *
274
+ * @example
275
+ * // Remove all items containing tags
276
+ * const count = collection.removeByTag('temporary', 'default', 'tag');
277
+ * console.log(`Removed ${count} items`);
278
+ */
279
+ removeByTag(tag: Tag[]): number;
280
+ /**
281
+ * Makes all items with the specified tag visible.
282
+ * Only affects items that have a 'show' property.
283
+ *
284
+ * @param tag - The tag identifying which items to show
285
+ * @returns The number of items affected
286
+ *
287
+ * @example
288
+ * // Show all buildings
289
+ * collection.show('buildings');
290
+ */
291
+ show(tag: Tag): number;
292
+ /**
293
+ * Hides all items with the specified tag.
294
+ * Only affects items that have a 'show' property.
295
+ *
296
+ * @param tag - The tag identifying which items to hide
297
+ * @returns The number of items affected
298
+ *
299
+ * @example
300
+ * // Hide all buildings
301
+ * collection.hide('buildings');
302
+ */
303
+ hide(tag: Tag): number;
304
+ /**
305
+ * Toggles visibility of all items with the specified tag.
306
+ * Only affects items that have a 'show' property.
307
+ *
308
+ * @param tag - The tag identifying which items to toggle
309
+ * @returns The number of items affected
310
+ *
311
+ * @example
312
+ * // Toggle visibility of all buildings
313
+ * collection.toggle('buildings');
314
+ */
315
+ toggle(tag: Tag): number;
316
+ /**
317
+ * Sets a property value on all items with the specified tag.
318
+ *
319
+ * @param tag - The tag identifying which items to update
320
+ * @param property - The property name to set
321
+ * @param value - The value to set
322
+ * @returns The number of items updated
323
+ *
324
+ * @example
325
+ * // Change color of all buildings to red
326
+ * collection.setProperty('buildings', 'color', Color.RED);
327
+ */
328
+ setProperty<K extends string, V>(tag: Tag, property: K, value: V): number;
329
+ /**
330
+ * Filters items in the collection based on a predicate function.
331
+ * Optionally only filters items with a specific tag.
332
+ *
333
+ * @param predicate - Function that tests each item
334
+ * @param tag - Optional tag to filter by before applying the predicate
335
+ * @returns Array of items that pass the test
336
+ *
337
+ * @example
338
+ * // Get all buildings taller than 100 meters
339
+ * const tallBuildings = collection.filter(
340
+ * entity => entity.properties?.height?.getValue() > 100,
341
+ * 'buildings'
342
+ * );
343
+ */
344
+ filter(predicate: (item: I) => boolean, tag?: Tag): I[];
345
+ /**
346
+ * Executes a callback function for each item in the collection.
347
+ * Optionally filters items by tag before execution.
348
+ *
349
+ * @param callback - Function to execute for each item
350
+ * @param tag - Optional tag to filter items (if not provided, processes all items)
351
+ *
352
+ * @example
353
+ * // Highlight all buildings
354
+ * collection.forEach((entity) => {
355
+ * if (entity.polygon) {
356
+ * entity.polygon.material = new ColorMaterialProperty(Color.YELLOW);
357
+ * }
358
+ * }, 'buildings');
359
+ */
360
+ forEach(callback: (item: I, index: number) => void, tag?: Tag): void;
361
+ }
362
+
363
+ export { type CesiumCollection, type CesiumCollectionItem, Collection, type CollectionEventType, type EventHandler, type Primitives, type Tag, type WithTag };
@@ -0,0 +1 @@
1
+ import{a as o}from"../chunk-CNYZN4AC.js";export{o as Collection};