index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _utils = require("../utils");
  5. var _deepClone = require("../utils/deep-clone");
  6. var _event = require("../utils/dom/event");
  7. var _touch = require("../mixins/touch");
  8. var _field = require("../mixins/field");
  9. var _createNamespace = (0, _utils.createNamespace)('slider'),
  10. createComponent = _createNamespace[0],
  11. bem = _createNamespace[1];
  12. var isSameValue = function isSameValue(newValue, oldValue) {
  13. return JSON.stringify(newValue) === JSON.stringify(oldValue);
  14. };
  15. var _default = createComponent({
  16. mixins: [_touch.TouchMixin, _field.FieldMixin],
  17. props: {
  18. disabled: Boolean,
  19. vertical: Boolean,
  20. range: Boolean,
  21. barHeight: [Number, String],
  22. buttonSize: [Number, String],
  23. activeColor: String,
  24. inactiveColor: String,
  25. min: {
  26. type: [Number, String],
  27. default: 0
  28. },
  29. max: {
  30. type: [Number, String],
  31. default: 100
  32. },
  33. step: {
  34. type: [Number, String],
  35. default: 1
  36. },
  37. value: {
  38. type: [Number, Array],
  39. default: 0
  40. }
  41. },
  42. data: function data() {
  43. return {
  44. dragStatus: ''
  45. };
  46. },
  47. computed: {
  48. scope: function scope() {
  49. return this.max - this.min;
  50. },
  51. buttonStyle: function buttonStyle() {
  52. if (this.buttonSize) {
  53. var size = (0, _utils.addUnit)(this.buttonSize);
  54. return {
  55. width: size,
  56. height: size
  57. };
  58. }
  59. }
  60. },
  61. created: function created() {
  62. // format initial value
  63. this.updateValue(this.value);
  64. },
  65. mounted: function mounted() {
  66. if (this.range) {
  67. this.bindTouchEvent(this.$refs.wrapper0);
  68. this.bindTouchEvent(this.$refs.wrapper1);
  69. } else {
  70. this.bindTouchEvent(this.$refs.wrapper);
  71. }
  72. },
  73. methods: {
  74. onTouchStart: function onTouchStart(event) {
  75. if (this.disabled) {
  76. return;
  77. }
  78. this.touchStart(event);
  79. this.currentValue = this.value;
  80. if (this.range) {
  81. this.startValue = this.value.map(this.format);
  82. } else {
  83. this.startValue = this.format(this.value);
  84. }
  85. this.dragStatus = 'start';
  86. },
  87. onTouchMove: function onTouchMove(event) {
  88. if (this.disabled) {
  89. return;
  90. }
  91. if (this.dragStatus === 'start') {
  92. this.$emit('drag-start');
  93. }
  94. (0, _event.preventDefault)(event, true);
  95. this.touchMove(event);
  96. this.dragStatus = 'draging';
  97. var rect = this.$el.getBoundingClientRect();
  98. var delta = this.vertical ? this.deltaY : this.deltaX;
  99. var total = this.vertical ? rect.height : rect.width;
  100. var diff = delta / total * this.scope;
  101. if (this.range) {
  102. this.currentValue[this.index] = this.startValue[this.index] + diff;
  103. } else {
  104. this.currentValue = this.startValue + diff;
  105. }
  106. this.updateValue(this.currentValue);
  107. },
  108. onTouchEnd: function onTouchEnd() {
  109. if (this.disabled) {
  110. return;
  111. }
  112. if (this.dragStatus === 'draging') {
  113. this.updateValue(this.currentValue, true);
  114. this.$emit('drag-end');
  115. }
  116. this.dragStatus = '';
  117. },
  118. onClick: function onClick(event) {
  119. event.stopPropagation();
  120. if (this.disabled) return;
  121. var rect = this.$el.getBoundingClientRect();
  122. var delta = this.vertical ? event.clientY - rect.top : event.clientX - rect.left;
  123. var total = this.vertical ? rect.height : rect.width;
  124. var value = +this.min + delta / total * this.scope;
  125. if (this.range) {
  126. var _this$value = this.value,
  127. left = _this$value[0],
  128. right = _this$value[1];
  129. var middle = (left + right) / 2;
  130. if (value <= middle) {
  131. left = value;
  132. } else {
  133. right = value;
  134. }
  135. value = [left, right];
  136. }
  137. this.startValue = this.value;
  138. this.updateValue(value, true);
  139. },
  140. // 处理两个滑块重叠之后的情况
  141. handleOverlap: function handleOverlap(value) {
  142. if (value[0] > value[1]) {
  143. value = (0, _deepClone.deepClone)(value);
  144. return value.reverse();
  145. }
  146. return value;
  147. },
  148. updateValue: function updateValue(value, end) {
  149. if (this.range) {
  150. value = this.handleOverlap(value).map(this.format);
  151. } else {
  152. value = this.format(value);
  153. }
  154. if (!isSameValue(value, this.value)) {
  155. this.$emit('input', value);
  156. }
  157. if (end && !isSameValue(value, this.startValue)) {
  158. this.$emit('change', value);
  159. }
  160. },
  161. format: function format(value) {
  162. return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step;
  163. }
  164. },
  165. render: function render() {
  166. var _wrapperStyle,
  167. _this = this,
  168. _barStyle;
  169. var h = arguments[0];
  170. var vertical = this.vertical;
  171. var mainAxis = vertical ? 'height' : 'width';
  172. var crossAxis = vertical ? 'width' : 'height';
  173. var wrapperStyle = (_wrapperStyle = {
  174. background: this.inactiveColor
  175. }, _wrapperStyle[crossAxis] = (0, _utils.addUnit)(this.barHeight), _wrapperStyle); // 计算选中条的长度百分比
  176. var calcMainAxis = function calcMainAxis() {
  177. var value = _this.value,
  178. min = _this.min,
  179. range = _this.range,
  180. scope = _this.scope;
  181. if (range) {
  182. return (value[1] - value[0]) * 100 / scope + "%";
  183. }
  184. return (value - min) * 100 / scope + "%";
  185. }; // 计算选中条的开始位置的偏移量
  186. var calcOffset = function calcOffset() {
  187. var value = _this.value,
  188. min = _this.min,
  189. range = _this.range,
  190. scope = _this.scope;
  191. if (range) {
  192. return (value[0] - min) * 100 / scope + "%";
  193. }
  194. return null;
  195. };
  196. var barStyle = (_barStyle = {}, _barStyle[mainAxis] = calcMainAxis(), _barStyle.left = this.vertical ? null : calcOffset(), _barStyle.top = this.vertical ? calcOffset() : null, _barStyle.background = this.activeColor, _barStyle);
  197. if (this.dragStatus) {
  198. barStyle.transition = 'none';
  199. }
  200. var renderButton = function renderButton(i) {
  201. var map = ['left', 'right'];
  202. var isNumber = typeof i === 'number';
  203. var getClassName = function getClassName() {
  204. if (isNumber) {
  205. return "button-wrapper-" + map[i];
  206. }
  207. return "button-wrapper";
  208. };
  209. var getRefName = function getRefName() {
  210. if (isNumber) {
  211. return "wrapper" + i;
  212. }
  213. return "wrapper";
  214. };
  215. return h("div", {
  216. "ref": getRefName(),
  217. "attrs": {
  218. "role": "slider",
  219. "tabindex": _this.disabled ? -1 : 0,
  220. "aria-valuemin": _this.min,
  221. "aria-valuenow": _this.value,
  222. "aria-valuemax": _this.max,
  223. "aria-orientation": _this.vertical ? 'vertical' : 'horizontal'
  224. },
  225. "class": bem(getClassName()),
  226. "on": {
  227. "touchstart": function touchstart() {
  228. if (isNumber) {
  229. // 保存当前按钮的索引
  230. _this.index = i;
  231. }
  232. },
  233. "click": function click(e) {
  234. return e.stopPropagation();
  235. }
  236. }
  237. }, [_this.slots('button') || h("div", {
  238. "class": bem('button'),
  239. "style": _this.buttonStyle
  240. })]);
  241. };
  242. return h("div", {
  243. "style": wrapperStyle,
  244. "class": bem({
  245. disabled: this.disabled,
  246. vertical: vertical
  247. }),
  248. "on": {
  249. "click": this.onClick
  250. }
  251. }, [h("div", {
  252. "class": bem('bar'),
  253. "style": barStyle
  254. }, [this.range ? [renderButton(0), renderButton(1)] : renderButton()])]);
  255. }
  256. });
  257. exports.default = _default;