index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. range: Boolean,
  11. disabled: Boolean,
  12. useButtonSlot: Boolean,
  13. activeColor: String,
  14. inactiveColor: String,
  15. max: {
  16. type: Number,
  17. value: 100,
  18. },
  19. min: {
  20. type: Number,
  21. value: 0,
  22. },
  23. step: {
  24. type: Number,
  25. value: 1,
  26. },
  27. value: {
  28. type: Number,
  29. value: 0,
  30. optionalTypes: [Array],
  31. observer: function (val) {
  32. if (val !== this.value) {
  33. this.updateValue(val);
  34. }
  35. },
  36. },
  37. barHeight: null,
  38. },
  39. created: function () {
  40. this.updateValue(this.data.value);
  41. },
  42. methods: {
  43. onTouchStart: function (event) {
  44. var _this = this;
  45. if (this.data.disabled) return;
  46. var index = event.currentTarget.dataset.index;
  47. if (typeof index === 'number') {
  48. this.buttonIndex = index;
  49. }
  50. this.touchStart(event);
  51. this.startValue = this.format(this.value);
  52. this.newValue = this.value;
  53. if (this.isRange(this.newValue)) {
  54. this.startValue = this.newValue.map(function (val) {
  55. return _this.format(val);
  56. });
  57. } else {
  58. this.startValue = this.format(this.newValue);
  59. }
  60. this.dragStatus = 'start';
  61. },
  62. onTouchMove: function (event) {
  63. var _this = this;
  64. if (this.data.disabled) return;
  65. if (this.dragStatus === 'start') {
  66. this.$emit('drag-start');
  67. }
  68. this.touchMove(event);
  69. this.dragStatus = 'draging';
  70. utils_1.getRect(this, '.van-slider').then(function (rect) {
  71. var diff = (_this.deltaX / rect.width) * _this.getRange();
  72. if (_this.isRange(_this.startValue)) {
  73. _this.newValue[_this.buttonIndex] =
  74. _this.startValue[_this.buttonIndex] + diff;
  75. } else {
  76. _this.newValue = _this.startValue + diff;
  77. }
  78. _this.updateValue(_this.newValue, false, true);
  79. });
  80. },
  81. onTouchEnd: function () {
  82. if (this.data.disabled) return;
  83. if (this.dragStatus === 'draging') {
  84. this.updateValue(this.newValue, true);
  85. this.$emit('drag-end');
  86. }
  87. },
  88. onClick: function (event) {
  89. var _this = this;
  90. if (this.data.disabled) return;
  91. var min = this.data.min;
  92. utils_1.getRect(this, '.van-slider').then(function (rect) {
  93. var value =
  94. ((event.detail.x - rect.left) / rect.width) * _this.getRange() + min;
  95. if (_this.isRange(_this.value)) {
  96. var _a = _this.value,
  97. left = _a[0],
  98. right = _a[1];
  99. var middle = (left + right) / 2;
  100. if (value <= middle) {
  101. _this.updateValue([value, right], true);
  102. } else {
  103. _this.updateValue([left, value], true);
  104. }
  105. } else {
  106. _this.updateValue(value, true);
  107. }
  108. });
  109. },
  110. isRange: function (val) {
  111. var range = this.data.range;
  112. return range && Array.isArray(val);
  113. },
  114. handleOverlap: function (value) {
  115. if (value[0] > value[1]) {
  116. return value.slice(0).reverse();
  117. }
  118. return value;
  119. },
  120. updateValue: function (value, end, drag) {
  121. var _this = this;
  122. if (this.isRange(value)) {
  123. value = this.handleOverlap(value).map(function (val) {
  124. return _this.format(val);
  125. });
  126. } else {
  127. value = this.format(value);
  128. }
  129. this.value = value;
  130. this.setData({
  131. barStyle:
  132. '\n width: ' +
  133. this.calcMainAxis() +
  134. ';\n left: ' +
  135. (this.isRange(value) ? value[0] + '%' : 0) +
  136. ';\n ' +
  137. (drag ? 'transition: none;' : '') +
  138. '\n ',
  139. });
  140. if (drag) {
  141. this.$emit('drag', { value: value });
  142. }
  143. if (end) {
  144. this.$emit('change', value);
  145. }
  146. if ((drag || end) && version_1.canIUseModel()) {
  147. this.setData({ value: value });
  148. }
  149. },
  150. getScope: function () {
  151. return Number(this.data.max) - Number(this.data.min);
  152. },
  153. getRange: function () {
  154. var _a = this.data,
  155. max = _a.max,
  156. min = _a.min;
  157. return max - min;
  158. },
  159. // 计算选中条的长度百分比
  160. calcMainAxis: function () {
  161. var value = this.value;
  162. var min = this.data.min;
  163. var scope = this.getScope();
  164. if (this.isRange(value)) {
  165. return ((value[1] - value[0]) * 100) / scope + '%';
  166. }
  167. return ((value - Number(min)) * 100) / scope + '%';
  168. },
  169. format: function (value) {
  170. var _a = this.data,
  171. max = _a.max,
  172. min = _a.min,
  173. step = _a.step;
  174. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  175. },
  176. },
  177. });