nhanh-pure-function 1.3.4 → 1.3.6

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.
package/lib/User/User.js CHANGED
@@ -1,484 +1,484 @@
1
- import { _IsObject, _NotNull, _Debounce } from "../Utility/Utility";
2
-
3
- /**
4
- * 滚动结束监听器
5
- * @param {(trigger: "vertical" | "horizontal") => void} callback
6
- */
7
- export function _ScrollEndListener(callback) {
8
- const debouncedCallback = _Debounce(callback, 100);
9
- let lastScrollTop = 0;
10
- let lastScrollLeft = 0;
11
- return function (payload) {
12
- const target = payload.target;
13
- if (!target) return;
14
-
15
- const {
16
- scrollTop,
17
- scrollHeight,
18
- clientHeight,
19
- scrollLeft,
20
- scrollWidth,
21
- clientWidth,
22
- } = target;
23
- function vertical() {
24
- if (lastScrollTop == scrollTop) return;
25
- /** 向上滚动? */
26
- const isUp = lastScrollTop > scrollTop;
27
- lastScrollTop = scrollTop;
28
- if (isUp) return;
29
- const bottom = scrollHeight - scrollTop - clientHeight;
30
- if (bottom <= 1) debouncedCallback("vertical");
31
- }
32
- function horizontal() {
33
- if (lastScrollLeft == scrollLeft) return;
34
- /** 向左滚动? */
35
- const isLeft = lastScrollLeft > scrollLeft;
36
- lastScrollLeft = scrollLeft;
37
- if (isLeft) return;
38
- const right = scrollWidth - scrollLeft - clientWidth;
39
- if (right <= 1) debouncedCallback("horizontal");
40
- }
41
-
42
- vertical();
43
- horizontal();
44
- };
45
- }
46
-
47
- /**
48
- * 设置量词属性
49
- * @param {*} data 需修改对象
50
- * @param {*} options 配置
51
- * @returns data
52
- */
53
- export function _SetQuantifierAttribute(data, options = []) {
54
- if (!_IsObject(data)) {
55
- console.error("异常输入:", data);
56
- return data;
57
- }
58
-
59
- options.forEach((item) => {
60
- if (typeof item === "string") {
61
- data[item] = _FormatNumberWithUnit(data[item]);
62
- } else if (Array.isArray(item)) {
63
- const [label, config] = data[item];
64
- if (_NotNull(label) && _IsObject(config))
65
- data[label] = _FormatNumberWithUnit(label, config);
66
- }
67
- });
68
- return data;
69
- }
70
-
71
- /**
72
- * 为属性值为null | undefined的属性设置默认值
73
- * @param data 需修改对象
74
- * @param options 配置
75
- * @returns
76
- */
77
- export function _SetDefaultValue(data, options = {}) {
78
- if (!_IsObject(data)) {
79
- console.error("异常输入:", data);
80
- return data;
81
- }
82
-
83
- const { defaultValue = "--", fieldsNotRequiringAction } = options;
84
-
85
- for (const key in data) {
86
- if (Object.prototype.hasOwnProperty.call(data, key)) {
87
- const element = data[key];
88
- if (fieldsNotRequiringAction) {
89
- if (!fieldsNotRequiringAction.includes(key) && !_NotNull(element)) {
90
- data[key] = defaultValue;
91
- }
92
- } else if (!_NotNull(element)) {
93
- data[key] = defaultValue;
94
- }
95
- }
96
- }
97
-
98
- return data;
99
- }
100
-
101
- /**
102
- * 将字典value转为对应label
103
- * @param data 需修改对象
104
- * @param options 配置
105
- * @returns
106
- */
107
- export function _SetDictionary(data, options = {}) {
108
- if (!_IsObject(data)) {
109
- console.error("异常输入:", data);
110
- return data;
111
- }
112
-
113
- const {
114
- dictionaryLabel = [],
115
- dictionaryLabelJoin = [],
116
- dictionaryOptions,
117
- defaultValue = "--",
118
- } = options;
119
-
120
- if (dictionaryOptions) {
121
- dictionaryLabel.forEach((label) => {
122
- if (_NotNull(data[label])) {
123
- const options = dictionaryOptions[label];
124
-
125
- if (options) {
126
- data[label] = options[data[label]];
127
- } else {
128
- data[label] = defaultValue;
129
- }
130
- } else {
131
- data[label] = defaultValue;
132
- }
133
- });
134
- dictionaryLabelJoin.forEach((label) => {
135
- if (_NotNull(data[label]) && data[label] != "") {
136
- const options = dictionaryOptions[label];
137
- if (options) {
138
- const oldvalue = data[label].split(",");
139
- data[label] = "";
140
- oldvalue.forEach((_label) => {
141
- data[label] += options[_label];
142
- });
143
- } else {
144
- data[label] = defaultValue;
145
- }
146
- } else {
147
- data[label] = defaultValue;
148
- }
149
- });
150
- }
151
-
152
- return data;
153
- }
154
-
155
- /**
156
- * 将字符串拼接的图片地址转为数组
157
- * @param data 需修改对象
158
- * @param options 配置
159
- * @returns
160
- */
161
- export function _SetPhoto(data, options = {}) {
162
- if (!_IsObject(data)) {
163
- console.error("异常输入:", data);
164
- return data;
165
- }
166
-
167
- const { label, defaultUrl } = options;
168
-
169
- if (label) {
170
- label.forEach((label) => {
171
- const defaultValue = (defaultUrl && defaultUrl[label]) || [];
172
- const value = data[label];
173
- if (typeof value === "string") {
174
- data[label] = value.split(",").filter(Boolean);
175
- } else {
176
- data[label] = defaultValue;
177
- }
178
- });
179
- }
180
-
181
- return data;
182
- }
183
-
184
- /**
185
- * 将接口返回的数据进行处理,得到展示数据
186
- * @param data object 类型的数据
187
- * @param options 配置
188
- * @returns exhibit_data
189
- */
190
- export function _Exhibit_details(data, options = {}) {
191
- if (!_IsObject(data)) {
192
- console.error("异常输入:", data);
193
- return {};
194
- }
195
-
196
- data = JSON.parse(JSON.stringify(data));
197
-
198
- const {
199
- dictionaryLabel = [],
200
- dictionaryLabelJoin = [],
201
- dictionaryOptions,
202
-
203
- photoLabel = [],
204
- photoDefaultUrl,
205
-
206
- quantifierLabel = [],
207
-
208
- filterLabel = [],
209
-
210
- defaultValue = "--",
211
- } = options;
212
-
213
- _SetDictionary(data, {
214
- dictionaryLabel,
215
- dictionaryLabelJoin,
216
- dictionaryOptions,
217
- defaultValue,
218
- });
219
-
220
- _SetPhoto(data, {
221
- label: photoLabel,
222
- defaultUrl: photoDefaultUrl,
223
- });
224
-
225
- _SetQuantifierAttribute(data, quantifierLabel);
226
-
227
- _SetDefaultValue(data, {
228
- defaultValue,
229
- fieldsNotRequiringAction: dictionaryLabel
230
- .concat(dictionaryLabelJoin)
231
- .concat(photoLabel)
232
- .concat(
233
- quantifierLabel
234
- .map((item) => {
235
- if (typeof item == "string") return item;
236
- if (Array.isArray(item)) return item[0];
237
- })
238
- .filter(Boolean)
239
- )
240
- .concat(filterLabel),
241
- });
242
-
243
- return data;
244
- }
245
-
246
- /**
247
- * 点击非指定dom(包含子级dom)时执行 callback
248
- * @param querySelector 允许点击的 dom 顶层祖先元素选择器
249
- * @param callback 满足条件时执行的回调
250
- *
251
- * @param options 其他配置
252
- * @param options.uiLibrary 项目使用的 ui库 , 用于排除 ui库 创建的元素 , 避免点击 ui库 创建的元素时意外的执行 callback
253
- * @param options.isClickAllowed 是否允许该点击 ( 如果不确定可以返回 undefined )
254
- */
255
- export function _CloseOnOutsideClick(querySelector, callback, options) {
256
- const { isClickAllowed, uiLibrary = ["naiveUI", "ElementPlus", "Element"] } =
257
- options || {};
258
-
259
- const UI = (function (obj) {
260
- const arr = [];
261
- for (const key in obj) {
262
- if (Object.hasOwnProperty.call(obj, key)) {
263
- if (uiLibrary.includes(key)) arr.push(...obj[key]);
264
- }
265
- }
266
- return arr;
267
- })({
268
- naiveUI: [
269
- ".v-binder-follower-container",
270
- ".n-image-preview-container",
271
- ".n-modal-container",
272
- ],
273
- ElementPlus: [".el-popper"],
274
- Element: [".el-popper"],
275
- });
276
-
277
- function end() {
278
- callback();
279
- document.removeEventListener("mousedown", mousedown);
280
- }
281
- function mousedown(event) {
282
- if (isClickAllowed) {
283
- const bool = isClickAllowed(event);
284
- if (bool) return;
285
- if (bool === false) return end();
286
- }
287
-
288
- const target = event.target;
289
-
290
- /** 元素这时可能已经被删除了 */
291
- if (!target?.closest("body")) return;
292
-
293
- const isClickable = querySelector
294
- .concat(UI)
295
- .some((className) => Boolean(target?.closest(className)));
296
-
297
- if (!isClickable) end();
298
- }
299
- requestAnimationFrame(() =>
300
- document.addEventListener("mousedown", mousedown)
301
- );
302
- }
303
-
304
- /** 拖拽dom */
305
- export class _Drag {
306
- #dom = null;
307
- #isAllowed = false;
308
- #eventFunction = {};
309
- #pageX = 0;
310
- #pageY = 0;
311
- #top = 0;
312
- #left = 0;
313
- #limit;
314
- #dragDom;
315
-
316
- init(dom, option) {
317
- this.#dom = dom;
318
- this.#limit = option?.limit;
319
- this.#dragDom = option?.dragDom;
320
- this.#eventFunction = {
321
- mousedown: this.mousedown.bind(this),
322
- mousemove: this.mousemove.bind(this),
323
- mouseup: this.mouseup.bind(this),
324
- };
325
-
326
- this.bindOrUnbindEvent("bind");
327
- }
328
- finish() {
329
- this.bindOrUnbindEvent("unbind");
330
- }
331
- bindOrUnbindEvent(type) {
332
- const EventType =
333
- type === "bind" ? "addEventListener" : "removeEventListener";
334
- if (!this.#dom) return console.error("No DOM");
335
-
336
- this.#dom[EventType]("mousedown", this.#eventFunction.mousedown);
337
- document[EventType]("mousemove", this.#eventFunction.mousemove);
338
- document[EventType]("mouseup", this.#eventFunction.mouseup);
339
- }
340
- alterLocation() {
341
- if (!this.#dom) return console.error("No DOM");
342
- if (this.#limit) {
343
- this.#top = Math.min(this.#top, this.#limit.max.top);
344
- this.#top = Math.max(this.#top, this.#limit.min.top);
345
- this.#left = Math.min(this.#left, this.#limit.max.left);
346
- this.#left = Math.max(this.#left, this.#limit.min.left);
347
- }
348
- this.#dom.style.setProperty("--top", this.#top + "px");
349
- this.#dom.style.setProperty("--left", this.#left + "px");
350
- }
351
- mousedown(event) {
352
- if (!this.#dom) return console.error("No DOM");
353
- if (this.#dragDom && event.target != this.#dragDom) return;
354
- document.body.classList.add("no-select");
355
-
356
- this.#isAllowed = true;
357
- const clientRect = this.#dom.getBoundingClientRect();
358
-
359
- const { pageX, pageY } = event;
360
- this.#pageX = pageX;
361
- this.#pageY = pageY;
362
- this.#top = clientRect.y;
363
- this.#left = clientRect.x;
364
- }
365
- mousemove(event) {
366
- const { pageX, pageY } = event;
367
- if (this.#isAllowed) {
368
- this.#top += pageY - this.#pageY;
369
- this.#left += pageX - this.#pageX;
370
- this.#pageX = pageX;
371
- this.#pageY = pageY;
372
-
373
- this.alterLocation();
374
- }
375
- }
376
- mouseup() {
377
- if (this.#isAllowed) {
378
- this.#isAllowed = false;
379
- document.body.classList.remove("no-select");
380
- }
381
- }
382
- }
383
-
384
- /** 局部拖拽 计算位置距离/百分比 */
385
- export class _LocalDrag {
386
- #parentDom = null;
387
- #isAllowed = false;
388
- #eventFunction = {};
389
- #clientRectX = 0;
390
- #clientRectY = 0;
391
- #top = 0;
392
- #left = 0;
393
- #limit;
394
- #update_move;
395
- #update_up;
396
-
397
- init(parentDom, options = {}) {
398
- this.#parentDom = parentDom;
399
- this.#limit = options.limit;
400
- this.#update_move = options.update_move;
401
- this.#update_up = options.update_up;
402
- this.#eventFunction = {
403
- mousedown: this.mousedown.bind(this),
404
- mousemove: this.mousemove.bind(this),
405
- mouseup: this.mouseup.bind(this),
406
- };
407
-
408
- this.bindOrUnbindEvent("bind");
409
- }
410
- finish() {
411
- this.bindOrUnbindEvent("unbind");
412
- }
413
- bindOrUnbindEvent(type) {
414
- const EventType =
415
- type === "bind" ? "addEventListener" : "removeEventListener";
416
- if (!this.#parentDom) return window.customize_error("No DOM");
417
-
418
- this.#parentDom[EventType]("mousedown", this.#eventFunction.mousedown);
419
- document[EventType]("mousemove", this.#eventFunction.mousemove);
420
- document[EventType]("mouseup", this.#eventFunction.mouseup);
421
- }
422
- updateValue() {
423
- const value = {
424
- top: this.#top,
425
- left: this.#left,
426
- };
427
- if (this.#limit) {
428
- const v = (type) =>
429
- this.#limit
430
- ? (value[type] - this.#limit.min[type]) /
431
- (this.#limit.max[type] - this.#limit.min[type])
432
- : 0;
433
-
434
- value.percentage = {
435
- top: v("top") || 0,
436
- left: v("left") || 0,
437
- };
438
- }
439
- return value;
440
- }
441
- alterLocation() {
442
- if (!this.#parentDom) return window.customize_error("No DOM");
443
- if (this.#limit) {
444
- this.#top = Math.min(this.#top, this.#limit.max.top);
445
- this.#top = Math.max(this.#top, this.#limit.min.top);
446
- this.#left = Math.min(this.#left, this.#limit.max.left);
447
- this.#left = Math.max(this.#left, this.#limit.min.left);
448
- }
449
- if (this.#update_move) this.#update_move(this.updateValue());
450
-
451
- this.#parentDom.style.setProperty("--top", this.#top + "px");
452
- this.#parentDom.style.setProperty("--left", this.#left + "px");
453
- }
454
- mousedown(event) {
455
- if (!this.#parentDom) return window.customize_error("No DOM");
456
- document.body.classList.add("no-select");
457
-
458
- this.#isAllowed = true;
459
- const clientRect = this.#parentDom.getBoundingClientRect();
460
- this.#clientRectY = clientRect.y;
461
- this.#clientRectX = clientRect.x;
462
-
463
- const { pageX, pageY } = event;
464
- this.#top = pageY - this.#clientRectY;
465
- this.#left = pageX - this.#clientRectX;
466
-
467
- this.alterLocation();
468
- }
469
- mousemove(event) {
470
- const { pageX, pageY } = event;
471
- if (this.#isAllowed) {
472
- this.#top = pageY - this.#clientRectY;
473
- this.#left = pageX - this.#clientRectX;
474
- this.alterLocation();
475
- }
476
- }
477
- mouseup() {
478
- if (this.#isAllowed) {
479
- this.#isAllowed = false;
480
- document.body.classList.remove("no-select");
481
- if (this.#update_up) this.#update_up(this.updateValue());
482
- }
483
- }
484
- }
1
+ import { _IsObject, _NotNull, _Debounce } from "../Utility/Utility";
2
+
3
+ /**
4
+ * 滚动结束监听器
5
+ * @param {(trigger: "vertical" | "horizontal") => void} callback
6
+ */
7
+ export function _ScrollEndListener(callback) {
8
+ const debouncedCallback = _Debounce(callback, 100);
9
+ let lastScrollTop = 0;
10
+ let lastScrollLeft = 0;
11
+ return function (payload) {
12
+ const target = payload.target;
13
+ if (!target) return;
14
+
15
+ const {
16
+ scrollTop,
17
+ scrollHeight,
18
+ clientHeight,
19
+ scrollLeft,
20
+ scrollWidth,
21
+ clientWidth,
22
+ } = target;
23
+ function vertical() {
24
+ if (lastScrollTop == scrollTop) return;
25
+ /** 向上滚动? */
26
+ const isUp = lastScrollTop > scrollTop;
27
+ lastScrollTop = scrollTop;
28
+ if (isUp) return;
29
+ const bottom = scrollHeight - scrollTop - clientHeight;
30
+ if (bottom <= 1) debouncedCallback("vertical");
31
+ }
32
+ function horizontal() {
33
+ if (lastScrollLeft == scrollLeft) return;
34
+ /** 向左滚动? */
35
+ const isLeft = lastScrollLeft > scrollLeft;
36
+ lastScrollLeft = scrollLeft;
37
+ if (isLeft) return;
38
+ const right = scrollWidth - scrollLeft - clientWidth;
39
+ if (right <= 1) debouncedCallback("horizontal");
40
+ }
41
+
42
+ vertical();
43
+ horizontal();
44
+ };
45
+ }
46
+
47
+ /**
48
+ * 设置量词属性
49
+ * @param {*} data 需修改对象
50
+ * @param {*} options 配置
51
+ * @returns data
52
+ */
53
+ export function _SetQuantifierAttribute(data, options = []) {
54
+ if (!_IsObject(data)) {
55
+ console.error("异常输入:", data);
56
+ return data;
57
+ }
58
+
59
+ options.forEach((item) => {
60
+ if (typeof item === "string") {
61
+ data[item] = _FormatNumberWithUnit(data[item]);
62
+ } else if (Array.isArray(item)) {
63
+ const [label, config] = data[item];
64
+ if (_NotNull(label) && _IsObject(config))
65
+ data[label] = _FormatNumberWithUnit(label, config);
66
+ }
67
+ });
68
+ return data;
69
+ }
70
+
71
+ /**
72
+ * 为属性值为null | undefined的属性设置默认值
73
+ * @param data 需修改对象
74
+ * @param options 配置
75
+ * @returns
76
+ */
77
+ export function _SetDefaultValue(data, options = {}) {
78
+ if (!_IsObject(data)) {
79
+ console.error("异常输入:", data);
80
+ return data;
81
+ }
82
+
83
+ const { defaultValue = "--", fieldsNotRequiringAction } = options;
84
+
85
+ for (const key in data) {
86
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
87
+ const element = data[key];
88
+ if (fieldsNotRequiringAction) {
89
+ if (!fieldsNotRequiringAction.includes(key) && !_NotNull(element)) {
90
+ data[key] = defaultValue;
91
+ }
92
+ } else if (!_NotNull(element)) {
93
+ data[key] = defaultValue;
94
+ }
95
+ }
96
+ }
97
+
98
+ return data;
99
+ }
100
+
101
+ /**
102
+ * 将字典value转为对应label
103
+ * @param data 需修改对象
104
+ * @param options 配置
105
+ * @returns
106
+ */
107
+ export function _SetDictionary(data, options = {}) {
108
+ if (!_IsObject(data)) {
109
+ console.error("异常输入:", data);
110
+ return data;
111
+ }
112
+
113
+ const {
114
+ dictionaryLabel = [],
115
+ dictionaryLabelJoin = [],
116
+ dictionaryOptions,
117
+ defaultValue = "--",
118
+ } = options;
119
+
120
+ if (dictionaryOptions) {
121
+ dictionaryLabel.forEach((label) => {
122
+ if (_NotNull(data[label])) {
123
+ const options = dictionaryOptions[label];
124
+
125
+ if (options) {
126
+ data[label] = options[data[label]];
127
+ } else {
128
+ data[label] = defaultValue;
129
+ }
130
+ } else {
131
+ data[label] = defaultValue;
132
+ }
133
+ });
134
+ dictionaryLabelJoin.forEach((label) => {
135
+ if (_NotNull(data[label]) && data[label] != "") {
136
+ const options = dictionaryOptions[label];
137
+ if (options) {
138
+ const oldvalue = data[label].split(",");
139
+ data[label] = "";
140
+ oldvalue.forEach((_label) => {
141
+ data[label] += options[_label];
142
+ });
143
+ } else {
144
+ data[label] = defaultValue;
145
+ }
146
+ } else {
147
+ data[label] = defaultValue;
148
+ }
149
+ });
150
+ }
151
+
152
+ return data;
153
+ }
154
+
155
+ /**
156
+ * 将字符串拼接的图片地址转为数组
157
+ * @param data 需修改对象
158
+ * @param options 配置
159
+ * @returns
160
+ */
161
+ export function _SetPhoto(data, options = {}) {
162
+ if (!_IsObject(data)) {
163
+ console.error("异常输入:", data);
164
+ return data;
165
+ }
166
+
167
+ const { label, defaultUrl } = options;
168
+
169
+ if (label) {
170
+ label.forEach((label) => {
171
+ const defaultValue = (defaultUrl && defaultUrl[label]) || [];
172
+ const value = data[label];
173
+ if (typeof value === "string") {
174
+ data[label] = value.split(",").filter(Boolean);
175
+ } else {
176
+ data[label] = defaultValue;
177
+ }
178
+ });
179
+ }
180
+
181
+ return data;
182
+ }
183
+
184
+ /**
185
+ * 将接口返回的数据进行处理,得到展示数据
186
+ * @param data object 类型的数据
187
+ * @param options 配置
188
+ * @returns exhibit_data
189
+ */
190
+ export function _Exhibit_details(data, options = {}) {
191
+ if (!_IsObject(data)) {
192
+ console.error("异常输入:", data);
193
+ return {};
194
+ }
195
+
196
+ data = JSON.parse(JSON.stringify(data));
197
+
198
+ const {
199
+ dictionaryLabel = [],
200
+ dictionaryLabelJoin = [],
201
+ dictionaryOptions,
202
+
203
+ photoLabel = [],
204
+ photoDefaultUrl,
205
+
206
+ quantifierLabel = [],
207
+
208
+ filterLabel = [],
209
+
210
+ defaultValue = "--",
211
+ } = options;
212
+
213
+ _SetDictionary(data, {
214
+ dictionaryLabel,
215
+ dictionaryLabelJoin,
216
+ dictionaryOptions,
217
+ defaultValue,
218
+ });
219
+
220
+ _SetPhoto(data, {
221
+ label: photoLabel,
222
+ defaultUrl: photoDefaultUrl,
223
+ });
224
+
225
+ _SetQuantifierAttribute(data, quantifierLabel);
226
+
227
+ _SetDefaultValue(data, {
228
+ defaultValue,
229
+ fieldsNotRequiringAction: dictionaryLabel
230
+ .concat(dictionaryLabelJoin)
231
+ .concat(photoLabel)
232
+ .concat(
233
+ quantifierLabel
234
+ .map((item) => {
235
+ if (typeof item == "string") return item;
236
+ if (Array.isArray(item)) return item[0];
237
+ })
238
+ .filter(Boolean)
239
+ )
240
+ .concat(filterLabel),
241
+ });
242
+
243
+ return data;
244
+ }
245
+
246
+ /**
247
+ * 点击非指定dom(包含子级dom)时执行 callback
248
+ * @param querySelector 允许点击的 dom 顶层祖先元素选择器
249
+ * @param callback 满足条件时执行的回调
250
+ *
251
+ * @param options 其他配置
252
+ * @param options.uiLibrary 项目使用的 ui库 , 用于排除 ui库 创建的元素 , 避免点击 ui库 创建的元素时意外的执行 callback
253
+ * @param options.isClickAllowed 是否允许该点击 ( 如果不确定可以返回 undefined )
254
+ */
255
+ export function _CloseOnOutsideClick(querySelector, callback, options) {
256
+ const { isClickAllowed, uiLibrary = ["naiveUI", "ElementPlus", "Element"] } =
257
+ options || {};
258
+
259
+ const UI = (function (obj) {
260
+ const arr = [];
261
+ for (const key in obj) {
262
+ if (Object.hasOwnProperty.call(obj, key)) {
263
+ if (uiLibrary.includes(key)) arr.push(...obj[key]);
264
+ }
265
+ }
266
+ return arr;
267
+ })({
268
+ naiveUI: [
269
+ ".v-binder-follower-container",
270
+ ".n-image-preview-container",
271
+ ".n-modal-container",
272
+ ],
273
+ ElementPlus: [".el-popper"],
274
+ Element: [".el-popper"],
275
+ });
276
+
277
+ function end() {
278
+ callback();
279
+ document.removeEventListener("mousedown", mousedown);
280
+ }
281
+ function mousedown(event) {
282
+ if (isClickAllowed) {
283
+ const bool = isClickAllowed(event);
284
+ if (bool) return;
285
+ if (bool === false) return end();
286
+ }
287
+
288
+ const target = event.target;
289
+
290
+ /** 元素这时可能已经被删除了 */
291
+ if (!target?.closest("body")) return;
292
+
293
+ const isClickable = querySelector
294
+ .concat(UI)
295
+ .some((className) => Boolean(target?.closest(className)));
296
+
297
+ if (!isClickable) end();
298
+ }
299
+ requestAnimationFrame(() =>
300
+ document.addEventListener("mousedown", mousedown)
301
+ );
302
+ }
303
+
304
+ /** 拖拽dom */
305
+ export class _Drag {
306
+ #dom = null;
307
+ #isAllowed = false;
308
+ #eventFunction = {};
309
+ #pageX = 0;
310
+ #pageY = 0;
311
+ #top = 0;
312
+ #left = 0;
313
+ #limit;
314
+ #dragDom;
315
+
316
+ init(dom, option) {
317
+ this.#dom = dom;
318
+ this.#limit = option?.limit;
319
+ this.#dragDom = option?.dragDom;
320
+ this.#eventFunction = {
321
+ mousedown: this.mousedown.bind(this),
322
+ mousemove: this.mousemove.bind(this),
323
+ mouseup: this.mouseup.bind(this),
324
+ };
325
+
326
+ this.bindOrUnbindEvent("bind");
327
+ }
328
+ finish() {
329
+ this.bindOrUnbindEvent("unbind");
330
+ }
331
+ bindOrUnbindEvent(type) {
332
+ const EventType =
333
+ type === "bind" ? "addEventListener" : "removeEventListener";
334
+ if (!this.#dom) return console.error("No DOM");
335
+
336
+ this.#dom[EventType]("mousedown", this.#eventFunction.mousedown);
337
+ document[EventType]("mousemove", this.#eventFunction.mousemove);
338
+ document[EventType]("mouseup", this.#eventFunction.mouseup);
339
+ }
340
+ alterLocation() {
341
+ if (!this.#dom) return console.error("No DOM");
342
+ if (this.#limit) {
343
+ this.#top = Math.min(this.#top, this.#limit.max.top);
344
+ this.#top = Math.max(this.#top, this.#limit.min.top);
345
+ this.#left = Math.min(this.#left, this.#limit.max.left);
346
+ this.#left = Math.max(this.#left, this.#limit.min.left);
347
+ }
348
+ this.#dom.style.setProperty("--top", this.#top + "px");
349
+ this.#dom.style.setProperty("--left", this.#left + "px");
350
+ }
351
+ mousedown(event) {
352
+ if (!this.#dom) return console.error("No DOM");
353
+ if (this.#dragDom && event.target != this.#dragDom) return;
354
+ document.body.classList.add("no-select");
355
+
356
+ this.#isAllowed = true;
357
+ const clientRect = this.#dom.getBoundingClientRect();
358
+
359
+ const { pageX, pageY } = event;
360
+ this.#pageX = pageX;
361
+ this.#pageY = pageY;
362
+ this.#top = clientRect.y;
363
+ this.#left = clientRect.x;
364
+ }
365
+ mousemove(event) {
366
+ const { pageX, pageY } = event;
367
+ if (this.#isAllowed) {
368
+ this.#top += pageY - this.#pageY;
369
+ this.#left += pageX - this.#pageX;
370
+ this.#pageX = pageX;
371
+ this.#pageY = pageY;
372
+
373
+ this.alterLocation();
374
+ }
375
+ }
376
+ mouseup() {
377
+ if (this.#isAllowed) {
378
+ this.#isAllowed = false;
379
+ document.body.classList.remove("no-select");
380
+ }
381
+ }
382
+ }
383
+
384
+ /** 局部拖拽 计算位置距离/百分比 */
385
+ export class _LocalDrag {
386
+ #parentDom = null;
387
+ #isAllowed = false;
388
+ #eventFunction = {};
389
+ #clientRectX = 0;
390
+ #clientRectY = 0;
391
+ #top = 0;
392
+ #left = 0;
393
+ #limit;
394
+ #update_move;
395
+ #update_up;
396
+
397
+ init(parentDom, options = {}) {
398
+ this.#parentDom = parentDom;
399
+ this.#limit = options.limit;
400
+ this.#update_move = options.update_move;
401
+ this.#update_up = options.update_up;
402
+ this.#eventFunction = {
403
+ mousedown: this.mousedown.bind(this),
404
+ mousemove: this.mousemove.bind(this),
405
+ mouseup: this.mouseup.bind(this),
406
+ };
407
+
408
+ this.bindOrUnbindEvent("bind");
409
+ }
410
+ finish() {
411
+ this.bindOrUnbindEvent("unbind");
412
+ }
413
+ bindOrUnbindEvent(type) {
414
+ const EventType =
415
+ type === "bind" ? "addEventListener" : "removeEventListener";
416
+ if (!this.#parentDom) return window.customize_error("No DOM");
417
+
418
+ this.#parentDom[EventType]("mousedown", this.#eventFunction.mousedown);
419
+ document[EventType]("mousemove", this.#eventFunction.mousemove);
420
+ document[EventType]("mouseup", this.#eventFunction.mouseup);
421
+ }
422
+ updateValue() {
423
+ const value = {
424
+ top: this.#top,
425
+ left: this.#left,
426
+ };
427
+ if (this.#limit) {
428
+ const v = (type) =>
429
+ this.#limit
430
+ ? (value[type] - this.#limit.min[type]) /
431
+ (this.#limit.max[type] - this.#limit.min[type])
432
+ : 0;
433
+
434
+ value.percentage = {
435
+ top: v("top") || 0,
436
+ left: v("left") || 0,
437
+ };
438
+ }
439
+ return value;
440
+ }
441
+ alterLocation() {
442
+ if (!this.#parentDom) return window.customize_error("No DOM");
443
+ if (this.#limit) {
444
+ this.#top = Math.min(this.#top, this.#limit.max.top);
445
+ this.#top = Math.max(this.#top, this.#limit.min.top);
446
+ this.#left = Math.min(this.#left, this.#limit.max.left);
447
+ this.#left = Math.max(this.#left, this.#limit.min.left);
448
+ }
449
+ if (this.#update_move) this.#update_move(this.updateValue());
450
+
451
+ this.#parentDom.style.setProperty("--top", this.#top + "px");
452
+ this.#parentDom.style.setProperty("--left", this.#left + "px");
453
+ }
454
+ mousedown(event) {
455
+ if (!this.#parentDom) return window.customize_error("No DOM");
456
+ document.body.classList.add("no-select");
457
+
458
+ this.#isAllowed = true;
459
+ const clientRect = this.#parentDom.getBoundingClientRect();
460
+ this.#clientRectY = clientRect.y;
461
+ this.#clientRectX = clientRect.x;
462
+
463
+ const { pageX, pageY } = event;
464
+ this.#top = pageY - this.#clientRectY;
465
+ this.#left = pageX - this.#clientRectX;
466
+
467
+ this.alterLocation();
468
+ }
469
+ mousemove(event) {
470
+ const { pageX, pageY } = event;
471
+ if (this.#isAllowed) {
472
+ this.#top = pageY - this.#clientRectY;
473
+ this.#left = pageX - this.#clientRectX;
474
+ this.alterLocation();
475
+ }
476
+ }
477
+ mouseup() {
478
+ if (this.#isAllowed) {
479
+ this.#isAllowed = false;
480
+ document.body.classList.remove("no-select");
481
+ if (this.#update_up) this.#update_up(this.updateValue());
482
+ }
483
+ }
484
+ }