@vcmap/viewshed 3.0.5 → 4.0.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,5 +1,11 @@
1
1
  import { reactive } from 'vue';
2
2
  import { Category, CesiumMap, VcsEvent } from '@vcmap/core';
3
+ import type {
4
+ CollectionComponentClass,
5
+ CollectionComponentListItem,
6
+ VcsUiApp,
7
+ MappingFunction,
8
+ } from '@vcmap/ui';
3
9
  import {
4
10
  createListExportAction,
5
11
  createListImportAction,
@@ -8,33 +14,40 @@ import {
8
14
  NotificationType,
9
15
  } from '@vcmap/ui';
10
16
  import { name } from '../package.json';
17
+ import type { ViewshedOptions, ViewshedTypes } from './viewshed.js';
11
18
  import Viewshed from './viewshed.js';
12
19
 
13
- /**
14
- * @typedef {Object} ViewshedCategoryHelper
15
- * @property {import("@vcmap/core").VcsEvent} renamed
16
- * @property {import("@vcmap/core").VcsEvent} visibilityChanged Event that is raised when the visibility of a viewshed is changed.
17
- * @property {function(string | null):void} setSelection Sets a single viewshed item as selected by providing its name.
18
- * @property {function():void} clearSelection Clears all selected items.
19
- * @property {function(string, boolean):void} setVisibility Sets the visibility of a viewshed item by providing its name and a boolean value.
20
- * @property {function(import("./viewshed.js").default):void} add Adds a viewshed to the categories collection. Also assigns new title to the viewshed.
21
- * @property {function(string):void} remove Removes viewshed from category collection by providing a name.
22
- * @property {import("@vcmap/ui").CollectionComponent} collectionComponent The collection component of the category.
23
- * @property {function():void} destroy Destroys category helper
24
- */
25
-
26
- class ViewshedCategory extends Category {
27
- static get className() {
20
+ export type ViewshedCategoryHelper = {
21
+ renamed: VcsEvent<{ item: Viewshed; title: string }>;
22
+ /** Event that is raised when the visibility of a viewshed is changed. */
23
+ visibilityChanged: VcsEvent<{ item: Viewshed; visible: boolean }>;
24
+ /** Sets a single viewshed item as selected by providing its name. */
25
+ setSelection(itemName: string | null): void;
26
+ /** Clears all selected items. */
27
+ clearSelection(): void;
28
+ /** Sets the visibility of a viewshed item by providing its name and a boolean value. */
29
+ setVisibility(itemName: string, visible: boolean): void;
30
+ /** Adds a viewshed to the categories collection. Also assigns new title to the viewshed. */
31
+ add(viewshed: Viewshed): void;
32
+ /** Removes viewshed from category collection by providing a name. */
33
+ remove(itemName: string): void;
34
+ /** The collection component of the category. */
35
+ collectionComponent: CollectionComponentClass<Viewshed>;
36
+ /** Destroys category helper */
37
+ destroy(): void;
38
+ };
39
+
40
+ class ViewshedCategory extends Category<Viewshed> {
41
+ static get className(): string {
28
42
  return 'ViewshedCategory';
29
43
  }
30
44
 
31
- async _deserializeItem(item) {
32
- const cesiumMap =
33
- /** @type {import("@vcmap/core").CesiumMap | undefined} */ (
34
- this._app?.maps.getByType(CesiumMap.className)[0]
35
- );
45
+ protected async _deserializeItem(item: ViewshedOptions): Promise<Viewshed> {
46
+ const cesiumMap = this._app?.maps.getByType(CesiumMap.className)[0] as
47
+ | CesiumMap
48
+ | undefined;
36
49
  if (cesiumMap) {
37
- return new Viewshed(item);
50
+ return Promise.resolve(new Viewshed(item));
38
51
  } else {
39
52
  throw new Error('No CesiumMap available');
40
53
  }
@@ -43,14 +56,11 @@ class ViewshedCategory extends Category {
43
56
 
44
57
  export default ViewshedCategory;
45
58
 
46
- /**
47
- *
48
- * @param {import("./viewshed.js").ViewshedTypes} viewshedType
49
- * @param {Array<import("./viewshed.js").default>} persistedViewsheds
50
- * @returns {string} The title for the viewshed.
51
- */
52
- export function getTitleForViewshed(viewshedType, persistedViewsheds) {
53
- let viewshedTitle;
59
+ export function getTitleForViewshed(
60
+ viewshedType: ViewshedTypes,
61
+ persistedViewsheds: Viewshed[],
62
+ ): string {
63
+ let viewshedTitle: string | undefined;
54
64
  let count = 0;
55
65
 
56
66
  const sameTypeViewshedsNames = new Set(
@@ -69,28 +79,24 @@ export function getTitleForViewshed(viewshedType, persistedViewsheds) {
69
79
  return viewshedTitle;
70
80
  }
71
81
 
72
- /**
73
- *
74
- * @param {import("@vcmap/ui").VcsUiApp} app
75
- * @returns {Promise<ViewshedCategoryHelper>}
76
- */
77
- export async function createCategory(app) {
78
- const renamed = new VcsEvent();
79
- const visibilityChanged = new VcsEvent();
82
+ export async function createCategory(
83
+ app: VcsUiApp,
84
+ ): Promise<ViewshedCategoryHelper> {
85
+ const renamed = new VcsEvent<{ item: Viewshed; title: string }>();
86
+ const visibilityChanged = new VcsEvent<{
87
+ item: Viewshed;
88
+ visible: boolean;
89
+ }>();
80
90
 
81
91
  const { collectionComponent, category } =
82
- await app.categoryManager.requestCategory(
92
+ await app.categoryManager.requestCategory<Viewshed>(
83
93
  {
84
94
  type: ViewshedCategory.className,
85
95
  name: 'Viewsheds',
86
96
  title: 'viewshed.viewshedCategory',
87
97
  },
88
98
  name,
89
- {
90
- selectable: true,
91
- renamable: true,
92
- removable: true,
93
- },
99
+ { selectable: true, renamable: true, removable: true },
94
100
  );
95
101
 
96
102
  collectionComponent.addItemMapping({
@@ -101,10 +107,14 @@ export async function createCategory(app) {
101
107
  owner: name,
102
108
  });
103
109
 
104
- const itemMappingFunction = (item, c, listItem) => {
105
- listItem.title = item.properties.title;
110
+ const itemMappingFunction: MappingFunction<Viewshed> = (
111
+ item: Viewshed,
112
+ _c: CollectionComponentClass<Viewshed>,
113
+ listItem: CollectionComponentListItem,
114
+ ): void => {
115
+ listItem.title = item.properties.title as string;
106
116
 
107
- listItem.titleChanged = (title) => {
117
+ listItem.titleChanged = (title): void => {
108
118
  item.properties.title = title;
109
119
  listItem.title = title;
110
120
  renamed.raiseEvent({ item, title });
@@ -115,7 +125,7 @@ export async function createCategory(app) {
115
125
  name: 'visibilityAction',
116
126
  icon: '$vcsCheckbox',
117
127
  callback() {
118
- visibilityChanged.raiseEvent(item);
128
+ visibilityChanged.raiseEvent({ item, visible: !!listItem.visible });
119
129
  },
120
130
  }),
121
131
  );
@@ -148,7 +158,7 @@ export async function createCategory(app) {
148
158
  files.map(async (file) => {
149
159
  const text = await file.text();
150
160
  try {
151
- const parsedOptions = JSON.parse(text);
161
+ const parsedOptions = JSON.parse(text) as ViewshedOptions[];
152
162
  return parsedOptions.map((options) => new Viewshed(options));
153
163
  } catch (e) {
154
164
  app.notifier.add({
@@ -200,18 +210,18 @@ export async function createCategory(app) {
200
210
  return {
201
211
  renamed,
202
212
  visibilityChanged,
203
- setSelection(itemName) {
213
+ setSelection(itemName): void {
204
214
  if (itemName) {
205
215
  collectionComponent.selection.value =
206
216
  collectionComponent.items.value.filter((i) => itemName === i.name);
207
217
  }
208
218
  },
209
- clearSelection() {
219
+ clearSelection(): void {
210
220
  if (collectionComponent.selection.value.length) {
211
221
  collectionComponent.selection.value = [];
212
222
  }
213
223
  },
214
- setVisibility(itemName, visible) {
224
+ setVisibility(itemName, visible): void {
215
225
  const listItem = collectionComponent.items.value.find(
216
226
  (i) => itemName === i.name,
217
227
  );
@@ -226,23 +236,20 @@ export async function createCategory(app) {
226
236
  }
227
237
  }
228
238
  },
229
- add(viewshed) {
230
- viewshed.properties.title = getTitleForViewshed(
231
- viewshed.type,
232
- /** @type {import("./viewshed.js").default[]} */ ([
233
- ...category.collection,
234
- ]),
235
- );
239
+ add(viewshed): void {
240
+ viewshed.properties.title = getTitleForViewshed(viewshed.type, [
241
+ ...category.collection,
242
+ ] as Viewshed[]);
236
243
  category.collection.add(viewshed);
237
244
  },
238
- remove(itemName) {
245
+ remove(itemName): void {
239
246
  const item = category.collection.getByKey(itemName);
240
247
  if (item) {
241
248
  category.collection.remove(item);
242
249
  }
243
250
  },
244
251
  collectionComponent,
245
- destroy() {
252
+ destroy(): void {
246
253
  app.categoryManager.removeOwner(name);
247
254
  renamed.destroy();
248
255
  visibilityChanged.destroy();
@@ -1,34 +1,40 @@
1
+ import type { InteractionEvent } from '@vcmap/core';
1
2
  import {
2
3
  AbstractInteraction,
3
4
  EventType,
4
5
  Projection,
5
6
  VcsEvent,
6
7
  } from '@vcmap/core';
8
+ import type Viewshed from './viewshed.js';
7
9
 
8
10
  class ViewshedInteraction extends AbstractInteraction {
9
- /**
10
- *
11
- * @param {import("./viewshed").default} viewshed
12
- */
13
- constructor(viewshed) {
11
+ static get className(): string {
12
+ return 'ViewshedInteraction';
13
+ }
14
+
15
+ private _viewshed: Viewshed;
16
+
17
+ private _positioned = new VcsEvent<null>();
18
+
19
+ private _finished = new VcsEvent<null>();
20
+
21
+ private _position = false;
22
+
23
+ constructor(viewshed: Viewshed) {
14
24
  super(EventType.CLICKMOVE);
15
25
  this._viewshed = viewshed;
16
- this._positioned = new VcsEvent();
17
- this._finished = new VcsEvent();
18
- this._position = false;
19
-
20
26
  this.setActive();
21
27
  }
22
28
 
23
- get finished() {
29
+ get finished(): VcsEvent<null> {
24
30
  return this._finished;
25
31
  }
26
32
 
27
- get positioned() {
33
+ get positioned(): VcsEvent<null> {
28
34
  return this._positioned;
29
35
  }
30
36
 
31
- async pipe(event) {
37
+ async pipe(event: InteractionEvent): Promise<InteractionEvent> {
32
38
  if (event.position) {
33
39
  if (!this._position) {
34
40
  this._viewshed.position = Projection.mercatorToWgs84(event.position);
@@ -44,10 +50,10 @@ class ViewshedInteraction extends AbstractInteraction {
44
50
  }
45
51
  }
46
52
  }
47
- return event;
53
+ return Promise.resolve(event);
48
54
  }
49
55
 
50
- destroy() {
56
+ destroy(): void {
51
57
  super.destroy();
52
58
  this._finished.destroy();
53
59
  this._positioned.destroy();