index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. (0, 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. vertical: Boolean,
  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)
  46. return;
  47. var index = event.currentTarget.dataset.index;
  48. if (typeof index === 'number') {
  49. this.buttonIndex = index;
  50. }
  51. this.touchStart(event);
  52. this.startValue = this.format(this.value);
  53. this.newValue = this.value;
  54. if (this.isRange(this.newValue)) {
  55. this.startValue = this.newValue.map(function (val) { 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)
  65. return;
  66. if (this.dragStatus === 'start') {
  67. this.$emit('drag-start');
  68. }
  69. this.touchMove(event);
  70. this.dragStatus = 'draging';
  71. (0, utils_1.getRect)(this, '.van-slider').then(function (rect) {
  72. var vertical = _this.data.vertical;
  73. var delta = vertical ? _this.deltaY : _this.deltaX;
  74. var total = vertical ? rect.height : rect.width;
  75. var diff = (delta / total) * _this.getRange();
  76. if (_this.isRange(_this.startValue)) {
  77. _this.newValue[_this.buttonIndex] =
  78. _this.startValue[_this.buttonIndex] + diff;
  79. }
  80. else {
  81. _this.newValue = _this.startValue + diff;
  82. }
  83. _this.updateValue(_this.newValue, false, true);
  84. });
  85. },
  86. onTouchEnd: function () {
  87. if (this.data.disabled)
  88. return;
  89. if (this.dragStatus === 'draging') {
  90. this.updateValue(this.newValue, true);
  91. this.$emit('drag-end');
  92. }
  93. },
  94. onClick: function (event) {
  95. var _this = this;
  96. if (this.data.disabled)
  97. return;
  98. var min = this.data.min;
  99. (0, utils_1.getRect)(this, '.van-slider').then(function (rect) {
  100. var vertical = _this.data.vertical;
  101. var delta = vertical
  102. ? event.detail.y - rect.top
  103. : event.detail.x - rect.left;
  104. var total = vertical ? rect.height : rect.width;
  105. var value = Number(min) + (delta / total) * _this.getRange();
  106. if (_this.isRange(_this.value)) {
  107. var _a = _this.value, left = _a[0], right = _a[1];
  108. var middle = (left + right) / 2;
  109. if (value <= middle) {
  110. _this.updateValue([value, right], true);
  111. }
  112. else {
  113. _this.updateValue([left, value], true);
  114. }
  115. }
  116. else {
  117. _this.updateValue(value, true);
  118. }
  119. });
  120. },
  121. isRange: function (val) {
  122. var range = this.data.range;
  123. return range && Array.isArray(val);
  124. },
  125. handleOverlap: function (value) {
  126. if (value[0] > value[1]) {
  127. return value.slice(0).reverse();
  128. }
  129. return value;
  130. },
  131. updateValue: function (value, end, drag) {
  132. var _this = this;
  133. if (this.isRange(value)) {
  134. value = this.handleOverlap(value).map(function (val) { return _this.format(val); });
  135. }
  136. else {
  137. value = this.format(value);
  138. }
  139. this.value = value;
  140. var vertical = this.data.vertical;
  141. var mainAxis = vertical ? 'height' : 'width';
  142. this.setData({
  143. wrapperStyle: "\n background: " + (this.data.inactiveColor || '') + ";\n " + mainAxis + ": " + ((0, utils_1.addUnit)(this.data.barHeight) || '') + ";\n ",
  144. barStyle: "\n " + mainAxis + ": " + this.calcMainAxis() + ";\n left: " + (vertical ? 0 : this.calcOffset()) + ";\n top: " + (vertical ? this.calcOffset() : 0) + ";\n " + (drag ? 'transition: none;' : '') + "\n ",
  145. });
  146. if (drag) {
  147. this.$emit('drag', { value: value });
  148. }
  149. if (end) {
  150. this.$emit('change', value);
  151. }
  152. if ((drag || end) && (0, version_1.canIUseModel)()) {
  153. this.setData({ value: value });
  154. }
  155. },
  156. getScope: function () {
  157. return Number(this.data.max) - Number(this.data.min);
  158. },
  159. getRange: function () {
  160. var _a = this.data, max = _a.max, min = _a.min;
  161. return max - min;
  162. },
  163. // 计算选中条的长度百分比
  164. calcMainAxis: function () {
  165. var value = this.value;
  166. var min = this.data.min;
  167. var scope = this.getScope();
  168. if (this.isRange(value)) {
  169. return ((value[1] - value[0]) * 100) / scope + "%";
  170. }
  171. return ((value - Number(min)) * 100) / scope + "%";
  172. },
  173. // 计算选中条的开始位置的偏移量
  174. calcOffset: function () {
  175. var value = this.value;
  176. var min = this.data.min;
  177. var scope = this.getScope();
  178. if (this.isRange(value)) {
  179. return ((value[0] - Number(min)) * 100) / scope + "%";
  180. }
  181. return '0%';
  182. },
  183. format: function (value) {
  184. var _a = this.data, max = _a.max, min = _a.min, step = _a.step;
  185. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  186. },
  187. },
  188. });