index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 current = isNumber ? _this.value[i] : _this.value;
  210. var getClassName = function getClassName() {
  211. if (isNumber) {
  212. return "button-wrapper-" + map[i];
  213. }
  214. return "button-wrapper";
  215. };
  216. var getRefName = function getRefName() {
  217. if (isNumber) {
  218. return "wrapper" + i;
  219. }
  220. return "wrapper";
  221. };
  222. var renderButtonContent = function renderButtonContent() {
  223. if (isNumber) {
  224. var slot = _this.slots(i === 0 ? 'left-button' : 'right-button', {
  225. value: current
  226. });
  227. if (slot) {
  228. return slot;
  229. }
  230. }
  231. if (_this.slots('button')) {
  232. return _this.slots('button');
  233. }
  234. return h("div", {
  235. "class": bem('button'),
  236. "style": _this.buttonStyle
  237. });
  238. };
  239. return h("div", {
  240. "ref": getRefName(),
  241. "attrs": {
  242. "role": "slider",
  243. "tabindex": _this.disabled ? -1 : 0,
  244. "aria-valuemin": _this.min,
  245. "aria-valuenow": _this.value,
  246. "aria-valuemax": _this.max,
  247. "aria-orientation": _this.vertical ? 'vertical' : 'horizontal'
  248. },
  249. "class": bem(getClassName()),
  250. "on": {
  251. "touchstart": function touchstart() {
  252. if (isNumber) {
  253. // 保存当前按钮的索引
  254. _this.index = i;
  255. }
  256. },
  257. "click": function click(e) {
  258. return e.stopPropagation();
  259. }
  260. }
  261. }, [renderButtonContent()]);
  262. };
  263. return h("div", {
  264. "style": wrapperStyle,
  265. "class": bem({
  266. disabled: this.disabled,
  267. vertical: vertical
  268. }),
  269. "on": {
  270. "click": this.onClick
  271. }
  272. }, [h("div", {
  273. "class": bem('bar'),
  274. "style": barStyle
  275. }, [this.range ? [renderButton(0), renderButton(1)] : renderButton()])]);
  276. }
  277. });
  278. exports.default = _default;