index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 utils_1 = require("../common/utils");
  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: function (value) {
  29. this.updateValue(value, false);
  30. }
  31. },
  32. barHeight: {
  33. type: null,
  34. value: '2px'
  35. }
  36. },
  37. created: function () {
  38. this.updateValue(this.data.value);
  39. },
  40. methods: {
  41. onTouchStart: function (event) {
  42. if (this.data.disabled)
  43. return;
  44. this.touchStart(event);
  45. this.startValue = this.format(this.data.value);
  46. this.dragStatus = 'start';
  47. },
  48. onTouchMove: function (event) {
  49. var _this = this;
  50. if (this.data.disabled)
  51. return;
  52. if (this.dragStatus === 'start') {
  53. this.$emit('drag-start');
  54. }
  55. this.touchMove(event);
  56. this.dragStatus = 'draging';
  57. this.getRect('.van-slider').then(function (rect) {
  58. var diff = (_this.deltaX / rect.width) * 100;
  59. _this.newValue = _this.startValue + diff;
  60. _this.updateValue(_this.newValue, false, true);
  61. });
  62. },
  63. onTouchEnd: function () {
  64. if (this.data.disabled)
  65. return;
  66. if (this.dragStatus === 'draging') {
  67. this.updateValue(this.newValue, true);
  68. this.$emit('drag-end');
  69. }
  70. },
  71. onClick: function (event) {
  72. var _this = this;
  73. if (this.data.disabled)
  74. return;
  75. var min = this.data.min;
  76. this.getRect('.van-slider').then(function (rect) {
  77. var value = ((event.detail.x - rect.left) / rect.width) * _this.getRange() + min;
  78. _this.updateValue(value, true);
  79. });
  80. },
  81. updateValue: function (value, end, drag) {
  82. value = this.format(value);
  83. var _a = this.data, barHeight = _a.barHeight, min = _a.min;
  84. var width = ((value - min) * 100) / this.getRange() + "%";
  85. this.setData({
  86. value: value,
  87. barStyle: "\n width: " + width + ";\n height: " + utils_1.addUnit(barHeight) + ";\n " + (drag ? 'transition: none;' : '') + "\n ",
  88. });
  89. if (drag) {
  90. this.$emit('drag', { value: value });
  91. }
  92. if (end) {
  93. this.$emit('change', value);
  94. }
  95. },
  96. getRange: function () {
  97. var _a = this.data, max = _a.max, min = _a.min;
  98. return max - min;
  99. },
  100. format: function (value) {
  101. var _a = this.data, max = _a.max, min = _a.min, step = _a.step;
  102. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  103. }
  104. }
  105. });