@whitesev/pops 3.2.1 → 3.3.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.
Files changed (127) hide show
  1. package/README.md +249 -249
  2. package/dist/index.amd.js +736 -948
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.amd.min.js +1 -1
  5. package/dist/index.amd.min.js.map +1 -1
  6. package/dist/index.cjs.js +736 -948
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.cjs.min.js +1 -1
  9. package/dist/index.cjs.min.js.map +1 -1
  10. package/dist/index.esm.js +736 -948
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.esm.min.js +1 -1
  13. package/dist/index.esm.min.js.map +1 -1
  14. package/dist/index.iife.js +736 -948
  15. package/dist/index.iife.js.map +1 -1
  16. package/dist/index.iife.min.js +1 -1
  17. package/dist/index.iife.min.js.map +1 -1
  18. package/dist/index.system.js +736 -948
  19. package/dist/index.system.js.map +1 -1
  20. package/dist/index.system.min.js +1 -1
  21. package/dist/index.system.min.js.map +1 -1
  22. package/dist/index.umd.js +736 -948
  23. package/dist/index.umd.js.map +1 -1
  24. package/dist/index.umd.min.js +1 -1
  25. package/dist/index.umd.min.js.map +1 -1
  26. package/dist/types/src/PopsCore.d.ts +15 -7
  27. package/dist/types/src/components/folder/types/index.d.ts +43 -13
  28. package/dist/types/src/types/PopsDOMUtilsEventType.d.ts +313 -313
  29. package/dist/types/src/types/animation.d.ts +19 -19
  30. package/dist/types/src/types/button.d.ts +94 -94
  31. package/dist/types/src/types/components.d.ts +211 -211
  32. package/dist/types/src/types/event.d.ts +43 -43
  33. package/dist/types/src/types/global.d.ts +31 -31
  34. package/dist/types/src/types/icon.d.ts +32 -32
  35. package/dist/types/src/types/inst.d.ts +28 -28
  36. package/dist/types/src/types/main.d.ts +66 -66
  37. package/dist/types/src/types/mask.d.ts +52 -52
  38. package/dist/types/src/types/position.d.ts +60 -60
  39. package/package.json +28 -27
  40. package/src/Pops.ts +206 -206
  41. package/src/PopsAnimation.ts +32 -32
  42. package/src/PopsCSS.ts +54 -54
  43. package/src/PopsCore.ts +53 -37
  44. package/src/PopsIcon.ts +95 -95
  45. package/src/PopsInst.ts +21 -21
  46. package/src/components/alert/defaultConfig.ts +62 -62
  47. package/src/components/alert/index.ts +163 -163
  48. package/src/components/alert/types/index.ts +23 -23
  49. package/src/components/confirm/defaultConfig.ts +90 -90
  50. package/src/components/confirm/index.ts +165 -165
  51. package/src/components/confirm/types/index.ts +13 -17
  52. package/src/components/drawer/defaultConfig.ts +89 -89
  53. package/src/components/drawer/index.css +37 -37
  54. package/src/components/drawer/index.ts +245 -245
  55. package/src/components/drawer/types/index.ts +62 -61
  56. package/src/components/folder/defaultConfig.ts +151 -151
  57. package/src/components/folder/folderIcon.ts +28 -28
  58. package/src/components/folder/index.css +303 -303
  59. package/src/components/folder/index.ts +953 -932
  60. package/src/components/folder/types/index.ts +143 -110
  61. package/src/components/iframe/defaultConfig.ts +60 -60
  62. package/src/components/iframe/index.css +76 -76
  63. package/src/components/iframe/index.ts +331 -331
  64. package/src/components/iframe/types/index.ts +96 -96
  65. package/src/components/loading/defaultConfig.ts +29 -29
  66. package/src/components/loading/index.css +66 -66
  67. package/src/components/loading/index.ts +101 -101
  68. package/src/components/loading/types/index.ts +36 -34
  69. package/src/components/panel/css/components-select.css +84 -84
  70. package/src/components/panel/defaultConfig.ts +868 -868
  71. package/src/components/panel/handlerComponents.ts +3993 -3993
  72. package/src/components/panel/index.css +1403 -1403
  73. package/src/components/panel/index.ts +221 -221
  74. package/src/components/panel/types/components-button.ts +56 -56
  75. package/src/components/panel/types/components-common.ts +73 -73
  76. package/src/components/panel/types/components-container.ts +25 -25
  77. package/src/components/panel/types/components-deepMenu.ts +64 -64
  78. package/src/components/panel/types/components-input.ts +90 -90
  79. package/src/components/panel/types/components-own.ts +16 -16
  80. package/src/components/panel/types/components-select.ts +169 -169
  81. package/src/components/panel/types/components-selectMultiple.ts +105 -104
  82. package/src/components/panel/types/components-slider.ts +55 -55
  83. package/src/components/panel/types/components-switch.ts +33 -33
  84. package/src/components/panel/types/components-textarea.ts +45 -45
  85. package/src/components/panel/types/index.ts +244 -244
  86. package/src/components/prompt/defaultConfig.ts +94 -94
  87. package/src/components/prompt/index.css +34 -34
  88. package/src/components/prompt/index.ts +187 -215
  89. package/src/components/prompt/types/index.ts +57 -56
  90. package/src/components/rightClickMenu/defaultConfig.ts +103 -103
  91. package/src/components/rightClickMenu/index.css +115 -115
  92. package/src/components/rightClickMenu/index.ts +662 -662
  93. package/src/components/rightClickMenu/types/index.ts +145 -143
  94. package/src/components/searchSuggestion/defaultConfig.ts +63 -63
  95. package/src/components/searchSuggestion/index.ts +813 -813
  96. package/src/components/searchSuggestion/types/index.ts +244 -242
  97. package/src/components/tooltip/defaultConfig.ts +33 -33
  98. package/src/components/tooltip/index.css +199 -199
  99. package/src/components/tooltip/index.ts +617 -617
  100. package/src/components/tooltip/types/index.ts +123 -121
  101. package/src/config/CommonCSSClassName.ts +17 -17
  102. package/src/config/GlobalConfig.ts +63 -63
  103. package/src/css/animation.css +987 -987
  104. package/src/css/button.css +551 -551
  105. package/src/css/common.css +54 -54
  106. package/src/css/index.css +253 -253
  107. package/src/css/ninePalaceGridPosition.css +50 -50
  108. package/src/css/scrollbar.css +22 -22
  109. package/src/handler/PopsElementHandler.ts +303 -303
  110. package/src/handler/PopsHandler.ts +611 -611
  111. package/src/types/PopsDOMUtilsEventType.d.ts +313 -313
  112. package/src/types/animation.d.ts +19 -19
  113. package/src/types/button.d.ts +94 -94
  114. package/src/types/components.d.ts +211 -211
  115. package/src/types/event.d.ts +43 -43
  116. package/src/types/global.d.ts +31 -31
  117. package/src/types/icon.d.ts +32 -32
  118. package/src/types/inst.d.ts +28 -28
  119. package/src/types/main.d.ts +66 -66
  120. package/src/types/mask.d.ts +52 -52
  121. package/src/types/position.d.ts +60 -60
  122. package/src/utils/PopsDOMUtils.ts +2483 -2483
  123. package/src/utils/PopsDOMUtilsEventsConfig.ts +4 -4
  124. package/src/utils/PopsInstanceUtils.ts +714 -714
  125. package/src/utils/PopsMathUtils.ts +71 -71
  126. package/src/utils/PopsSafeUtils.ts +22 -22
  127. package/src/utils/PopsUtils.ts +391 -421
@@ -1,617 +1,617 @@
1
- import { GlobalConfig } from "../../config/GlobalConfig";
2
- import { PopsHandler } from "../../handler/PopsHandler";
3
- import { PopsCSS } from "../../PopsCSS";
4
- import type { PopsType } from "../../types/main";
5
- import { popsDOMUtils } from "../../utils/PopsDOMUtils";
6
- import { PopsSafeUtils } from "../../utils/PopsSafeUtils";
7
- import { popsUtils } from "../../utils/PopsUtils";
8
- import { PopsTooltipDefaultConfig } from "./defaultConfig";
9
- import type { PopsToolTipConfig } from "./types/index";
10
-
11
- type ToolTipEventTypeName = "MouseEvent" | "TouchEvent";
12
-
13
- export class ToolTip {
14
- $el = {
15
- $shadowContainer: null as unknown as HTMLDivElement,
16
- $shadowRoot: null as unknown as ShadowRoot | HTMLElement,
17
- $toolTip: null as unknown as HTMLElement,
18
- $content: null as unknown as HTMLElement,
19
- $arrow: null as unknown as HTMLElement,
20
- };
21
- $data = {
22
- config: null as any as Required<PopsToolTipConfig>,
23
- guid: null as any as string,
24
- timeId_close_TouchEvent: <number[]>[],
25
- timeId_close_MouseEvent: <number[]>[],
26
- };
27
- constructor(
28
- config: Required<PopsToolTipConfig>,
29
- guid: string,
30
- ShadowInfo: {
31
- $shadowContainer: HTMLDivElement;
32
- $shadowRoot: ShadowRoot | HTMLElement;
33
- }
34
- ) {
35
- this.$data.config = config;
36
- this.$data.guid = guid;
37
- this.$el.$shadowContainer = ShadowInfo.$shadowContainer;
38
- this.$el.$shadowRoot = ShadowInfo.$shadowRoot;
39
- this.show = this.show.bind(this);
40
- this.close = this.close.bind(this);
41
- this.toolTipAnimationFinishEvent = this.toolTipAnimationFinishEvent.bind(this);
42
- this.toolTipMouseEnterEvent = this.toolTipMouseEnterEvent.bind(this);
43
- this.toolTipMouseLeaveEvent = this.toolTipMouseLeaveEvent.bind(this);
44
- this.init();
45
- }
46
- init() {
47
- const toolTipInfo = this.createToolTip();
48
- this.$el.$toolTip = toolTipInfo.$toolTipContainer;
49
- this.$el.$content = toolTipInfo.$toolTipContent;
50
- this.$el.$arrow = toolTipInfo.$toolTipArrow;
51
-
52
- this.changeContent();
53
- this.changeZIndex();
54
- this.changePosition();
55
-
56
- if (!this.$data.config.alwaysShow) {
57
- this.offEvent();
58
- this.onEvent();
59
- }
60
- }
61
- /**
62
- * 创建提示元素
63
- */
64
- createToolTip() {
65
- const $toolTipContainer = popsDOMUtils.createElement(
66
- "div",
67
- {
68
- className: "pops-tip",
69
- innerHTML: /*html*/ `
70
- <div class="pops-tip-content" style="text-align: center;"></div>
71
- <div class="pops-tip-arrow"></div>
72
- `,
73
- },
74
- {
75
- "data-position": this.$data.config.isFixed ? "fixed" : "absolute",
76
- "data-guid": this.$data.guid,
77
- }
78
- );
79
- /** 内容 */
80
- const $toolTipContent = $toolTipContainer.querySelector<HTMLElement>(".pops-tip-content")!;
81
- /** 箭头 */
82
- const $toolTipArrow = $toolTipContainer.querySelector<HTMLElement>(".pops-tip-arrow")!;
83
-
84
- // 处理className
85
- popsDOMUtils.addClassName($toolTipContainer, this.$data.config.className);
86
- // 添加z-index
87
- $toolTipContainer.style.zIndex = PopsHandler.handleZIndex(this.$data.config.zIndex).toString();
88
- if (this.$data.config.style != null) {
89
- // 添加自定义style
90
- const cssNode = popsDOMUtils.createElement("style", {
91
- type: "text/css",
92
- innerHTML: this.$data.config.style,
93
- });
94
- $toolTipContainer.appendChild(cssNode);
95
- }
96
- // 处理是否显示箭头元素
97
- if (!this.$data.config.showArrow) {
98
- $toolTipArrow.remove();
99
- }
100
- return {
101
- $toolTipContainer: $toolTipContainer,
102
- $toolTipArrow: $toolTipArrow,
103
- $toolTipContent: $toolTipContent,
104
- };
105
- }
106
- /**
107
- * 获取提示的内容
108
- */
109
- getContent() {
110
- return typeof this.$data.config.content === "function" ? this.$data.config.content() : this.$data.config.content;
111
- }
112
- /**
113
- * 修改提示的内容
114
- * @param text
115
- */
116
- changeContent(text?: string) {
117
- if (text == null) {
118
- text = this.getContent();
119
- }
120
- if (this.$data.config.isDiffContent) {
121
- const contentPropKey = "data-content";
122
- const originContentText: string = Reflect.get(this.$el.$content, contentPropKey);
123
- if (typeof originContentText === "string") {
124
- if (originContentText === text) {
125
- // 内容未改变,不修改避免渲染
126
- return;
127
- }
128
- }
129
- Reflect.set(this.$el.$content, contentPropKey, text);
130
- }
131
- PopsSafeUtils.setSafeHTML(this.$el.$content, text);
132
- }
133
- /**
134
- * 获取z-index
135
- */
136
- getZIndex() {
137
- const zIndex = PopsHandler.handleZIndex(this.$data.config.zIndex);
138
- return zIndex;
139
- }
140
- /**
141
- * 动态修改z-index
142
- */
143
- changeZIndex() {
144
- const zIndex = this.getZIndex();
145
- this.$el.$toolTip.style.setProperty("z-index", zIndex.toString());
146
- }
147
- /**
148
- * 计算 提示框的位置
149
- * @param event 触发的事件
150
- * @param targetElement 目标元素
151
- * @param arrowDistance 箭头和目标元素的距离
152
- * @param otherDistance 其它位置的偏移
153
- */
154
- calcToolTipPosition(
155
- targetElement: HTMLElement,
156
- arrowDistance: number,
157
- otherDistance: number,
158
- event?: MouseEvent | TouchEvent | PointerEvent
159
- ) {
160
- const offsetInfo = popsDOMUtils.offset(targetElement, !this.$data.config.isFixed);
161
- // 目标 宽
162
- const targetElement_width = offsetInfo.width;
163
- // 目标 高
164
- const targetElement_height = offsetInfo.height;
165
- // 目标 顶部距离
166
- const targetElement_top = offsetInfo.top;
167
-
168
- // let targetElement_bottom = offsetInfo.bottom;
169
- // 目标 左边距离
170
- const targetElement_left = offsetInfo.left;
171
-
172
- // let targetElement_right = offsetInfo.right;
173
-
174
- const toolTipElement_width = popsDOMUtils.outerWidth(this.$el.$toolTip);
175
- const toolTipElement_height = popsDOMUtils.outerHeight(this.$el.$toolTip);
176
- // 目标元素的x轴的中间位置
177
- const targetElement_X_center_pos = targetElement_left + targetElement_width / 2 - toolTipElement_width / 2;
178
- // 目标元素的Y轴的中间位置
179
- const targetElement_Y_center_pos = targetElement_top + targetElement_height / 2 - toolTipElement_height / 2;
180
-
181
- let mouseX = 0;
182
- let mouseY = 0;
183
- if (event != null) {
184
- if (event instanceof MouseEvent || event instanceof PointerEvent) {
185
- mouseX = event.pageX;
186
- mouseY = event.y;
187
- } else if (event instanceof TouchEvent) {
188
- const touchEvent = event.touches[0];
189
- mouseX = touchEvent.pageX;
190
- mouseY = touchEvent.pageY;
191
- } else {
192
- if (typeof (<MouseEvent>event).clientX === "number") {
193
- mouseX = (<MouseEvent>event).clientX;
194
- }
195
- if (typeof (<MouseEvent>event).clientY === "number") {
196
- mouseY = (<MouseEvent>event).clientY;
197
- }
198
- }
199
- }
200
- return {
201
- TOP: {
202
- left: targetElement_X_center_pos - otherDistance,
203
- top: targetElement_top - toolTipElement_height - arrowDistance,
204
- arrow: "bottom",
205
- motion: "fadeInTop",
206
- },
207
- RIGHT: {
208
- left: targetElement_left + targetElement_width + arrowDistance,
209
- top: targetElement_Y_center_pos + otherDistance,
210
- arrow: "left",
211
- motion: "fadeInRight",
212
- },
213
- BOTTOM: {
214
- left: targetElement_X_center_pos - otherDistance,
215
- top: targetElement_top + targetElement_height + arrowDistance,
216
- arrow: "top",
217
- motion: "fadeInBottom",
218
- },
219
- LEFT: {
220
- left: targetElement_left - toolTipElement_width - arrowDistance,
221
- top: targetElement_Y_center_pos + otherDistance,
222
- arrow: "right",
223
- motion: "fadeInLeft",
224
- },
225
- FOLLOW: {
226
- left: mouseX + otherDistance,
227
- top: mouseY + otherDistance,
228
- arrow: "follow",
229
- motion: "",
230
- },
231
- };
232
- }
233
- /**
234
- * 动态修改tooltip的位置
235
- */
236
- changePosition(event?: MouseEvent | TouchEvent | PointerEvent) {
237
- const positionInfo = this.calcToolTipPosition(
238
- this.$data.config.$target,
239
- this.$data.config.arrowDistance,
240
- this.$data.config.otherDistance,
241
- event
242
- );
243
- const positionKey = this.$data.config.position.toUpperCase() as keyof typeof positionInfo;
244
- const position = positionInfo[positionKey];
245
- if (position) {
246
- this.$el.$toolTip.style.left = position.left + "px";
247
- this.$el.$toolTip.style.top = position.top + "px";
248
- this.$el.$toolTip.setAttribute("data-motion", position.motion);
249
-
250
- this.$el.$arrow.setAttribute("data-position", position.arrow);
251
- } else {
252
- console.error("不存在该位置", this.$data.config.position);
253
- }
254
- }
255
- /**
256
- * 事件绑定
257
- */
258
- onEvent() {
259
- // 监听动画结束事件
260
- this.onToolTipAnimationFinishEvent();
261
- this.onShowEvent();
262
- this.onCloseEvent();
263
- this.onToolTipMouseEnterEvent();
264
- this.onToolTipMouseLeaveEvent();
265
- }
266
- /**
267
- * 取消事件绑定
268
- */
269
- offEvent() {
270
- this.offToolTipAnimationFinishEvent();
271
- this.offShowEvent();
272
- this.offCloseEvent();
273
- this.offToolTipMouseEnterEvent();
274
- this.offToolTipMouseLeaveEvent();
275
- }
276
- /**
277
- * 添加关闭的timeId
278
- * @param type
279
- * @param timeId
280
- */
281
- addCloseTimeoutId(type: ToolTipEventTypeName, timeId: number) {
282
- if (type === "MouseEvent") {
283
- this.$data.timeId_close_MouseEvent.push(timeId);
284
- } else {
285
- this.$data.timeId_close_TouchEvent.push(timeId);
286
- }
287
- }
288
- /**
289
- * 清除延迟的timeId
290
- * @param type 事件类型
291
- */
292
- clearCloseTimeoutId(type: ToolTipEventTypeName, timeId?: number) {
293
- const timeIdList = type === "MouseEvent" ? this.$data.timeId_close_MouseEvent : this.$data.timeId_close_TouchEvent;
294
- for (let index = 0; index < timeIdList.length; index++) {
295
- const currentTimeId = timeIdList[index];
296
- if (typeof timeId === "number") {
297
- // 只清除一个
298
- if (timeId == currentTimeId) {
299
- popsUtils.clearTimeout(timeId);
300
- timeIdList.splice(index, 1);
301
- break;
302
- }
303
- } else {
304
- popsUtils.clearTimeout(currentTimeId);
305
- timeIdList.splice(index, 1);
306
- index--;
307
- }
308
- }
309
- }
310
- /**
311
- * 显示提示框
312
- */
313
- show(...args: any[]) {
314
- const event = args[0] as MouseEvent | TouchEvent;
315
- const eventType: ToolTipEventTypeName = event instanceof MouseEvent ? "MouseEvent" : "TouchEvent";
316
- this.clearCloseTimeoutId(eventType);
317
-
318
- if (typeof this.$data.config.showBeforeCallBack === "function") {
319
- const result = this.$data.config.showBeforeCallBack(this.$el.$toolTip);
320
- if (typeof result === "boolean" && !result) {
321
- return;
322
- }
323
- }
324
- if (!popsUtils.contains(this.$el.$shadowRoot, this.$el.$toolTip)) {
325
- // 不在容器中,添加
326
- this.init();
327
- popsDOMUtils.append(this.$el.$shadowRoot, this.$el.$toolTip);
328
- }
329
-
330
- if (!popsUtils.contains(this.$el.$shadowContainer)) {
331
- // 页面不存在Shadow,添加
332
- if (typeof this.$data.config.beforeAppendToPageCallBack === "function") {
333
- this.$data.config.beforeAppendToPageCallBack(this.$el.$shadowRoot, this.$el.$shadowContainer);
334
- }
335
- popsDOMUtils.append(document.body, this.$el.$shadowContainer);
336
- }
337
-
338
- // 更新内容
339
- this.changeContent();
340
- // 更新tip的位置
341
- this.changePosition(event);
342
- if (typeof this.$data.config.showAfterCallBack === "function") {
343
- this.$data.config.showAfterCallBack(this.$el.$toolTip);
344
- }
345
- }
346
- /**
347
- * 绑定 显示事件
348
- */
349
- onShowEvent() {
350
- popsDOMUtils.on(
351
- this.$data.config.$target,
352
- this.$data.config.onShowEventName,
353
- this.show,
354
- this.$data.config.eventOption
355
- );
356
- }
357
- /**
358
- * 取消绑定 显示事件
359
- */
360
- offShowEvent() {
361
- popsDOMUtils.off(
362
- this.$data.config.$target,
363
- this.$data.config.onShowEventName,
364
- this.show,
365
- this.$data.config.eventOption
366
- );
367
- }
368
- /**
369
- * 关闭提示框
370
- */
371
- close(...args: any[]) {
372
- const event = args[0] as MouseEvent | TouchEvent;
373
- const eventType: ToolTipEventTypeName = event instanceof MouseEvent ? "MouseEvent" : "TouchEvent";
374
- // 只判断鼠标事件
375
- // 其它的如Touch事件不做处理
376
- if (event && event instanceof MouseEvent) {
377
- const $target = event.composedPath()[0];
378
- // 如果是目标元素的子元素/tooltip元素的子元素触发的话,那就不管
379
- if ($target != this.$data.config.$target && $target != this.$el.$toolTip) {
380
- return;
381
- }
382
- }
383
- if (typeof this.$data.config.closeBeforeCallBack === "function") {
384
- const result = this.$data.config.closeBeforeCallBack(this.$el.$toolTip);
385
- if (typeof result === "boolean" && !result) {
386
- return;
387
- }
388
- }
389
- if (
390
- this.$data.config.delayCloseTime == null ||
391
- (typeof this.$data.config.delayCloseTime === "number" && this.$data.config.delayCloseTime <= 0)
392
- ) {
393
- this.$data.config.delayCloseTime = 100;
394
- }
395
- const timeId = popsUtils.setTimeout(() => {
396
- // 设置属性触发关闭动画
397
- this.clearCloseTimeoutId(eventType, timeId);
398
- if (this.$el.$toolTip == null) {
399
- // 已清除了
400
- return;
401
- }
402
- const motion = this.$el.$toolTip.getAttribute("data-motion");
403
- if (motion == null || motion.trim() === "") {
404
- // 没有动画
405
- this.toolTipAnimationFinishEvent();
406
- } else {
407
- // 修改data-motion触发动画关闭
408
- this.$el.$toolTip.setAttribute(
409
- "data-motion",
410
- this.$el.$toolTip.getAttribute("data-motion")!.replace("fadeIn", "fadeOut")
411
- );
412
- }
413
- }, this.$data.config.delayCloseTime);
414
- this.addCloseTimeoutId(eventType, timeId);
415
- if (typeof this.$data.config.closeAfterCallBack === "function") {
416
- this.$data.config.closeAfterCallBack(this.$el.$toolTip);
417
- }
418
- }
419
- /**
420
- * 绑定 关闭事件
421
- */
422
- onCloseEvent() {
423
- popsDOMUtils.on(
424
- this.$data.config.$target,
425
- this.$data.config.onCloseEventName,
426
- this.close,
427
- this.$data.config.eventOption
428
- );
429
- }
430
- /**
431
- * 取消绑定 关闭事件
432
- */
433
- offCloseEvent() {
434
- popsDOMUtils.off(
435
- this.$data.config.$target,
436
- this.$data.config.onCloseEventName,
437
- this.close,
438
- this.$data.config.eventOption
439
- );
440
- }
441
- /**
442
- * 销毁元素
443
- */
444
- destory() {
445
- if (this.$el.$toolTip) {
446
- this.$el.$toolTip.remove();
447
- }
448
- // @ts-expect-error
449
- this.$el.$toolTip = null;
450
- // @ts-expect-error
451
- this.$el.$arrow = null;
452
- // @ts-expect-error
453
- this.$el.$content = null;
454
- }
455
- /**
456
- * 动画结束事件
457
- */
458
- toolTipAnimationFinishEvent() {
459
- if (!this.$el.$toolTip) {
460
- return;
461
- }
462
- if (this.$el.$toolTip.getAttribute("data-motion")!.includes("In")) {
463
- return;
464
- }
465
- this.destory();
466
- }
467
- /**
468
- * 监听tooltip的动画结束
469
- */
470
- onToolTipAnimationFinishEvent() {
471
- popsDOMUtils.on(this.$el.$toolTip, popsDOMUtils.getAnimationEndNameList(), this.toolTipAnimationFinishEvent);
472
- }
473
- /**
474
- * 取消tooltip监听动画结束
475
- */
476
- offToolTipAnimationFinishEvent() {
477
- popsDOMUtils.off(this.$el.$toolTip, popsDOMUtils.getAnimationEndNameList(), this.toolTipAnimationFinishEvent);
478
- }
479
- /**
480
- * 鼠标|触摸进入事件
481
- */
482
- toolTipMouseEnterEvent() {
483
- this.clearCloseTimeoutId("MouseEvent");
484
- this.clearCloseTimeoutId("TouchEvent");
485
- // 重置动画状态
486
- // this.$el.$toolTip.style.animationPlayState = "paused";
487
- // if (parseInt(getComputedStyle(toolTipElement)) > 0.5) {
488
- // toolTipElement.style.animationPlayState = "paused";
489
- // }
490
- }
491
- /**
492
- * 监听鼠标|触摸事件
493
- */
494
- onToolTipMouseEnterEvent() {
495
- this.clearCloseTimeoutId("MouseEvent");
496
- this.clearCloseTimeoutId("TouchEvent");
497
- popsDOMUtils.on(
498
- this.$el.$toolTip,
499
- "mouseenter touchstart",
500
- this.toolTipMouseEnterEvent,
501
- this.$data.config.eventOption
502
- );
503
- }
504
- /**
505
- * 取消监听事件 - 鼠标|触摸
506
- */
507
- offToolTipMouseEnterEvent() {
508
- popsDOMUtils.off(
509
- this.$el.$toolTip,
510
- "mouseenter touchstart",
511
- this.toolTipMouseEnterEvent,
512
- this.$data.config.eventOption
513
- );
514
- }
515
- /**
516
- * 离开事件 - 鼠标|触摸
517
- */
518
- toolTipMouseLeaveEvent(event: MouseEvent | PointerEvent) {
519
- this.close(event);
520
- // this.$el.$toolTip.style.animationPlayState = "running";
521
- }
522
- /**
523
- * 监听离开事件 - 鼠标|触摸
524
- */
525
- onToolTipMouseLeaveEvent() {
526
- popsDOMUtils.on(
527
- this.$el.$toolTip,
528
- "mouseleave touchend touchcancel",
529
- this.toolTipMouseLeaveEvent,
530
- this.$data.config.eventOption
531
- );
532
- }
533
- /**
534
- * 取消监听离开事件 - 鼠标|触摸
535
- */
536
- offToolTipMouseLeaveEvent() {
537
- popsDOMUtils.off(
538
- this.$el.$toolTip,
539
- "mouseleave touchend touchcancel",
540
- this.toolTipMouseLeaveEvent,
541
- this.$data.config.eventOption
542
- );
543
- }
544
- }
545
-
546
- export type PopsTooltipResult<T extends PopsToolTipConfig> = {
547
- guid: string;
548
- config: T;
549
- $shadowContainer: HTMLDivElement;
550
- $shadowRoot: ShadowRoot;
551
- toolTip: typeof ToolTip.prototype;
552
- };
553
-
554
- export const PopsTooltip = {
555
- init(__config__: PopsToolTipConfig) {
556
- const guid = popsUtils.getRandomGUID();
557
- // 设置当前类型
558
- const popsType: PopsType = "tooltip";
559
-
560
- let config = PopsTooltipDefaultConfig();
561
- config = popsUtils.assign(config, GlobalConfig.getGlobalConfig());
562
- config = popsUtils.assign(config, __config__);
563
- if (!(config.$target instanceof HTMLElement)) {
564
- throw new TypeError("config.target 必须是HTMLElement类型");
565
- }
566
- config = PopsHandler.handleOnly(popsType, config);
567
-
568
- if (config.position === "follow") {
569
- config.onShowEventName = config.onShowEventName.trim();
570
- const showEventNameSplit = config.onShowEventName.split(" ");
571
- ["mousemove", "touchmove"].forEach((it) => {
572
- if (showEventNameSplit.includes(it)) return;
573
- config.onShowEventName += ` ${it}`;
574
- });
575
- }
576
-
577
- const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow(config);
578
- PopsHandler.handleInit($shadowRoot, [
579
- {
580
- name: "index",
581
- css: PopsCSS.index,
582
- },
583
- {
584
- name: "anim",
585
- css: PopsCSS.anim,
586
- },
587
- {
588
- name: "common",
589
- css: PopsCSS.common,
590
- },
591
- {
592
- name: "tooltipCSS",
593
- css: PopsCSS.tooltipCSS,
594
- },
595
- ]);
596
-
597
- const toolTip = new ToolTip(config, guid, {
598
- $shadowContainer,
599
- $shadowRoot,
600
- });
601
- if (config.alwaysShow) {
602
- // 总是显示
603
- // 直接显示
604
- toolTip.show();
605
- } else {
606
- // 事件触发才显示
607
- }
608
-
609
- return {
610
- guid,
611
- config,
612
- $shadowContainer,
613
- $shadowRoot,
614
- toolTip,
615
- };
616
- },
617
- };
1
+ import { GlobalConfig } from "../../config/GlobalConfig";
2
+ import { PopsHandler } from "../../handler/PopsHandler";
3
+ import { PopsCSS } from "../../PopsCSS";
4
+ import type { PopsType } from "../../types/main";
5
+ import { popsDOMUtils } from "../../utils/PopsDOMUtils";
6
+ import { PopsSafeUtils } from "../../utils/PopsSafeUtils";
7
+ import { popsUtils } from "../../utils/PopsUtils";
8
+ import { PopsTooltipDefaultConfig } from "./defaultConfig";
9
+ import type { PopsToolTipConfig } from "./types/index";
10
+
11
+ type ToolTipEventTypeName = "MouseEvent" | "TouchEvent";
12
+
13
+ export class ToolTip {
14
+ $el = {
15
+ $shadowContainer: null as unknown as HTMLDivElement,
16
+ $shadowRoot: null as unknown as ShadowRoot | HTMLElement,
17
+ $toolTip: null as unknown as HTMLElement,
18
+ $content: null as unknown as HTMLElement,
19
+ $arrow: null as unknown as HTMLElement,
20
+ };
21
+ $data = {
22
+ config: null as any as Required<PopsToolTipConfig>,
23
+ guid: null as any as string,
24
+ timeId_close_TouchEvent: <number[]>[],
25
+ timeId_close_MouseEvent: <number[]>[],
26
+ };
27
+ constructor(
28
+ config: Required<PopsToolTipConfig>,
29
+ guid: string,
30
+ ShadowInfo: {
31
+ $shadowContainer: HTMLDivElement;
32
+ $shadowRoot: ShadowRoot | HTMLElement;
33
+ }
34
+ ) {
35
+ this.$data.config = config;
36
+ this.$data.guid = guid;
37
+ this.$el.$shadowContainer = ShadowInfo.$shadowContainer;
38
+ this.$el.$shadowRoot = ShadowInfo.$shadowRoot;
39
+ this.show = this.show.bind(this);
40
+ this.close = this.close.bind(this);
41
+ this.toolTipAnimationFinishEvent = this.toolTipAnimationFinishEvent.bind(this);
42
+ this.toolTipMouseEnterEvent = this.toolTipMouseEnterEvent.bind(this);
43
+ this.toolTipMouseLeaveEvent = this.toolTipMouseLeaveEvent.bind(this);
44
+ this.init();
45
+ }
46
+ init() {
47
+ const toolTipInfo = this.createToolTip();
48
+ this.$el.$toolTip = toolTipInfo.$toolTipContainer;
49
+ this.$el.$content = toolTipInfo.$toolTipContent;
50
+ this.$el.$arrow = toolTipInfo.$toolTipArrow;
51
+
52
+ this.changeContent();
53
+ this.changeZIndex();
54
+ this.changePosition();
55
+
56
+ if (!this.$data.config.alwaysShow) {
57
+ this.offEvent();
58
+ this.onEvent();
59
+ }
60
+ }
61
+ /**
62
+ * 创建提示元素
63
+ */
64
+ createToolTip() {
65
+ const $toolTipContainer = popsDOMUtils.createElement(
66
+ "div",
67
+ {
68
+ className: "pops-tip",
69
+ innerHTML: /*html*/ `
70
+ <div class="pops-tip-content" style="text-align: center;"></div>
71
+ <div class="pops-tip-arrow"></div>
72
+ `,
73
+ },
74
+ {
75
+ "data-position": this.$data.config.isFixed ? "fixed" : "absolute",
76
+ "data-guid": this.$data.guid,
77
+ }
78
+ );
79
+ /** 内容 */
80
+ const $toolTipContent = $toolTipContainer.querySelector<HTMLElement>(".pops-tip-content")!;
81
+ /** 箭头 */
82
+ const $toolTipArrow = $toolTipContainer.querySelector<HTMLElement>(".pops-tip-arrow")!;
83
+
84
+ // 处理className
85
+ popsDOMUtils.addClassName($toolTipContainer, this.$data.config.className);
86
+ // 添加z-index
87
+ $toolTipContainer.style.zIndex = PopsHandler.handleZIndex(this.$data.config.zIndex).toString();
88
+ if (this.$data.config.style != null) {
89
+ // 添加自定义style
90
+ const cssNode = popsDOMUtils.createElement("style", {
91
+ type: "text/css",
92
+ innerHTML: this.$data.config.style,
93
+ });
94
+ $toolTipContainer.appendChild(cssNode);
95
+ }
96
+ // 处理是否显示箭头元素
97
+ if (!this.$data.config.showArrow) {
98
+ $toolTipArrow.remove();
99
+ }
100
+ return {
101
+ $toolTipContainer: $toolTipContainer,
102
+ $toolTipArrow: $toolTipArrow,
103
+ $toolTipContent: $toolTipContent,
104
+ };
105
+ }
106
+ /**
107
+ * 获取提示的内容
108
+ */
109
+ getContent() {
110
+ return typeof this.$data.config.content === "function" ? this.$data.config.content() : this.$data.config.content;
111
+ }
112
+ /**
113
+ * 修改提示的内容
114
+ * @param text
115
+ */
116
+ changeContent(text?: string) {
117
+ if (text == null) {
118
+ text = this.getContent();
119
+ }
120
+ if (this.$data.config.isDiffContent) {
121
+ const contentPropKey = "data-content";
122
+ const originContentText: string = Reflect.get(this.$el.$content, contentPropKey);
123
+ if (typeof originContentText === "string") {
124
+ if (originContentText === text) {
125
+ // 内容未改变,不修改避免渲染
126
+ return;
127
+ }
128
+ }
129
+ Reflect.set(this.$el.$content, contentPropKey, text);
130
+ }
131
+ PopsSafeUtils.setSafeHTML(this.$el.$content, text);
132
+ }
133
+ /**
134
+ * 获取z-index
135
+ */
136
+ getZIndex() {
137
+ const zIndex = PopsHandler.handleZIndex(this.$data.config.zIndex);
138
+ return zIndex;
139
+ }
140
+ /**
141
+ * 动态修改z-index
142
+ */
143
+ changeZIndex() {
144
+ const zIndex = this.getZIndex();
145
+ this.$el.$toolTip.style.setProperty("z-index", zIndex.toString());
146
+ }
147
+ /**
148
+ * 计算 提示框的位置
149
+ * @param event 触发的事件
150
+ * @param targetElement 目标元素
151
+ * @param arrowDistance 箭头和目标元素的距离
152
+ * @param otherDistance 其它位置的偏移
153
+ */
154
+ calcToolTipPosition(
155
+ targetElement: HTMLElement,
156
+ arrowDistance: number,
157
+ otherDistance: number,
158
+ event?: MouseEvent | TouchEvent | PointerEvent
159
+ ) {
160
+ const offsetInfo = popsDOMUtils.offset(targetElement, !this.$data.config.isFixed);
161
+ // 目标 宽
162
+ const targetElement_width = offsetInfo.width;
163
+ // 目标 高
164
+ const targetElement_height = offsetInfo.height;
165
+ // 目标 顶部距离
166
+ const targetElement_top = offsetInfo.top;
167
+
168
+ // let targetElement_bottom = offsetInfo.bottom;
169
+ // 目标 左边距离
170
+ const targetElement_left = offsetInfo.left;
171
+
172
+ // let targetElement_right = offsetInfo.right;
173
+
174
+ const toolTipElement_width = popsDOMUtils.outerWidth(this.$el.$toolTip);
175
+ const toolTipElement_height = popsDOMUtils.outerHeight(this.$el.$toolTip);
176
+ // 目标元素的x轴的中间位置
177
+ const targetElement_X_center_pos = targetElement_left + targetElement_width / 2 - toolTipElement_width / 2;
178
+ // 目标元素的Y轴的中间位置
179
+ const targetElement_Y_center_pos = targetElement_top + targetElement_height / 2 - toolTipElement_height / 2;
180
+
181
+ let mouseX = 0;
182
+ let mouseY = 0;
183
+ if (event != null) {
184
+ if (event instanceof MouseEvent || event instanceof PointerEvent) {
185
+ mouseX = event.pageX;
186
+ mouseY = event.y;
187
+ } else if (event instanceof TouchEvent) {
188
+ const touchEvent = event.touches[0];
189
+ mouseX = touchEvent.pageX;
190
+ mouseY = touchEvent.pageY;
191
+ } else {
192
+ if (typeof (<MouseEvent>event).clientX === "number") {
193
+ mouseX = (<MouseEvent>event).clientX;
194
+ }
195
+ if (typeof (<MouseEvent>event).clientY === "number") {
196
+ mouseY = (<MouseEvent>event).clientY;
197
+ }
198
+ }
199
+ }
200
+ return {
201
+ TOP: {
202
+ left: targetElement_X_center_pos - otherDistance,
203
+ top: targetElement_top - toolTipElement_height - arrowDistance,
204
+ arrow: "bottom",
205
+ motion: "fadeInTop",
206
+ },
207
+ RIGHT: {
208
+ left: targetElement_left + targetElement_width + arrowDistance,
209
+ top: targetElement_Y_center_pos + otherDistance,
210
+ arrow: "left",
211
+ motion: "fadeInRight",
212
+ },
213
+ BOTTOM: {
214
+ left: targetElement_X_center_pos - otherDistance,
215
+ top: targetElement_top + targetElement_height + arrowDistance,
216
+ arrow: "top",
217
+ motion: "fadeInBottom",
218
+ },
219
+ LEFT: {
220
+ left: targetElement_left - toolTipElement_width - arrowDistance,
221
+ top: targetElement_Y_center_pos + otherDistance,
222
+ arrow: "right",
223
+ motion: "fadeInLeft",
224
+ },
225
+ FOLLOW: {
226
+ left: mouseX + otherDistance,
227
+ top: mouseY + otherDistance,
228
+ arrow: "follow",
229
+ motion: "",
230
+ },
231
+ };
232
+ }
233
+ /**
234
+ * 动态修改tooltip的位置
235
+ */
236
+ changePosition(event?: MouseEvent | TouchEvent | PointerEvent) {
237
+ const positionInfo = this.calcToolTipPosition(
238
+ this.$data.config.$target,
239
+ this.$data.config.arrowDistance,
240
+ this.$data.config.otherDistance,
241
+ event
242
+ );
243
+ const positionKey = this.$data.config.position.toUpperCase() as keyof typeof positionInfo;
244
+ const position = positionInfo[positionKey];
245
+ if (position) {
246
+ this.$el.$toolTip.style.left = position.left + "px";
247
+ this.$el.$toolTip.style.top = position.top + "px";
248
+ this.$el.$toolTip.setAttribute("data-motion", position.motion);
249
+
250
+ this.$el.$arrow.setAttribute("data-position", position.arrow);
251
+ } else {
252
+ console.error("不存在该位置", this.$data.config.position);
253
+ }
254
+ }
255
+ /**
256
+ * 事件绑定
257
+ */
258
+ onEvent() {
259
+ // 监听动画结束事件
260
+ this.onToolTipAnimationFinishEvent();
261
+ this.onShowEvent();
262
+ this.onCloseEvent();
263
+ this.onToolTipMouseEnterEvent();
264
+ this.onToolTipMouseLeaveEvent();
265
+ }
266
+ /**
267
+ * 取消事件绑定
268
+ */
269
+ offEvent() {
270
+ this.offToolTipAnimationFinishEvent();
271
+ this.offShowEvent();
272
+ this.offCloseEvent();
273
+ this.offToolTipMouseEnterEvent();
274
+ this.offToolTipMouseLeaveEvent();
275
+ }
276
+ /**
277
+ * 添加关闭的timeId
278
+ * @param type
279
+ * @param timeId
280
+ */
281
+ addCloseTimeoutId(type: ToolTipEventTypeName, timeId: number) {
282
+ if (type === "MouseEvent") {
283
+ this.$data.timeId_close_MouseEvent.push(timeId);
284
+ } else {
285
+ this.$data.timeId_close_TouchEvent.push(timeId);
286
+ }
287
+ }
288
+ /**
289
+ * 清除延迟的timeId
290
+ * @param type 事件类型
291
+ */
292
+ clearCloseTimeoutId(type: ToolTipEventTypeName, timeId?: number) {
293
+ const timeIdList = type === "MouseEvent" ? this.$data.timeId_close_MouseEvent : this.$data.timeId_close_TouchEvent;
294
+ for (let index = 0; index < timeIdList.length; index++) {
295
+ const currentTimeId = timeIdList[index];
296
+ if (typeof timeId === "number") {
297
+ // 只清除一个
298
+ if (timeId == currentTimeId) {
299
+ popsUtils.clearTimeout(timeId);
300
+ timeIdList.splice(index, 1);
301
+ break;
302
+ }
303
+ } else {
304
+ popsUtils.clearTimeout(currentTimeId);
305
+ timeIdList.splice(index, 1);
306
+ index--;
307
+ }
308
+ }
309
+ }
310
+ /**
311
+ * 显示提示框
312
+ */
313
+ show(...args: any[]) {
314
+ const event = args[0] as MouseEvent | TouchEvent;
315
+ const eventType: ToolTipEventTypeName = event instanceof MouseEvent ? "MouseEvent" : "TouchEvent";
316
+ this.clearCloseTimeoutId(eventType);
317
+
318
+ if (typeof this.$data.config.showBeforeCallBack === "function") {
319
+ const result = this.$data.config.showBeforeCallBack(this.$el.$toolTip);
320
+ if (typeof result === "boolean" && !result) {
321
+ return;
322
+ }
323
+ }
324
+ if (!popsUtils.contains(this.$el.$shadowRoot, this.$el.$toolTip)) {
325
+ // 不在容器中,添加
326
+ this.init();
327
+ popsDOMUtils.append(this.$el.$shadowRoot, this.$el.$toolTip);
328
+ }
329
+
330
+ if (!popsUtils.contains(this.$el.$shadowContainer)) {
331
+ // 页面不存在Shadow,添加
332
+ if (typeof this.$data.config.beforeAppendToPageCallBack === "function") {
333
+ this.$data.config.beforeAppendToPageCallBack(this.$el.$shadowRoot, this.$el.$shadowContainer);
334
+ }
335
+ popsDOMUtils.append(document.body, this.$el.$shadowContainer);
336
+ }
337
+
338
+ // 更新内容
339
+ this.changeContent();
340
+ // 更新tip的位置
341
+ this.changePosition(event);
342
+ if (typeof this.$data.config.showAfterCallBack === "function") {
343
+ this.$data.config.showAfterCallBack(this.$el.$toolTip);
344
+ }
345
+ }
346
+ /**
347
+ * 绑定 显示事件
348
+ */
349
+ onShowEvent() {
350
+ popsDOMUtils.on(
351
+ this.$data.config.$target,
352
+ this.$data.config.onShowEventName,
353
+ this.show,
354
+ this.$data.config.eventOption
355
+ );
356
+ }
357
+ /**
358
+ * 取消绑定 显示事件
359
+ */
360
+ offShowEvent() {
361
+ popsDOMUtils.off(
362
+ this.$data.config.$target,
363
+ this.$data.config.onShowEventName,
364
+ this.show,
365
+ this.$data.config.eventOption
366
+ );
367
+ }
368
+ /**
369
+ * 关闭提示框
370
+ */
371
+ close(...args: any[]) {
372
+ const event = args[0] as MouseEvent | TouchEvent;
373
+ const eventType: ToolTipEventTypeName = event instanceof MouseEvent ? "MouseEvent" : "TouchEvent";
374
+ // 只判断鼠标事件
375
+ // 其它的如Touch事件不做处理
376
+ if (event && event instanceof MouseEvent) {
377
+ const $target = event.composedPath()[0];
378
+ // 如果是目标元素的子元素/tooltip元素的子元素触发的话,那就不管
379
+ if ($target != this.$data.config.$target && $target != this.$el.$toolTip) {
380
+ return;
381
+ }
382
+ }
383
+ if (typeof this.$data.config.closeBeforeCallBack === "function") {
384
+ const result = this.$data.config.closeBeforeCallBack(this.$el.$toolTip);
385
+ if (typeof result === "boolean" && !result) {
386
+ return;
387
+ }
388
+ }
389
+ if (
390
+ this.$data.config.delayCloseTime == null ||
391
+ (typeof this.$data.config.delayCloseTime === "number" && this.$data.config.delayCloseTime <= 0)
392
+ ) {
393
+ this.$data.config.delayCloseTime = 100;
394
+ }
395
+ const timeId = popsUtils.setTimeout(() => {
396
+ // 设置属性触发关闭动画
397
+ this.clearCloseTimeoutId(eventType, timeId);
398
+ if (this.$el.$toolTip == null) {
399
+ // 已清除了
400
+ return;
401
+ }
402
+ const motion = this.$el.$toolTip.getAttribute("data-motion");
403
+ if (motion == null || motion.trim() === "") {
404
+ // 没有动画
405
+ this.toolTipAnimationFinishEvent();
406
+ } else {
407
+ // 修改data-motion触发动画关闭
408
+ this.$el.$toolTip.setAttribute(
409
+ "data-motion",
410
+ this.$el.$toolTip.getAttribute("data-motion")!.replace("fadeIn", "fadeOut")
411
+ );
412
+ }
413
+ }, this.$data.config.delayCloseTime);
414
+ this.addCloseTimeoutId(eventType, timeId);
415
+ if (typeof this.$data.config.closeAfterCallBack === "function") {
416
+ this.$data.config.closeAfterCallBack(this.$el.$toolTip);
417
+ }
418
+ }
419
+ /**
420
+ * 绑定 关闭事件
421
+ */
422
+ onCloseEvent() {
423
+ popsDOMUtils.on(
424
+ this.$data.config.$target,
425
+ this.$data.config.onCloseEventName,
426
+ this.close,
427
+ this.$data.config.eventOption
428
+ );
429
+ }
430
+ /**
431
+ * 取消绑定 关闭事件
432
+ */
433
+ offCloseEvent() {
434
+ popsDOMUtils.off(
435
+ this.$data.config.$target,
436
+ this.$data.config.onCloseEventName,
437
+ this.close,
438
+ this.$data.config.eventOption
439
+ );
440
+ }
441
+ /**
442
+ * 销毁元素
443
+ */
444
+ destory() {
445
+ if (this.$el.$toolTip) {
446
+ this.$el.$toolTip.remove();
447
+ }
448
+ // @ts-expect-error
449
+ this.$el.$toolTip = null;
450
+ // @ts-expect-error
451
+ this.$el.$arrow = null;
452
+ // @ts-expect-error
453
+ this.$el.$content = null;
454
+ }
455
+ /**
456
+ * 动画结束事件
457
+ */
458
+ toolTipAnimationFinishEvent() {
459
+ if (!this.$el.$toolTip) {
460
+ return;
461
+ }
462
+ if (this.$el.$toolTip.getAttribute("data-motion")!.includes("In")) {
463
+ return;
464
+ }
465
+ this.destory();
466
+ }
467
+ /**
468
+ * 监听tooltip的动画结束
469
+ */
470
+ onToolTipAnimationFinishEvent() {
471
+ popsDOMUtils.on(this.$el.$toolTip, popsDOMUtils.getAnimationEndNameList(), this.toolTipAnimationFinishEvent);
472
+ }
473
+ /**
474
+ * 取消tooltip监听动画结束
475
+ */
476
+ offToolTipAnimationFinishEvent() {
477
+ popsDOMUtils.off(this.$el.$toolTip, popsDOMUtils.getAnimationEndNameList(), this.toolTipAnimationFinishEvent);
478
+ }
479
+ /**
480
+ * 鼠标|触摸进入事件
481
+ */
482
+ toolTipMouseEnterEvent() {
483
+ this.clearCloseTimeoutId("MouseEvent");
484
+ this.clearCloseTimeoutId("TouchEvent");
485
+ // 重置动画状态
486
+ // this.$el.$toolTip.style.animationPlayState = "paused";
487
+ // if (parseInt(getComputedStyle(toolTipElement)) > 0.5) {
488
+ // toolTipElement.style.animationPlayState = "paused";
489
+ // }
490
+ }
491
+ /**
492
+ * 监听鼠标|触摸事件
493
+ */
494
+ onToolTipMouseEnterEvent() {
495
+ this.clearCloseTimeoutId("MouseEvent");
496
+ this.clearCloseTimeoutId("TouchEvent");
497
+ popsDOMUtils.on(
498
+ this.$el.$toolTip,
499
+ "mouseenter touchstart",
500
+ this.toolTipMouseEnterEvent,
501
+ this.$data.config.eventOption
502
+ );
503
+ }
504
+ /**
505
+ * 取消监听事件 - 鼠标|触摸
506
+ */
507
+ offToolTipMouseEnterEvent() {
508
+ popsDOMUtils.off(
509
+ this.$el.$toolTip,
510
+ "mouseenter touchstart",
511
+ this.toolTipMouseEnterEvent,
512
+ this.$data.config.eventOption
513
+ );
514
+ }
515
+ /**
516
+ * 离开事件 - 鼠标|触摸
517
+ */
518
+ toolTipMouseLeaveEvent(event: MouseEvent | PointerEvent) {
519
+ this.close(event);
520
+ // this.$el.$toolTip.style.animationPlayState = "running";
521
+ }
522
+ /**
523
+ * 监听离开事件 - 鼠标|触摸
524
+ */
525
+ onToolTipMouseLeaveEvent() {
526
+ popsDOMUtils.on(
527
+ this.$el.$toolTip,
528
+ "mouseleave touchend touchcancel",
529
+ this.toolTipMouseLeaveEvent,
530
+ this.$data.config.eventOption
531
+ );
532
+ }
533
+ /**
534
+ * 取消监听离开事件 - 鼠标|触摸
535
+ */
536
+ offToolTipMouseLeaveEvent() {
537
+ popsDOMUtils.off(
538
+ this.$el.$toolTip,
539
+ "mouseleave touchend touchcancel",
540
+ this.toolTipMouseLeaveEvent,
541
+ this.$data.config.eventOption
542
+ );
543
+ }
544
+ }
545
+
546
+ export type PopsTooltipResult<T extends PopsToolTipConfig> = {
547
+ guid: string;
548
+ config: T;
549
+ $shadowContainer: HTMLDivElement;
550
+ $shadowRoot: ShadowRoot;
551
+ toolTip: typeof ToolTip.prototype;
552
+ };
553
+
554
+ export const PopsTooltip = {
555
+ init(__config__: PopsToolTipConfig) {
556
+ const guid = popsUtils.getRandomGUID();
557
+ // 设置当前类型
558
+ const popsType: PopsType = "tooltip";
559
+
560
+ let config = PopsTooltipDefaultConfig();
561
+ config = popsUtils.assign(config, GlobalConfig.getGlobalConfig());
562
+ config = popsUtils.assign(config, __config__);
563
+ if (!(config.$target instanceof HTMLElement)) {
564
+ throw new TypeError("config.target 必须是HTMLElement类型");
565
+ }
566
+ config = PopsHandler.handleOnly(popsType, config);
567
+
568
+ if (config.position === "follow") {
569
+ config.onShowEventName = config.onShowEventName.trim();
570
+ const showEventNameSplit = config.onShowEventName.split(" ");
571
+ ["mousemove", "touchmove"].forEach((it) => {
572
+ if (showEventNameSplit.includes(it)) return;
573
+ config.onShowEventName += ` ${it}`;
574
+ });
575
+ }
576
+
577
+ const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow(config);
578
+ PopsHandler.handleInit($shadowRoot, [
579
+ {
580
+ name: "index",
581
+ css: PopsCSS.index,
582
+ },
583
+ {
584
+ name: "anim",
585
+ css: PopsCSS.anim,
586
+ },
587
+ {
588
+ name: "common",
589
+ css: PopsCSS.common,
590
+ },
591
+ {
592
+ name: "tooltipCSS",
593
+ css: PopsCSS.tooltipCSS,
594
+ },
595
+ ]);
596
+
597
+ const toolTip = new ToolTip(config, guid, {
598
+ $shadowContainer,
599
+ $shadowRoot,
600
+ });
601
+ if (config.alwaysShow) {
602
+ // 总是显示
603
+ // 直接显示
604
+ toolTip.show();
605
+ } else {
606
+ // 事件触发才显示
607
+ }
608
+
609
+ return {
610
+ guid,
611
+ config,
612
+ $shadowContainer,
613
+ $shadowRoot,
614
+ toolTip,
615
+ };
616
+ },
617
+ };