@kaitify/core 0.0.1-beta.6 → 0.0.1-beta.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.
- package/lib/kaitify-core.es.js +562 -773
- package/lib/kaitify-core.umd.js +1 -1
- package/package.json +3 -3
package/lib/kaitify-core.es.js
CHANGED
|
@@ -1,6 +1,405 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
const common = {
|
|
5
|
+
/**
|
|
6
|
+
* 常用判断
|
|
7
|
+
* @param {Object} text 要判断的字符串
|
|
8
|
+
* @param {Object} param 判断的类型字符串
|
|
9
|
+
*/
|
|
10
|
+
matchingText(text2, param) {
|
|
11
|
+
if (param == "Chinese") {
|
|
12
|
+
return /^[\u4e00-\u9fa5]+$/.test(text2);
|
|
13
|
+
}
|
|
14
|
+
if (param == "chinese") {
|
|
15
|
+
return /[\u4e00-\u9fa5]/.test(text2);
|
|
16
|
+
}
|
|
17
|
+
if (param == "email") {
|
|
18
|
+
return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(text2);
|
|
19
|
+
}
|
|
20
|
+
if (param == "username") {
|
|
21
|
+
return /^[a-zA-Z0-9_]{4,16}$/.test(text2);
|
|
22
|
+
}
|
|
23
|
+
if (param == "int+") {
|
|
24
|
+
return /^\d+$/.test(text2);
|
|
25
|
+
}
|
|
26
|
+
if (param == "int-") {
|
|
27
|
+
return /^-\d+$/.test(text2);
|
|
28
|
+
}
|
|
29
|
+
if (param == "int") {
|
|
30
|
+
return /^-?\d+$/.test(text2);
|
|
31
|
+
}
|
|
32
|
+
if (param == "pos") {
|
|
33
|
+
return /^\d*\.?\d+$/.test(text2);
|
|
34
|
+
}
|
|
35
|
+
if (param == "neg") {
|
|
36
|
+
return /^-\d*\.?\d+$/.test(text2);
|
|
37
|
+
}
|
|
38
|
+
if (param == "number") {
|
|
39
|
+
return /^-?\d*\.?\d+$/.test(text2);
|
|
40
|
+
}
|
|
41
|
+
if (param == "phone") {
|
|
42
|
+
return /^1[0-9]\d{9}$/.test(text2);
|
|
43
|
+
}
|
|
44
|
+
if (param == "idCard") {
|
|
45
|
+
return /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(text2);
|
|
46
|
+
}
|
|
47
|
+
if (param == "url") {
|
|
48
|
+
return /^(https?|ftp):\/\/(-\.)?([^\s\/?\.#-]+\.?)+(\/[^\s]*)?$/.test(text2);
|
|
49
|
+
}
|
|
50
|
+
if (param == "IPv4") {
|
|
51
|
+
return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(text2);
|
|
52
|
+
}
|
|
53
|
+
if (param == "hex") {
|
|
54
|
+
return /^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(text2);
|
|
55
|
+
}
|
|
56
|
+
if (param == "rgb") {
|
|
57
|
+
return /^rgb\((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\)$/.test(text2);
|
|
58
|
+
}
|
|
59
|
+
if (param == "rgba") {
|
|
60
|
+
return /^rgba\((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(0?\.\d|1(\.0)?|0)\)$/.test(text2);
|
|
61
|
+
}
|
|
62
|
+
if (param == "QQ") {
|
|
63
|
+
return /^[1-9][0-9]{4,10}$/.test(text2);
|
|
64
|
+
}
|
|
65
|
+
if (param == "weixin") {
|
|
66
|
+
return /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(text2);
|
|
67
|
+
}
|
|
68
|
+
if (param == "plate") {
|
|
69
|
+
return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/.test(text2);
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
},
|
|
73
|
+
/**
|
|
74
|
+
* 根据参数名获取地址栏参数值
|
|
75
|
+
* @param {Object} name
|
|
76
|
+
*/
|
|
77
|
+
getUrlParams(name) {
|
|
78
|
+
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
|
|
79
|
+
let search = window.location.search.substring(1);
|
|
80
|
+
if (!search) {
|
|
81
|
+
const arr = window.location.hash.split("?");
|
|
82
|
+
if (arr.length == 2) {
|
|
83
|
+
search = arr[1];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
let r2 = search.match(reg);
|
|
87
|
+
if (r2) {
|
|
88
|
+
return decodeURIComponent(r2[2]);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
/**
|
|
92
|
+
* 判断是否空对象
|
|
93
|
+
* @param {Object} obj
|
|
94
|
+
*/
|
|
95
|
+
isEmptyObject(obj) {
|
|
96
|
+
return this.isObject(obj) && Object.keys(obj).length == 0;
|
|
97
|
+
},
|
|
98
|
+
/**
|
|
99
|
+
* 判断两个参数是否相等
|
|
100
|
+
* @param {Object} a
|
|
101
|
+
* @param {Object} b
|
|
102
|
+
*/
|
|
103
|
+
equal(a, b) {
|
|
104
|
+
if (typeof a !== typeof b) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (this.isObject(a) && this.isObject(b)) {
|
|
108
|
+
const aProps = Object.getOwnPropertyNames(a);
|
|
109
|
+
const bProps = Object.getOwnPropertyNames(b);
|
|
110
|
+
if (aProps.length != bProps.length) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const length = aProps.length;
|
|
114
|
+
let isEqual = true;
|
|
115
|
+
for (let i = 0; i < length; i++) {
|
|
116
|
+
const propName = aProps[i];
|
|
117
|
+
const propA = a[propName];
|
|
118
|
+
const propB = b[propName];
|
|
119
|
+
if (!this.equal(propA, propB)) {
|
|
120
|
+
isEqual = false;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return isEqual;
|
|
125
|
+
}
|
|
126
|
+
return a === b;
|
|
127
|
+
},
|
|
128
|
+
/**
|
|
129
|
+
* 是否对象
|
|
130
|
+
* @param {Object} val
|
|
131
|
+
*/
|
|
132
|
+
isObject(val) {
|
|
133
|
+
return typeof val === "object" && !!val;
|
|
134
|
+
},
|
|
135
|
+
/**
|
|
136
|
+
* 文本复制
|
|
137
|
+
* @param {Object} text
|
|
138
|
+
*/
|
|
139
|
+
copyText(text2) {
|
|
140
|
+
if (!navigator.clipboard) {
|
|
141
|
+
throw new Error("navigator.clipboard must be obtained in a secure environment, such as localhost, 127.0.0.1, or https, so the method won't work");
|
|
142
|
+
}
|
|
143
|
+
return navigator.clipboard.writeText(text2);
|
|
144
|
+
},
|
|
145
|
+
/**
|
|
146
|
+
* 深度克隆
|
|
147
|
+
* @param {Object} data
|
|
148
|
+
*/
|
|
149
|
+
clone(data2) {
|
|
150
|
+
if (this.isObject(data2)) {
|
|
151
|
+
if (Array.isArray(data2)) {
|
|
152
|
+
return data2.map((item) => {
|
|
153
|
+
return this.clone(item);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
const newData = {};
|
|
157
|
+
for (let key in data2) {
|
|
158
|
+
newData[key] = this.clone(data2[key]);
|
|
159
|
+
}
|
|
160
|
+
return newData;
|
|
161
|
+
}
|
|
162
|
+
return data2;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const color = {
|
|
166
|
+
/**
|
|
167
|
+
* rgb转hsv值
|
|
168
|
+
* @param {Object} rgb rgb值,数组
|
|
169
|
+
*/
|
|
170
|
+
rgb2hsv(rgb) {
|
|
171
|
+
if (rgb.length != 3) {
|
|
172
|
+
throw new TypeError("Invalid argument");
|
|
173
|
+
}
|
|
174
|
+
let h = 0;
|
|
175
|
+
let s = 0;
|
|
176
|
+
let v = 0;
|
|
177
|
+
let r2 = rgb[0] >= 255 ? 255 : rgb[0];
|
|
178
|
+
let g = rgb[1] >= 255 ? 255 : rgb[1];
|
|
179
|
+
let b = rgb[2] >= 255 ? 255 : rgb[2];
|
|
180
|
+
r2 = r2 <= 0 ? 0 : r2;
|
|
181
|
+
g = g <= 0 ? 0 : g;
|
|
182
|
+
b = b <= 0 ? 0 : b;
|
|
183
|
+
let max = Math.max(r2, g, b);
|
|
184
|
+
let min = Math.min(r2, g, b);
|
|
185
|
+
v = max / 255;
|
|
186
|
+
if (max === 0) {
|
|
187
|
+
s = 0;
|
|
188
|
+
} else {
|
|
189
|
+
s = 1 - min / max;
|
|
190
|
+
}
|
|
191
|
+
if (max === min) {
|
|
192
|
+
h = 0;
|
|
193
|
+
} else if (max === r2 && g >= b) {
|
|
194
|
+
h = 60 * ((g - b) / (max - min)) + 0;
|
|
195
|
+
} else if (max === r2 && g < b) {
|
|
196
|
+
h = 60 * ((g - b) / (max - min)) + 360;
|
|
197
|
+
} else if (max === g) {
|
|
198
|
+
h = 60 * ((b - r2) / (max - min)) + 120;
|
|
199
|
+
} else if (max === b) {
|
|
200
|
+
h = 60 * ((r2 - g) / (max - min)) + 240;
|
|
201
|
+
}
|
|
202
|
+
return [h, s * 100, v * 100];
|
|
203
|
+
},
|
|
204
|
+
/**
|
|
205
|
+
* hsv格式值转rgb值
|
|
206
|
+
* @param {Object} hsv hsv值,数组
|
|
207
|
+
*/
|
|
208
|
+
hsv2rgb(hsv) {
|
|
209
|
+
if (hsv.length != 3) {
|
|
210
|
+
throw new TypeError("Invalid argument");
|
|
211
|
+
}
|
|
212
|
+
let h = hsv[0] >= 360 || hsv[0] <= 0 ? 0 : hsv[0];
|
|
213
|
+
let s = hsv[1] >= 100 ? 100 : hsv[1];
|
|
214
|
+
s = s <= 0 ? 0 : s;
|
|
215
|
+
let v = hsv[2] >= 100 ? 100 : hsv[2];
|
|
216
|
+
v = v <= 0 ? 0 : v;
|
|
217
|
+
s = s / 100;
|
|
218
|
+
v = v / 100;
|
|
219
|
+
let r2 = 0;
|
|
220
|
+
let g = 0;
|
|
221
|
+
let b = 0;
|
|
222
|
+
let i = parseInt(h / 60 % 6 + "");
|
|
223
|
+
let f = h / 60 - i;
|
|
224
|
+
let p = v * (1 - s);
|
|
225
|
+
let q = v * (1 - f * s);
|
|
226
|
+
let t = v * (1 - (1 - f) * s);
|
|
227
|
+
switch (i) {
|
|
228
|
+
case 0:
|
|
229
|
+
r2 = v;
|
|
230
|
+
g = t;
|
|
231
|
+
b = p;
|
|
232
|
+
break;
|
|
233
|
+
case 1:
|
|
234
|
+
r2 = q;
|
|
235
|
+
g = v;
|
|
236
|
+
b = p;
|
|
237
|
+
break;
|
|
238
|
+
case 2:
|
|
239
|
+
r2 = p;
|
|
240
|
+
g = v;
|
|
241
|
+
b = t;
|
|
242
|
+
break;
|
|
243
|
+
case 3:
|
|
244
|
+
r2 = p;
|
|
245
|
+
g = q;
|
|
246
|
+
b = v;
|
|
247
|
+
break;
|
|
248
|
+
case 4:
|
|
249
|
+
r2 = t;
|
|
250
|
+
g = p;
|
|
251
|
+
b = v;
|
|
252
|
+
break;
|
|
253
|
+
case 5:
|
|
254
|
+
r2 = v;
|
|
255
|
+
g = p;
|
|
256
|
+
b = q;
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
r2 = parseInt(r2 * 255 + "");
|
|
260
|
+
g = parseInt(g * 255 + "");
|
|
261
|
+
b = parseInt(b * 255 + "");
|
|
262
|
+
return [r2, g, b];
|
|
263
|
+
},
|
|
264
|
+
/**
|
|
265
|
+
* rgb值转十六进制
|
|
266
|
+
* @param {Array} rgb rgb值,数组
|
|
267
|
+
*/
|
|
268
|
+
rgb2hex(rgb) {
|
|
269
|
+
if (rgb.length != 3) {
|
|
270
|
+
throw new TypeError("Invalid argument");
|
|
271
|
+
}
|
|
272
|
+
let r2 = rgb[0];
|
|
273
|
+
let g = rgb[1];
|
|
274
|
+
let b = rgb[2];
|
|
275
|
+
let hex = "#" + ((1 << 24) + (r2 << 16) + (g << 8) + b).toString(16).slice(1);
|
|
276
|
+
return hex;
|
|
277
|
+
},
|
|
278
|
+
/**
|
|
279
|
+
* 十六进制颜色转rgb
|
|
280
|
+
* @param {String} hex 十六进制颜色值
|
|
281
|
+
*/
|
|
282
|
+
hex2rgb(hex) {
|
|
283
|
+
let color2 = hex.toLowerCase();
|
|
284
|
+
if (!common.matchingText(color2, "hex")) {
|
|
285
|
+
throw new TypeError("The argument must be a hexadecimal color value");
|
|
286
|
+
}
|
|
287
|
+
if (color2.length === 4) {
|
|
288
|
+
let colorNew = "#";
|
|
289
|
+
for (let i = 1; i < 4; i += 1) {
|
|
290
|
+
colorNew += color2.slice(i, i + 1).concat(color2.slice(i, i + 1));
|
|
291
|
+
}
|
|
292
|
+
color2 = colorNew;
|
|
293
|
+
}
|
|
294
|
+
let colorChange = [];
|
|
295
|
+
for (let i = 1; i < 7; i += 2) {
|
|
296
|
+
colorChange.push(parseInt("0x" + color2.slice(i, i + 2)));
|
|
297
|
+
}
|
|
298
|
+
return colorChange;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
const dataName = "_dap-datas";
|
|
302
|
+
const data = {
|
|
303
|
+
/**
|
|
304
|
+
* 移除指定数据
|
|
305
|
+
* @param {Object} el
|
|
306
|
+
* @param {Object} key
|
|
307
|
+
*/
|
|
308
|
+
remove(el, key) {
|
|
309
|
+
const data2 = el[dataName] || {};
|
|
310
|
+
if (key) {
|
|
311
|
+
delete data2[key];
|
|
312
|
+
el[dataName] = data2;
|
|
313
|
+
} else {
|
|
314
|
+
el[dataName] = {};
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
/**
|
|
318
|
+
* 判断是否含有指定数据
|
|
319
|
+
* @param {Object} el
|
|
320
|
+
* @param {Object} key
|
|
321
|
+
*/
|
|
322
|
+
has(el, key) {
|
|
323
|
+
return (el[dataName] || {}).hasOwnProperty(key);
|
|
324
|
+
},
|
|
325
|
+
/**
|
|
326
|
+
* 获取元素指定数据
|
|
327
|
+
* @param {Object} el
|
|
328
|
+
* @param {Object} key
|
|
329
|
+
*/
|
|
330
|
+
get(el, key) {
|
|
331
|
+
const data2 = el[dataName] || {};
|
|
332
|
+
return !!key ? data2[key] : data2;
|
|
333
|
+
},
|
|
334
|
+
/**
|
|
335
|
+
* 设置元素指定数据
|
|
336
|
+
* @param {Object} el
|
|
337
|
+
* @param {Object} key
|
|
338
|
+
* @param {Object} value
|
|
339
|
+
*/
|
|
340
|
+
set(el, key, value) {
|
|
341
|
+
const data2 = el[dataName] || {};
|
|
342
|
+
data2[key] = value;
|
|
343
|
+
el[dataName] = data2;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
const string = {
|
|
347
|
+
/**
|
|
348
|
+
* 向指定位置插入字符串
|
|
349
|
+
* @param {Object} original 原始字符串
|
|
350
|
+
* @param {Object} str 插入的字符串
|
|
351
|
+
* @param {Object} index 插入的位置
|
|
352
|
+
*/
|
|
353
|
+
insert(original, str, index2) {
|
|
354
|
+
if (index2 < 0) {
|
|
355
|
+
throw new Error("The third argument cannot be less than 0");
|
|
356
|
+
}
|
|
357
|
+
return original.substring(0, index2) + str + original.substring(index2, original.length);
|
|
358
|
+
},
|
|
359
|
+
/**
|
|
360
|
+
* 删除指定位置的字符串
|
|
361
|
+
* @param {Object} original 原始字符串
|
|
362
|
+
* @param {Object} index 删除的位置序列
|
|
363
|
+
* @param {Object} num 删除的字符串长度
|
|
364
|
+
*/
|
|
365
|
+
delete(original, index2, num) {
|
|
366
|
+
if (index2 < 0) {
|
|
367
|
+
throw new Error("The second argument cannot be less than 0");
|
|
368
|
+
}
|
|
369
|
+
if (num < 0) {
|
|
370
|
+
throw new Error("The third argument cannot be less than 0");
|
|
371
|
+
}
|
|
372
|
+
return original.substring(0, index2) + original.substring(index2 + num, original.length);
|
|
373
|
+
},
|
|
374
|
+
/**
|
|
375
|
+
* 替换指定位置的字符串
|
|
376
|
+
* @param {Object} original 原始字符串
|
|
377
|
+
* @param {Object} start 开始位置
|
|
378
|
+
* @param {Object} end 结束位置
|
|
379
|
+
* @param {Object} str 替换的字符串
|
|
380
|
+
*/
|
|
381
|
+
replace(original, start, end, str) {
|
|
382
|
+
if (start < 0) {
|
|
383
|
+
throw new Error("The second argument cannot be less than 0");
|
|
384
|
+
}
|
|
385
|
+
if (end < 0) {
|
|
386
|
+
throw new Error("The third argument cannot be less than 0");
|
|
387
|
+
}
|
|
388
|
+
return original.substring(0, start) + str + original.substring(end, original.length);
|
|
389
|
+
},
|
|
390
|
+
/**
|
|
391
|
+
* 去除字符串空格
|
|
392
|
+
* @param {Object} str 原始字符串
|
|
393
|
+
* @param {Object} global 为true时去除所有空格,否则只去除两边空格
|
|
394
|
+
*/
|
|
395
|
+
trim(str, global2) {
|
|
396
|
+
let result = str.replace(/(^\s+)|(\s+$)/g, "");
|
|
397
|
+
if (global2) {
|
|
398
|
+
result = result.replace(/\s/g, "");
|
|
399
|
+
}
|
|
400
|
+
return result;
|
|
401
|
+
}
|
|
402
|
+
};
|
|
4
403
|
const number = {
|
|
5
404
|
/**
|
|
6
405
|
* 数字格式化
|
|
@@ -9,9 +408,8 @@ const number = {
|
|
|
9
408
|
formatNumber(num) {
|
|
10
409
|
if (this.isNumber(num)) {
|
|
11
410
|
return num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
|
|
12
|
-
} else {
|
|
13
|
-
return num;
|
|
14
411
|
}
|
|
412
|
+
return num.toString();
|
|
15
413
|
},
|
|
16
414
|
/**
|
|
17
415
|
* 判断是否数字
|
|
@@ -85,112 +483,22 @@ const number = {
|
|
|
85
483
|
/**
|
|
86
484
|
* 多个数的除法运算
|
|
87
485
|
*/
|
|
88
|
-
divide(...values) {
|
|
89
|
-
return values.reduce((num, value) => {
|
|
90
|
-
let t1 = 0;
|
|
91
|
-
let t2 = 0;
|
|
92
|
-
let s1 = num.toString();
|
|
93
|
-
let s2 = value.toString();
|
|
94
|
-
try {
|
|
95
|
-
t1 = s1.split(".")[1].length;
|
|
96
|
-
} catch (e) {
|
|
97
|
-
}
|
|
98
|
-
try {
|
|
99
|
-
t2 = s2.split(".")[1].length;
|
|
100
|
-
} catch (e) {
|
|
101
|
-
}
|
|
102
|
-
return Number(s1.replace(".", "")) / Number(s2.replace(".", "")) * Math.pow(10, t2 - t1);
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
const string = {
|
|
107
|
-
/**
|
|
108
|
-
* 向指定位置插入字符串
|
|
109
|
-
* @param {Object} original 原始字符串
|
|
110
|
-
* @param {Object} str 插入的字符串
|
|
111
|
-
* @param {Object} index 插入的位置
|
|
112
|
-
*/
|
|
113
|
-
insert(original, str, index) {
|
|
114
|
-
if (!original || typeof original != "string") {
|
|
115
|
-
throw new TypeError("The first argument must be a string");
|
|
116
|
-
}
|
|
117
|
-
if (typeof str != "string") {
|
|
118
|
-
throw new TypeError("The second argument must be a string");
|
|
119
|
-
}
|
|
120
|
-
if (!number.isNumber(index)) {
|
|
121
|
-
throw new TypeError("The third argument must be a number");
|
|
122
|
-
}
|
|
123
|
-
if (index < 0) {
|
|
124
|
-
throw new Error("The third argument cannot be less than 0");
|
|
125
|
-
}
|
|
126
|
-
return original.substring(0, index) + str + original.substring(index, original.length);
|
|
127
|
-
},
|
|
128
|
-
/**
|
|
129
|
-
* 删除指定位置的字符串
|
|
130
|
-
* @param {Object} original 原始字符串
|
|
131
|
-
* @param {Object} index 删除的位置序列
|
|
132
|
-
* @param {Object} num 删除的字符串长度
|
|
133
|
-
*/
|
|
134
|
-
delete(original, index, num) {
|
|
135
|
-
if (!original || typeof original != "string") {
|
|
136
|
-
throw new TypeError("The first argument must be a string");
|
|
137
|
-
}
|
|
138
|
-
if (!number.isNumber(index)) {
|
|
139
|
-
throw new TypeError("The second argument must be a number");
|
|
140
|
-
}
|
|
141
|
-
if (index < 0) {
|
|
142
|
-
throw new Error("The second argument cannot be less than 0");
|
|
143
|
-
}
|
|
144
|
-
if (!number.isNumber(num)) {
|
|
145
|
-
throw new TypeError("The third argument must be a number");
|
|
146
|
-
}
|
|
147
|
-
if (num < 0) {
|
|
148
|
-
throw new Error("The third argument cannot be less than 0");
|
|
149
|
-
}
|
|
150
|
-
return original.substring(0, index) + original.substring(index + num, original.length);
|
|
151
|
-
},
|
|
152
|
-
/**
|
|
153
|
-
* 替换指定位置的字符串
|
|
154
|
-
* @param {Object} original 原始字符串
|
|
155
|
-
* @param {Object} start 开始位置
|
|
156
|
-
* @param {Object} end 结束位置
|
|
157
|
-
* @param {Object} str 替换的字符串
|
|
158
|
-
*/
|
|
159
|
-
replace(original, start, end, str) {
|
|
160
|
-
if (!original || typeof original != "string") {
|
|
161
|
-
throw new TypeError("The first argument must be a string");
|
|
162
|
-
}
|
|
163
|
-
if (!number.isNumber(start)) {
|
|
164
|
-
throw new TypeError("The second argument must be a number");
|
|
165
|
-
}
|
|
166
|
-
if (start < 0) {
|
|
167
|
-
throw new Error("The second argument cannot be less than 0");
|
|
168
|
-
}
|
|
169
|
-
if (!number.isNumber(end)) {
|
|
170
|
-
throw new TypeError("The third argument must be a number");
|
|
171
|
-
}
|
|
172
|
-
if (end < 0) {
|
|
173
|
-
throw new Error("The third argument cannot be less than 0");
|
|
174
|
-
}
|
|
175
|
-
if (typeof str != "string") {
|
|
176
|
-
throw new TypeError("The fourth argument must be a string");
|
|
177
|
-
}
|
|
178
|
-
return original.substring(0, start) + str + original.substring(end, original.length);
|
|
179
|
-
},
|
|
180
|
-
/**
|
|
181
|
-
* 去除字符串空格
|
|
182
|
-
* @param {Object} str 原始字符串
|
|
183
|
-
* @param {Object} global 为true时去除所有空格,否则只去除两边空格
|
|
184
|
-
*/
|
|
185
|
-
trim(str, global2) {
|
|
186
|
-
if (typeof str != "string") {
|
|
187
|
-
throw new TypeError("The first argument must be a string");
|
|
188
|
-
}
|
|
189
|
-
let result = str.replace(/(^\s+)|(\s+$)/g, "");
|
|
190
|
-
if (global2) {
|
|
191
|
-
result = result.replace(/\s/g, "");
|
|
192
|
-
}
|
|
193
|
-
return result;
|
|
486
|
+
divide(...values) {
|
|
487
|
+
return values.reduce((num, value) => {
|
|
488
|
+
let t1 = 0;
|
|
489
|
+
let t2 = 0;
|
|
490
|
+
let s1 = num.toString();
|
|
491
|
+
let s2 = value.toString();
|
|
492
|
+
try {
|
|
493
|
+
t1 = s1.split(".")[1].length;
|
|
494
|
+
} catch (e) {
|
|
495
|
+
}
|
|
496
|
+
try {
|
|
497
|
+
t2 = s2.split(".")[1].length;
|
|
498
|
+
} catch (e) {
|
|
499
|
+
}
|
|
500
|
+
return Number(s1.replace(".", "")) / Number(s2.replace(".", "")) * Math.pow(10, t2 - t1);
|
|
501
|
+
});
|
|
194
502
|
}
|
|
195
503
|
};
|
|
196
504
|
const element = {
|
|
@@ -207,16 +515,13 @@ const element = {
|
|
|
207
515
|
* @param {Object} root 定位父元素或者祖先元素,未指定则为document.body
|
|
208
516
|
*/
|
|
209
517
|
getElementPoint(el, root) {
|
|
210
|
-
if (!this.isElement(el)) {
|
|
211
|
-
throw new TypeError("The first argument must be an element");
|
|
212
|
-
}
|
|
213
518
|
if (!this.isElement(root)) {
|
|
214
519
|
root = document.body;
|
|
215
520
|
}
|
|
216
521
|
if (!this.isContains(root, el)) {
|
|
217
522
|
throw new Error("The second argument and the first argument have no hierarchical relationship");
|
|
218
523
|
}
|
|
219
|
-
|
|
524
|
+
const obj = el;
|
|
220
525
|
let offsetTop = 0;
|
|
221
526
|
let offsetLeft = 0;
|
|
222
527
|
while (this.isElement(el) && this.isContains(root, el) && root !== el) {
|
|
@@ -224,8 +529,8 @@ const element = {
|
|
|
224
529
|
offsetLeft += el.offsetLeft;
|
|
225
530
|
el = el.offsetParent;
|
|
226
531
|
}
|
|
227
|
-
let offsetRight = root.offsetWidth - offsetLeft -
|
|
228
|
-
let offsetBottom = root.offsetHeight - offsetTop -
|
|
532
|
+
let offsetRight = root.offsetWidth - offsetLeft - obj.offsetWidth;
|
|
533
|
+
let offsetBottom = root.offsetHeight - offsetTop - obj.offsetHeight;
|
|
229
534
|
return {
|
|
230
535
|
top: offsetTop,
|
|
231
536
|
left: offsetLeft,
|
|
@@ -239,12 +544,6 @@ const element = {
|
|
|
239
544
|
* @param {Object} childNode 子元素
|
|
240
545
|
*/
|
|
241
546
|
isContains(parentNode, childNode) {
|
|
242
|
-
if (!this.isElement(parentNode)) {
|
|
243
|
-
throw new TypeError("The first argument must be an element");
|
|
244
|
-
}
|
|
245
|
-
if (!this.isElement(childNode)) {
|
|
246
|
-
throw new TypeError("The second argument must be an element");
|
|
247
|
-
}
|
|
248
547
|
if (parentNode === childNode) {
|
|
249
548
|
return true;
|
|
250
549
|
}
|
|
@@ -262,12 +561,6 @@ const element = {
|
|
|
262
561
|
* @param {Object} childNode 子元素
|
|
263
562
|
*/
|
|
264
563
|
isParentNode(parentNode, childNode) {
|
|
265
|
-
if (!this.isElement(parentNode)) {
|
|
266
|
-
throw new TypeError("The first argument must be an element");
|
|
267
|
-
}
|
|
268
|
-
if (!this.isElement(childNode)) {
|
|
269
|
-
throw new TypeError("The second argument must be an element");
|
|
270
|
-
}
|
|
271
564
|
if (parentNode === childNode) {
|
|
272
565
|
return false;
|
|
273
566
|
}
|
|
@@ -279,13 +572,7 @@ const element = {
|
|
|
279
572
|
* @param {Object} selector 支持多选择器,等同于querySelectorAll的参数
|
|
280
573
|
*/
|
|
281
574
|
children(el, selector) {
|
|
282
|
-
|
|
283
|
-
throw new TypeError("The first argument must be an element");
|
|
284
|
-
}
|
|
285
|
-
if (selector && typeof selector != "string") {
|
|
286
|
-
throw new TypeError("The second argument must be a string");
|
|
287
|
-
}
|
|
288
|
-
const res = el.querySelectorAll(selector || "*");
|
|
575
|
+
const res = el.querySelectorAll(selector ?? "*");
|
|
289
576
|
return [...res].filter((ele) => {
|
|
290
577
|
return ele.parentNode === el;
|
|
291
578
|
});
|
|
@@ -296,16 +583,10 @@ const element = {
|
|
|
296
583
|
* @param {Object} selector 取值等同于queryselectorAll的参数,支持多选择器
|
|
297
584
|
*/
|
|
298
585
|
siblings(el, selector) {
|
|
299
|
-
if (!this.isElement(el)) {
|
|
300
|
-
throw new TypeError("The first argument must be an element");
|
|
301
|
-
}
|
|
302
|
-
if (selector && typeof selector != "string") {
|
|
303
|
-
throw new TypeError("The second argument must be a string");
|
|
304
|
-
}
|
|
305
586
|
if (!el.parentNode) {
|
|
306
587
|
return [];
|
|
307
588
|
}
|
|
308
|
-
const res = el.parentNode.querySelectorAll(selector
|
|
589
|
+
const res = el.parentNode.querySelectorAll(selector ?? "*");
|
|
309
590
|
return [...res].filter((ele) => {
|
|
310
591
|
return ele.parentNode === el.parentNode && ele != el;
|
|
311
592
|
});
|
|
@@ -315,10 +596,7 @@ const element = {
|
|
|
315
596
|
* @param {Object} num rem数值
|
|
316
597
|
*/
|
|
317
598
|
rem2px(num) {
|
|
318
|
-
|
|
319
|
-
throw new TypeError("The argument must be a number");
|
|
320
|
-
}
|
|
321
|
-
let fs = this.getCssStyle(document.documentElement, "font-size");
|
|
599
|
+
const fs = this.getCssStyle(document.documentElement, "font-size");
|
|
322
600
|
return number.mutiply(num, parseFloat(fs));
|
|
323
601
|
},
|
|
324
602
|
/**
|
|
@@ -326,10 +604,7 @@ const element = {
|
|
|
326
604
|
* @param {Object} num px数值
|
|
327
605
|
*/
|
|
328
606
|
px2rem(num) {
|
|
329
|
-
|
|
330
|
-
throw new TypeError("The argument must be a number");
|
|
331
|
-
}
|
|
332
|
-
let fs = this.getCssStyle(document.documentElement, "font-size");
|
|
607
|
+
const fs = this.getCssStyle(document.documentElement, "font-size");
|
|
333
608
|
return number.divide(num, parseFloat(fs));
|
|
334
609
|
},
|
|
335
610
|
/**
|
|
@@ -343,9 +618,9 @@ const element = {
|
|
|
343
618
|
if (!this.isElement(el)) {
|
|
344
619
|
el = document.body;
|
|
345
620
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
621
|
+
const clientWidth = el.clientWidth;
|
|
622
|
+
const paddingLeft_width = parseFloat(this.getCssStyle(el, "padding-left"));
|
|
623
|
+
const paddingRight_width = parseFloat(this.getCssStyle(el, "padding-right"));
|
|
349
624
|
return number.subtract(clientWidth, paddingLeft_width, paddingRight_width);
|
|
350
625
|
},
|
|
351
626
|
/**
|
|
@@ -359,9 +634,9 @@ const element = {
|
|
|
359
634
|
if (!this.isElement(el)) {
|
|
360
635
|
el = document.body;
|
|
361
636
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
637
|
+
const clientHeight = el.clientHeight;
|
|
638
|
+
const paddingTop_height = parseFloat(this.getCssStyle(el, "padding-top"));
|
|
639
|
+
const paddingBottom_height = parseFloat(this.getCssStyle(el, "padding-bottom"));
|
|
365
640
|
return number.subtract(clientHeight, paddingTop_height, paddingBottom_height);
|
|
366
641
|
},
|
|
367
642
|
/**
|
|
@@ -370,16 +645,9 @@ const element = {
|
|
|
370
645
|
* @param {Object} className 支持多类,以空格划分
|
|
371
646
|
*/
|
|
372
647
|
removeClass(el, className) {
|
|
373
|
-
|
|
374
|
-
throw new TypeError("The first argument must be an element");
|
|
375
|
-
}
|
|
376
|
-
if (!className || typeof className != "string") {
|
|
377
|
-
throw new TypeError("The second argument must be a string");
|
|
378
|
-
}
|
|
379
|
-
let classList = el.classList;
|
|
380
|
-
let classArray = string.trim(className).split(/\s+/);
|
|
648
|
+
const classArray = string.trim(className).split(/\s+/);
|
|
381
649
|
classArray.forEach((item) => {
|
|
382
|
-
classList.remove(item);
|
|
650
|
+
el.classList.remove(item);
|
|
383
651
|
});
|
|
384
652
|
},
|
|
385
653
|
/**
|
|
@@ -388,16 +656,9 @@ const element = {
|
|
|
388
656
|
* @param {Object} className 支持多类,以空格划分
|
|
389
657
|
*/
|
|
390
658
|
addClass(el, className) {
|
|
391
|
-
|
|
392
|
-
throw new TypeError("The first argument must be an element");
|
|
393
|
-
}
|
|
394
|
-
if (!className || typeof className != "string") {
|
|
395
|
-
throw new TypeError("The second argument must be a string");
|
|
396
|
-
}
|
|
397
|
-
let classList = el.classList;
|
|
398
|
-
let classArray = string.trim(className).split(/\s+/);
|
|
659
|
+
const classArray = string.trim(className).split(/\s+/);
|
|
399
660
|
classArray.forEach((item) => {
|
|
400
|
-
classList.add(item);
|
|
661
|
+
el.classList.add(item);
|
|
401
662
|
});
|
|
402
663
|
},
|
|
403
664
|
/**
|
|
@@ -406,16 +667,9 @@ const element = {
|
|
|
406
667
|
* @param {Object} className 支持多类,以空格划分
|
|
407
668
|
*/
|
|
408
669
|
hasClass(el, className) {
|
|
409
|
-
|
|
410
|
-
throw new TypeError("The first argument must be an element");
|
|
411
|
-
}
|
|
412
|
-
if (!className || typeof className != "string") {
|
|
413
|
-
throw new TypeError("The second argument must be a string");
|
|
414
|
-
}
|
|
415
|
-
let classList = el.classList;
|
|
416
|
-
let classArray = string.trim(className).split(/\s+/);
|
|
670
|
+
const classArray = string.trim(className).split(/\s+/);
|
|
417
671
|
return classArray.every((item) => {
|
|
418
|
-
return classList.contains(item);
|
|
672
|
+
return el.classList.contains(item);
|
|
419
673
|
});
|
|
420
674
|
},
|
|
421
675
|
/**
|
|
@@ -437,7 +691,7 @@ const element = {
|
|
|
437
691
|
let flag = true;
|
|
438
692
|
scrollEle.addEventListener("scroll", () => {
|
|
439
693
|
if (this.getScrollTop(scrollEle) <= 0) {
|
|
440
|
-
|
|
694
|
+
const options = {
|
|
441
695
|
state: "top",
|
|
442
696
|
target: scrollEle
|
|
443
697
|
};
|
|
@@ -449,7 +703,7 @@ const element = {
|
|
|
449
703
|
callback(options);
|
|
450
704
|
}
|
|
451
705
|
} else {
|
|
452
|
-
|
|
706
|
+
const options = {
|
|
453
707
|
state: "bottom",
|
|
454
708
|
target: scrollEle
|
|
455
709
|
};
|
|
@@ -523,8 +777,8 @@ const element = {
|
|
|
523
777
|
if (typeof el == "string" && el) {
|
|
524
778
|
el = document.body.querySelector(el);
|
|
525
779
|
}
|
|
526
|
-
|
|
527
|
-
|
|
780
|
+
const number$1 = options.number || 0;
|
|
781
|
+
const time = options.time || 0;
|
|
528
782
|
if (!this.isElement(el) || el == document.body || el == document.documentElement || el == window) {
|
|
529
783
|
isWindow = true;
|
|
530
784
|
}
|
|
@@ -537,11 +791,11 @@ const element = {
|
|
|
537
791
|
}
|
|
538
792
|
resolve();
|
|
539
793
|
} else {
|
|
540
|
-
|
|
794
|
+
const spacingTime = 10;
|
|
541
795
|
let spacingIndex = number.divide(time, spacingTime);
|
|
542
796
|
let nowTop = this.getScrollTop(el);
|
|
543
|
-
|
|
544
|
-
|
|
797
|
+
const everTop = number.divide(number.subtract(number$1, nowTop), spacingIndex);
|
|
798
|
+
const scrollTimer = setInterval(() => {
|
|
545
799
|
if (spacingIndex > 0) {
|
|
546
800
|
spacingIndex--;
|
|
547
801
|
if (isWindow) {
|
|
@@ -607,8 +861,8 @@ const element = {
|
|
|
607
861
|
if (typeof el == "string" && el) {
|
|
608
862
|
el = document.body.querySelector(el);
|
|
609
863
|
}
|
|
610
|
-
|
|
611
|
-
|
|
864
|
+
const number$1 = options.number || 0;
|
|
865
|
+
const time = options.time || 0;
|
|
612
866
|
if (!this.isElement(el) || el == document.body || el == document.documentElement || el == window) {
|
|
613
867
|
isWindow = true;
|
|
614
868
|
}
|
|
@@ -621,10 +875,10 @@ const element = {
|
|
|
621
875
|
}
|
|
622
876
|
resolve();
|
|
623
877
|
} else {
|
|
624
|
-
|
|
878
|
+
const spacingTime = 10;
|
|
625
879
|
let spacingIndex = number.divide(time, spacingTime);
|
|
626
880
|
let nowLeft = this.getScrollLeft(el);
|
|
627
|
-
|
|
881
|
+
const everLeft = number.divide(number.subtract(number$1, nowLeft), spacingIndex);
|
|
628
882
|
let scrollTimer = setInterval(() => {
|
|
629
883
|
if (spacingIndex > 0) {
|
|
630
884
|
spacingIndex--;
|
|
@@ -647,12 +901,6 @@ const element = {
|
|
|
647
901
|
* @param {Object} cssName 样式名称
|
|
648
902
|
*/
|
|
649
903
|
getCssStyle(el, cssName) {
|
|
650
|
-
if (!this.isElement(el)) {
|
|
651
|
-
throw new TypeError("The first argument must be an element");
|
|
652
|
-
}
|
|
653
|
-
if (!cssName || typeof cssName != "string") {
|
|
654
|
-
throw new TypeError("The second argument must be a string");
|
|
655
|
-
}
|
|
656
904
|
let cssText = "";
|
|
657
905
|
if (document.defaultView && document.defaultView.getComputedStyle) {
|
|
658
906
|
cssText = document.defaultView.getComputedStyle(el)[cssName];
|
|
@@ -663,378 +911,116 @@ const element = {
|
|
|
663
911
|
},
|
|
664
912
|
/**
|
|
665
913
|
* 判断字符串属于哪种选择器
|
|
666
|
-
* @param {Object} selector
|
|
667
|
-
*/
|
|
668
|
-
getCssSelector(selector) {
|
|
669
|
-
if (
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
type: "id",
|
|
675
|
-
value: selector.substr(1)
|
|
676
|
-
};
|
|
677
|
-
}
|
|
678
|
-
if (/^\./.test(selector)) {
|
|
679
|
-
return {
|
|
680
|
-
type: "class",
|
|
681
|
-
value: selector.substr(1)
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
if (/^\[(.+)\]$/.test(selector)) {
|
|
685
|
-
let type = "attribute";
|
|
686
|
-
let value = "";
|
|
687
|
-
let attribute = string.trim(selector, true).substring(1, string.trim(selector, true).length - 1);
|
|
688
|
-
let arry = attribute.split("=");
|
|
689
|
-
if (arry.length == 1) {
|
|
690
|
-
value = arry[0];
|
|
691
|
-
}
|
|
692
|
-
if (arry.length == 2) {
|
|
693
|
-
value = {
|
|
694
|
-
attributeName: arry[0],
|
|
695
|
-
attributeValue: arry[1].replace(/\'/g, "").replace(/\"/g, "")
|
|
696
|
-
//去除属性值的单引号或者双引号
|
|
697
|
-
};
|
|
698
|
-
}
|
|
699
|
-
return {
|
|
700
|
-
type,
|
|
701
|
-
value
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
return {
|
|
705
|
-
type: "tag",
|
|
706
|
-
value: selector
|
|
707
|
-
};
|
|
708
|
-
},
|
|
709
|
-
/**
|
|
710
|
-
* 获取元素距离可视窗口的位置
|
|
711
|
-
* @param {Object} el 支持css选择器字符串 未指定则为document.body
|
|
712
|
-
*/
|
|
713
|
-
getElementBounding(el) {
|
|
714
|
-
if (typeof el == "string" && el) {
|
|
715
|
-
el = document.body.querySelector(el);
|
|
716
|
-
}
|
|
717
|
-
if (!this.isElement(el)) {
|
|
718
|
-
el = document.body;
|
|
719
|
-
}
|
|
720
|
-
let point = el.getBoundingClientRect();
|
|
721
|
-
let top = point.top;
|
|
722
|
-
let bottom = number.subtract(document.documentElement.clientHeight || window.innerHeight, point.bottom);
|
|
723
|
-
let left = point.left;
|
|
724
|
-
let right = number.subtract(document.documentElement.clientWidth || window.innerWidth, point.right);
|
|
725
|
-
return {
|
|
726
|
-
top,
|
|
727
|
-
bottom,
|
|
728
|
-
left,
|
|
729
|
-
right
|
|
730
|
-
};
|
|
731
|
-
},
|
|
732
|
-
/**
|
|
733
|
-
* 判断是否是元素
|
|
734
|
-
* @param {Object} el
|
|
735
|
-
*/
|
|
736
|
-
isElement(el) {
|
|
737
|
-
return el && el instanceof Node && el.nodeType === 1;
|
|
738
|
-
},
|
|
739
|
-
/**
|
|
740
|
-
* 字符串转dom
|
|
741
|
-
* @param {Object} html
|
|
742
|
-
*/
|
|
743
|
-
string2dom(html) {
|
|
744
|
-
if (!html || typeof html != "string") {
|
|
745
|
-
throw new TypeError("The argument must be an HTML string");
|
|
746
|
-
}
|
|
747
|
-
const template = document.createElement("template");
|
|
748
|
-
template.innerHTML = html;
|
|
749
|
-
if (template.content.children.length == 1) {
|
|
750
|
-
return template.content.children[0];
|
|
751
|
-
} else {
|
|
752
|
-
return Array.from(template.content.children);
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
};
|
|
756
|
-
const dataName = "_dap-datas";
|
|
757
|
-
const data = {
|
|
758
|
-
/**
|
|
759
|
-
* 移除指定数据
|
|
760
|
-
* @param {Object} el
|
|
761
|
-
* @param {Object} key
|
|
762
|
-
*/
|
|
763
|
-
remove(el, key) {
|
|
764
|
-
if (!(el instanceof Document) && !element.isElement(el) && !element.isWindow(el)) {
|
|
765
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
766
|
-
}
|
|
767
|
-
let data2 = el[dataName] || {};
|
|
768
|
-
if (key === void 0 || key === null || key === "") {
|
|
769
|
-
el[dataName] = {};
|
|
770
|
-
} else {
|
|
771
|
-
delete data2[key];
|
|
772
|
-
el[dataName] = data2;
|
|
773
|
-
}
|
|
774
|
-
},
|
|
775
|
-
/**
|
|
776
|
-
* 判断是否含有指定数据
|
|
777
|
-
* @param {Object} el
|
|
778
|
-
* @param {Object} key
|
|
779
|
-
*/
|
|
780
|
-
has(el, key) {
|
|
781
|
-
if (!(el instanceof Document) && !element.isElement(el) && !element.isWindow(el)) {
|
|
782
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
783
|
-
}
|
|
784
|
-
if (key === void 0 || key === null || key === "") {
|
|
785
|
-
throw new TypeError("The second parameter must be a unique key");
|
|
786
|
-
}
|
|
787
|
-
let data2 = el[dataName] || {};
|
|
788
|
-
return data2.hasOwnProperty(key);
|
|
789
|
-
},
|
|
790
|
-
/**
|
|
791
|
-
* 获取元素指定数据
|
|
792
|
-
* @param {Object} el
|
|
793
|
-
* @param {Object} key
|
|
794
|
-
*/
|
|
795
|
-
get(el, key) {
|
|
796
|
-
if (!(el instanceof Document) && !element.isElement(el) && !element.isWindow(el)) {
|
|
797
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
798
|
-
}
|
|
799
|
-
let data2 = el[dataName] || {};
|
|
800
|
-
if (key === void 0 || key === null || key === "") {
|
|
801
|
-
return data2;
|
|
802
|
-
} else {
|
|
803
|
-
return data2[key];
|
|
804
|
-
}
|
|
805
|
-
},
|
|
806
|
-
/**
|
|
807
|
-
* 设置元素指定数据
|
|
808
|
-
* @param {Object} el
|
|
809
|
-
* @param {Object} key
|
|
810
|
-
* @param {Object} value
|
|
811
|
-
*/
|
|
812
|
-
set(el, key, value) {
|
|
813
|
-
if (!(el instanceof Document) && !element.isElement(el) && !element.isWindow(el)) {
|
|
814
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
815
|
-
}
|
|
816
|
-
if (key === void 0 || key === null || key === "") {
|
|
817
|
-
throw new TypeError("The second parameter must be a unique key");
|
|
818
|
-
}
|
|
819
|
-
let data2 = el[dataName] || {};
|
|
820
|
-
data2[key] = value;
|
|
821
|
-
el[dataName] = data2;
|
|
822
|
-
}
|
|
823
|
-
};
|
|
824
|
-
const common = {
|
|
825
|
-
/**
|
|
826
|
-
* 常用判断
|
|
827
|
-
* @param {Object} text 要判断的字符串
|
|
828
|
-
* @param {Object} param 判断的类型字符串
|
|
829
|
-
*/
|
|
830
|
-
matchingText(text2, param) {
|
|
831
|
-
if (!text2 || typeof text2 != "string") {
|
|
832
|
-
throw new TypeError("The first argument must be a string");
|
|
833
|
-
}
|
|
834
|
-
if (!param || typeof param != "string") {
|
|
835
|
-
throw new TypeError("The second argument must be a string");
|
|
836
|
-
}
|
|
837
|
-
let reg = null;
|
|
838
|
-
if (param == "Chinese") {
|
|
839
|
-
reg = /^[\u4e00-\u9fa5]+$/;
|
|
840
|
-
}
|
|
841
|
-
if (param == "chinese") {
|
|
842
|
-
reg = /[\u4e00-\u9fa5]/;
|
|
843
|
-
}
|
|
844
|
-
if (param == "email") {
|
|
845
|
-
reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
|
|
846
|
-
}
|
|
847
|
-
if (param == "username") {
|
|
848
|
-
reg = /^[a-zA-Z0-9_]{4,16}$/;
|
|
849
|
-
}
|
|
850
|
-
if (param == "int+") {
|
|
851
|
-
reg = /^\d+$/;
|
|
852
|
-
}
|
|
853
|
-
if (param == "int-") {
|
|
854
|
-
reg = /^-\d+$/;
|
|
855
|
-
}
|
|
856
|
-
if (param == "int") {
|
|
857
|
-
reg = /^-?\d+$/;
|
|
858
|
-
}
|
|
859
|
-
if (param == "pos") {
|
|
860
|
-
reg = /^\d*\.?\d+$/;
|
|
861
|
-
}
|
|
862
|
-
if (param == "neg") {
|
|
863
|
-
reg = /^-\d*\.?\d+$/;
|
|
864
|
-
}
|
|
865
|
-
if (param == "number") {
|
|
866
|
-
reg = /^-?\d*\.?\d+$/;
|
|
867
|
-
}
|
|
868
|
-
if (param == "phone") {
|
|
869
|
-
reg = /^1[0-9]\d{9}$/;
|
|
870
|
-
}
|
|
871
|
-
if (param == "idCard") {
|
|
872
|
-
reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
|
|
873
|
-
}
|
|
874
|
-
if (param == "url") {
|
|
875
|
-
reg = /^(https?|ftp):\/\/(-\.)?([^\s\/?\.#-]+\.?)+(\/[^\s]*)?$/;
|
|
876
|
-
}
|
|
877
|
-
if (param == "IPv4") {
|
|
878
|
-
reg = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
879
|
-
}
|
|
880
|
-
if (param == "hex") {
|
|
881
|
-
reg = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
|
|
882
|
-
}
|
|
883
|
-
if (param == "rgb") {
|
|
884
|
-
reg = /^rgb\((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\)$/;
|
|
885
|
-
}
|
|
886
|
-
if (param == "rgba") {
|
|
887
|
-
reg = /^rgba\((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d),\s?(0?\.\d|1(\.0)?|0)\)$/;
|
|
888
|
-
}
|
|
889
|
-
if (param == "QQ") {
|
|
890
|
-
reg = /^[1-9][0-9]{4,10}$/;
|
|
891
|
-
}
|
|
892
|
-
if (param == "weixin") {
|
|
893
|
-
reg = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;
|
|
894
|
-
}
|
|
895
|
-
if (param == "plate") {
|
|
896
|
-
reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
|
|
897
|
-
}
|
|
898
|
-
if (!reg) {
|
|
899
|
-
throw new Error("The second parameter is out of scope");
|
|
900
|
-
}
|
|
901
|
-
return reg.test(text2);
|
|
902
|
-
},
|
|
903
|
-
/**
|
|
904
|
-
* 根据参数名获取地址栏参数值
|
|
905
|
-
* @param {Object} name
|
|
906
|
-
*/
|
|
907
|
-
getUrlParams(name) {
|
|
908
|
-
if (!name || typeof name != "string") {
|
|
909
|
-
throw new TypeError("The argument must be a string");
|
|
910
|
-
}
|
|
911
|
-
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
|
|
912
|
-
let search = window.location.search.substr(1);
|
|
913
|
-
if (!search) {
|
|
914
|
-
let arr = window.location.hash.split("?");
|
|
915
|
-
if (arr.length == 2) {
|
|
916
|
-
search = arr[1];
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
let r2 = search.match(reg);
|
|
920
|
-
if (r2) {
|
|
921
|
-
return decodeURIComponent(r2[2]);
|
|
922
|
-
}
|
|
923
|
-
return null;
|
|
924
|
-
},
|
|
925
|
-
/**
|
|
926
|
-
* 判断是否空对象
|
|
927
|
-
* @param {Object} obj
|
|
928
|
-
*/
|
|
929
|
-
isEmptyObject(obj2) {
|
|
930
|
-
if (this.isObject(obj2)) {
|
|
931
|
-
if (Object.keys(obj2).length == 0) {
|
|
932
|
-
return true;
|
|
933
|
-
}
|
|
934
|
-
return false;
|
|
935
|
-
}
|
|
936
|
-
return false;
|
|
937
|
-
},
|
|
938
|
-
/**
|
|
939
|
-
* 判断两个参数是否相等
|
|
940
|
-
* @param {Object} a
|
|
941
|
-
* @param {Object} b
|
|
942
|
-
*/
|
|
943
|
-
equal(a, b) {
|
|
944
|
-
if (typeof a !== typeof b) {
|
|
945
|
-
return false;
|
|
914
|
+
* @param {Object} selector
|
|
915
|
+
*/
|
|
916
|
+
getCssSelector(selector) {
|
|
917
|
+
if (/^#{1}/.test(selector)) {
|
|
918
|
+
return {
|
|
919
|
+
type: "id",
|
|
920
|
+
value: selector.substr(1)
|
|
921
|
+
};
|
|
946
922
|
}
|
|
947
|
-
if (
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
923
|
+
if (/^\./.test(selector)) {
|
|
924
|
+
return {
|
|
925
|
+
type: "class",
|
|
926
|
+
value: selector.substr(1)
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
if (/^\[(.+)\]$/.test(selector)) {
|
|
930
|
+
let type = "attribute";
|
|
931
|
+
let value = "";
|
|
932
|
+
let attribute = string.trim(selector, true).substring(1, string.trim(selector, true).length - 1);
|
|
933
|
+
let arry = attribute.split("=");
|
|
934
|
+
if (arry.length == 1) {
|
|
935
|
+
value = arry[0];
|
|
952
936
|
}
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
if (!this.equal(propA, propB)) {
|
|
960
|
-
isEqual = false;
|
|
961
|
-
break;
|
|
962
|
-
}
|
|
937
|
+
if (arry.length == 2) {
|
|
938
|
+
value = {
|
|
939
|
+
attributeName: arry[0],
|
|
940
|
+
attributeValue: arry[1].replace(/\'/g, "").replace(/\"/g, "")
|
|
941
|
+
//去除属性值的单引号或者双引号
|
|
942
|
+
};
|
|
963
943
|
}
|
|
964
|
-
return
|
|
944
|
+
return {
|
|
945
|
+
type,
|
|
946
|
+
value
|
|
947
|
+
};
|
|
965
948
|
}
|
|
966
|
-
return
|
|
949
|
+
return {
|
|
950
|
+
type: "tag",
|
|
951
|
+
value: selector
|
|
952
|
+
};
|
|
967
953
|
},
|
|
968
954
|
/**
|
|
969
|
-
*
|
|
970
|
-
* @param {Object}
|
|
955
|
+
* 获取元素距离可视窗口的位置
|
|
956
|
+
* @param {Object} el 支持css选择器字符串 未指定则为document.body
|
|
971
957
|
*/
|
|
972
|
-
|
|
973
|
-
if (typeof
|
|
974
|
-
|
|
958
|
+
getElementBounding(el) {
|
|
959
|
+
if (typeof el == "string" && el) {
|
|
960
|
+
el = document.body.querySelector(el);
|
|
975
961
|
}
|
|
976
|
-
|
|
962
|
+
if (!this.isElement(el)) {
|
|
963
|
+
el = document.body;
|
|
964
|
+
}
|
|
965
|
+
const point = el.getBoundingClientRect();
|
|
966
|
+
const top = point.top;
|
|
967
|
+
const bottom = number.subtract(document.documentElement.clientHeight || window.innerHeight, point.bottom);
|
|
968
|
+
const left = point.left;
|
|
969
|
+
const right = number.subtract(document.documentElement.clientWidth || window.innerWidth, point.right);
|
|
970
|
+
return {
|
|
971
|
+
top,
|
|
972
|
+
bottom,
|
|
973
|
+
left,
|
|
974
|
+
right
|
|
975
|
+
};
|
|
977
976
|
},
|
|
978
977
|
/**
|
|
979
|
-
*
|
|
980
|
-
* @param {Object}
|
|
978
|
+
* 判断是否是元素
|
|
979
|
+
* @param {Object} el
|
|
981
980
|
*/
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
throw new TypeError("No text to copy is defined");
|
|
985
|
-
}
|
|
986
|
-
if (!navigator.clipboard) {
|
|
987
|
-
throw new Error("navigator.clipboard must be obtained in a secure environment, such as localhost, 127.0.0.1, or https, so the method won't work");
|
|
988
|
-
}
|
|
989
|
-
return navigator.clipboard.writeText(text2);
|
|
981
|
+
isElement(el) {
|
|
982
|
+
return el && el instanceof Node && el.nodeType === 1;
|
|
990
983
|
},
|
|
991
984
|
/**
|
|
992
|
-
*
|
|
993
|
-
* @param {Object}
|
|
985
|
+
* 字符串转dom
|
|
986
|
+
* @param {Object} html
|
|
994
987
|
*/
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
let newData = {};
|
|
1003
|
-
for (let key in data2) {
|
|
1004
|
-
newData[key] = this.clone(data2[key]);
|
|
1005
|
-
}
|
|
1006
|
-
return newData;
|
|
988
|
+
string2dom(html) {
|
|
989
|
+
const template = document.createElement("template");
|
|
990
|
+
template.innerHTML = html;
|
|
991
|
+
if (template.content.children.length == 1) {
|
|
992
|
+
return template.content.children[0];
|
|
1007
993
|
}
|
|
1008
|
-
return
|
|
994
|
+
return Array.from(template.content.children);
|
|
1009
995
|
}
|
|
1010
996
|
};
|
|
1011
997
|
const parseEventName = (eventName) => {
|
|
1012
|
-
|
|
1013
|
-
|
|
998
|
+
const eventNames = eventName.split(/[\s]+/g);
|
|
999
|
+
const result = [];
|
|
1014
1000
|
eventNames.forEach((name) => {
|
|
1015
|
-
|
|
1016
|
-
|
|
1001
|
+
const arr = name.split(".");
|
|
1002
|
+
const obj = {
|
|
1017
1003
|
eventName: arr[0]
|
|
1018
1004
|
};
|
|
1019
1005
|
if (arr.length > 1) {
|
|
1020
|
-
|
|
1006
|
+
obj.guid = arr[1];
|
|
1021
1007
|
}
|
|
1022
|
-
result.push(
|
|
1008
|
+
result.push(obj);
|
|
1023
1009
|
});
|
|
1024
1010
|
return result;
|
|
1025
1011
|
};
|
|
1026
1012
|
const updateEvents = (events) => {
|
|
1027
|
-
|
|
1028
|
-
|
|
1013
|
+
const obj = {};
|
|
1014
|
+
const keys = Object.keys(events);
|
|
1029
1015
|
keys.forEach((key) => {
|
|
1030
1016
|
if (events[key]) {
|
|
1031
|
-
|
|
1017
|
+
obj[key] = events[key];
|
|
1032
1018
|
}
|
|
1033
1019
|
});
|
|
1034
|
-
return
|
|
1020
|
+
return obj;
|
|
1035
1021
|
};
|
|
1036
1022
|
const bindSingleListener = (el, eventName, guid, fn, options) => {
|
|
1037
|
-
|
|
1023
|
+
const events = data.get(el, "dap-defined-events") || {};
|
|
1038
1024
|
if (!guid) {
|
|
1039
1025
|
guid = data.get(el, "dap-event-guid") || 0;
|
|
1040
1026
|
data.set(el, "dap-event-guid", guid + 1);
|
|
@@ -1052,12 +1038,12 @@ const bindSingleListener = (el, eventName, guid, fn, options) => {
|
|
|
1052
1038
|
data.set(el, "dap-defined-events", events);
|
|
1053
1039
|
};
|
|
1054
1040
|
const unbindSingleListener = (el, eventName, guid) => {
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1041
|
+
const events = data.get(el, "dap-defined-events") || {};
|
|
1042
|
+
const keys = Object.keys(events);
|
|
1043
|
+
const length = keys.length;
|
|
1058
1044
|
for (let i = 0; i < length; i++) {
|
|
1059
|
-
|
|
1060
|
-
if (events[key].type == eventName) {
|
|
1045
|
+
const key = keys[i];
|
|
1046
|
+
if (events[key] && events[key].type == eventName) {
|
|
1061
1047
|
if (guid) {
|
|
1062
1048
|
if (key == eventName + "." + guid) {
|
|
1063
1049
|
el.removeEventListener(events[key].type, events[key].fn, events[key].options);
|
|
@@ -1069,8 +1055,7 @@ const unbindSingleListener = (el, eventName, guid) => {
|
|
|
1069
1055
|
}
|
|
1070
1056
|
}
|
|
1071
1057
|
}
|
|
1072
|
-
events
|
|
1073
|
-
data.set(el, "dap-defined-events", events);
|
|
1058
|
+
data.set(el, "dap-defined-events", updateEvents(events));
|
|
1074
1059
|
};
|
|
1075
1060
|
const event = {
|
|
1076
1061
|
/**
|
|
@@ -1081,18 +1066,6 @@ const event = {
|
|
|
1081
1066
|
* @param {Object} options 参数
|
|
1082
1067
|
*/
|
|
1083
1068
|
on(el, eventName, fn, options) {
|
|
1084
|
-
if (!(el instanceof Document) && !element.isElement(el) && !element.isWindow(el)) {
|
|
1085
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
1086
|
-
}
|
|
1087
|
-
if (!eventName || typeof eventName != "string") {
|
|
1088
|
-
throw new TypeError("The second argument must be a string");
|
|
1089
|
-
}
|
|
1090
|
-
if (!fn || typeof fn != "function") {
|
|
1091
|
-
throw new TypeError("The third argument must be a function");
|
|
1092
|
-
}
|
|
1093
|
-
if (!common.isObject(options)) {
|
|
1094
|
-
options = {};
|
|
1095
|
-
}
|
|
1096
1069
|
const result = parseEventName(eventName);
|
|
1097
1070
|
result.forEach((res) => {
|
|
1098
1071
|
bindSingleListener(el, res.eventName, res.guid, fn.bind(el), options);
|
|
@@ -1104,18 +1077,15 @@ const event = {
|
|
|
1104
1077
|
* @param {Object} eventName 事件名称
|
|
1105
1078
|
*/
|
|
1106
1079
|
off(el, eventName) {
|
|
1107
|
-
|
|
1108
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
1109
|
-
}
|
|
1110
|
-
let events = data.get(el, "dap-defined-events");
|
|
1080
|
+
const events = data.get(el, "dap-defined-events");
|
|
1111
1081
|
if (!events) {
|
|
1112
1082
|
return;
|
|
1113
1083
|
}
|
|
1114
1084
|
if (!eventName) {
|
|
1115
|
-
|
|
1116
|
-
|
|
1085
|
+
const keys = Object.keys(events);
|
|
1086
|
+
const length = keys.length;
|
|
1117
1087
|
for (let i = 0; i < length; i++) {
|
|
1118
|
-
|
|
1088
|
+
const key = keys[i];
|
|
1119
1089
|
el.removeEventListener(events[key].type, events[key].fn, events[key].options);
|
|
1120
1090
|
}
|
|
1121
1091
|
data.remove(el, "dap-defined-events");
|
|
@@ -1132,164 +1102,19 @@ const event = {
|
|
|
1132
1102
|
* @param {*} el
|
|
1133
1103
|
*/
|
|
1134
1104
|
get(el) {
|
|
1135
|
-
|
|
1136
|
-
throw new TypeError("The first argument must be an element node or window or document");
|
|
1137
|
-
}
|
|
1138
|
-
let events = data.get(el, "dap-defined-events");
|
|
1105
|
+
const events = data.get(el, "dap-defined-events");
|
|
1139
1106
|
if (!events) {
|
|
1140
1107
|
return;
|
|
1141
1108
|
}
|
|
1142
1109
|
return events;
|
|
1143
1110
|
}
|
|
1144
1111
|
};
|
|
1145
|
-
const color = {
|
|
1146
|
-
/**
|
|
1147
|
-
* rgb转hsv值
|
|
1148
|
-
* @param {Object} rgb rgb值,数组
|
|
1149
|
-
*/
|
|
1150
|
-
rgb2hsv(rgb) {
|
|
1151
|
-
if (!Array.isArray(rgb) || rgb.length != 3) {
|
|
1152
|
-
throw new TypeError("Invalid argument");
|
|
1153
|
-
}
|
|
1154
|
-
let h = 0;
|
|
1155
|
-
let s = 0;
|
|
1156
|
-
let v = 0;
|
|
1157
|
-
let r2 = rgb[0] >= 255 ? 255 : rgb[0];
|
|
1158
|
-
let g = rgb[1] >= 255 ? 255 : rgb[1];
|
|
1159
|
-
let b = rgb[2] >= 255 ? 255 : rgb[2];
|
|
1160
|
-
r2 = r2 <= 0 ? 0 : r2;
|
|
1161
|
-
g = g <= 0 ? 0 : g;
|
|
1162
|
-
b = b <= 0 ? 0 : b;
|
|
1163
|
-
let max = Math.max(r2, g, b);
|
|
1164
|
-
let min = Math.min(r2, g, b);
|
|
1165
|
-
v = max / 255;
|
|
1166
|
-
if (max === 0) {
|
|
1167
|
-
s = 0;
|
|
1168
|
-
} else {
|
|
1169
|
-
s = 1 - min / max;
|
|
1170
|
-
}
|
|
1171
|
-
if (max === min) {
|
|
1172
|
-
h = 0;
|
|
1173
|
-
} else if (max === r2 && g >= b) {
|
|
1174
|
-
h = 60 * ((g - b) / (max - min)) + 0;
|
|
1175
|
-
} else if (max === r2 && g < b) {
|
|
1176
|
-
h = 60 * ((g - b) / (max - min)) + 360;
|
|
1177
|
-
} else if (max === g) {
|
|
1178
|
-
h = 60 * ((b - r2) / (max - min)) + 120;
|
|
1179
|
-
} else if (max === b) {
|
|
1180
|
-
h = 60 * ((r2 - g) / (max - min)) + 240;
|
|
1181
|
-
}
|
|
1182
|
-
return [h, s * 100, v * 100];
|
|
1183
|
-
},
|
|
1184
|
-
/**
|
|
1185
|
-
* hsv格式值转rgb值
|
|
1186
|
-
* @param {Object} hsv hsv值,数组
|
|
1187
|
-
*/
|
|
1188
|
-
hsv2rgb(hsv) {
|
|
1189
|
-
if (!Array.isArray(hsv) || hsv.length != 3) {
|
|
1190
|
-
throw new TypeError("Invalid argument");
|
|
1191
|
-
}
|
|
1192
|
-
let h = hsv[0] >= 360 || hsv[0] <= 0 ? 0 : hsv[0];
|
|
1193
|
-
let s = hsv[1] >= 100 ? 100 : hsv[1];
|
|
1194
|
-
s = s <= 0 ? 0 : s;
|
|
1195
|
-
let v = hsv[2] >= 100 ? 100 : hsv[2];
|
|
1196
|
-
v = v <= 0 ? 0 : v;
|
|
1197
|
-
s = s / 100;
|
|
1198
|
-
v = v / 100;
|
|
1199
|
-
let r2 = 0;
|
|
1200
|
-
let g = 0;
|
|
1201
|
-
let b = 0;
|
|
1202
|
-
let i = parseInt(h / 60 % 6 + "");
|
|
1203
|
-
let f = h / 60 - i;
|
|
1204
|
-
let p = v * (1 - s);
|
|
1205
|
-
let q = v * (1 - f * s);
|
|
1206
|
-
let t = v * (1 - (1 - f) * s);
|
|
1207
|
-
switch (i) {
|
|
1208
|
-
case 0:
|
|
1209
|
-
r2 = v;
|
|
1210
|
-
g = t;
|
|
1211
|
-
b = p;
|
|
1212
|
-
break;
|
|
1213
|
-
case 1:
|
|
1214
|
-
r2 = q;
|
|
1215
|
-
g = v;
|
|
1216
|
-
b = p;
|
|
1217
|
-
break;
|
|
1218
|
-
case 2:
|
|
1219
|
-
r2 = p;
|
|
1220
|
-
g = v;
|
|
1221
|
-
b = t;
|
|
1222
|
-
break;
|
|
1223
|
-
case 3:
|
|
1224
|
-
r2 = p;
|
|
1225
|
-
g = q;
|
|
1226
|
-
b = v;
|
|
1227
|
-
break;
|
|
1228
|
-
case 4:
|
|
1229
|
-
r2 = t;
|
|
1230
|
-
g = p;
|
|
1231
|
-
b = v;
|
|
1232
|
-
break;
|
|
1233
|
-
case 5:
|
|
1234
|
-
r2 = v;
|
|
1235
|
-
g = p;
|
|
1236
|
-
b = q;
|
|
1237
|
-
break;
|
|
1238
|
-
}
|
|
1239
|
-
r2 = parseInt(r2 * 255 + "");
|
|
1240
|
-
g = parseInt(g * 255 + "");
|
|
1241
|
-
b = parseInt(b * 255 + "");
|
|
1242
|
-
return [r2, g, b];
|
|
1243
|
-
},
|
|
1244
|
-
/**
|
|
1245
|
-
* rgb值转十六进制
|
|
1246
|
-
* @param {Array} rgb rgb值,数组
|
|
1247
|
-
*/
|
|
1248
|
-
rgb2hex(rgb) {
|
|
1249
|
-
if (!Array.isArray(rgb) || rgb.length != 3) {
|
|
1250
|
-
throw new TypeError("Invalid argument");
|
|
1251
|
-
}
|
|
1252
|
-
let r2 = rgb[0];
|
|
1253
|
-
let g = rgb[1];
|
|
1254
|
-
let b = rgb[2];
|
|
1255
|
-
let hex = "#" + ((1 << 24) + (r2 << 16) + (g << 8) + b).toString(16).slice(1);
|
|
1256
|
-
return hex;
|
|
1257
|
-
},
|
|
1258
|
-
/**
|
|
1259
|
-
* 十六进制颜色转rgb
|
|
1260
|
-
* @param {String} hex 十六进制颜色值
|
|
1261
|
-
*/
|
|
1262
|
-
hex2rgb(hex) {
|
|
1263
|
-
if (!hex || typeof hex != "string") {
|
|
1264
|
-
throw new TypeError("The argument must be a string");
|
|
1265
|
-
}
|
|
1266
|
-
let color2 = hex.toLowerCase();
|
|
1267
|
-
if (!common.matchingText(color2, "hex")) {
|
|
1268
|
-
throw new TypeError("The argument must be a hexadecimal color value");
|
|
1269
|
-
}
|
|
1270
|
-
if (color2.length === 4) {
|
|
1271
|
-
let colorNew = "#";
|
|
1272
|
-
for (let i = 1; i < 4; i += 1) {
|
|
1273
|
-
colorNew += color2.slice(i, i + 1).concat(color2.slice(i, i + 1));
|
|
1274
|
-
}
|
|
1275
|
-
color2 = colorNew;
|
|
1276
|
-
}
|
|
1277
|
-
let colorChange = [];
|
|
1278
|
-
for (let i = 1; i < 7; i += 2) {
|
|
1279
|
-
colorChange.push(parseInt("0x" + color2.slice(i, i + 2)));
|
|
1280
|
-
}
|
|
1281
|
-
return colorChange;
|
|
1282
|
-
}
|
|
1283
|
-
};
|
|
1284
1112
|
const file = {
|
|
1285
1113
|
/**
|
|
1286
1114
|
* 根据文件获取可预览的图片路径
|
|
1287
1115
|
* @param {Object} file
|
|
1288
1116
|
*/
|
|
1289
1117
|
getImageUrl(file2) {
|
|
1290
|
-
if (!file2 || !(file2 instanceof File)) {
|
|
1291
|
-
throw new TypeError("The argument must be a File object");
|
|
1292
|
-
}
|
|
1293
1118
|
return window.URL.createObjectURL(file2);
|
|
1294
1119
|
},
|
|
1295
1120
|
/**
|
|
@@ -1297,14 +1122,11 @@ const file = {
|
|
|
1297
1122
|
* @param {Object} file
|
|
1298
1123
|
*/
|
|
1299
1124
|
dataFileToBase64(file2) {
|
|
1300
|
-
return new Promise((resolve
|
|
1301
|
-
|
|
1302
|
-
reject(new TypeError("The argument must be a File object"));
|
|
1303
|
-
}
|
|
1304
|
-
let reader = new FileReader();
|
|
1125
|
+
return new Promise((resolve) => {
|
|
1126
|
+
const reader = new FileReader();
|
|
1305
1127
|
reader.readAsDataURL(file2);
|
|
1306
1128
|
reader.onloadend = () => {
|
|
1307
|
-
|
|
1129
|
+
const dataURL = reader.result;
|
|
1308
1130
|
resolve(dataURL);
|
|
1309
1131
|
};
|
|
1310
1132
|
});
|
|
@@ -1315,17 +1137,11 @@ const file = {
|
|
|
1315
1137
|
* @param {Object} fileName 转换后的文件名字,包含后缀
|
|
1316
1138
|
*/
|
|
1317
1139
|
dataBase64toFile(base64String, fileName) {
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
if (!fileName || typeof fileName != "string") {
|
|
1322
|
-
throw new TypeError("The second argument must be a string");
|
|
1323
|
-
}
|
|
1324
|
-
let arr = base64String.split(",");
|
|
1325
|
-
let mime = arr[0].match(/:(.*?);/)[1];
|
|
1326
|
-
let bstr = atob(arr[1]);
|
|
1140
|
+
const arr = base64String.split(",");
|
|
1141
|
+
const mime = arr[0].match(/:(.*?);/)[1];
|
|
1142
|
+
const bstr = atob(arr[1]);
|
|
1327
1143
|
let n = bstr.length;
|
|
1328
|
-
|
|
1144
|
+
const u8arr = new Uint8Array(n);
|
|
1329
1145
|
while (n--) {
|
|
1330
1146
|
u8arr[n] = bstr.charCodeAt(n);
|
|
1331
1147
|
}
|
|
@@ -1336,63 +1152,34 @@ const file = {
|
|
|
1336
1152
|
/**
|
|
1337
1153
|
* 图片压缩方法
|
|
1338
1154
|
* @param {*} file 需要压缩的图片File文件
|
|
1339
|
-
* @param {*}
|
|
1155
|
+
* @param {*} options 压缩参数
|
|
1340
1156
|
*/
|
|
1341
|
-
compressImage(file2,
|
|
1342
|
-
const options = {
|
|
1343
|
-
//压缩图片的宽,单位px,如果不设置默认为原图宽
|
|
1344
|
-
width: void 0,
|
|
1345
|
-
//压缩图片质量,默认为原图的0.8
|
|
1346
|
-
quality: 0.8,
|
|
1347
|
-
//图片类型,jpeg或者webp,默认为jpeg
|
|
1348
|
-
mimeType: "jpeg",
|
|
1349
|
-
//压缩后的最大值,单位kb,默认为0表示不设置此值
|
|
1350
|
-
maxSize: 0,
|
|
1351
|
-
//小于该大小的图片不进行压缩,单位kb,默认为0表示任何图片都要压缩
|
|
1352
|
-
minSize: 0
|
|
1353
|
-
};
|
|
1354
|
-
if (common.isObject(opts)) {
|
|
1355
|
-
if (number.isNumber(opts.width)) {
|
|
1356
|
-
options.width = opts.width;
|
|
1357
|
-
}
|
|
1358
|
-
if (number.isNumber(opts.quality) && opts.quality >= 0 && opts.quality <= 1) {
|
|
1359
|
-
options.quality = opts.quality;
|
|
1360
|
-
}
|
|
1361
|
-
if (opts.mimeType == "jpeg" || opts.mimeType == "webp") {
|
|
1362
|
-
options.mimeType = opts.mimeType;
|
|
1363
|
-
}
|
|
1364
|
-
if (number.isNumber(opts.maxSize)) {
|
|
1365
|
-
options.maxSize = opts.maxSize;
|
|
1366
|
-
}
|
|
1367
|
-
if (number.isNumber(opts.minSize)) {
|
|
1368
|
-
options.minSize = opts.minSize;
|
|
1369
|
-
}
|
|
1370
|
-
}
|
|
1157
|
+
compressImage(file2, options) {
|
|
1371
1158
|
const createFile = (canvas, fileName, quality) => {
|
|
1372
|
-
let url = canvas.toDataURL("image/" + options.mimeType, quality);
|
|
1373
|
-
let
|
|
1374
|
-
if (options.maxSize > 0 &&
|
|
1159
|
+
let url = canvas.toDataURL("image/" + (options.mimeType ?? "jpeg"), quality);
|
|
1160
|
+
let file3 = this.dataBase64toFile(url, fileName);
|
|
1161
|
+
if (options.maxSize && options.maxSize > 0 && file3.size > options.maxSize * 1024) {
|
|
1375
1162
|
quality = quality <= 0 ? 0 : Number((quality - 0.01).toFixed(2));
|
|
1376
1163
|
const res = createFile(canvas, fileName, quality);
|
|
1377
1164
|
url = res.url;
|
|
1378
|
-
|
|
1165
|
+
file3 = res.file;
|
|
1379
1166
|
quality = res.quality;
|
|
1380
1167
|
}
|
|
1381
1168
|
return {
|
|
1382
|
-
file:
|
|
1169
|
+
file: file3,
|
|
1383
1170
|
url,
|
|
1384
1171
|
quality
|
|
1385
1172
|
};
|
|
1386
1173
|
};
|
|
1387
1174
|
return new Promise((resolve, reject) => {
|
|
1388
|
-
|
|
1175
|
+
const reader = new FileReader();
|
|
1389
1176
|
reader.readAsDataURL(file2);
|
|
1390
1177
|
reader.onload = () => {
|
|
1391
|
-
|
|
1392
|
-
|
|
1178
|
+
const url = reader.result;
|
|
1179
|
+
const img = new Image();
|
|
1393
1180
|
img.src = url;
|
|
1394
1181
|
img.onload = () => {
|
|
1395
|
-
if (options.minSize > 0 && file2.size <= options.minSize * 1024) {
|
|
1182
|
+
if (options.minSize && options.minSize > 0 && file2.size <= options.minSize * 1024) {
|
|
1396
1183
|
resolve({
|
|
1397
1184
|
file: file2,
|
|
1398
1185
|
url,
|
|
@@ -1402,14 +1189,14 @@ const file = {
|
|
|
1402
1189
|
});
|
|
1403
1190
|
return;
|
|
1404
1191
|
}
|
|
1405
|
-
|
|
1406
|
-
|
|
1192
|
+
const canvas = document.createElement("canvas");
|
|
1193
|
+
const context = canvas.getContext("2d");
|
|
1407
1194
|
canvas.width = options.width || img.width;
|
|
1408
1195
|
canvas.height = options.width ? options.width / (img.width / img.height) : img.height;
|
|
1409
1196
|
context.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
1410
|
-
|
|
1411
|
-
const fileName = file2.name.substring(0,
|
|
1412
|
-
let res = createFile(canvas, fileName, options.quality);
|
|
1197
|
+
const index2 = file2.name.lastIndexOf(".");
|
|
1198
|
+
const fileName = file2.name.substring(0, index2) + "." + (options.mimeType ?? "jpeg");
|
|
1199
|
+
let res = createFile(canvas, fileName, options.quality ?? 0.8);
|
|
1413
1200
|
resolve({
|
|
1414
1201
|
...res,
|
|
1415
1202
|
width: canvas.width,
|
|
@@ -1429,7 +1216,7 @@ const file = {
|
|
|
1429
1216
|
const platform = {
|
|
1430
1217
|
//设备语言类型
|
|
1431
1218
|
language() {
|
|
1432
|
-
return window.navigator.
|
|
1219
|
+
return window.navigator.language;
|
|
1433
1220
|
},
|
|
1434
1221
|
/**
|
|
1435
1222
|
* 获取设备类型
|
|
@@ -1485,9 +1272,11 @@ const platform = {
|
|
|
1485
1272
|
const userAgent = window.navigator.userAgent;
|
|
1486
1273
|
if (userAgent.includes("Presto")) {
|
|
1487
1274
|
return "opera";
|
|
1488
|
-
}
|
|
1275
|
+
}
|
|
1276
|
+
if (userAgent.includes("AppleWebKit")) {
|
|
1489
1277
|
return "webkit";
|
|
1490
|
-
}
|
|
1278
|
+
}
|
|
1279
|
+
if (userAgent.includes("Gecko") && !userAgent.includes("KHTML")) {
|
|
1491
1280
|
return "gecko";
|
|
1492
1281
|
}
|
|
1493
1282
|
return "";
|