utils.js 2.9 KB

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