index.js 7.8 KB

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