utils.js
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.getAllRect = exports.getRect = exports.pickExclude = exports.requestAnimationFrame = exports.addUnit = exports.getSystemInfoSync = exports.nextTick = exports.range = exports.isObj = exports.isDef = void 0;
var validator_1 = require('./validator');
function isDef(value) {
return value !== undefined && value !== null;
}
exports.isDef = isDef;
function isObj(x) {
var type = typeof x;
return x !== null && (type === 'object' || type === 'function');
}
exports.isObj = isObj;
function range(num, min, max) {
return Math.min(Math.max(num, min), max);
}
exports.range = range;
function nextTick(fn) {
setTimeout(function () {
fn();
}, 1000 / 30);
}
exports.nextTick = nextTick;
var systemInfo;
function getSystemInfoSync() {
if (systemInfo == null) {
systemInfo = wx.getSystemInfoSync();
}
return systemInfo;
}
exports.getSystemInfoSync = getSystemInfoSync;
function addUnit(value) {
if (!isDef(value)) {
return undefined;
}
value = String(value);
return validator_1.isNumber(value) ? value + 'px' : value;
}
exports.addUnit = addUnit;
function requestAnimationFrame(cb) {
var systemInfo = getSystemInfoSync();
if (systemInfo.platform === 'devtools') {
return nextTick(cb);
}
return wx
.createSelectorQuery()
.selectViewport()
.boundingClientRect()
.exec(function () {
cb();
});
}
exports.requestAnimationFrame = requestAnimationFrame;
function pickExclude(obj, keys) {
if (!validator_1.isPlainObject(obj)) {
return {};
}
return Object.keys(obj).reduce(function (prev, key) {
if (!keys.includes(key)) {
prev[key] = obj[key];
}
return prev;
}, {});
}
exports.pickExclude = pickExclude;
function getRect(selector) {
var _this = this;
return new Promise(function (resolve) {
wx.createSelectorQuery()
.in(_this)
.select(selector)
.boundingClientRect()
.exec(function (rect) {
if (rect === void 0) {
rect = [];
}
return resolve(rect[0]);
});
});
}
exports.getRect = getRect;
function getAllRect(selector) {
var _this = this;
return new Promise(function (resolve) {
wx.createSelectorQuery()
.in(_this)
.selectAll(selector)
.boundingClientRect()
.exec(function (rect) {
if (rect === void 0) {
rect = [];
}
return resolve(rect[0]);
});
});
}
exports.getAllRect = getAllRect;