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