@whitesev/pops 1.4.0 → 1.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 (105) hide show
  1. package/dist/index.amd.js.map +1 -1
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.iife.js.map +1 -1
  5. package/dist/index.system.js.map +1 -1
  6. package/dist/index.umd.js.map +1 -1
  7. package/package.json +50 -48
  8. package/src/Config.ts +3 -0
  9. package/src/Core.ts +45 -0
  10. package/src/Pops.ts +340 -0
  11. package/src/components/alert/index.css +45 -0
  12. package/src/components/alert/index.ts +216 -0
  13. package/src/components/alert/indexType.ts +30 -0
  14. package/src/components/confirm/index.css +45 -0
  15. package/src/components/confirm/index.ts +264 -0
  16. package/src/components/confirm/indexType.ts +17 -0
  17. package/src/components/drawer/index.css +47 -0
  18. package/src/components/drawer/index.ts +338 -0
  19. package/src/components/drawer/indexType.ts +53 -0
  20. package/src/components/folder/folderIcon.ts +28 -0
  21. package/src/components/folder/index.css +291 -0
  22. package/src/components/folder/index.ts +1111 -0
  23. package/src/components/folder/indexType.ts +87 -0
  24. package/src/components/iframe/index.css +90 -0
  25. package/src/components/iframe/index.ts +415 -0
  26. package/src/components/iframe/indexType.ts +144 -0
  27. package/src/components/loading/index.css +60 -0
  28. package/src/components/loading/index.ts +123 -0
  29. package/src/components/loading/indexType.ts +31 -0
  30. package/src/components/panel/buttonType.ts +60 -0
  31. package/src/components/panel/commonType.ts +50 -0
  32. package/src/components/panel/deepMenuType.ts +59 -0
  33. package/src/components/panel/formsType.ts +32 -0
  34. package/src/components/panel/index.css +754 -0
  35. package/src/components/panel/index.ts +2435 -0
  36. package/src/components/panel/indexType.ts +107 -0
  37. package/src/components/panel/inputType.ts +65 -0
  38. package/src/components/panel/ownType.ts +28 -0
  39. package/src/components/panel/selectType.ts +80 -0
  40. package/src/components/panel/sliderType.ts +59 -0
  41. package/src/components/panel/switchType.ts +43 -0
  42. package/src/components/panel/textareaType.ts +54 -0
  43. package/src/components/prompt/index.css +60 -0
  44. package/src/components/prompt/index.ts +293 -0
  45. package/src/components/prompt/indexType.ts +47 -0
  46. package/src/components/rightClickMenu/index.ts +729 -0
  47. package/src/components/rightClickMenu/indexType.ts +89 -0
  48. package/src/components/searchSuggestion/index.css +0 -0
  49. package/src/components/searchSuggestion/index.ts +656 -0
  50. package/src/components/searchSuggestion/indexType.ts +238 -0
  51. package/src/components/tooltip/index.css +171 -0
  52. package/src/components/tooltip/index.ts +358 -0
  53. package/src/components/tooltip/indexType.ts +95 -0
  54. package/src/css/animation.css +2240 -0
  55. package/src/css/button.css +290 -0
  56. package/src/css/common.css +24 -0
  57. package/src/css/index.css +135 -0
  58. package/src/css/ninePalaceGridPosition.css +50 -0
  59. package/src/css/scrollbar.css +18 -0
  60. package/src/handler/PopsElementHandler.ts +353 -0
  61. package/src/handler/PopsHandler.ts +659 -0
  62. package/src/svg/arrowLeft.svg +4 -0
  63. package/src/svg/arrowRight.svg +4 -0
  64. package/src/svg/chromeFilled.svg +14 -0
  65. package/src/svg/circleClose.svg +8 -0
  66. package/src/svg/close.svg +5 -0
  67. package/src/svg/cpu.svg +8 -0
  68. package/src/svg/delete.svg +5 -0
  69. package/src/svg/documentCopy.svg +5 -0
  70. package/src/svg/edit.svg +8 -0
  71. package/src/svg/eleme.svg +5 -0
  72. package/src/svg/elemePlus.svg +5 -0
  73. package/src/svg/headset.svg +5 -0
  74. package/src/svg/hide.svg +8 -0
  75. package/src/svg/keyboard.svg +8 -0
  76. package/src/svg/loading.svg +5 -0
  77. package/src/svg/max.svg +5 -0
  78. package/src/svg/min.svg +5 -0
  79. package/src/svg/mise.svg +5 -0
  80. package/src/svg/monitor.svg +5 -0
  81. package/src/svg/next.svg +5 -0
  82. package/src/svg/picture.svg +8 -0
  83. package/src/svg/prev.svg +5 -0
  84. package/src/svg/search.svg +5 -0
  85. package/src/svg/share.svg +5 -0
  86. package/src/svg/upload.svg +5 -0
  87. package/src/svg/videoPause.svg +5 -0
  88. package/src/svg/videoPlay.svg +5 -0
  89. package/src/svg/view.svg +5 -0
  90. package/src/types/PopsDOMUtilsEventType.d.ts +246 -0
  91. package/src/types/animation.d.ts +19 -0
  92. package/src/types/button.d.ts +226 -0
  93. package/src/types/components.d.ts +197 -0
  94. package/src/types/event.d.ts +62 -0
  95. package/src/types/global.d.ts +11 -0
  96. package/src/types/icon.d.ts +32 -0
  97. package/src/types/layer.d.ts +20 -0
  98. package/src/types/main.d.ts +136 -0
  99. package/src/types/mask.d.ts +35 -0
  100. package/src/types/position.d.ts +60 -0
  101. package/src/utils/AnyTouch.js +1394 -0
  102. package/src/utils/PopsDOMUtils.ts +2013 -0
  103. package/src/utils/PopsInstanceUtils.ts +685 -0
  104. package/src/utils/PopsMathUtils.ts +77 -0
  105. package/src/utils/PopsUtils.ts +380 -0
@@ -0,0 +1,729 @@
1
+ import { OriginPrototype } from "../../Core";
2
+ import { PopsHandler } from "../../handler/PopsHandler";
3
+ import { pops } from "../../Pops";
4
+ import type { PopsIcon } from "../../types/icon";
5
+ import { popsDOMUtils } from "../../utils/PopsDOMUtils";
6
+ import { popsUtils } from "../../utils/PopsUtils";
7
+ import type {
8
+ PopsRightClickMenuDataDetails,
9
+ PopsRightClickMenuDetails,
10
+ } from "./indexType";
11
+
12
+ export class PopsRightClickMenu {
13
+ constructor(details: PopsRightClickMenuDetails) {
14
+ const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow();
15
+ PopsHandler.handleInit($shadowRoot, [
16
+ pops.config.cssText.index,
17
+ pops.config.cssText.anim,
18
+ pops.config.cssText.common,
19
+ ]);
20
+
21
+ let config: Required<PopsRightClickMenuDetails> = {
22
+ target: document.documentElement,
23
+ targetSelector: null,
24
+ data: [
25
+ {
26
+ icon: pops.config.iconSVG.search,
27
+ iconIsLoading: false,
28
+ text: "搜索",
29
+ callback(clickEvent, contextMenuEvent, liElement) {
30
+ console.log("点击:" + this.text, [
31
+ clickEvent,
32
+ contextMenuEvent,
33
+ liElement,
34
+ ]);
35
+ },
36
+ },
37
+ {
38
+ icon: pops.config.iconSVG.documentCopy,
39
+ iconIsLoading: false,
40
+ text: "复制",
41
+ callback(clickEvent, contextMenuEvent, liElement) {
42
+ console.log("点击:" + this.text, [
43
+ clickEvent,
44
+ contextMenuEvent,
45
+ liElement,
46
+ ]);
47
+ },
48
+ },
49
+ {
50
+ icon: pops.config.iconSVG.delete,
51
+ text: "删除",
52
+ iconIsLoading: false,
53
+ callback(clickEvent, contextMenuEvent, liElement) {
54
+ console.log("点击:" + this.text, [
55
+ clickEvent,
56
+ contextMenuEvent,
57
+ liElement,
58
+ ]);
59
+ },
60
+ },
61
+ {
62
+ icon: pops.config.iconSVG.loading,
63
+ iconIsLoading: true,
64
+ text: "加载",
65
+ callback(clickEvent, contextMenuEvent, liElement) {
66
+ console.log("点击:" + this.text, [
67
+ clickEvent,
68
+ contextMenuEvent,
69
+ liElement,
70
+ ]);
71
+ return false;
72
+ },
73
+ },
74
+ {
75
+ icon: pops.config.iconSVG.elemePlus,
76
+ iconIsLoading: true,
77
+ text: "饿了么",
78
+ callback(clickEvent, contextMenuEvent, liElement) {
79
+ console.log("点击:" + this.text, [
80
+ clickEvent,
81
+ contextMenuEvent,
82
+ liElement,
83
+ ]);
84
+ return false;
85
+ },
86
+ item: [
87
+ {
88
+ icon: "",
89
+ iconIsLoading: false,
90
+ text: "处理文件",
91
+ callback(clickEvent, contextMenuEvent, liElement) {
92
+ console.log("点击:" + this.text, [
93
+ clickEvent,
94
+ contextMenuEvent,
95
+ liElement,
96
+ ]);
97
+ },
98
+ },
99
+ {
100
+ icon: "",
101
+ iconIsLoading: false,
102
+ text: "其它处理",
103
+ callback(clickEvent, contextMenuEvent, liElement) {
104
+ console.log("点击:" + this.text, [
105
+ clickEvent,
106
+ contextMenuEvent,
107
+ liElement,
108
+ ]);
109
+ },
110
+ item: [
111
+ {
112
+ icon: pops.config.iconSVG.view,
113
+ iconIsLoading: false,
114
+ text: "查看",
115
+ callback(clickEvent, contextMenuEvent, liElement) {
116
+ console.log("点击:" + this.text, [
117
+ clickEvent,
118
+ contextMenuEvent,
119
+ liElement,
120
+ ]);
121
+ },
122
+ },
123
+ ],
124
+ },
125
+ ],
126
+ },
127
+ ],
128
+ className: "",
129
+ isAnimation: true,
130
+ only: false,
131
+ zIndex: 10000,
132
+ preventDefault: true,
133
+ style: null,
134
+ beforeAppendToPageCallBack() {},
135
+ };
136
+ config = popsUtils.assign(config, details);
137
+ if (config.target == null) {
138
+ throw "config.target 不能为空";
139
+ }
140
+ if (details.data) {
141
+ config.data = details.data;
142
+ }
143
+ let guid = popsUtils.getRandomGUID();
144
+ const PopsType = "rightClickMenu";
145
+ config = PopsHandler.handleOnly(PopsType, config);
146
+
147
+ if (config.style != null) {
148
+ let cssNode = document.createElement("style");
149
+ cssNode.setAttribute("type", "text/css");
150
+ cssNode.innerHTML = config.style;
151
+ $shadowRoot.appendChild(cssNode);
152
+ }
153
+
154
+ const PopsContextMenu = {
155
+ /**
156
+ * 根元素
157
+ */
158
+ rootElement: null as any as HTMLElement,
159
+ /**
160
+ * 全局点击检测
161
+ * @param event
162
+ */
163
+ windowCheckClickEvent(event: MouseEvent | PointerEvent) {
164
+ if (!PopsContextMenu.rootElement) {
165
+ return;
166
+ }
167
+ let $click = event.target as HTMLElement;
168
+ if ($click.closest(`.pops-${PopsType}`)) {
169
+ return;
170
+ }
171
+ if (
172
+ $click.className &&
173
+ $click.className === "pops-shadow-container" &&
174
+ $click.shadowRoot != null
175
+ ) {
176
+ return;
177
+ }
178
+ PopsContextMenu.closeAllMenu(PopsContextMenu.rootElement);
179
+ },
180
+ /**
181
+ * target为shadowRoot或shadowRoot内的全局点击检测
182
+ * @param event
183
+ */
184
+ shadowRootCheckClickEvent(event: MouseEvent | PointerEvent) {
185
+ if (!PopsContextMenu.rootElement) {
186
+ return;
187
+ }
188
+ let $click = event.target as HTMLElement;
189
+ if ($click.closest(`.pops-${PopsType}`)) {
190
+ return;
191
+ }
192
+ PopsContextMenu.closeAllMenu(PopsContextMenu.rootElement);
193
+ },
194
+ /**
195
+ * 添加全局点击检测事件
196
+ */
197
+ addWindowCheckClickListener() {
198
+ popsDOMUtils.on(
199
+ globalThis,
200
+ "click touchstart",
201
+ void 0,
202
+ PopsContextMenu.windowCheckClickEvent,
203
+ {
204
+ capture: true,
205
+ }
206
+ );
207
+ if (config.target instanceof Node) {
208
+ const $shadowRoot = config.target.getRootNode();
209
+ if ($shadowRoot instanceof ShadowRoot) {
210
+ popsDOMUtils.on(
211
+ $shadowRoot,
212
+ "click touchstart",
213
+ void 0,
214
+ PopsContextMenu.shadowRootCheckClickEvent,
215
+ {
216
+ capture: true,
217
+ }
218
+ );
219
+ }
220
+ }
221
+ },
222
+ /**
223
+ * 移除全局点击检测事件
224
+ */
225
+ removeWindowCheckClickListener() {
226
+ popsDOMUtils.off(
227
+ globalThis,
228
+ "click touchstart",
229
+ void 0,
230
+ PopsContextMenu.windowCheckClickEvent,
231
+ {
232
+ capture: true,
233
+ }
234
+ );
235
+ if (config.target instanceof Node) {
236
+ const $shadowRoot = config.target.getRootNode();
237
+ if ($shadowRoot instanceof ShadowRoot) {
238
+ popsDOMUtils.off(
239
+ $shadowRoot,
240
+ "click touchstart",
241
+ void 0,
242
+ PopsContextMenu.windowCheckClickEvent,
243
+ {
244
+ capture: true,
245
+ }
246
+ );
247
+ }
248
+ }
249
+ },
250
+ /**
251
+ * contextmenu事件
252
+ * @param event
253
+ */
254
+ contextMenuEvent(event: PointerEvent) {
255
+ if (config.preventDefault) {
256
+ popsDOMUtils.preventEvent(event);
257
+ }
258
+ if (PopsContextMenu.rootElement) {
259
+ PopsContextMenu.closeAllMenu(PopsContextMenu.rootElement);
260
+ }
261
+ PopsContextMenu.rootElement = PopsContextMenu.showMenu(
262
+ event,
263
+ config.data
264
+ );
265
+ },
266
+ /**
267
+ * 添加contextmenu事件
268
+ * @param target 目标
269
+ * @param selector 子元素选择器
270
+ */
271
+ addContextMenuEvent(
272
+ target: PopsRightClickMenuDetails["target"],
273
+ selector?: string
274
+ ) {
275
+ popsDOMUtils.on(
276
+ target!,
277
+ "contextmenu",
278
+ selector,
279
+ PopsContextMenu.contextMenuEvent
280
+ );
281
+ },
282
+ /**
283
+ * 移除contextmenu事件
284
+ * @param target 目标
285
+ * @param selector 子元素选择器
286
+ */
287
+ removeContextMenuEvent(
288
+ target: HTMLElement | typeof globalThis | Window,
289
+ selector?: string
290
+ ) {
291
+ popsDOMUtils.off(
292
+ target,
293
+ "contextmenu",
294
+ selector,
295
+ PopsContextMenu.contextMenuEvent
296
+ );
297
+ },
298
+ /**
299
+ * 自动判断是否存在动画,存在动画就执行关闭动画并删除
300
+ * @param element
301
+ */
302
+ animationCloseMenu(element: HTMLElement) {
303
+ /**
304
+ * 动画结束触发的事件
305
+ */
306
+ function transitionEndEvent(event: TransitionEvent) {
307
+ popsDOMUtils.off(
308
+ element,
309
+ popsDOMUtils.getTransitionEndNameList(),
310
+ void 0,
311
+ transitionEndEvent,
312
+ {
313
+ capture: true,
314
+ }
315
+ );
316
+ element.remove();
317
+ }
318
+ if (element.classList.contains(`pops-${PopsType}-anim-show`)) {
319
+ /* 有动画 */
320
+ popsDOMUtils.on(
321
+ element,
322
+ popsDOMUtils.getTransitionEndNameList(),
323
+ void 0,
324
+ transitionEndEvent,
325
+ {
326
+ capture: true,
327
+ }
328
+ );
329
+ element.classList.remove(`pops-${PopsType}-anim-show`);
330
+ } else {
331
+ /* 无动画 */
332
+ element.remove();
333
+ }
334
+ },
335
+ /**
336
+ * 关闭所有菜单
337
+ * @param rootElement
338
+ */
339
+ closeAllMenu(rootElement: HTMLElement) {
340
+ if ((rootElement as any)?.["__menuData__"]?.root) {
341
+ rootElement = (rootElement as any)?.["__menuData__"]?.root;
342
+ }
343
+
344
+ let childMenuList = (rootElement as any)["__menuData__"]
345
+ .child as HTMLElement[];
346
+ childMenuList.forEach((childMenuElement) => {
347
+ this.animationCloseMenu(childMenuElement);
348
+ });
349
+ this.animationCloseMenu(rootElement);
350
+ PopsContextMenu.rootElement = null as any;
351
+ },
352
+ /**
353
+ * 获取菜单容器
354
+ * @param zIndex z-index值
355
+ * @param isChildren 是否是rightClickMenu的某一项的子菜单
356
+ */
357
+ getMenuContainerElement(zIndex: number, isChildren: boolean) {
358
+ let menuElement = popsUtils.parseTextToDOM(`
359
+ <div class="pops-${PopsType}" ${isChildren ? 'is-children="true"' : ""}>
360
+ <style type="text/css" data-from="pops-${PopsType}">
361
+ .pops-${PopsType} *{
362
+ -webkit-box-sizing: border-box;
363
+ box-sizing: border-box;
364
+ margin: 0;
365
+ padding: 0;
366
+ -webkit-tap-highlight-color: transparent;
367
+ scrollbar-width: thin;
368
+ }
369
+ .pops-${PopsType}{
370
+ position: fixed;
371
+ z-index: ${zIndex};
372
+ text-align: center;
373
+ border-radius: 3px;
374
+ font-size: 16px;
375
+ font-weight: 500;
376
+ background: #fff;
377
+ box-shadow: 0px 1px 6px 1px #cacaca;
378
+ }
379
+ .pops-${PopsType}-anim-grid{
380
+ display: grid;
381
+ transition: 0.3s;
382
+ grid-template-rows: 0fr;
383
+ }
384
+ .pops-${PopsType}-anim-show{
385
+ grid-template-rows: 1fr;
386
+ }
387
+ .pops-${PopsType}-is-visited{
388
+ background: #dfdfdf;
389
+ }
390
+ i.pops-${PopsType}-icon {
391
+ height: 1em;
392
+ width: 1em;
393
+ line-height: 1em;
394
+ display: inline-flex;
395
+ justify-content: center;
396
+ align-items: center;
397
+ position: relative;
398
+ fill: currentColor;
399
+ color: inherit;
400
+ font-size: inherit;
401
+ margin-right: 6px;
402
+ }
403
+ i.pops-${PopsType}-icon[is-loading="true"]{
404
+ animation: rotating 2s linear infinite;
405
+ }
406
+ .pops-${PopsType} li:hover{background:#dfdfdf;cursor:pointer}
407
+ .pops-${PopsType} ul{
408
+ margin: 0;
409
+ padding: 0;
410
+ display: flex;
411
+ flex-direction: column;
412
+ align-items: flex-start;
413
+ justify-content: center;
414
+ overflow: hidden;
415
+ }
416
+ .pops-${PopsType} ul li {
417
+ padding: 5px 10px;
418
+ margin: 2.5px 5px;
419
+ border-radius: 3px;
420
+ display: flex;
421
+ width: -webkit-fill-available;
422
+ width: -moz-available;
423
+ text-align: left;
424
+ user-select: none;
425
+ -webkit-user-select: none;
426
+ -moz-user-select: none;
427
+ -ms-user-select: none;
428
+ align-items: center;
429
+ }
430
+ .pops-${PopsType} ul li:first-child{
431
+ margin-top: 5px;
432
+ }
433
+ .pops-${PopsType} ul li:last-child{
434
+ margin-bottom: 5px;
435
+ }
436
+ </style>
437
+ <ul></ul>
438
+ </div>
439
+ `);
440
+ /* 添加动画 */
441
+ if (config.isAnimation) {
442
+ popsDOMUtils.addClassName(menuElement, `pops-${PopsType}-anim-grid`);
443
+ }
444
+ return menuElement;
445
+ },
446
+ /**
447
+ * 获取left、top偏移
448
+ * @param menuElement 菜单元素
449
+ * @param x
450
+ * @param y
451
+ */
452
+ getOffset(menuElement: HTMLElement, x: number, y: number) {
453
+ /* left最大偏移 */
454
+ let maxLeftOffset =
455
+ popsDOMUtils.width(globalThis) - popsDOMUtils.width(menuElement) - 1;
456
+ /* top最大偏移 */
457
+ let maxTopOffset =
458
+ popsDOMUtils.height(globalThis) -
459
+ popsDOMUtils.height(menuElement) -
460
+ 8;
461
+
462
+ let currentLeftOffset = x;
463
+ let currentTopOffset = y;
464
+ /* 不允许超出left最大值 */
465
+ currentLeftOffset = currentLeftOffset < 0 ? 0 : currentLeftOffset;
466
+ currentLeftOffset =
467
+ currentLeftOffset < maxLeftOffset ? currentLeftOffset : maxLeftOffset;
468
+ /* 不允许超出top最大值 */
469
+ currentTopOffset = currentTopOffset < 0 ? 0 : currentTopOffset;
470
+ currentTopOffset =
471
+ currentTopOffset < maxTopOffset ? currentTopOffset : maxTopOffset;
472
+ return {
473
+ left: currentLeftOffset,
474
+ top: currentTopOffset,
475
+ };
476
+ },
477
+ /**
478
+ * 显示菜单
479
+ * @param menuEvent 触发的事件
480
+ * @param _config_
481
+ */
482
+ showMenu(
483
+ menuEvent: PointerEvent,
484
+ _config_: PopsRightClickMenuDataDetails[]
485
+ ) {
486
+ let menuElement = this.getMenuContainerElement(config.zIndex, false);
487
+ (menuElement as any)["__menuData__"] = {
488
+ child: [],
489
+ };
490
+ PopsContextMenu.addMenuLiELement(
491
+ menuEvent,
492
+ menuElement as HTMLDivElement,
493
+ menuElement as HTMLDivElement,
494
+ _config_
495
+ );
496
+ /* 先隐藏 */
497
+ popsDOMUtils.css(menuElement, {
498
+ display: "none",
499
+ });
500
+ popsDOMUtils.append($shadowRoot, menuElement);
501
+ /* 添加到页面 */
502
+ if (!document.contains($shadowContainer)) {
503
+ if (typeof config.beforeAppendToPageCallBack === "function") {
504
+ config.beforeAppendToPageCallBack($shadowRoot, $shadowContainer);
505
+ }
506
+ popsDOMUtils.appendBody($shadowContainer);
507
+ }
508
+ let { left: menuLeftOffset, top: menuTopOffset } = this.getOffset(
509
+ menuElement,
510
+ menuEvent.clientX,
511
+ menuEvent.clientY
512
+ );
513
+ popsDOMUtils.css(menuElement, {
514
+ left: menuLeftOffset,
515
+ top: menuTopOffset,
516
+ display: "",
517
+ });
518
+ /* 过渡动画 */
519
+ if (config.isAnimation) {
520
+ popsDOMUtils.addClassName(menuElement, `pops-${PopsType}-anim-show`);
521
+ }
522
+ return menuElement;
523
+ },
524
+ /**
525
+ * 显示子菜单
526
+ * @param menuEvent 事件
527
+ * @param posInfo 位置信息
528
+ * @param _config_
529
+ * @param rootElement 根菜单元素
530
+ * @param targetLiElement 父li项元素
531
+ */
532
+ showClildMenu(
533
+ menuEvent: PointerEvent,
534
+ posInfo: {
535
+ clientX: number;
536
+ clientY: number;
537
+ },
538
+ _config_: PopsRightClickMenuDataDetails[],
539
+ rootElement: HTMLDivElement,
540
+ targetLiElement: HTMLLIElement
541
+ ) {
542
+ let menuElement = this.getMenuContainerElement(config.zIndex, true);
543
+ (menuElement as any)["__menuData__"] = {
544
+ parent: targetLiElement,
545
+ root: rootElement,
546
+ };
547
+ (rootElement as any)["__menuData__"].child.push(menuElement);
548
+ PopsContextMenu.addMenuLiELement(
549
+ menuEvent as PointerEvent,
550
+ rootElement as HTMLDivElement,
551
+ menuElement as HTMLDivElement,
552
+ _config_
553
+ );
554
+ /* 先隐藏 */
555
+ popsDOMUtils.css(menuElement, {
556
+ display: "none",
557
+ });
558
+ /* 添加到页面 */
559
+ popsDOMUtils.append($shadowRoot, menuElement);
560
+ let { left: menuLeftOffset, top: menuTopOffset } = this.getOffset(
561
+ menuElement,
562
+ posInfo.clientX,
563
+ posInfo.clientY
564
+ );
565
+ popsDOMUtils.css(menuElement, {
566
+ left: menuLeftOffset,
567
+ top: menuTopOffset,
568
+ display: "",
569
+ });
570
+ /* 过渡动画 */
571
+ if (config.isAnimation) {
572
+ popsDOMUtils.addClassName(menuElement, `pops-${PopsType}-anim-show`);
573
+ }
574
+ return menuElement;
575
+ },
576
+ /**
577
+ * 获取菜单项的元素
578
+ * @param menuEvent 事件
579
+ * @param rootElement 根元素
580
+ * @param menuElement 菜单元素
581
+ * @param _config_ 配置
582
+ */
583
+ addMenuLiELement(
584
+ menuEvent: PointerEvent,
585
+ rootElement: HTMLDivElement,
586
+ menuElement: HTMLDivElement,
587
+ _config_: PopsRightClickMenuDataDetails[]
588
+ ) {
589
+ let menuEventTarget = menuEvent.target;
590
+ let menuULElement = menuElement.querySelector<HTMLUListElement>("ul")!;
591
+ _config_.forEach((item) => {
592
+ let menuLiElement =
593
+ popsUtils.parseTextToDOM<HTMLLIElement>(`<li></li>`);
594
+ /* 判断有无图标,有就添加进去 */
595
+ if (typeof item.icon === "string" && item.icon.trim() !== "") {
596
+ let iconSVGHTML =
597
+ pops.config.iconSVG[item.icon as PopsIcon] ?? item.icon;
598
+ let iconElement = popsUtils.parseTextToDOM(
599
+ `<i class="pops-${PopsType}-icon" is-loading="${item.iconIsLoading}">${iconSVGHTML}</i>`
600
+ );
601
+ menuLiElement.appendChild(iconElement);
602
+ }
603
+ /* 插入文字 */
604
+ menuLiElement.insertAdjacentHTML(
605
+ "beforeend",
606
+ `<span>${item.text}</span>`
607
+ );
608
+ /* 如果存在子数据,显示 */
609
+ if (item.item && Array.isArray(item.item)) {
610
+ popsDOMUtils.addClassName(menuLiElement, `pops-${PopsType}-item`);
611
+ }
612
+ /* 鼠标|触摸 移入事件 */
613
+ function liElementHoverEvent() {
614
+ Array.from(
615
+ menuULElement.children as any as HTMLLIElement[]
616
+ ).forEach((liElement) => {
617
+ popsDOMUtils.removeClassName(
618
+ liElement,
619
+ `pops-${PopsType}-is-visited`
620
+ );
621
+ if (!(liElement as any).__menuData__) {
622
+ return;
623
+ }
624
+ function removeElement(element: HTMLElement) {
625
+ element.querySelectorAll("ul li").forEach((ele) => {
626
+ if ((ele as any)?.__menuData__?.child) {
627
+ removeElement((ele as any).__menuData__.child);
628
+ }
629
+ });
630
+ element.remove();
631
+ }
632
+ /* 遍历根元素的上的__menuData__.child,判断 */
633
+ removeElement((liElement as any).__menuData__.child);
634
+ });
635
+ /* 清理根元素上的children不存在于页面中的元素 */
636
+ for (
637
+ let index = 0;
638
+ index < (rootElement as any).__menuData__.child.length;
639
+ index++
640
+ ) {
641
+ let element = (rootElement as any).__menuData__.child[index];
642
+ if (!$shadowRoot.contains(element)) {
643
+ (rootElement as any).__menuData__.child.splice(index, 1);
644
+ index--;
645
+ }
646
+ }
647
+ popsDOMUtils.addClassName(
648
+ menuLiElement,
649
+ `pops-${PopsType}-is-visited`
650
+ );
651
+ if (!item.item) {
652
+ return;
653
+ }
654
+ let rect = menuLiElement.getBoundingClientRect();
655
+ let childMenu = PopsContextMenu.showClildMenu(
656
+ menuEvent,
657
+ {
658
+ clientX: rect.left + popsDOMUtils.outerWidth(menuLiElement),
659
+ clientY: rect.top,
660
+ },
661
+ item.item,
662
+ rootElement as HTMLDivElement,
663
+ menuLiElement
664
+ );
665
+ (menuLiElement as any).__menuData__ = {
666
+ child: childMenu,
667
+ };
668
+ }
669
+ /**
670
+ * 点击事件
671
+ * @param clickEvent
672
+ * @returns
673
+ */
674
+ async function liElementClickEvent(
675
+ clickEvent: MouseEvent | PointerEvent
676
+ ) {
677
+ if (typeof item.callback === "function") {
678
+ OriginPrototype.Object.defineProperty(menuEvent, "target", {
679
+ get() {
680
+ return menuEventTarget;
681
+ },
682
+ });
683
+ let callbackResult = await item.callback(
684
+ clickEvent as PointerEvent,
685
+ menuEvent,
686
+ menuLiElement
687
+ );
688
+ if (
689
+ typeof callbackResult === "boolean" &&
690
+ callbackResult == false
691
+ ) {
692
+ return;
693
+ }
694
+ }
695
+ /* 取消绑定的鼠标/触摸事件,防止关闭的时候再次触发 */
696
+ Array.from(
697
+ menuULElement.children as any as HTMLLIElement[]
698
+ ).forEach((liEle) => {
699
+ popsDOMUtils.off(liEle, "mouseenter touchstart");
700
+ });
701
+ PopsContextMenu.closeAllMenu(rootElement);
702
+ }
703
+ popsDOMUtils.on(
704
+ menuLiElement,
705
+ "mouseenter touchstart",
706
+ void 0,
707
+ liElementHoverEvent
708
+ );
709
+ /* 项-点击事件 */
710
+ popsDOMUtils.on(menuLiElement, "click", void 0, liElementClickEvent);
711
+ menuULElement.appendChild(menuLiElement);
712
+ });
713
+ },
714
+ };
715
+
716
+ PopsContextMenu.addContextMenuEvent(config.target, config.targetSelector!);
717
+ PopsContextMenu.addWindowCheckClickListener();
718
+
719
+ return {
720
+ guid: guid,
721
+ config: config,
722
+ removeWindowCheckClickListener:
723
+ PopsContextMenu.removeWindowCheckClickListener,
724
+ addWindowCheckClickListener: PopsContextMenu.addWindowCheckClickListener,
725
+ removeContextMenuEvent: PopsContextMenu.removeContextMenuEvent,
726
+ addContextMenuEvent: PopsContextMenu.addContextMenuEvent,
727
+ };
728
+ }
729
+ }