utils.js 3.1 KB

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