@whitesev/pops 3.3.5 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/index.amd.js +13118 -12932
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +1 -1
  4. package/dist/index.amd.min.js.map +1 -1
  5. package/dist/index.cjs.js +761 -575
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +1 -1
  8. package/dist/index.cjs.min.js.map +1 -1
  9. package/dist/index.esm.js +761 -575
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +1 -1
  12. package/dist/index.esm.min.js.map +1 -1
  13. package/dist/index.iife.js +13119 -12933
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +1 -1
  16. package/dist/index.iife.min.js.map +1 -1
  17. package/dist/index.system.js +13122 -12936
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +1 -1
  20. package/dist/index.system.min.js.map +1 -1
  21. package/dist/index.umd.js +13121 -12935
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +1 -1
  24. package/dist/index.umd.min.js.map +1 -1
  25. package/dist/types/src/Pops.d.ts +66 -52
  26. package/dist/types/src/PopsAnimation.d.ts +33 -0
  27. package/dist/types/src/PopsIcon.d.ts +1 -1
  28. package/dist/types/src/components/alert/index.d.ts +3 -1
  29. package/dist/types/src/components/confirm/index.d.ts +3 -1
  30. package/dist/types/src/components/drawer/index.d.ts +3 -1
  31. package/dist/types/src/components/folder/index.d.ts +3 -1
  32. package/dist/types/src/components/iframe/index.d.ts +6 -1
  33. package/dist/types/src/components/iframe/types/index.d.ts +4 -2
  34. package/dist/types/src/components/loading/index.d.ts +3 -1
  35. package/dist/types/src/components/panel/handlerComponents.d.ts +4 -1
  36. package/dist/types/src/components/panel/index.d.ts +2 -16
  37. package/dist/types/src/components/panel/types/index.d.ts +7 -2
  38. package/dist/types/src/components/prompt/index.d.ts +3 -1
  39. package/dist/types/src/components/searchSuggestion/index.d.ts +4 -1
  40. package/dist/types/src/components/tooltip/index.d.ts +5 -1
  41. package/dist/types/src/event/EventEmiter.d.ts +33 -0
  42. package/dist/types/src/handler/PopsHandler.d.ts +16 -11
  43. package/dist/types/src/handler/PopsInstHandler.d.ts +67 -0
  44. package/dist/types/src/types/EventEmitter.d.ts +18 -0
  45. package/dist/types/src/types/PopsDOMUtilsEventType.d.ts +4 -0
  46. package/dist/types/src/types/button.d.ts +2 -1
  47. package/dist/types/src/types/event.d.ts +6 -2
  48. package/dist/types/src/types/inst.d.ts +4 -4
  49. package/dist/types/src/utils/PopsDOMUtils.d.ts +0 -16
  50. package/dist/types/src/utils/PopsInstanceUtils.d.ts +0 -88
  51. package/dist/types/src/utils/PopsUtils.d.ts +57 -0
  52. package/package.json +3 -3
  53. package/src/Pops.ts +7 -2
  54. package/src/PopsAnimation.ts +126 -0
  55. package/src/components/alert/index.ts +9 -9
  56. package/src/components/confirm/index.ts +9 -9
  57. package/src/components/drawer/index.ts +12 -9
  58. package/src/components/folder/index.ts +9 -8
  59. package/src/components/iframe/index.ts +18 -12
  60. package/src/components/iframe/types/index.ts +10 -8
  61. package/src/components/loading/index.ts +12 -6
  62. package/src/components/panel/defaultConfig.ts +1 -0
  63. package/src/components/panel/handlerComponents.ts +41 -111
  64. package/src/components/panel/index.ts +34 -28
  65. package/src/components/panel/types/index.ts +7 -2
  66. package/src/components/prompt/index.ts +9 -8
  67. package/src/components/rightClickMenu/index.ts +17 -14
  68. package/src/components/searchSuggestion/index.ts +5 -1
  69. package/src/components/tooltip/index.ts +20 -10
  70. package/src/event/EventEmiter.ts +95 -0
  71. package/src/handler/PopsElementHandler.ts +6 -4
  72. package/src/handler/PopsHandler.ts +66 -48
  73. package/src/handler/PopsInstHandler.ts +557 -0
  74. package/src/types/EventEmitter.d.ts +18 -0
  75. package/src/types/PopsDOMUtilsEventType.d.ts +4 -0
  76. package/src/types/button.d.ts +2 -1
  77. package/src/types/event.d.ts +6 -2
  78. package/src/types/inst.d.ts +4 -4
  79. package/src/utils/PopsDOMUtils.ts +1 -40
  80. package/src/utils/PopsInstanceUtils.ts +19 -627
  81. package/src/utils/PopsUtils.ts +194 -0
@@ -1,103 +1,7 @@
1
- import type { PopsAlertConfig } from "../components/alert/types";
2
- import type { PopsConfirmConfig } from "../components/confirm/types";
3
- import type { PopsDrawerConfig } from "../components/drawer/types";
4
- import type { PopsFolderConfig } from "../components/folder/types";
5
- import type { PopsIframeConfig } from "../components/iframe/types";
6
- import type { PopsLoadingConfig } from "../components/loading/types";
7
- import type { PopsPanelConfig } from "../components/panel/types";
8
- import type { PopsPromptConfig } from "../components/prompt/types/index";
9
- import type { PopsInstGeneralConfig } from "../types/inst";
10
- import type { PopsInstStoreType } from "../types/main";
11
- import { popsDOMUtils } from "./PopsDOMUtils";
12
- import { popsUtils } from "./PopsUtils";
13
- import { PopsCore } from "../PopsCore";
14
1
  import { PopsInstData } from "../PopsInst";
15
- import { PopsAnimation } from "../PopsAnimation";
16
- import type { PopsRightClickMenuConfig } from "../components/rightClickMenu/types";
17
- import type { PopsToolTipConfig } from "../components/tooltip/types";
2
+ import type { PopsInstStoreType } from "../types/main";
18
3
 
19
4
  export const PopsInstanceUtils = {
20
- /**
21
- * 获取页面中最大的z-index的元素信息
22
- * @param deviation 获取最大的z-index值的偏移,默认是+1
23
- * @param node 进行判断的元素,默认是document
24
- * @param ignoreCallBack 执行元素处理时调用的函数,返回false可忽略不想要处理的元素
25
- * @example
26
- * Utils.getMaxZIndexNodeInfo();
27
- * > {
28
- * node: ...,
29
- * zIndex: 1001
30
- * }
31
- **/
32
- getMaxZIndexNodeInfo(
33
- deviation = 1,
34
- target: Element | ShadowRoot | Document = PopsCore.document,
35
- ignoreCallBack?: ($ele: Element | HTMLElement | ShadowRoot) => boolean | void
36
- ): {
37
- node: Element;
38
- zIndex: number;
39
- } {
40
- deviation = Number.isNaN(deviation) ? 1 : deviation;
41
- // 最大值 2147483647
42
- // const maxZIndex = Math.pow(2, 31) - 1;
43
- // 比较值 2000000000
44
- const maxZIndexCompare = 2 * Math.pow(10, 9);
45
- // 当前页面最大的z-index
46
- let zIndex = 0;
47
- // 当前的最大z-index的元素,调试使用
48
- let maxZIndexNode: Element | null = null;
49
- /**
50
- * 元素是否可见
51
- * @param $css
52
- */
53
- function isVisibleNode($css: CSSStyleDeclaration): boolean {
54
- return $css.position !== "static" && $css.display !== "none";
55
- }
56
- /**
57
- * 查询元素的z-index
58
- * 并比较值是否是已获取的最大值
59
- * @param $ele
60
- */
61
- function queryMaxZIndex($ele: Element) {
62
- if (typeof ignoreCallBack === "function") {
63
- const ignoreResult = ignoreCallBack($ele);
64
- if (typeof ignoreResult === "boolean" && !ignoreResult) {
65
- return;
66
- }
67
- }
68
- /** 元素的样式 */
69
- const nodeStyle = PopsCore.window.getComputedStyle($ele);
70
- // 不对position为static和display为none的元素进行获取它们的z-index
71
- if (isVisibleNode(nodeStyle)) {
72
- const nodeZIndex = parseInt(nodeStyle.zIndex);
73
- if (!isNaN(nodeZIndex)) {
74
- if (nodeZIndex > zIndex) {
75
- // 赋值到全局
76
- zIndex = nodeZIndex;
77
- maxZIndexNode = $ele;
78
- }
79
- }
80
- // 判断shadowRoot
81
- if ($ele.shadowRoot != null && $ele instanceof ShadowRoot) {
82
- $ele.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
83
- queryMaxZIndex($shadowEle);
84
- });
85
- }
86
- }
87
- }
88
- target.querySelectorAll("*").forEach(($ele) => {
89
- queryMaxZIndex($ele);
90
- });
91
- zIndex += deviation;
92
- if (zIndex >= maxZIndexCompare) {
93
- // 最好不要超过最大值
94
- zIndex = maxZIndexCompare;
95
- }
96
- return {
97
- node: maxZIndexNode!,
98
- zIndex: zIndex,
99
- };
100
- },
101
5
  /**
102
6
  * 获取pops所有弹窗中的最大的z-index
103
7
  * @param deviation
@@ -115,10 +19,25 @@ export const PopsInstanceUtils = {
115
19
 
116
20
  /**
117
21
  * 元素是否可见
22
+ * @param $el
118
23
  * @param $css
119
24
  */
120
- function isVisibleNode($css: CSSStyleDeclaration): boolean {
121
- return $css.position !== "static" && $css.display !== "none";
25
+ function isVisibleNode($el: Element, $css: CSSStyleDeclaration): boolean {
26
+ let flag = true;
27
+ if (typeof $el.checkVisibility === "function") {
28
+ flag = $el.checkVisibility();
29
+ } else {
30
+ flag =
31
+ $css.position !== "static" && $css.display !== "none" && $css.visibility !== "hidden" && $css.opacity !== "0";
32
+ }
33
+ if (flag) {
34
+ // css样式上可见
35
+ // 再判断宽高
36
+ const rect = $el.getBoundingClientRect();
37
+ // 确保该元素的中心点在屏幕内
38
+ flag = rect.width > 0 && rect.height > 0 && rect.x > 0 && rect.y > 0;
39
+ }
40
+ return flag;
122
41
  }
123
42
  Object.keys(PopsInstData).forEach((instKeyName) => {
124
43
  const instData = PopsInstData[instKeyName as PopsInstStoreType];
@@ -126,7 +45,7 @@ export const PopsInstanceUtils = {
126
45
  const inst = instData[index];
127
46
  const nodeStyle = window.getComputedStyle(inst.$anim);
128
47
  // 不对position为static和display为none的元素进行获取它们的z-index
129
- if (isVisibleNode(nodeStyle)) {
48
+ if (isVisibleNode(inst.$anim, nodeStyle)) {
130
49
  const nodeZIndex = parseInt(nodeStyle.zIndex);
131
50
  if (!isNaN(nodeZIndex)) {
132
51
  if (nodeZIndex > zIndex) {
@@ -145,533 +64,6 @@ export const PopsInstanceUtils = {
145
64
  }
146
65
  return { zIndex: zIndex, animElement: maxZIndexNode, isOverMaxZIndex };
147
66
  },
148
- /**
149
- * 获取页面中最大的z-index
150
- * @param deviation 获取最大的z-index值的偏移,默认是+1
151
- * @example
152
- * getMaxZIndex();
153
- * > 1001
154
- **/
155
- getMaxZIndex(deviation = 1): number {
156
- return this.getMaxZIndexNodeInfo(deviation).zIndex;
157
- },
158
- /**
159
- * 删除配置中对应的对象
160
- * @param totalInstConfigList 配置实例列表
161
- * @param guid 唯一标识
162
- * @param isAll 是否全部删除
163
- */
164
- async removeInstance(totalInstConfigList: PopsInstGeneralConfig[][], guid?: string, isAll = false) {
165
- /**
166
- * 移除元素实例
167
- * @param instCommonConfig
168
- */
169
- const removeInst = function (instCommonConfig: PopsInstGeneralConfig) {
170
- if (typeof instCommonConfig.beforeRemoveCallBack === "function") {
171
- // 调用移除签的回调
172
- instCommonConfig.beforeRemoveCallBack(instCommonConfig);
173
- }
174
- instCommonConfig?.$anim?.remove();
175
- instCommonConfig?.$pops?.remove();
176
- instCommonConfig?.$mask?.remove();
177
- instCommonConfig?.$shadowContainer?.remove();
178
- };
179
- const asyncInstTask: Promise<void>[] = [];
180
- // [ inst[], inst[],...]
181
- totalInstConfigList.forEach((instConfigList) => {
182
- // inst[]
183
- instConfigList.forEach(async (instConfigItem, index) => {
184
- // 移除全部或者guid相同
185
- if (isAll || (typeof guid === "string" && instConfigItem.guid === guid)) {
186
- // 判断是否有动画
187
- const animName = instConfigItem.$anim.getAttribute("anim")!;
188
- if (PopsAnimation.hasAnim(animName)) {
189
- const reverseAnimName = animName + "-reverse";
190
- popsDOMUtils.css(instConfigItem.$anim, "width", "100%");
191
- popsDOMUtils.css(instConfigItem.$anim, "height", "100%");
192
- popsDOMUtils.css(instConfigItem.$anim, "animation-name", reverseAnimName);
193
- if (PopsAnimation.hasAnim(popsDOMUtils.css(instConfigItem.$anim, "animation-name"))) {
194
- asyncInstTask.push(
195
- new Promise<void>((resolve) => {
196
- popsDOMUtils.on(
197
- instConfigItem.$anim,
198
- popsDOMUtils.getAnimationEndNameList(),
199
- function () {
200
- removeInst(instConfigItem);
201
- resolve();
202
- },
203
- {
204
- capture: true,
205
- }
206
- );
207
- })
208
- );
209
- } else {
210
- removeInst(instConfigItem);
211
- }
212
- } else {
213
- removeInst(instConfigItem);
214
- }
215
- instConfigList.splice(index, 1);
216
- }
217
- });
218
- });
219
- await Promise.all(asyncInstTask);
220
- return totalInstConfigList;
221
- },
222
- /**
223
- * 隐藏
224
- * @param popsType
225
- * @param instConfigList
226
- * @param guid
227
- * @param config
228
- * @param $anim
229
- * @param $mask
230
- */
231
- hide(
232
- config:
233
- | PopsAlertConfig
234
- | PopsDrawerConfig
235
- | PopsPromptConfig
236
- | PopsConfirmConfig
237
- | PopsIframeConfig
238
- | PopsLoadingConfig
239
- | PopsPanelConfig
240
- | PopsFolderConfig,
241
- popsType: PopsInstStoreType,
242
- instConfigList: PopsInstGeneralConfig[],
243
- guid: string,
244
- $anim: HTMLElement,
245
- $mask?: HTMLElement
246
- ) {
247
- return new Promise<void>((resolve) => {
248
- const $pops = $anim.querySelector<HTMLDivElement>(".pops[type-value]")!;
249
- if (popsType === "drawer") {
250
- const drawerConfig = config as Required<PopsDrawerConfig>;
251
- popsUtils.setTimeout(() => {
252
- if ($mask) {
253
- popsDOMUtils.css($mask, "display", "none");
254
- }
255
- if (["top", "bottom"].includes(drawerConfig.direction)) {
256
- $pops.style.setProperty("height", "0");
257
- } else if (["left", "right"].includes(drawerConfig.direction)) {
258
- $pops.style.setProperty("width", "0");
259
- } else {
260
- console.error("未知direction:", drawerConfig.direction);
261
- }
262
- resolve();
263
- }, drawerConfig.closeDelay);
264
- } else {
265
- const fintInst = instConfigList.find((instConfigItem) => instConfigItem.guid === guid);
266
- if (fintInst) {
267
- // 存在动画
268
- const instConfigItem = fintInst;
269
- instConfigItem.$anim.style.width = "100%";
270
- instConfigItem.$anim.style.height = "100%";
271
- Reflect.set(
272
- instConfigItem.$anim.style,
273
- "animation-name",
274
- instConfigItem.$anim.getAttribute("anim") + "-reverse"
275
- );
276
- if (PopsAnimation.hasAnim(Reflect.get(instConfigItem.$anim.style, "animation-name"))) {
277
- /**
278
- * 动画结束的回调
279
- */
280
- function animationendCallBack() {
281
- instConfigItem.$anim.style.display = "none";
282
- if (instConfigItem.$mask) {
283
- instConfigItem.$mask.style.display = "none";
284
- }
285
- popsDOMUtils.off(instConfigItem.$anim, popsDOMUtils.getAnimationEndNameList(), animationendCallBack, {
286
- capture: true,
287
- });
288
- resolve();
289
- }
290
- popsDOMUtils.on(instConfigItem.$anim, popsDOMUtils.getAnimationEndNameList(), animationendCallBack, {
291
- capture: true,
292
- });
293
- } else {
294
- instConfigItem.$anim.style.display = "none";
295
- if (instConfigItem.$mask) {
296
- instConfigItem.$mask.style.display = "none";
297
- }
298
-
299
- resolve();
300
- }
301
- }
302
- }
303
- });
304
- },
305
- /**
306
- * 显示
307
- * @param popsType
308
- * @param instConfigList
309
- * @param guid
310
- * @param config
311
- * @param $anim
312
- * @param $mask
313
- */
314
- show(
315
- config:
316
- | PopsAlertConfig
317
- | PopsDrawerConfig
318
- | PopsPromptConfig
319
- | PopsConfirmConfig
320
- | PopsIframeConfig
321
- | PopsLoadingConfig
322
- | PopsPanelConfig
323
- | PopsFolderConfig,
324
- popsType: PopsInstStoreType,
325
- instConfigList: PopsInstGeneralConfig[],
326
- guid: string,
327
- $anim: HTMLElement,
328
- $mask?: HTMLElement
329
- ) {
330
- return new Promise<void>((resolve) => {
331
- const $pops = $anim.querySelector<HTMLDivElement>(".pops[type-value]")!;
332
- if (popsType === "drawer") {
333
- const drawerConfig = config as PopsDrawerConfig;
334
- popsUtils.setTimeout(() => {
335
- if ($mask) {
336
- popsDOMUtils.css($mask, "display", "");
337
- }
338
- const direction = drawerConfig.direction!;
339
- const size = drawerConfig.size!.toString();
340
- if (["top", "bottom"].includes(direction)) {
341
- $pops.style.setProperty("height", size);
342
- } else if (["left", "right"].includes(direction)) {
343
- $pops.style.setProperty("width", size);
344
- } else {
345
- console.error("未知direction:", direction);
346
- }
347
- resolve();
348
- }, drawerConfig.openDelay ?? 0);
349
- } else {
350
- const fintInst = instConfigList.find((instConfigItem) => instConfigItem.guid === guid);
351
- if (fintInst) {
352
- const instConfigItem = fintInst;
353
- instConfigItem.$anim.style.width = "";
354
- instConfigItem.$anim.style.height = "";
355
- Reflect.set(
356
- instConfigItem.$anim.style,
357
- "animation-name",
358
- instConfigItem.$anim!.getAttribute("anim")!.replace("-reverse", "")
359
- );
360
- if (PopsAnimation.hasAnim(Reflect.get(instConfigItem.$anim.style, "animation-name"))) {
361
- /**
362
- * 动画结束的回调
363
- */
364
- function animationendCallBack() {
365
- popsDOMUtils.off(instConfigItem.$anim, popsDOMUtils.getAnimationEndNameList(), animationendCallBack, {
366
- capture: true,
367
- });
368
- resolve();
369
- }
370
- instConfigItem.$anim.style.display = "";
371
- if (instConfigItem.$mask) {
372
- instConfigItem.$mask.style.display = "";
373
- }
374
- popsDOMUtils.on(instConfigItem.$anim, popsDOMUtils.getAnimationEndNameList(), animationendCallBack, {
375
- capture: true,
376
- });
377
- } else {
378
- instConfigItem.$anim.style.display = "";
379
- if (instConfigItem.$mask) {
380
- instConfigItem.$mask.style.display = "";
381
- }
382
- resolve();
383
- }
384
- }
385
- }
386
- });
387
- },
388
- /**
389
- * 关闭
390
- * @param popsType
391
- * @param instConfigList
392
- * @param guid
393
- * @param config
394
- * @param $anim
395
- */
396
- async close(
397
- config:
398
- | PopsAlertConfig
399
- | PopsDrawerConfig
400
- | PopsPromptConfig
401
- | PopsConfirmConfig
402
- | PopsIframeConfig
403
- | PopsLoadingConfig
404
- | PopsPanelConfig
405
- | PopsFolderConfig,
406
- popsType: string,
407
- instConfigList: PopsInstGeneralConfig[],
408
- guid: string,
409
- $anim: HTMLElement
410
- ) {
411
- // eslint-disable-next-line no-async-promise-executor
412
- await new Promise<void>(async (resolve) => {
413
- const $pops = $anim.querySelector<HTMLDivElement>(".pops[type-value]")!;
414
- const drawerConfig = config as Required<PopsDrawerConfig>;
415
- /**
416
- * 动画结束事件
417
- */
418
- function transitionendEvent() {
419
- /**
420
- * 弹窗已关闭的回调
421
- */
422
- async function closeCallBack(event: Event) {
423
- if ((event as TransitionEvent).propertyName !== "transform") {
424
- return;
425
- }
426
- popsDOMUtils.off($pops, popsDOMUtils.getTransitionEndNameList(), closeCallBack);
427
- await PopsInstanceUtils.removeInstance([instConfigList], guid);
428
- resolve();
429
- }
430
- // 监听过渡结束
431
- popsDOMUtils.on($pops, popsDOMUtils.getTransitionEndNameList(), closeCallBack);
432
- const popsTransForm = getComputedStyle($pops).transform;
433
- if (popsTransForm !== "none") {
434
- popsDOMUtils.emit($pops, popsDOMUtils.getTransitionEndNameList(), void 0, true);
435
- return;
436
- }
437
- if (["top"].includes(drawerConfig.direction)) {
438
- $pops.style.setProperty("transform", "translateY(-100%)");
439
- } else if (["bottom"].includes(drawerConfig.direction)) {
440
- $pops.style.setProperty("transform", "translateY(100%)");
441
- } else if (["left"].includes(drawerConfig.direction)) {
442
- $pops.style.setProperty("transform", "translateX(-100%)");
443
- } else if (["right"].includes(drawerConfig.direction)) {
444
- $pops.style.setProperty("transform", "translateX(100%)");
445
- } else {
446
- console.error("未知direction:", drawerConfig.direction);
447
- }
448
- }
449
-
450
- if (popsType === "drawer") {
451
- popsUtils.setTimeout(() => {
452
- transitionendEvent();
453
- }, drawerConfig.closeDelay);
454
- } else {
455
- await PopsInstanceUtils.removeInstance([instConfigList], guid);
456
- resolve();
457
- }
458
- });
459
-
460
- // 判断组件内是否有rightClickMenu、tooltip、searchSuggestion组件
461
- // 有的话也需要关闭
462
- PopsInstData.rightClickMenu.forEach((itemConfig) => {
463
- const config = itemConfig.config as PopsRightClickMenuConfig;
464
- if (config.$target instanceof HTMLElement) {
465
- const $root = config.$target.getRootNode();
466
- if ($root instanceof HTMLElement && $root.parentElement == null) {
467
- // 触发销毁元素
468
- itemConfig.destory();
469
- }
470
- }
471
- });
472
- PopsInstData.tooltip.forEach((itemConfig) => {
473
- const config = itemConfig.config as PopsToolTipConfig;
474
- if (config.$target instanceof HTMLElement) {
475
- const $root = config.$target.getRootNode();
476
- if ($root instanceof HTMLElement && $root.parentElement == null) {
477
- // 触发销毁元素
478
- itemConfig.destory();
479
- }
480
- }
481
- });
482
- },
483
- /**
484
- * 拖拽元素
485
- * 说明:
486
- * + 元素的position为absolute或者fixed
487
- * + 会为元素的
488
- * @param $move 需要拖拽的元素
489
- * @param options 配置
490
- */
491
- drag(
492
- $move: HTMLElement,
493
- options: {
494
- dragElement: HTMLElement;
495
- limit: boolean;
496
- emitClick?: boolean;
497
- extraDistance: number;
498
- container?: Window | typeof globalThis | HTMLElement;
499
- moveCallBack?: (moveElement: HTMLElement, left: number, top: number) => void;
500
- endCallBack?: (moveElement: HTMLElement, left: number, top: number) => void;
501
- preventEvent?: (event: TouchEvent | PointerEvent) => boolean;
502
- }
503
- ) {
504
- options = Object.assign(
505
- {
506
- limit: true,
507
- extraDistance: 3,
508
- container: PopsCore.globalThis,
509
- emitClick: true,
510
- },
511
- options
512
- );
513
- let isMove = false;
514
- // 点击元素,left偏移
515
- let clickElementLeftOffset = 0;
516
- // 点击元素,top偏移
517
- let clickElementTopOffset = 0;
518
- const AnyTouch = popsUtils.AnyTouch();
519
- const anyTouchElement = new AnyTouch(options.dragElement, {
520
- preventDefault(event: Event) {
521
- if (typeof options.preventEvent === "function") {
522
- return options.preventEvent(event as any);
523
- } else {
524
- // 返回true阻止滑动
525
- return true;
526
- }
527
- },
528
- });
529
- popsDOMUtils.css(options.dragElement, {
530
- cursor: "move",
531
- });
532
- /**
533
- * 修改移动的元素的style
534
- */
535
- function changeMoveElementStyle(element: HTMLElement) {
536
- const old_transitionDuration = element.style.transitionDuration;
537
- if (globalThis.getComputedStyle(element).transitionDuration !== "0s") {
538
- element.style.transitionDuration = "0s";
539
- }
540
- return () => {
541
- element.style.transitionDuration = old_transitionDuration;
542
- };
543
- }
544
- /**
545
- * 获取容器的高度、宽度,一般是window为容器
546
- */
547
- function getContainerWidthOrHeight(container: HTMLElement | Window | typeof globalThis) {
548
- container = container ?? globalThis;
549
- return {
550
- width: popsDOMUtils.width(container),
551
- height: popsDOMUtils.height(container),
552
- };
553
- }
554
- /**
555
- * 获取容器的最小left、top偏移
556
- */
557
-
558
- function getContainerTopOrLeft(container: HTMLElement | Window | typeof globalThis) {
559
- container = container ?? globalThis;
560
- if (popsUtils.isWin(container)) {
561
- return {
562
- left: 0,
563
- top: 0,
564
- };
565
- } else {
566
- const rect = (container as HTMLElement).getBoundingClientRect();
567
- return {
568
- left: rect.left,
569
- top: rect.top,
570
- };
571
- }
572
- }
573
- // 获取transform偏移
574
- let transformInfo = popsDOMUtils.getTransform($move);
575
-
576
- let resumeMoveElementStyle: ((...args: any[]) => any) | null = null;
577
-
578
- anyTouchElement.on("pan", function (event) {
579
- if (!isMove) {
580
- isMove = true;
581
- const rect = options.dragElement.getBoundingClientRect();
582
- clickElementLeftOffset = event.x - rect.left;
583
- clickElementTopOffset = event.y - rect.top;
584
- transformInfo = popsDOMUtils.getTransform($move);
585
- //if (event.nativeEvent.offsetX) {
586
- // clickElementLeftOffset = parseInt(event.nativeEvent.offsetX);
587
- //} else {
588
- // clickElementLeftOffset = parseInt(event.getOffset().x);
589
- //}
590
- //if (event.nativeEvent.offsetY) {
591
- // clickElementTopOffset = parseInt(event.nativeEvent.offsetY);
592
- //} else {
593
- // clickElementTopOffset = parseInt(event.getOffset().y);
594
- //}
595
- resumeMoveElementStyle = changeMoveElementStyle($move);
596
- }
597
-
598
- /** 当前移动的left偏移 */
599
- let currentMoveLeftOffset = event.x - clickElementLeftOffset + transformInfo.transformLeft;
600
- /** 当前移动的top偏移 */
601
- let currentMoveTopOffset = event.y - clickElementTopOffset + transformInfo.transformTop;
602
- // 拖拽移动
603
- if (event.phase === "move") {
604
- if (options.limit) {
605
- // 限制在容器内移动
606
- // left偏移最大值
607
- const maxLeftOffset =
608
- getContainerWidthOrHeight(options.container!).width -
609
- popsDOMUtils.width($move) +
610
- transformInfo.transformLeft;
611
- const { left: minLeftOffset, top: minTopOffset } = getContainerTopOrLeft(options.container!);
612
- // top偏移的最大值
613
- const maxTopOffset =
614
- getContainerWidthOrHeight(options.container!).height -
615
- popsDOMUtils.height($move) +
616
- transformInfo.transformTop;
617
- if (currentMoveLeftOffset > maxLeftOffset) {
618
- // 不允许超过容器的最大宽度
619
- currentMoveLeftOffset = maxLeftOffset;
620
- }
621
- if (currentMoveTopOffset > maxTopOffset) {
622
- // 不允许超过容器的最大高度
623
- currentMoveTopOffset = maxTopOffset;
624
- }
625
- if (currentMoveLeftOffset - options.extraDistance * 2 < minLeftOffset + transformInfo.transformLeft) {
626
- // 不允许left偏移小于容器最小值
627
- currentMoveLeftOffset = minLeftOffset + transformInfo.transformLeft;
628
- // 最左边 +额外距离
629
- currentMoveLeftOffset += options.extraDistance;
630
- } else {
631
- // 最右边 -额外距离
632
- currentMoveLeftOffset -= options.extraDistance;
633
- }
634
- if (currentMoveTopOffset - options.extraDistance * 2 < minTopOffset + transformInfo.transformTop) {
635
- // 不允许top偏移小于容器最小值
636
- currentMoveTopOffset = minTopOffset + transformInfo.transformTop;
637
- // 最上面 +额外距离
638
- currentMoveTopOffset += options.extraDistance;
639
- } else {
640
- // 最下面 -额外距离
641
- currentMoveTopOffset -= options.extraDistance;
642
- }
643
- }
644
- if (typeof options.moveCallBack === "function") {
645
- options.moveCallBack($move, currentMoveLeftOffset, currentMoveTopOffset);
646
- }
647
-
648
- popsDOMUtils.css($move, {
649
- left: currentMoveLeftOffset + "px",
650
- top: currentMoveTopOffset + "px",
651
- });
652
- }
653
-
654
- // 停止拖拽
655
- if (event.phase === "end") {
656
- isMove = false;
657
- if (typeof resumeMoveElementStyle === "function") {
658
- resumeMoveElementStyle();
659
- resumeMoveElementStyle = null;
660
- }
661
- if (typeof options.endCallBack === "function") {
662
- options.endCallBack($move, currentMoveLeftOffset, currentMoveTopOffset);
663
- }
664
- }
665
- });
666
- if (options.emitClick) {
667
- // 因为会覆盖上面的点击事件,主动触发一下
668
- anyTouchElement.on(["tap"], function (event) {
669
- event.changedPoints.forEach((item) => {
670
- popsDOMUtils.emit(item.target! as HTMLElement, "click", void 0, true);
671
- });
672
- });
673
- }
674
- },
675
67
  /**
676
68
  * 排序数组
677
69
  * @param getBeforeValueFun