utils.js 2.9 KB

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