utils.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getCurrentPage = exports.toPromise = exports.groupSetData = exports.getAllRect = exports.getRect = exports.pickExclude = exports.requestAnimationFrame = exports.addUnit = exports.getSystemInfoSync = exports.nextTick = exports.range = exports.isDef = void 0;
  4. var validator_1 = require("./validator");
  5. var version_1 = require("./version");
  6. var validator_2 = require("./validator");
  7. Object.defineProperty(exports, "isDef", { enumerable: true, get: function () { return validator_2.isDef; } });
  8. function range(num, min, max) {
  9. return Math.min(Math.max(num, min), max);
  10. }
  11. exports.range = range;
  12. function nextTick(cb) {
  13. if (version_1.canIUseNextTick()) {
  14. wx.nextTick(cb);
  15. }
  16. else {
  17. setTimeout(function () {
  18. cb();
  19. }, 1000 / 30);
  20. }
  21. }
  22. exports.nextTick = nextTick;
  23. var systemInfo;
  24. function getSystemInfoSync() {
  25. if (systemInfo == null) {
  26. systemInfo = wx.getSystemInfoSync();
  27. }
  28. return systemInfo;
  29. }
  30. exports.getSystemInfoSync = getSystemInfoSync;
  31. function addUnit(value) {
  32. if (!validator_1.isDef(value)) {
  33. return undefined;
  34. }
  35. value = String(value);
  36. return validator_1.isNumber(value) ? value + "px" : value;
  37. }
  38. exports.addUnit = addUnit;
  39. function requestAnimationFrame(cb) {
  40. var systemInfo = getSystemInfoSync();
  41. if (systemInfo.platform === 'devtools') {
  42. return setTimeout(function () {
  43. cb();
  44. }, 1000 / 30);
  45. }
  46. return wx
  47. .createSelectorQuery()
  48. .selectViewport()
  49. .boundingClientRect()
  50. .exec(function () {
  51. cb();
  52. });
  53. }
  54. exports.requestAnimationFrame = requestAnimationFrame;
  55. function pickExclude(obj, keys) {
  56. if (!validator_1.isPlainObject(obj)) {
  57. return {};
  58. }
  59. return Object.keys(obj).reduce(function (prev, key) {
  60. if (!keys.includes(key)) {
  61. prev[key] = obj[key];
  62. }
  63. return prev;
  64. }, {});
  65. }
  66. exports.pickExclude = pickExclude;
  67. function getRect(context, selector) {
  68. return new Promise(function (resolve) {
  69. wx.createSelectorQuery()
  70. .in(context)
  71. .select(selector)
  72. .boundingClientRect()
  73. .exec(function (rect) {
  74. if (rect === void 0) { rect = []; }
  75. return resolve(rect[0]);
  76. });
  77. });
  78. }
  79. exports.getRect = getRect;
  80. function getAllRect(context, selector) {
  81. return new Promise(function (resolve) {
  82. wx.createSelectorQuery()
  83. .in(context)
  84. .selectAll(selector)
  85. .boundingClientRect()
  86. .exec(function (rect) {
  87. if (rect === void 0) { rect = []; }
  88. return resolve(rect[0]);
  89. });
  90. });
  91. }
  92. exports.getAllRect = getAllRect;
  93. function groupSetData(context, cb) {
  94. if (version_1.canIUseGroupSetData()) {
  95. context.groupSetData(cb);
  96. }
  97. else {
  98. cb();
  99. }
  100. }
  101. exports.groupSetData = groupSetData;
  102. function toPromise(promiseLike) {
  103. if (validator_1.isPromise(promiseLike)) {
  104. return promiseLike;
  105. }
  106. return Promise.resolve(promiseLike);
  107. }
  108. exports.toPromise = toPromise;
  109. function getCurrentPage() {
  110. var pages = getCurrentPages();
  111. return pages[pages.length - 1];
  112. }
  113. exports.getCurrentPage = getCurrentPage;