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