index.js 6.0 KB

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