index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. component_1.VantComponent({
  7. mixins: [touch_1.touch],
  8. props: {
  9. disabled: Boolean,
  10. useButtonSlot: Boolean,
  11. activeColor: String,
  12. inactiveColor: String,
  13. max: {
  14. type: Number,
  15. value: 100,
  16. },
  17. min: {
  18. type: Number,
  19. value: 0,
  20. },
  21. step: {
  22. type: Number,
  23. value: 1,
  24. },
  25. value: {
  26. type: Number,
  27. value: 0,
  28. observer: 'updateValue',
  29. },
  30. barHeight: {
  31. type: null,
  32. value: '2px',
  33. },
  34. },
  35. created: function () {
  36. this.updateValue(this.data.value);
  37. },
  38. methods: {
  39. onTouchStart: function (event) {
  40. if (this.data.disabled) return;
  41. this.touchStart(event);
  42. this.startValue = this.format(this.data.value);
  43. this.dragStatus = 'start';
  44. },
  45. onTouchMove: function (event) {
  46. var _this = this;
  47. if (this.data.disabled) return;
  48. if (this.dragStatus === 'start') {
  49. this.$emit('drag-start');
  50. }
  51. this.touchMove(event);
  52. this.dragStatus = 'draging';
  53. this.getRect('.van-slider').then(function (rect) {
  54. var diff = (_this.deltaX / rect.width) * 100;
  55. _this.newValue = _this.startValue + diff;
  56. _this.updateValue(_this.newValue, false, true);
  57. });
  58. },
  59. onTouchEnd: function () {
  60. if (this.data.disabled) return;
  61. if (this.dragStatus === 'draging') {
  62. this.updateValue(this.newValue, true);
  63. this.$emit('drag-end');
  64. }
  65. },
  66. onClick: function (event) {
  67. var _this = this;
  68. if (this.data.disabled) return;
  69. var min = this.data.min;
  70. this.getRect('.van-slider').then(function (rect) {
  71. var value =
  72. ((event.detail.x - rect.left) / rect.width) * _this.getRange() + min;
  73. _this.updateValue(value, true);
  74. });
  75. },
  76. updateValue: function (value, end, drag) {
  77. value = this.format(value);
  78. var min = this.data.min;
  79. var width = ((value - min) * 100) / this.getRange() + '%';
  80. this.setData({
  81. value: value,
  82. barStyle:
  83. '\n width: ' +
  84. width +
  85. ';\n ' +
  86. (drag ? 'transition: none;' : '') +
  87. '\n ',
  88. });
  89. if (drag) {
  90. this.$emit('drag', { value: value });
  91. }
  92. if (end) {
  93. this.$emit('change', value);
  94. }
  95. if ((drag || end) && version_1.canIUseModel()) {
  96. this.setData({ value: value });
  97. }
  98. },
  99. getRange: function () {
  100. var _a = this.data,
  101. max = _a.max,
  102. min = _a.min;
  103. return max - min;
  104. },
  105. format: function (value) {
  106. var _a = this.data,
  107. max = _a.max,
  108. min = _a.min,
  109. step = _a.step;
  110. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  111. },
  112. },
  113. });