index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. var touch_1 = require("../mixins/touch");
  5. var version_1 = require("../common/version");
  6. var utils_1 = require("../common/utils");
  7. var DRAG_STATUS = {
  8. START: 'start',
  9. MOVING: 'moving',
  10. END: 'end',
  11. };
  12. (0, component_1.VantComponent)({
  13. mixins: [touch_1.touch],
  14. props: {
  15. range: Boolean,
  16. disabled: Boolean,
  17. useButtonSlot: Boolean,
  18. activeColor: String,
  19. inactiveColor: String,
  20. max: {
  21. type: Number,
  22. value: 100,
  23. },
  24. min: {
  25. type: Number,
  26. value: 0,
  27. },
  28. step: {
  29. type: Number,
  30. value: 1,
  31. },
  32. value: {
  33. type: null,
  34. value: 0,
  35. observer: function (val) {
  36. if (val !== this.value) {
  37. this.updateValue(val);
  38. }
  39. },
  40. },
  41. vertical: Boolean,
  42. barHeight: null,
  43. },
  44. created: function () {
  45. this.updateValue(this.data.value);
  46. },
  47. methods: {
  48. onTouchStart: function (event) {
  49. var _this = this;
  50. if (this.data.disabled)
  51. return;
  52. var index = event.currentTarget.dataset.index;
  53. if (typeof index === 'number') {
  54. this.buttonIndex = index;
  55. }
  56. this.touchStart(event);
  57. this.startValue = this.format(this.value);
  58. this.newValue = this.value;
  59. if (this.isRange(this.newValue)) {
  60. this.startValue = this.newValue.map(function (val) { return _this.format(val); });
  61. }
  62. else {
  63. this.startValue = this.format(this.newValue);
  64. }
  65. this.dragStatus = DRAG_STATUS.START;
  66. },
  67. onTouchMove: function (event) {
  68. var _this = this;
  69. if (this.data.disabled)
  70. return;
  71. if (this.dragStatus === DRAG_STATUS.START) {
  72. this.$emit('drag-start');
  73. }
  74. this.touchMove(event);
  75. this.dragStatus = DRAG_STATUS.MOVING;
  76. (0, utils_1.getRect)(this, '.van-slider').then(function (rect) {
  77. var vertical = _this.data.vertical;
  78. var delta = vertical ? _this.deltaY : _this.deltaX;
  79. var total = vertical ? rect.height : rect.width;
  80. var diff = (delta / total) * _this.getRange();
  81. if (_this.isRange(_this.startValue)) {
  82. _this.newValue[_this.buttonIndex] =
  83. _this.startValue[_this.buttonIndex] + diff;
  84. }
  85. else {
  86. _this.newValue = _this.startValue + diff;
  87. }
  88. _this.updateValue(_this.newValue, false, true);
  89. });
  90. },
  91. onTouchEnd: function () {
  92. var _this = this;
  93. if (this.data.disabled)
  94. return;
  95. if (this.dragStatus === DRAG_STATUS.MOVING) {
  96. this.dragStatus = DRAG_STATUS.END;
  97. (0, utils_1.nextTick)(function () {
  98. _this.updateValue(_this.newValue, true);
  99. _this.$emit('drag-end');
  100. });
  101. }
  102. },
  103. onClick: function (event) {
  104. var _this = this;
  105. if (this.data.disabled)
  106. return;
  107. var min = this.data.min;
  108. (0, utils_1.getRect)(this, '.van-slider').then(function (rect) {
  109. var vertical = _this.data.vertical;
  110. var touch = event.touches[0];
  111. var delta = vertical
  112. ? touch.clientY - rect.top
  113. : touch.clientX - rect.left;
  114. var total = vertical ? rect.height : rect.width;
  115. var value = Number(min) + (delta / total) * _this.getRange();
  116. if (_this.isRange(_this.value)) {
  117. var _a = _this.value, left = _a[0], right = _a[1];
  118. var middle = (left + right) / 2;
  119. if (value <= middle) {
  120. _this.updateValue([value, right], true);
  121. }
  122. else {
  123. _this.updateValue([left, value], true);
  124. }
  125. }
  126. else {
  127. _this.updateValue(value, true);
  128. }
  129. });
  130. },
  131. isRange: function (val) {
  132. var range = this.data.range;
  133. return range && Array.isArray(val);
  134. },
  135. handleOverlap: function (value) {
  136. if (value[0] > value[1]) {
  137. return value.slice(0).reverse();
  138. }
  139. return value;
  140. },
  141. updateValue: function (value, end, drag) {
  142. var _this = this;
  143. if (this.isRange(value)) {
  144. value = this.handleOverlap(value).map(function (val) { return _this.format(val); });
  145. }
  146. else {
  147. value = this.format(value);
  148. }
  149. this.value = value;
  150. var vertical = this.data.vertical;
  151. var mainAxis = vertical ? 'height' : 'width';
  152. this.setData({
  153. wrapperStyle: "\n background: ".concat(this.data.inactiveColor || '', ";\n ").concat(vertical ? 'width' : 'height', ": ").concat((0, utils_1.addUnit)(this.data.barHeight) || '', ";\n "),
  154. barStyle: "\n ".concat(mainAxis, ": ").concat(this.calcMainAxis(), ";\n left: ").concat(vertical ? 0 : this.calcOffset(), ";\n top: ").concat(vertical ? this.calcOffset() : 0, ";\n ").concat(drag ? 'transition: none;' : '', "\n "),
  155. });
  156. if (drag) {
  157. this.$emit('drag', { value: value });
  158. }
  159. if (end) {
  160. this.$emit('change', value);
  161. }
  162. if ((drag || end) && (0, version_1.canIUseModel)()) {
  163. this.setData({ value: value });
  164. }
  165. },
  166. getScope: function () {
  167. return Number(this.data.max) - Number(this.data.min);
  168. },
  169. getRange: function () {
  170. var _a = this.data, max = _a.max, min = _a.min;
  171. return max - min;
  172. },
  173. getOffsetWidth: function (current, min) {
  174. var scope = this.getScope();
  175. // 避免最小值小于最小step时出现负数情况
  176. return "".concat(Math.max(((current - min) * 100) / scope, 0), "%");
  177. },
  178. // 计算选中条的长度百分比
  179. calcMainAxis: function () {
  180. var value = this.value;
  181. var min = this.data.min;
  182. if (this.isRange(value)) {
  183. return this.getOffsetWidth(value[1], value[0]);
  184. }
  185. return this.getOffsetWidth(value, Number(min));
  186. },
  187. // 计算选中条的开始位置的偏移量
  188. calcOffset: function () {
  189. var value = this.value;
  190. var min = this.data.min;
  191. var scope = this.getScope();
  192. if (this.isRange(value)) {
  193. return "".concat(((value[0] - Number(min)) * 100) / scope, "%");
  194. }
  195. return '0%';
  196. },
  197. format: function (value) {
  198. var min = +this.data.min;
  199. var max = +this.data.max;
  200. var step = +this.data.step;
  201. value = (0, utils_1.clamp)(value, min, max);
  202. var diff = Math.round((value - min) / step) * step;
  203. return (0, utils_1.addNumber)(min, diff);
  204. },
  205. },
  206. });