my-openlayer 0.1.18 → 1.0.1

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,36 +1,388 @@
1
- // import { createApp } from "vue";
2
- import Overlay from "ol/Overlay";
1
+ import Overlay from 'ol/Overlay';
2
+ import { DomPointState } from '../types';
3
+ import { ErrorHandler, ErrorType } from '../utils/ErrorHandler';
4
+ import { ValidationUtils } from '../utils/ValidationUtils';
5
+ /**
6
+ * DOM点位管理类
7
+ * 用于在地图上添加和管理DOM元素覆盖物
8
+ */
3
9
  export default class DomPoint {
10
+ /**
11
+ * 构造函数
12
+ * @param map OpenLayers地图实例
13
+ * @param options 配置选项
14
+ * @throws 当参数无效时抛出错误
15
+ */
4
16
  constructor(map, options) {
5
- this.map = map;
6
- const { Vue, Template, lgtd, lttd, props, } = options;
7
- this.dom = document.createElement('div');
8
- this.map.getViewport().appendChild(this.dom);
9
- if (Vue.version.startsWith('3')) {
10
- this.app = Vue.createApp(Object.assign(Template, {
11
- props: { ...props }
12
- }));
13
- this.app.mount(this.dom);
14
- }
15
- else {
16
- this.app = new Vue({
17
- el: this.dom,
18
- render: (h) => h(Template, { props })
19
- });
20
- }
21
- this.anchor = new Overlay({
22
- element: this.dom,
17
+ this.app = null;
18
+ this.state = DomPointState.CREATED;
19
+ this.errorHandler = ErrorHandler.getInstance();
20
+ try {
21
+ // 参数验证
22
+ this.validateConstructorParams(map, options);
23
+ this.map = map;
24
+ this.options = this.mergeDefaultOptions(options);
25
+ this.id = this.generateUniqueId();
26
+ this.position = [
27
+ this.options.longitude ?? this.options.lgtd,
28
+ this.options.latitude ?? this.options.lttd
29
+ ];
30
+ // 创建DOM元素
31
+ this.dom = this.createDomElement();
32
+ // 创建Vue应用实例
33
+ this.createVueApp();
34
+ // 创建覆盖层
35
+ this.anchor = this.createOverlay();
36
+ // 添加到地图
37
+ this.map.addOverlay(this.anchor);
38
+ // 设置初始状态
39
+ this.state = DomPointState.MOUNTED;
40
+ this.setVisible(this.options.visible ?? true);
41
+ }
42
+ catch (error) {
43
+ this.handleError('Failed to create DOM point', error, { map, options });
44
+ throw error;
45
+ }
46
+ }
47
+ /**
48
+ * 验证构造函数参数
49
+ * @param map 地图实例
50
+ * @param options 配置选项
51
+ * @private
52
+ */
53
+ validateConstructorParams(map, options) {
54
+ ValidationUtils.validateRequired(map, 'Map instance is required');
55
+ ValidationUtils.validateRequired(options, 'Options are required');
56
+ const { Vue, Template, longitude, latitude } = options;
57
+ ValidationUtils.validateRequired(Vue, 'Vue is required in options');
58
+ ValidationUtils.validateRequired(Template, 'Template is required in options');
59
+ if (typeof longitude !== 'number' || typeof latitude !== 'number') {
60
+ throw new Error('Longitude and latitude must be numbers');
61
+ }
62
+ ValidationUtils.validateCoordinate(longitude, latitude);
63
+ }
64
+ /**
65
+ * 合并默认配置选项
66
+ * @param options 用户配置选项
67
+ * @returns 合并后的配置选项
68
+ * @private
69
+ */
70
+ mergeDefaultOptions(options) {
71
+ return {
23
72
  positioning: 'center-center',
24
- stopEvent: false
73
+ stopEvent: false,
74
+ visible: true,
75
+ zIndex: 1,
76
+ ...options
77
+ };
78
+ }
79
+ /**
80
+ * 生成唯一ID
81
+ * @returns 唯一标识符
82
+ * @private
83
+ */
84
+ generateUniqueId() {
85
+ return `dom-point-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
86
+ }
87
+ /**
88
+ * 创建DOM元素
89
+ * @returns DOM元素
90
+ * @private
91
+ */
92
+ createDomElement() {
93
+ const element = document.createElement('div');
94
+ element.id = this.id;
95
+ if (this.options.className) {
96
+ element.className = this.options.className;
97
+ }
98
+ if (this.options.zIndex) {
99
+ element.style.zIndex = this.options.zIndex.toString();
100
+ }
101
+ return element;
102
+ }
103
+ /**
104
+ * 创建Vue应用实例
105
+ * @private
106
+ */
107
+ createVueApp() {
108
+ const { Vue, Template, props } = this.options;
109
+ try {
110
+ if (this.isVue3(Vue)) {
111
+ // Vue 3
112
+ this.app = Vue.createApp({
113
+ ...Template,
114
+ props: props || {}
115
+ });
116
+ this.app.mount(this.dom);
117
+ }
118
+ else {
119
+ // Vue 2
120
+ this.app = new Vue({
121
+ el: this.dom,
122
+ render: (h) => h(Template, { props: props || {} })
123
+ });
124
+ }
125
+ }
126
+ catch (error) {
127
+ throw new Error(`Failed to create Vue app: ${error}`);
128
+ }
129
+ }
130
+ /**
131
+ * 判断是否为Vue 3
132
+ * @param Vue Vue构造函数
133
+ * @returns 是否为Vue 3
134
+ * @private
135
+ */
136
+ isVue3(Vue) {
137
+ return !!(Vue.version && Vue.version.startsWith('3')) || !!Vue.createApp;
138
+ }
139
+ /**
140
+ * 创建覆盖层
141
+ * @returns 覆盖层实例
142
+ * @private
143
+ */
144
+ createOverlay() {
145
+ const overlay = new Overlay({
146
+ element: this.dom,
147
+ positioning: this.options.positioning || 'center-center',
148
+ stopEvent: this.options.stopEvent || false
25
149
  });
26
- this.anchor.setPosition([lgtd, lttd]);
27
- this.map.addOverlay(this.anchor);
150
+ overlay.setPosition(this.position);
151
+ return overlay;
28
152
  }
153
+ /**
154
+ * 错误处理
155
+ * @param message 错误消息
156
+ * @param error 原始错误
157
+ * @param context 错误上下文
158
+ * @private
159
+ */
160
+ handleError(message, error, context) {
161
+ this.errorHandler.createAndHandleError(`${message}: ${error}`, ErrorType.COMPONENT_ERROR, { ...context, domPointId: this.id });
162
+ }
163
+ /**
164
+ * 设置可见性
165
+ * @param visible 是否可见
166
+ * @throws 当操作失败时抛出错误
167
+ */
29
168
  setVisible(visible) {
30
- this.dom.style.visibility = visible ? 'visible' : 'hidden';
169
+ if (this.state === DomPointState.DESTROYED) {
170
+ throw new Error('Cannot set visibility on destroyed DOM point');
171
+ }
172
+ ValidationUtils.validateType(visible, 'boolean', 'Visible parameter must be a boolean');
173
+ try {
174
+ this.dom.style.visibility = visible ? 'visible' : 'hidden';
175
+ this.state = visible ? DomPointState.VISIBLE : DomPointState.HIDDEN;
176
+ }
177
+ catch (error) {
178
+ this.handleError('Failed to set visibility', error, { visible });
179
+ throw error;
180
+ }
181
+ }
182
+ /**
183
+ * 获取当前可见性状态
184
+ * @returns 是否可见
185
+ */
186
+ isVisible() {
187
+ return this.state === DomPointState.VISIBLE;
188
+ }
189
+ /**
190
+ * 更新位置
191
+ * @param longitude 新经度
192
+ * @param latitude 新纬度
193
+ * @throws 当操作失败时抛出错误
194
+ */
195
+ updatePosition(longitude, latitude) {
196
+ if (this.state === DomPointState.DESTROYED) {
197
+ throw new Error('Cannot update position on destroyed DOM point');
198
+ }
199
+ ValidationUtils.validateCoordinate(longitude, latitude);
200
+ try {
201
+ this.position = [longitude, latitude];
202
+ this.anchor.setPosition(this.position);
203
+ }
204
+ catch (error) {
205
+ this.handleError('Failed to update position', error, { longitude, latitude });
206
+ throw error;
207
+ }
208
+ }
209
+ /**
210
+ * 获取当前位置
211
+ * @returns 当前坐标位置
212
+ */
213
+ getPosition() {
214
+ return [...this.position];
215
+ }
216
+ /**
217
+ * 更新组件属性
218
+ * @param newProps 新的属性对象
219
+ * @throws 当操作失败时抛出错误
220
+ */
221
+ updateProps(newProps) {
222
+ if (this.state === DomPointState.DESTROYED) {
223
+ throw new Error('Cannot update props on destroyed DOM point');
224
+ }
225
+ try {
226
+ // 重新创建Vue应用实例
227
+ this.destroyVueApp();
228
+ this.options.props = { ...this.options.props, ...newProps };
229
+ this.createVueApp();
230
+ }
231
+ catch (error) {
232
+ this.handleError('Failed to update props', error, { newProps });
233
+ throw error;
234
+ }
235
+ }
236
+ /**
237
+ * 设置CSS样式
238
+ * @param styles 样式对象
239
+ * @throws 当操作失败时抛出错误
240
+ */
241
+ setStyle(styles) {
242
+ if (this.state === DomPointState.DESTROYED) {
243
+ throw new Error('Cannot set style on destroyed DOM point');
244
+ }
245
+ try {
246
+ Object.assign(this.dom.style, styles);
247
+ }
248
+ catch (error) {
249
+ this.handleError('Failed to set style', error, { styles });
250
+ throw error;
251
+ }
31
252
  }
253
+ /**
254
+ * 添加CSS类名
255
+ * @param className 要添加的类名
256
+ * @throws 当操作失败时抛出错误
257
+ */
258
+ addClass(className) {
259
+ if (this.state === DomPointState.DESTROYED) {
260
+ throw new Error('Cannot add class on destroyed DOM point');
261
+ }
262
+ ValidationUtils.validateNonEmptyString(className, 'Valid class name is required');
263
+ try {
264
+ this.dom.classList.add(className);
265
+ }
266
+ catch (error) {
267
+ this.handleError('Failed to add class', error, { className });
268
+ throw error;
269
+ }
270
+ }
271
+ /**
272
+ * 移除CSS类名
273
+ * @param className 要移除的类名
274
+ * @throws 当操作失败时抛出错误
275
+ */
276
+ removeClass(className) {
277
+ if (this.state === DomPointState.DESTROYED) {
278
+ throw new Error('Cannot remove class on destroyed DOM point');
279
+ }
280
+ ValidationUtils.validateNonEmptyString(className, 'Valid class name is required');
281
+ try {
282
+ this.dom.classList.remove(className);
283
+ }
284
+ catch (error) {
285
+ this.handleError('Failed to remove class', error, { className });
286
+ throw error;
287
+ }
288
+ }
289
+ /**
290
+ * 销毁Vue应用实例
291
+ * @private
292
+ */
293
+ destroyVueApp() {
294
+ if (this.app) {
295
+ try {
296
+ if ('unmount' in this.app) {
297
+ // Vue 3
298
+ this.app.unmount();
299
+ }
300
+ else if ('$destroy' in this.app) {
301
+ // Vue 2
302
+ this.app.$destroy();
303
+ }
304
+ }
305
+ catch (error) {
306
+ console.warn('Error destroying Vue app:', error);
307
+ }
308
+ finally {
309
+ this.app = null;
310
+ }
311
+ }
312
+ }
313
+ /**
314
+ * 移除点位
315
+ * @throws 当移除失败时抛出错误
316
+ */
32
317
  remove() {
33
- this.app.unmount();
34
- this.map.removeOverlay(this.anchor);
318
+ if (this.state === DomPointState.DESTROYED) {
319
+ console.warn('DOM point already destroyed');
320
+ return;
321
+ }
322
+ try {
323
+ // 销毁Vue应用实例
324
+ this.destroyVueApp();
325
+ // 从地图移除覆盖层
326
+ this.map.removeOverlay(this.anchor);
327
+ // 清理DOM元素
328
+ if (this.dom && this.dom.parentNode) {
329
+ this.dom.parentNode.removeChild(this.dom);
330
+ }
331
+ // 更新状态
332
+ this.state = DomPointState.DESTROYED;
333
+ }
334
+ catch (error) {
335
+ this.handleError('Failed to remove DOM point', error);
336
+ throw error;
337
+ }
338
+ }
339
+ /**
340
+ * 获取覆盖层
341
+ * @returns 覆盖层实例
342
+ */
343
+ getOverlay() {
344
+ return this.anchor;
345
+ }
346
+ /**
347
+ * 获取点位ID
348
+ * @returns 点位唯一标识符
349
+ */
350
+ getId() {
351
+ return this.id;
352
+ }
353
+ /**
354
+ * 获取DOM元素
355
+ * @returns DOM元素
356
+ */
357
+ getDomElement() {
358
+ return this.dom;
359
+ }
360
+ /**
361
+ * 获取当前状态
362
+ * @returns 当前状态
363
+ */
364
+ getState() {
365
+ return this.state;
366
+ }
367
+ /**
368
+ * 获取配置选项
369
+ * @returns 配置选项的副本
370
+ */
371
+ getOptions() {
372
+ return Object.freeze({ ...this.options });
373
+ }
374
+ /**
375
+ * 检查是否已销毁
376
+ * @returns 是否已销毁
377
+ */
378
+ isDestroyed() {
379
+ return this.state === DomPointState.DESTROYED;
380
+ }
381
+ /**
382
+ * 获取地图实例
383
+ * @returns 地图实例
384
+ */
385
+ getMap() {
386
+ return this.map;
35
387
  }
36
388
  }
@@ -0,0 +1,131 @@
1
+ import { Map as OLMap } from 'ol';
2
+ import { Pixel } from 'ol/pixel';
3
+ import { FeatureLike } from 'ol/Feature';
4
+ /**
5
+ * 事件类型定义
6
+ */
7
+ export type MapEventType = 'click' | 'dblclick' | 'hover' | 'moveend' | 'zoomend' | 'pointermove';
8
+ /**
9
+ * 事件回调函数类型
10
+ */
11
+ export type EventCallback = (event: MapEventData) => void;
12
+ /**
13
+ * 地图事件数据接口
14
+ */
15
+ export interface MapEventData {
16
+ type: MapEventType;
17
+ originalEvent?: Event;
18
+ coordinate?: number[];
19
+ pixel?: Pixel;
20
+ features?: FeatureLike[];
21
+ feature?: FeatureLike;
22
+ zoom?: number;
23
+ [key: string]: any;
24
+ }
25
+ /**
26
+ * 事件管理器类
27
+ * 用于统一管理地图事件的注册、触发和移除
28
+ */
29
+ export declare class EventManager {
30
+ private readonly map;
31
+ private listeners;
32
+ private eventCounters;
33
+ /**
34
+ * 构造函数
35
+ * @param map OpenLayers地图实例
36
+ */
37
+ constructor(map: OLMap);
38
+ /**
39
+ * 初始化事件计数器
40
+ */
41
+ private initializeEventCounters;
42
+ /**
43
+ * 注册事件监听器
44
+ * @param type 事件类型
45
+ * @param callback 回调函数
46
+ * @param options 选项
47
+ * @returns 事件监听器ID
48
+ */
49
+ on(type: MapEventType, callback: EventCallback, options?: {
50
+ once?: boolean;
51
+ filter?: (event: MapEventData) => boolean;
52
+ }): string;
53
+ /**
54
+ * 移除事件监听器
55
+ * @param id 监听器ID
56
+ */
57
+ off(id: string): boolean;
58
+ /**
59
+ * 移除指定类型的所有事件监听器
60
+ * @param type 事件类型
61
+ */
62
+ offAll(type: MapEventType): void;
63
+ /**
64
+ * 清除所有事件监听器
65
+ */
66
+ clear(): void;
67
+ /**
68
+ * 获取指定类型的监听器数量
69
+ * @param type 事件类型
70
+ */
71
+ getListenerCount(type: MapEventType): number;
72
+ /**
73
+ * 生成监听器ID
74
+ * @param type 事件类型
75
+ */
76
+ private generateListenerId;
77
+ /**
78
+ * 附加地图事件
79
+ * @param type 事件类型
80
+ */
81
+ private attachMapEvent;
82
+ /**
83
+ * 分离地图事件(如果不再需要)
84
+ * @param type 事件类型
85
+ */
86
+ private detachMapEventIfNeeded;
87
+ /**
88
+ * 处理点击事件
89
+ * @param event 地图浏览器事件
90
+ */
91
+ private handleClickEvent;
92
+ /**
93
+ * 处理双击事件
94
+ * @param event 地图浏览器事件
95
+ */
96
+ private handleDblClickEvent;
97
+ /**
98
+ * 处理指针移动事件
99
+ * @param event 地图浏览器事件
100
+ */
101
+ private handlePointerMoveEvent;
102
+ /**
103
+ * 处理移动结束事件
104
+ */
105
+ private handleMoveEndEvent;
106
+ /**
107
+ * 处理缩放结束事件
108
+ */
109
+ private handleZoomEndEvent;
110
+ /**
111
+ * 创建事件数据
112
+ * @param type 事件类型
113
+ * @param event 原始事件
114
+ */
115
+ private createEventData;
116
+ /**
117
+ * 触发监听器
118
+ * @param type 事件类型
119
+ * @param eventData 事件数据
120
+ */
121
+ private triggerListeners;
122
+ /**
123
+ * 获取监听器信息(用于调试)
124
+ */
125
+ getListenersInfo(): Array<{
126
+ id: string;
127
+ type: MapEventType;
128
+ hasFilter: boolean;
129
+ isOnce: boolean;
130
+ }>;
131
+ }