controller.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { __awaiter, __generator } from "tslib";
  2. import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
  3. var SliderController = /** @class */ (function () {
  4. function SliderController(_value, _props) {
  5. this._value = _value;
  6. this._props = _props;
  7. this.id = 0;
  8. this.valueId = 0;
  9. this._callback = null;
  10. this._moveStatus = null;
  11. }
  12. Object.defineProperty(SliderController.prototype, "value", {
  13. get: function () {
  14. return this._value;
  15. },
  16. enumerable: false,
  17. configurable: true
  18. });
  19. Object.defineProperty(SliderController.prototype, "props", {
  20. get: function () {
  21. return this._props;
  22. },
  23. enumerable: false,
  24. configurable: true
  25. });
  26. SliderController.prototype.handleMove = function (component, e, type) {
  27. var _this = this;
  28. if (this.props.disabled) {
  29. return;
  30. }
  31. var currentId = this.getId();
  32. this.getRect(component, e).then(function (res) {
  33. var _a = _this.getValue(res, type), value = _a.value, moveStatus = _a.moveStatus;
  34. var formatValue = _this.formatValue(value);
  35. _this.fireChange(currentId, formatValue, moveStatus, type, e);
  36. });
  37. };
  38. SliderController.prototype.fireChange = function (id, value, moveStatus, type, event) {
  39. if (id < this.valueId) {
  40. return;
  41. }
  42. if (this._callback) {
  43. var changed = !this.isSliderValueEqual(this._value, value);
  44. var moveStatusChanged = this.isMoveStatusChanged(this._moveStatus, moveStatus);
  45. this._value = value;
  46. this.valueId = id;
  47. if (changed || moveStatusChanged) {
  48. this._callback(value, moveStatus, {
  49. valueChange: changed,
  50. moveStatusChange: moveStatusChanged,
  51. type: type,
  52. event: event,
  53. });
  54. }
  55. }
  56. };
  57. SliderController.prototype.isMoveStatusChanged = function (value1, value2) {
  58. if (value1 === value2) {
  59. return false;
  60. }
  61. if (!value1 || !value2) {
  62. return true;
  63. }
  64. if (value1.changingStart !== value2.changingStart) {
  65. return true;
  66. }
  67. if (value1.changingEnd !== value2.changingEnd) {
  68. return true;
  69. }
  70. return false;
  71. };
  72. SliderController.prototype.isSliderValueEqual = function (value1, value2) {
  73. if (value1 === value2) {
  74. return true;
  75. }
  76. if (value1 === undefined || value2 === undefined) {
  77. return false;
  78. }
  79. if (typeof value1 === 'number' || typeof value2 == 'number') {
  80. return value1 === value2;
  81. }
  82. if (value1[0] === value2[0] && value1[1] === value2[1]) {
  83. return true;
  84. }
  85. return false;
  86. };
  87. SliderController.prototype.getId = function () {
  88. var id = this.id;
  89. this.id = this.id + 1;
  90. return id;
  91. };
  92. SliderController.prototype.getRect = function (component, e) {
  93. return __awaiter(this, void 0, void 0, function () {
  94. var elementId, instance, element, touch;
  95. return __generator(this, function (_a) {
  96. switch (_a.label) {
  97. case 0:
  98. elementId = e.currentTarget.id;
  99. instance = component;
  100. instance = my;
  101. return [4 /*yield*/, getInstanceBoundingClientRect(instance, "#".concat(elementId))];
  102. case 1:
  103. element = _a.sent();
  104. touch = e.changedTouches[0];
  105. if (element) {
  106. return [2 /*return*/, {
  107. touch: {
  108. pageX: touch.pageX,
  109. },
  110. element: {
  111. left: element.left,
  112. width: element.width,
  113. },
  114. }];
  115. }
  116. return [2 /*return*/];
  117. }
  118. });
  119. });
  120. };
  121. SliderController.prototype.fitSliderValue = function (value, min, max, isRange) {
  122. if (value === undefined || value === null) {
  123. if (isRange) {
  124. return [min, min];
  125. }
  126. else {
  127. return min !== null && min !== void 0 ? min : 0;
  128. }
  129. }
  130. if (typeof value === 'number') {
  131. if (value > max) {
  132. return max;
  133. }
  134. if (value < min) {
  135. return min;
  136. }
  137. return value;
  138. }
  139. var leftValue = Math.min(value[0], value[1]);
  140. var rightValue = Math.max(value[0], value[1]);
  141. return [Math.max(min, leftValue), Math.min(max, rightValue)];
  142. };
  143. SliderController.prototype.getValue = function (rect, type) {
  144. var touchPosition = (rect.touch.pageX - rect.element.left) / rect.element.width;
  145. var props = this.props;
  146. var currentValue = this.value;
  147. var value = props.min + touchPosition * (props.max - props.min);
  148. if (!props.range) {
  149. return {
  150. value: value,
  151. moveStatus: type === 'end'
  152. ? {
  153. changingEnd: false,
  154. changingStart: false,
  155. }
  156. : {
  157. changingEnd: true,
  158. },
  159. };
  160. }
  161. else {
  162. var leftValue = currentValue[0];
  163. var rightValue = currentValue[1];
  164. var leftDistance = Math.abs(leftValue - value);
  165. var rightDistance = Math.abs(rightValue - value);
  166. var isFarFromLeft = leftDistance > rightDistance;
  167. var farValue = isFarFromLeft ? leftValue : rightValue;
  168. return {
  169. value: [value, farValue],
  170. moveStatus: type === 'end'
  171. ? {
  172. changingEnd: false,
  173. changingStart: false,
  174. }
  175. : isFarFromLeft
  176. ? {
  177. changingEnd: true,
  178. }
  179. : {
  180. changingStart: true,
  181. },
  182. };
  183. }
  184. };
  185. SliderController.prototype.formatValue = function (val) {
  186. var props = this.props;
  187. var value = this.fitSliderValue(val, props.min, props.max, props.range);
  188. value = this.getRoundedValue(value, props.step);
  189. return value;
  190. };
  191. SliderController.prototype.getRoundedValue = function (value, step) {
  192. if (step === void 0) { step = 1; }
  193. if (value === undefined) {
  194. return 0;
  195. }
  196. if (typeof value === 'number') {
  197. return Math.round(value / step) * step;
  198. }
  199. return [
  200. Math.round(value[0] / step) * step,
  201. Math.round(value[1] / step) * step,
  202. ];
  203. };
  204. SliderController.prototype.updateProps = function (props) {
  205. this._props = props;
  206. };
  207. SliderController.prototype.updateValue = function (value) {
  208. this._value = value;
  209. };
  210. SliderController.prototype.updateMoveStatus = function (moveStatus) {
  211. this._moveStatus = moveStatus;
  212. };
  213. SliderController.prototype.onChange = function (callback) {
  214. this._callback = callback;
  215. };
  216. return SliderController;
  217. }());
  218. export { SliderController };