index.js 3.0 KB

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