index.js 3.0 KB

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