index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { use } from '../utils';
  2. import { TouchMixin } from '../mixins/touch';
  3. import { preventDefault } from '../utils/event';
  4. var _use = use('slider'),
  5. sfc = _use[0],
  6. bem = _use[1];
  7. export default sfc({
  8. mixins: [TouchMixin],
  9. props: {
  10. min: Number,
  11. value: Number,
  12. disabled: Boolean,
  13. vertical: Boolean,
  14. activeColor: String,
  15. inactiveColor: String,
  16. max: {
  17. type: Number,
  18. "default": 100
  19. },
  20. step: {
  21. type: Number,
  22. "default": 1
  23. },
  24. barHeight: {
  25. type: String,
  26. "default": '2px'
  27. }
  28. },
  29. methods: {
  30. onTouchStart: function onTouchStart(event) {
  31. if (this.disabled) return;
  32. this.touchStart(event);
  33. this.startValue = this.format(this.value);
  34. },
  35. onTouchMove: function onTouchMove(event) {
  36. preventDefault(event, true);
  37. if (this.disabled) return;
  38. this.touchMove(event);
  39. var rect = this.$el.getBoundingClientRect();
  40. var delta = this.vertical ? this.deltaY : this.deltaX;
  41. var total = this.vertical ? rect.height : rect.width;
  42. var diff = delta / total * 100;
  43. this.newValue = this.startValue + diff;
  44. this.updateValue(this.newValue);
  45. },
  46. onTouchEnd: function onTouchEnd() {
  47. if (this.disabled) return;
  48. this.updateValue(this.newValue, true);
  49. },
  50. onClick: function onClick(event) {
  51. event.stopPropagation();
  52. if (this.disabled) return;
  53. var rect = this.$el.getBoundingClientRect();
  54. var delta = this.vertical ? event.clientY - rect.top : event.clientX - rect.left;
  55. var total = this.vertical ? rect.height : rect.width;
  56. var value = delta / total * 100;
  57. this.updateValue(value, true);
  58. },
  59. updateValue: function updateValue(value, end) {
  60. value = this.format(value);
  61. this.$emit('input', value);
  62. if (end) {
  63. this.$emit('change', value);
  64. }
  65. },
  66. format: function format(value) {
  67. return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step;
  68. }
  69. },
  70. render: function render(h) {
  71. var _barStyle;
  72. var vertical = this.vertical;
  73. var style = {
  74. background: this.inactiveColor
  75. };
  76. var mainAxis = vertical ? 'height' : 'width';
  77. var crossAxis = vertical ? 'width' : 'height';
  78. var barStyle = (_barStyle = {}, _barStyle[mainAxis] = this.format(this.value) + "%", _barStyle[crossAxis] = this.barHeight, _barStyle.background = this.activeColor, _barStyle);
  79. return h("div", {
  80. "style": style,
  81. "class": bem({
  82. disabled: this.disabled,
  83. vertical: vertical
  84. }),
  85. "on": {
  86. "click": this.onClick
  87. }
  88. }, [h("div", {
  89. "class": bem('bar'),
  90. "style": barStyle
  91. }, [h("div", {
  92. "class": bem('button-wrapper'),
  93. "on": {
  94. "touchstart": this.onTouchStart,
  95. "touchmove": this.onTouchMove,
  96. "touchend": this.onTouchEnd,
  97. "touchcancel": this.onTouchEnd
  98. }
  99. }, [this.slots('button') || h("div", {
  100. "class": bem('button')
  101. })])])]);
  102. }
  103. });