@whitesev/pops 1.8.5 → 1.8.7

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.
@@ -4,334 +4,528 @@ import { pops } from "../../Pops";
4
4
  import { popsDOMUtils } from "../../utils/PopsDOMUtils";
5
5
  import { popsUtils } from "../../utils/PopsUtils";
6
6
  import { PopsTooltipConfig } from "./config";
7
- import type { PopsToolTipDetails, PopsTooltipPosition } from "./indexType";
7
+ import type { PopsToolTipDetails } from "./indexType";
8
8
 
9
- export class PopsTooltip {
10
- constructor(details: PopsToolTipDetails) {
11
- const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow();
12
- PopsHandler.handleInit($shadowRoot, [
13
- pops.config.cssText.index,
14
- pops.config.cssText.anim,
15
- pops.config.cssText.common,
16
- pops.config.cssText.tooltipCSS,
17
- ]);
18
-
19
- let config: Required<PopsToolTipDetails> = PopsTooltipConfig();
20
- config = popsUtils.assign(config, GlobalConfig.getGlobalConfig());
21
- config = popsUtils.assign(config, details);
22
- if (!(config.target instanceof HTMLElement)) {
23
- throw "config.target 必须是HTMLElement类型";
24
- }
25
- let guid = popsUtils.getRandomGUID();
26
- const PopsType = "tooltip";
27
-
28
- config = PopsHandler.handleOnly(PopsType, config);
29
- function getContent() {
30
- return typeof config.content === "function"
31
- ? config.content()
32
- : config.content;
33
- }
34
- /**
35
- * 获取悬浮提示的元素信息
36
- */
37
- function getToolTipElementInfo() {
38
- let _toolTipHTML_ = `<div class="pops-tip"></div>`;
39
- let _toolTipElement_ =
40
- popsUtils.parseTextToDOM<HTMLDivElement>(_toolTipHTML_);
41
- if (
42
- typeof config.className === "string" &&
43
- config.className.trim() !== ""
44
- ) {
45
- popsDOMUtils.addClassName(_toolTipElement_, config.className);
46
- }
47
- _toolTipElement_.setAttribute("data-guid", guid);
48
-
49
- _toolTipElement_.style.zIndex = PopsHandler.handleZIndex(
50
- config.zIndex
51
- ).toString();
52
- _toolTipElement_.innerHTML = `<div style="text-align: center;">${getContent()}</div>`;
53
- /* 箭头元素 */
54
- let _toolTipArrowHTML_ = '<div class="pops-tip-arrow"></div>';
55
- let _toolTipArrowNode_ =
56
- popsUtils.parseTextToDOM<HTMLDivElement>(_toolTipArrowHTML_);
57
- _toolTipElement_.appendChild(_toolTipArrowNode_);
58
- if (config.style != null) {
59
- /* 添加自定义style */
60
- let cssNode = document.createElement("style");
61
- cssNode.setAttribute("type", "text/css");
62
- cssNode.innerHTML = config.style;
63
- _toolTipElement_.appendChild(cssNode);
64
- }
65
- return {
66
- toolTipElement: _toolTipElement_,
67
- toolTipArrowElement: _toolTipArrowNode_,
68
- toolTipHTML: _toolTipHTML_,
69
- toolTipArrowHTML: _toolTipArrowHTML_,
70
- };
9
+ export class ToolTip {
10
+ $el = {
11
+ $shadowContainer: null as unknown as HTMLDivElement,
12
+ $shadowRoot: null as unknown as ShadowRoot,
13
+ $toolTip: null as unknown as HTMLElement,
14
+ $content: null as unknown as HTMLElement,
15
+ $arrow: null as unknown as HTMLElement,
16
+ };
17
+ $data = {
18
+ config: null as any as Required<PopsToolTipDetails>,
19
+ guid: null as any as string,
20
+ timeId_show: <number[]>[],
21
+ timeId_close: <number[]>[],
22
+ };
23
+ constructor(
24
+ config: Required<PopsToolTipDetails>,
25
+ guid: string,
26
+ ShadowInfo: {
27
+ $shadowContainer: HTMLDivElement;
28
+ $shadowRoot: ShadowRoot;
71
29
  }
30
+ ) {
31
+ this.$data.config = config;
32
+ this.$data.guid = guid;
33
+ this.$el.$shadowContainer = ShadowInfo.$shadowContainer;
34
+ this.$el.$shadowRoot = ShadowInfo.$shadowRoot;
72
35
 
73
- config.position =
74
- config.position.toLowerCase() as any as PopsTooltipPosition;
75
- let { toolTipElement } = getToolTipElementInfo();
36
+ this.init();
37
+ }
38
+ init() {
39
+ let toolTipInfo = this.createToolTip();
40
+ this.$el.$toolTip = toolTipInfo.$toolTipContainer;
41
+ this.$el.$content = toolTipInfo.$toolTipContent;
42
+ this.$el.$arrow = toolTipInfo.$toolTipArrow;
76
43
 
77
- /**
78
- * 设置 提示框的位置
79
- */
80
- function setToolTipPosition(positionDetails: any) {
81
- let positionDetail = positionDetails[config.position.toUpperCase()];
82
- if (positionDetail) {
83
- toolTipElement.style.left = positionDetail.left + "px";
84
- toolTipElement.style.top = positionDetail.top + "px";
85
- toolTipElement.setAttribute("data-motion", positionDetail.motion);
44
+ this.changeContent();
45
+ this.changeZIndex();
46
+ this.changePosition();
86
47
 
87
- toolTipElement
88
- .querySelector<HTMLDivElement>(".pops-tip-arrow")
89
- ?.setAttribute("data-position", positionDetail.arrow);
90
- } else {
91
- console.error("不存在该位置", config.position);
92
- }
48
+ if (!this.$data.config.alwaysShow) {
49
+ this.offEvent();
50
+ this.onEvent();
93
51
  }
52
+ }
53
+ /**
54
+ * 创建提示元素
55
+ */
56
+ createToolTip() {
57
+ let $toolTipContainer = popsDOMUtils.createElement("div", {
58
+ className: "pops-tip",
59
+ innerHTML: /*html*/ `
60
+ <div class="pops-tip-content" style="text-align: center;"></div>
61
+ <div class="pops-tip-arrow"></div>
62
+ `,
63
+ });
64
+ /** 内容 */
65
+ let $toolTipContent =
66
+ $toolTipContainer.querySelector<HTMLElement>(".pops-tip-content")!;
67
+ /** 箭头 */
68
+ let $toolTipArrow =
69
+ $toolTipContainer.querySelector<HTMLElement>(".pops-tip-arrow")!;
94
70
 
95
- /**
96
- * 获取 提示框的位置
97
- * @param targetElement 目标元素
98
- * @param arrowDistance 箭头和目标元素的距离
99
- * @param otherDistance 其它位置的偏移
100
- */
101
- function getToolTipPosition(
102
- targetElement: HTMLElement,
103
- arrowDistance: number,
104
- otherDistance: number
71
+ // 处理className
72
+ if (
73
+ typeof this.$data.config.className === "string" &&
74
+ this.$data.config.className.trim() !== ""
105
75
  ) {
106
- let targetElement_width = popsDOMUtils.offset(targetElement).width;
107
- let targetElement_height = popsDOMUtils.offset(targetElement).height;
108
- let targetElement_top = popsDOMUtils.offset(targetElement).top;
76
+ popsDOMUtils.addClassName($toolTipContainer, this.$data.config.className);
77
+ }
78
+ // 添加attr
79
+ $toolTipContainer.setAttribute("data-guid", this.$data.guid);
80
+ // 添加z-index
81
+ $toolTipContainer.style.zIndex = PopsHandler.handleZIndex(
82
+ this.$data.config.zIndex
83
+ ).toString();
84
+ if (this.$data.config.style != null) {
85
+ /* 添加自定义style */
86
+ let cssNode = popsDOMUtils.createElement("style", {
87
+ type: "text/css",
88
+ innerHTML: this.$data.config.style,
89
+ });
90
+ $toolTipContainer.appendChild(cssNode);
91
+ }
92
+ return {
93
+ $toolTipContainer: $toolTipContainer,
94
+ $toolTipArrow: $toolTipArrow,
95
+ $toolTipContent: $toolTipContent,
96
+ };
97
+ }
98
+ /**
99
+ * 获取提示的内容
100
+ */
101
+ getContent() {
102
+ return typeof this.$data.config.content === "function"
103
+ ? this.$data.config.content()
104
+ : this.$data.config.content;
105
+ }
106
+ /**
107
+ * 修改提示的内容
108
+ * @param text
109
+ */
110
+ changeContent(text?: string) {
111
+ if (text == null) {
112
+ text = this.getContent();
113
+ }
114
+ this.$el.$content.innerHTML = text;
115
+ }
116
+ /**
117
+ * 获取z-index
118
+ */
119
+ getZIndex() {
120
+ const zIndex = PopsHandler.handleZIndex(this.$data.config.zIndex);
121
+ return zIndex;
122
+ }
123
+ /**
124
+ * 动态修改z-index
125
+ */
126
+ changeZIndex() {
127
+ const zIndex = this.getZIndex();
128
+ this.$el.$toolTip.style.setProperty("z-index", zIndex.toString());
129
+ }
130
+ /**
131
+ * 获取 提示框的位置
132
+ * @param targetElement 目标元素
133
+ * @param arrowDistance 箭头和目标元素的距离
134
+ * @param otherDistance 其它位置的偏移
135
+ */
136
+ getPosition(
137
+ targetElement: HTMLElement,
138
+ arrowDistance: number,
139
+ otherDistance: number
140
+ ) {
141
+ let targetElement_width = popsDOMUtils.offset(targetElement).width;
142
+ let targetElement_height = popsDOMUtils.offset(targetElement).height;
143
+ let targetElement_top = popsDOMUtils.offset(targetElement).top;
109
144
 
110
- // let targetElement_bottom = popsDOMUtils.offset(targetElement).bottom;
111
- let targetElement_left = popsDOMUtils.offset(targetElement).left;
145
+ // let targetElement_bottom = popsDOMUtils.offset(targetElement).bottom;
146
+ let targetElement_left = popsDOMUtils.offset(targetElement).left;
112
147
 
113
- // let targetElement_right = popsDOMUtils.offset(targetElement).right;
148
+ // let targetElement_right = popsDOMUtils.offset(targetElement).right;
114
149
 
115
- let toolTipElement_width = popsDOMUtils.outerWidth(toolTipElement);
116
- let toolTipElement_height = popsDOMUtils.outerHeight(toolTipElement);
117
- /* 目标元素的x轴的中间位置 */
118
- let targetElement_X_center_pos =
119
- targetElement_left + targetElement_width / 2 - toolTipElement_width / 2;
120
- /* 目标元素的Y轴的中间位置 */
121
- let targetElement_Y_center_pos =
122
- targetElement_top +
123
- targetElement_height / 2 -
124
- toolTipElement_height / 2;
125
- return {
126
- TOP: {
127
- left: targetElement_X_center_pos - otherDistance,
128
- top: targetElement_top - toolTipElement_height - arrowDistance,
129
- arrow: "bottom",
130
- motion: "fadeInTop",
131
- },
132
- RIGHT: {
133
- left: targetElement_left + targetElement_width + arrowDistance,
134
- top: targetElement_Y_center_pos + otherDistance,
135
- arrow: "left",
136
- motion: "fadeInRight",
137
- },
138
- BOTTOM: {
139
- left: targetElement_X_center_pos - otherDistance,
140
- top: targetElement_top + targetElement_height + arrowDistance,
141
- arrow: "top",
142
- motion: "fadeInBottom",
143
- },
144
- LEFT: {
145
- left: targetElement_left - toolTipElement_width - arrowDistance,
146
- top: targetElement_Y_center_pos + otherDistance,
147
- arrow: "right",
148
- motion: "fadeInLeft",
149
- },
150
- };
151
- }
150
+ let toolTipElement_width = popsDOMUtils.outerWidth(this.$el.$toolTip);
151
+ let toolTipElement_height = popsDOMUtils.outerHeight(this.$el.$toolTip);
152
+ /* 目标元素的x轴的中间位置 */
153
+ let targetElement_X_center_pos =
154
+ targetElement_left + targetElement_width / 2 - toolTipElement_width / 2;
155
+ /* 目标元素的Y轴的中间位置 */
156
+ let targetElement_Y_center_pos =
157
+ targetElement_top + targetElement_height / 2 - toolTipElement_height / 2;
158
+ return {
159
+ TOP: {
160
+ left: targetElement_X_center_pos - otherDistance,
161
+ top: targetElement_top - toolTipElement_height - arrowDistance,
162
+ arrow: "bottom",
163
+ motion: "fadeInTop",
164
+ },
165
+ RIGHT: {
166
+ left: targetElement_left + targetElement_width + arrowDistance,
167
+ top: targetElement_Y_center_pos + otherDistance,
168
+ arrow: "left",
169
+ motion: "fadeInRight",
170
+ },
171
+ BOTTOM: {
172
+ left: targetElement_X_center_pos - otherDistance,
173
+ top: targetElement_top + targetElement_height + arrowDistance,
174
+ arrow: "top",
175
+ motion: "fadeInBottom",
176
+ },
177
+ LEFT: {
178
+ left: targetElement_left - toolTipElement_width - arrowDistance,
179
+ top: targetElement_Y_center_pos + otherDistance,
180
+ arrow: "right",
181
+ motion: "fadeInLeft",
182
+ },
183
+ };
184
+ }
185
+ /**
186
+ * 动态修改tooltip的位置
187
+ */
188
+ changePosition() {
189
+ let positionInfo = this.getPosition(
190
+ this.$data.config.target,
191
+ this.$data.config.arrowDistance,
192
+ this.$data.config.otherDistance
193
+ );
194
+ let positionKey =
195
+ this.$data.config.position.toUpperCase() as any as keyof typeof positionInfo;
196
+ let positionDetail = positionInfo[positionKey];
197
+ if (positionDetail) {
198
+ this.$el.$toolTip.style.left = positionDetail.left + "px";
199
+ this.$el.$toolTip.style.top = positionDetail.top + "px";
200
+ this.$el.$toolTip.setAttribute("data-motion", positionDetail.motion);
152
201
 
153
- /**
154
- * 显示提示框
155
- */
156
- function showToolTipNode() {
157
- if (typeof config.showBeforeCallBack === "function") {
158
- let result = config.showBeforeCallBack();
159
- if (typeof result === "boolean" && !result) {
160
- return;
202
+ this.$el.$arrow.setAttribute("data-position", positionDetail.arrow);
203
+ } else {
204
+ console.error("不存在该位置", this.$data.config.position);
205
+ }
206
+ }
207
+ /**
208
+ * 事件绑定
209
+ */
210
+ onEvent() {
211
+ // 监听动画结束事件
212
+ this.onAnimationFinishEvent();
213
+ this.onShowEvent();
214
+ this.onCloseEvent();
215
+ this.onMouseEnterEvent();
216
+ this.onMouseLeaveEvent();
217
+ }
218
+ /**
219
+ * 取消事件绑定
220
+ */
221
+ offEvent() {
222
+ this.offAnimationFinishEvent();
223
+ this.offShowEvent();
224
+ this.offCloseEvent();
225
+ this.offMouseEnterEvent();
226
+ this.offMouseLeaveEvent();
227
+ }
228
+ /**
229
+ * 清除延迟的timeId
230
+ */
231
+ clearCloseTimeoutId(timeId?: number) {
232
+ for (let index = 0; index < this.$data.timeId_close.length; index++) {
233
+ const currentTimeId = this.$data.timeId_close[index];
234
+ if (typeof timeId === "number") {
235
+ // 只清除一个
236
+ if (timeId == currentTimeId) {
237
+ clearTimeout(timeId);
238
+ this.$data.timeId_close.splice(index, 1);
239
+ break;
161
240
  }
241
+ } else {
242
+ clearTimeout(currentTimeId);
243
+ this.$data.timeId_close.splice(index, 1);
244
+ index--;
162
245
  }
246
+ }
247
+ }
248
+ /**
249
+ * 显示提示框
250
+ */
251
+ show() {
252
+ if (typeof this.$data.config.showBeforeCallBack === "function") {
253
+ let result = this.$data.config.showBeforeCallBack(this.$el.$toolTip);
254
+ if (typeof result === "boolean" && !result) {
255
+ return;
256
+ }
257
+ }
258
+ this.clearCloseTimeoutId();
259
+ if (!popsUtils.contains(this.$el.$shadowRoot, this.$el.$toolTip)) {
260
+ // 不在容器中,添加
261
+ this.init();
262
+ popsDOMUtils.append(this.$el.$shadowRoot, this.$el.$toolTip);
263
+ }
163
264
 
164
- if (!popsUtils.contains($shadowRoot, toolTipElement)) {
165
- popsDOMUtils.append($shadowRoot, toolTipElement);
265
+ if (!popsUtils.contains(this.$el.$shadowContainer)) {
266
+ // 页面不存在Shadow,添加
267
+ if (typeof this.$data.config.beforeAppendToPageCallBack === "function") {
268
+ this.$data.config.beforeAppendToPageCallBack(
269
+ this.$el.$shadowRoot,
270
+ this.$el.$shadowContainer
271
+ );
166
272
  }
273
+ popsDOMUtils.append(document.body, this.$el.$shadowContainer);
274
+ }
167
275
 
168
- if (!popsUtils.contains($shadowContainer)) {
169
- if (typeof config.beforeAppendToPageCallBack === "function") {
170
- config.beforeAppendToPageCallBack($shadowRoot, $shadowContainer);
171
- }
172
- popsDOMUtils.append(document.body, $shadowContainer);
276
+ // 更新内容
277
+ this.changeContent();
278
+ // 更新tip的位置
279
+ this.changePosition();
280
+ if (typeof this.$data.config.showAfterCallBack === "function") {
281
+ this.$data.config.showAfterCallBack(this.$el.$toolTip);
282
+ }
283
+ }
284
+ /**
285
+ * 绑定 显示事件
286
+ */
287
+ onShowEvent() {
288
+ popsDOMUtils.on(
289
+ this.$data.config.target,
290
+ this.$data.config.triggerShowEventName,
291
+ this.show.bind(this),
292
+ this.$data.config.eventOption
293
+ );
294
+ }
295
+ /**
296
+ * 取消绑定 显示事件
297
+ */
298
+ offShowEvent() {
299
+ popsDOMUtils.off(
300
+ this.$data.config.target,
301
+ this.$data.config.triggerShowEventName,
302
+ this.show.bind(this),
303
+ {
304
+ capture: true,
173
305
  }
174
- setToolTipPosition(
175
- getToolTipPosition(
176
- config.target,
177
- config.arrowDistance,
178
- config.otherDistance
179
- )
180
- );
181
- if (typeof config.showAfterCallBack === "function") {
182
- config.showAfterCallBack(toolTipElement);
306
+ );
307
+ }
308
+ /**
309
+ * 关闭提示框
310
+ */
311
+ close(...args: any[]) {
312
+ let event = args[0] as MouseEvent | TouchEvent;
313
+ // 只判断鼠标事件
314
+ // 其它的如Touch事件不做处理
315
+ if (event && event instanceof MouseEvent) {
316
+ let $target = event.composedPath()[0];
317
+ // 如果是子元素触发的话,忽视
318
+ if ($target != this.$data.config.target) {
319
+ return;
183
320
  }
184
321
  }
185
- /**
186
- * 关闭提示框
187
- */
188
- function closeToolTipNode() {
189
- if (typeof config.closeBeforeCallBack === "function") {
190
- let result = config.closeBeforeCallBack(toolTipElement);
191
- if (typeof result === "boolean" && !result) {
192
- return;
193
- }
194
- }
195
- toolTipElement.setAttribute(
196
- "data-motion",
197
- toolTipElement.getAttribute("data-motion")!.replace("fadeIn", "fadeOut")
198
- );
199
- if (typeof config.closeAfterCallBack === "function") {
200
- config.closeAfterCallBack(toolTipElement);
322
+ if (typeof this.$data.config.closeBeforeCallBack === "function") {
323
+ let result = this.$data.config.closeBeforeCallBack(this.$el.$toolTip);
324
+ if (typeof result === "boolean" && !result) {
325
+ return;
201
326
  }
202
327
  }
203
- /**
204
- * 绑定 显示事件
205
- */
206
- function onShowEvent() {
207
- popsDOMUtils.on(
208
- config.target,
209
- config.triggerShowEventName,
210
- showToolTipNode,
211
- config.eventOption
212
- );
328
+ if (
329
+ this.$data.config.delayCloseTime == null ||
330
+ (typeof this.$data.config.delayCloseTime === "number" &&
331
+ this.$data.config.delayCloseTime <= 0)
332
+ ) {
333
+ this.$data.config.delayCloseTime = 100;
213
334
  }
214
- /**
215
- * 绑定 关闭事件
216
- */
217
- function onCloseEvent() {
218
- popsDOMUtils.on(
219
- config.target,
220
- config.triggerCloseEventName,
221
- closeToolTipNode,
222
- config.eventOption
335
+ let timeId = setTimeout(() => {
336
+ // 设置属性触发关闭动画
337
+ this.clearCloseTimeoutId(timeId);
338
+ this.$el.$toolTip.setAttribute(
339
+ "data-motion",
340
+ this.$el.$toolTip
341
+ .getAttribute("data-motion")!
342
+ .replace("fadeIn", "fadeOut")
223
343
  );
344
+ }, this.$data.config.delayCloseTime);
345
+ this.$data.timeId_close.push(timeId);
346
+ if (typeof this.$data.config.closeAfterCallBack === "function") {
347
+ this.$data.config.closeAfterCallBack(this.$el.$toolTip);
224
348
  }
225
- /**
226
- * 取消绑定 显示事件
227
- */
228
- function offShowEvent() {
229
- popsDOMUtils.off(
230
- config.target,
231
- config.triggerShowEventName,
232
- showToolTipNode,
233
- {
234
- capture: true,
235
- }
236
- );
349
+ }
350
+ /**
351
+ * 绑定 关闭事件
352
+ */
353
+ onCloseEvent() {
354
+ popsDOMUtils.on(
355
+ this.$data.config.target,
356
+ this.$data.config.triggerCloseEventName,
357
+ this.close.bind(this),
358
+ this.$data.config.eventOption
359
+ );
360
+ }
361
+ /**
362
+ * 取消绑定 关闭事件
363
+ */
364
+ offCloseEvent() {
365
+ popsDOMUtils.off(
366
+ this.$data.config.target,
367
+ this.$data.config.triggerCloseEventName,
368
+ this.close.bind(this),
369
+ {
370
+ capture: true,
371
+ }
372
+ );
373
+ }
374
+ /**
375
+ * 销毁元素
376
+ */
377
+ destory() {
378
+ if (this.$el.$toolTip) {
379
+ this.$el.$shadowRoot.removeChild(this.$el.$toolTip);
237
380
  }
238
- /**
239
- * 取消绑定 关闭事件
240
- */
241
- function offCloseEvent() {
242
- popsDOMUtils.off(
243
- config.target,
244
- config.triggerCloseEventName,
245
- closeToolTipNode,
246
- {
247
- capture: true,
248
- }
249
- );
381
+ // @ts-ignore
382
+ this.$el.$toolTip = null;
383
+ // @ts-ignore
384
+ this.$el.$arrow = null;
385
+ // @ts-ignore
386
+ this.$el.$content = null;
387
+ }
388
+ /**
389
+ * 动画结束事件
390
+ */
391
+ animationFinishEvent() {
392
+ if (!this.$el.$toolTip) {
393
+ return;
250
394
  }
395
+ if (this.$el.$toolTip.getAttribute("data-motion")!.includes("In")) {
396
+ return;
397
+ }
398
+ this.destory();
399
+ }
400
+ /**
401
+ * 监听动画结束
402
+ */
403
+ onAnimationFinishEvent() {
404
+ popsDOMUtils.on(
405
+ this.$el.$toolTip,
406
+ popsDOMUtils.getAnimationEndNameList(),
407
+ this.animationFinishEvent.bind(this)
408
+ );
409
+ }
410
+ /**
411
+ * 取消监听动画结束
412
+ */
413
+ offAnimationFinishEvent() {
414
+ popsDOMUtils.off(
415
+ this.$el.$toolTip,
416
+ popsDOMUtils.getAnimationEndNameList(),
417
+ this.animationFinishEvent.bind(this)
418
+ );
419
+ }
420
+ /**
421
+ * 鼠标|触摸进入事件
422
+ */
423
+ mouseEnterEvent() {
424
+ this.$el.$toolTip.style.animationPlayState = "paused";
425
+ // if (parseInt(getComputedStyle(toolTipElement)) > 0.5) {
426
+ // toolTipElement.style.animationPlayState = "paused";
427
+ // }
428
+ }
429
+ /**
430
+ * 监听鼠标|触摸事件
431
+ */
432
+ onMouseEnterEvent() {
433
+ this.clearCloseTimeoutId();
434
+ popsDOMUtils.on(
435
+ this.$el.$toolTip,
436
+ "mouseenter touchstart",
437
+ this.mouseEnterEvent.bind(this),
438
+ this.$data.config.eventOption
439
+ );
440
+ }
441
+ /**
442
+ * 取消监听鼠标|触摸事件
443
+ */
444
+ offMouseEnterEvent() {
445
+ popsDOMUtils.off(
446
+ this.$el.$toolTip,
447
+ "mouseenter touchstart",
448
+ this.mouseEnterEvent.bind(this),
449
+ this.$data.config.eventOption
450
+ );
451
+ }
452
+ /**
453
+ * 鼠标|触摸离开事件
454
+ */
455
+ mouseLeaveEvent() {
456
+ this.$el.$toolTip.style.animationPlayState = "running";
457
+ }
458
+ /**
459
+ * 监听鼠标|触摸离开事件
460
+ */
461
+ onMouseLeaveEvent() {
462
+ popsDOMUtils.on(
463
+ this.$el.$toolTip,
464
+ "mouseleave touchend",
465
+ this.mouseLeaveEvent.bind(this),
466
+ this.$data.config.eventOption
467
+ );
468
+ }
469
+ /**
470
+ * 取消监听鼠标|触摸离开事件
471
+ */
472
+ offMouseLeaveEvent() {
473
+ popsDOMUtils.off(
474
+ this.$el.$toolTip,
475
+ "mouseleave touchend",
476
+ this.mouseLeaveEvent.bind(this),
477
+ this.$data.config.eventOption
478
+ );
479
+ }
480
+ }
251
481
 
252
- /**
253
- * 即使存在动画属性,但是当前设置的动画Out结束后移除元素
254
- */
255
- function endEvent() {
256
- if (toolTipElement.getAttribute("data-motion")!.includes("In")) {
257
- return;
258
- }
259
- toolTipElement.remove();
482
+ export type PopsTooltipResult<T extends PopsToolTipDetails> = {
483
+ guid: string;
484
+ config: T;
485
+ $shadowContainer: HTMLDivElement;
486
+ $shadowRoot: ShadowRoot;
487
+ toolTip: typeof ToolTip.prototype;
488
+ };
489
+
490
+ export class PopsTooltip {
491
+ constructor(details: PopsToolTipDetails) {
492
+ const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow();
493
+ PopsHandler.handleInit($shadowRoot, [
494
+ pops.config.cssText.index,
495
+ pops.config.cssText.anim,
496
+ pops.config.cssText.common,
497
+ pops.config.cssText.tooltipCSS,
498
+ ]);
499
+
500
+ let config: Required<PopsToolTipDetails> = PopsTooltipConfig();
501
+ config = popsUtils.assign(config, GlobalConfig.getGlobalConfig());
502
+ config = popsUtils.assign(config, details);
503
+ if (!(config.target instanceof HTMLElement)) {
504
+ throw "config.target 必须是HTMLElement类型";
260
505
  }
506
+ let guid = popsUtils.getRandomGUID();
507
+ const PopsType = "tooltip";
508
+
509
+ config = PopsHandler.handleOnly(PopsType, config);
510
+
511
+ let toolTip = new ToolTip(config, guid, {
512
+ $shadowContainer,
513
+ $shadowRoot,
514
+ });
261
515
  if (config.alwaysShow) {
262
516
  /* 总是显示 */
263
- showToolTipNode();
264
- return {
265
- guid: guid,
266
- config: config,
267
- toolTipNode: toolTipElement,
268
- show: showToolTipNode,
269
- close() {
270
- popsDOMUtils.on(
271
- toolTipElement,
272
- popsDOMUtils.getAnimationEndNameList(),
273
- endEvent,
274
- config.eventOption
275
- );
276
- closeToolTipNode();
277
- },
278
- off: null,
279
- on: null,
280
- };
517
+ /* 直接显示 */
518
+ toolTip.show();
281
519
  } else {
282
520
  /* 事件触发才显示 */
283
-
284
- /**
285
- * 当鼠标|手触摸,暂停当前动画
286
- */
287
- popsDOMUtils.on(
288
- toolTipElement,
289
- "mouseenter touchstart",
290
- () => {
291
- toolTipElement.style.animationPlayState = "paused";
292
- // if (parseInt(getComputedStyle(toolTipElement)) > 0.5) {
293
- // toolTipElement.style.animationPlayState = "paused";
294
- // }
295
- },
296
- config.eventOption
297
- );
298
- /**
299
- * 当鼠标|手离开,开始当前动画
300
- */
301
- popsDOMUtils.on(
302
- toolTipElement,
303
- "mouseleave touchend",
304
- () => {
305
- toolTipElement.style.animationPlayState = "running";
306
- },
307
- config.eventOption
308
- );
309
- popsDOMUtils.on(
310
- toolTipElement,
311
- popsDOMUtils.getAnimationEndNameList(),
312
- endEvent,
313
- config.eventOption
314
- );
315
-
316
- onShowEvent();
317
- onCloseEvent();
318
- return {
319
- guid: guid,
320
- $shadowContainer: $shadowContainer,
321
- $shadowRoot: $shadowRoot,
322
- config: config,
323
- toolTipNode: toolTipElement,
324
- show: showToolTipNode,
325
- close: closeToolTipNode,
326
- off() {
327
- offShowEvent();
328
- offCloseEvent();
329
- },
330
- on() {
331
- onShowEvent();
332
- onCloseEvent();
333
- },
334
- };
335
521
  }
522
+
523
+ return {
524
+ guid: guid,
525
+ config: config,
526
+ $shadowContainer: $shadowContainer,
527
+ $shadowRoot: $shadowRoot,
528
+ toolTip: toolTip,
529
+ };
336
530
  }
337
531
  }