index.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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: null,
  29. value: 0,
  30. observer: function (val) {
  31. if (val !== this.value) {
  32. this.updateValue(val);
  33. }
  34. },
  35. },
  36. barHeight: null,
  37. },
  38. created: function () {
  39. this.updateValue(this.data.value);
  40. },
  41. methods: {
  42. onTouchStart: function (event) {
  43. var _this = this;
  44. if (this.data.disabled)
  45. 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) { return _this.format(val); });
  55. }
  56. else {
  57. this.startValue = this.format(this.newValue);
  58. }
  59. this.dragStatus = 'start';
  60. },
  61. onTouchMove: function (event) {
  62. var _this = this;
  63. if (this.data.disabled)
  64. 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. }
  76. else {
  77. _this.newValue = _this.startValue + diff;
  78. }
  79. _this.updateValue(_this.newValue, false, true);
  80. });
  81. },
  82. onTouchEnd: function () {
  83. if (this.data.disabled)
  84. return;
  85. if (this.dragStatus === 'draging') {
  86. this.updateValue(this.newValue, true);
  87. this.$emit('drag-end');
  88. }
  89. },
  90. onClick: function (event) {
  91. var _this = this;
  92. if (this.data.disabled)
  93. return;
  94. var min = this.data.min;
  95. utils_1.getRect(this, '.van-slider').then(function (rect) {
  96. var value = ((event.detail.x - rect.left) / rect.width) * _this.getRange() + min;
  97. if (_this.isRange(_this.value)) {
  98. var _a = _this.value, left = _a[0], right = _a[1];
  99. var middle = (left + right) / 2;
  100. if (value <= middle) {
  101. _this.updateValue([value, right], true);
  102. }
  103. else {
  104. _this.updateValue([left, value], true);
  105. }
  106. }
  107. else {
  108. _this.updateValue(value, true);
  109. }
  110. });
  111. },
  112. isRange: function (val) {
  113. var range = this.data.range;
  114. return range && Array.isArray(val);
  115. },
  116. handleOverlap: function (value) {
  117. if (value[0] > value[1]) {
  118. return value.slice(0).reverse();
  119. }
  120. return value;
  121. },
  122. updateValue: function (value, end, drag) {
  123. var _this = this;
  124. if (this.isRange(value)) {
  125. value = this.handleOverlap(value).map(function (val) { return _this.format(val); });
  126. }
  127. else {
  128. value = this.format(value);
  129. }
  130. this.value = value;
  131. this.setData({
  132. barStyle: "\n width: " + this.calcMainAxis() + ";\n left: " + (this.isRange(value) ? value[0] + "%" : 0) + ";\n " + (drag ? 'transition: none;' : '') + "\n ",
  133. });
  134. if (drag) {
  135. this.$emit('drag', { value: value });
  136. }
  137. if (end) {
  138. this.$emit('change', value);
  139. }
  140. if ((drag || end) && version_1.canIUseModel()) {
  141. this.setData({ value: value });
  142. }
  143. },
  144. getScope: function () {
  145. return Number(this.data.max) - Number(this.data.min);
  146. },
  147. getRange: function () {
  148. var _a = this.data, max = _a.max, min = _a.min;
  149. return max - min;
  150. },
  151. // 计算选中条的长度百分比
  152. calcMainAxis: function () {
  153. var value = this.value;
  154. var min = this.data.min;
  155. var scope = this.getScope();
  156. if (this.isRange(value)) {
  157. return ((value[1] - value[0]) * 100) / scope + "%";
  158. }
  159. return ((value - Number(min)) * 100) / scope + "%";
  160. },
  161. format: function (value) {
  162. var _a = this.data, max = _a.max, min = _a.min, step = _a.step;
  163. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  164. },
  165. },
  166. });