index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { __awaiter, __generator } from "tslib";
  2. import { RateDefaultProps } from './props';
  3. import createValue from '../mixins/value';
  4. import '../_util/assert-component2';
  5. function getBoundingClientRect(selector) {
  6. return new Promise(function (resolve, reject) {
  7. my.createSelectorQuery()
  8. .select(selector)
  9. .boundingClientRect()
  10. .exec(function (ret) {
  11. if (ret && ret[0]) {
  12. resolve(ret[0]);
  13. return;
  14. }
  15. reject();
  16. });
  17. });
  18. }
  19. Component({
  20. props: RateDefaultProps,
  21. mixins: [
  22. createValue({
  23. transformValue: function (value) {
  24. if (this.props.allowHalf) {
  25. return {
  26. needUpdate: true,
  27. value: value % 0.5 !== 0 ? Math.round(value) : value,
  28. };
  29. }
  30. return {
  31. needUpdate: true,
  32. value: Math.ceil(value),
  33. };
  34. },
  35. }),
  36. ],
  37. methods: {
  38. handleStarTap: function (e) {
  39. return __awaiter(this, void 0, void 0, function () {
  40. var clientX, startTapRate, rate;
  41. return __generator(this, function (_a) {
  42. switch (_a.label) {
  43. case 0:
  44. if (this.props.readonly) {
  45. return [2 /*return*/];
  46. }
  47. clientX = e.detail.clientX;
  48. startTapRate = this.getValue();
  49. return [4 /*yield*/, this.updateRate(clientX)];
  50. case 1:
  51. rate = _a.sent();
  52. if (startTapRate === rate && this.props.allowClear) {
  53. rate = 0;
  54. }
  55. if (!this.isControlled()) {
  56. this.update(rate);
  57. }
  58. if (startTapRate !== rate && this.props.onChange) {
  59. this.props.onChange(rate);
  60. }
  61. return [2 /*return*/];
  62. }
  63. });
  64. });
  65. },
  66. startMoveRate: undefined,
  67. handleStarMove: function (e) {
  68. return __awaiter(this, void 0, void 0, function () {
  69. var touches, clientX, rate;
  70. return __generator(this, function (_a) {
  71. switch (_a.label) {
  72. case 0:
  73. if (this.props.readonly) {
  74. return [2 /*return*/];
  75. }
  76. touches = e.touches;
  77. clientX = touches[0].clientX;
  78. if (typeof this.startMoveRate === 'undefined') {
  79. this.startMoveRate = this.getValue();
  80. }
  81. return [4 /*yield*/, this.updateRate(clientX)];
  82. case 1:
  83. rate = _a.sent();
  84. this.update(rate);
  85. return [2 /*return*/];
  86. }
  87. });
  88. });
  89. },
  90. handleStarMoveEnd: function () {
  91. if (this.props.readonly) {
  92. return;
  93. }
  94. if (typeof this.startMoveRate === 'undefined') {
  95. return;
  96. }
  97. var startMoveRate = this.startMoveRate;
  98. this.startMoveRate = undefined;
  99. var rate = this.getValue();
  100. if (this.isControlled()) {
  101. this.update(startMoveRate);
  102. }
  103. if (startMoveRate !== rate && this.props.onChange) {
  104. this.props.onChange(rate);
  105. }
  106. },
  107. updateRate: function (clientX) {
  108. return __awaiter(this, void 0, void 0, function () {
  109. var _a, gutter, count, _b, left, width, halfRateWidth, num, halfRateCount, val, rate;
  110. return __generator(this, function (_c) {
  111. switch (_c.label) {
  112. case 0:
  113. _a = this.props, gutter = _a.gutter, count = _a.count;
  114. return [4 /*yield*/, getBoundingClientRect("#ant-rate-container-".concat(this.$id))];
  115. case 1:
  116. _b = _c.sent(), left = _b.left, width = _b.width;
  117. halfRateWidth = (width - (count - 1) * gutter) / count / 2;
  118. num = clientX - left;
  119. halfRateCount = 0;
  120. /* eslint-disable no-constant-condition */
  121. while (true) {
  122. val = halfRateWidth * halfRateCount +
  123. gutter * Math.floor(halfRateCount / 2);
  124. if (halfRateCount >= count * 2 || num <= val) {
  125. break;
  126. }
  127. halfRateCount++;
  128. }
  129. rate = this.props.allowHalf
  130. ? halfRateCount * 0.5
  131. : Math.ceil(halfRateCount * 0.5);
  132. return [2 /*return*/, rate];
  133. }
  134. });
  135. });
  136. },
  137. },
  138. });