vue-antd-ui
Version:
An enterprise-class UI design language and Vue-based implementation
374 lines (349 loc) • 12.8 kB
JavaScript
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _slicedToArray from 'babel-runtime/helpers/slicedToArray';
import _extends from 'babel-runtime/helpers/extends';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import classNames from 'classnames';
import PropTypes from '../../../_util/vue-types';
import addEventListener from '../../../_util/Dom/addEventListener';
import warning from '../../../_util/warning';
import { initDefaultProps } from '../../../_util/props-util';
import Steps from './Steps';
import Marks from './Marks';
import Handle from '../Handle';
import * as utils from '../utils';
function noop() {}
export default function createSlider(Component) {
// const displayName = `ComponentEnhancer(${Component.displayName})`
var propTypes = {
min: PropTypes.number,
max: PropTypes.number,
step: PropTypes.number,
marks: PropTypes.object,
included: PropTypes.bool,
prefixCls: PropTypes.string,
disabled: PropTypes.bool,
handle: PropTypes.func,
dots: PropTypes.bool,
vertical: PropTypes.bool,
minimumTrackStyle: PropTypes.object, // just for compatibility, will be deperecate
maximumTrackStyle: PropTypes.object, // just for compatibility, will be deperecate
handleStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]),
trackStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]),
railStyle: PropTypes.object,
dotStyle: PropTypes.object,
activeDotStyle: PropTypes.object,
autoFocus: PropTypes.bool
};
return {
name: 'createSlider',
mixins: [Component],
model: {
prop: 'value',
event: 'change'
},
props: initDefaultProps(propTypes, {
prefixCls: 'rc-slider',
min: 0,
max: 100,
step: 1,
marks: {},
handle: function handle(h, _ref) {
var index = _ref.index,
ref = _ref.ref,
className = _ref.className,
style = _ref.style,
restProps = _objectWithoutProperties(_ref, ['index', 'ref', 'className', 'style']);
delete restProps.dragging;
var handleProps = {
props: _extends({}, restProps),
'class': className,
style: style,
key: index,
ref: ref
};
return h(Handle, handleProps);
},
included: true,
disabled: false,
dots: false,
vertical: false,
trackStyle: [{}],
handleStyle: [{}],
railStyle: {},
dotStyle: {},
activeDotStyle: {}
}),
data: function data() {
if (process.env.NODE_ENV !== 'production') {
var step = this.step,
max = this.max,
min = this.min;
warning(step && Math.floor(step) === step ? (max - min) % step === 0 : true, 'Slider[max] - Slider[min] (%s) should be a multiple of Slider[step] (%s)', max - min, step);
}
return {};
},
beforeDestroy: function beforeDestroy() {
var _this = this;
this.$nextTick(function () {
// if (super.componentWillUnmount) super.componentWillUnmount()
_this.removeDocumentEvents();
});
},
mounted: function mounted() {
var _this2 = this;
this.$nextTick(function () {
// Snapshot testing cannot handle refs, so be sure to null-check this.
_this2.document = _this2.$refs.sliderRef && _this2.$refs.sliderRef.ownerDocument;
// this.setHandleRefs()
});
},
computed: {
handlesRefs: function handlesRefs() {
var handlesRefs = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = Object.entries(this.$refs)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ref2 = _step.value;
var _ref3 = _slicedToArray(_ref2, 2);
var k = _ref3[0];
var v = _ref3[1];
var matchs = k.match(/handleRefs_(\d+$)/);
if (matchs) {
handlesRefs[+matchs[1]] = v;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return handlesRefs;
}
},
methods: {
onMouseDown: function onMouseDown(e) {
if (e.button !== 0) {
return;
}
var isVertical = this.vertical;
var position = utils.getMousePosition(isVertical, e);
if (!utils.isEventFromHandle(e, this.handlesRefs)) {
this.dragOffset = 0;
} else {
var handlePosition = utils.getHandleCenterPosition(isVertical, e.target);
this.dragOffset = position - handlePosition;
position = handlePosition;
}
this.removeDocumentEvents();
this.onStart(position);
this.addDocumentMouseEvents();
},
onTouchStart: function onTouchStart(e) {
if (utils.isNotTouchEvent(e)) return;
var isVertical = this.vertical;
var position = utils.getTouchPosition(isVertical, e);
if (!utils.isEventFromHandle(e, this.handlesRefs)) {
this.dragOffset = 0;
} else {
var handlePosition = utils.getHandleCenterPosition(isVertical, e.target);
this.dragOffset = position - handlePosition;
position = handlePosition;
}
this.onStart(position);
this.addDocumentTouchEvents();
utils.pauseEvent(e);
},
onFocus: function onFocus(e) {
var vertical = this.vertical;
if (utils.isEventFromHandle(e, this.handlesRefs)) {
var handlePosition = utils.getHandleCenterPosition(vertical, e.target);
this.dragOffset = 0;
this.onStart(handlePosition);
utils.pauseEvent(e);
this.$emit('focus', e);
}
},
onBlur: function onBlur(e) {
this.onEnd(e);
this.$emit('blur', e);
},
addDocumentTouchEvents: function addDocumentTouchEvents() {
// just work for Chrome iOS Safari and Android Browser
this.onTouchMoveListener = addEventListener(this.document, 'touchmove', this.onTouchMove);
this.onTouchUpListener = addEventListener(this.document, 'touchend', this.onEnd);
},
addDocumentMouseEvents: function addDocumentMouseEvents() {
this.onMouseMoveListener = addEventListener(this.document, 'mousemove', this.onMouseMove);
this.onMouseUpListener = addEventListener(this.document, 'mouseup', this.onEnd);
},
removeDocumentEvents: function removeDocumentEvents() {
/* eslint-disable no-unused-expressions */
this.onTouchMoveListener && this.onTouchMoveListener.remove();
this.onTouchUpListener && this.onTouchUpListener.remove();
this.onMouseMoveListener && this.onMouseMoveListener.remove();
this.onMouseUpListener && this.onMouseUpListener.remove();
/* eslint-enable no-unused-expressions */
},
onMouseUp: function onMouseUp() {
// if (this.$children && this.$children[this.prevMovedHandleIndex]) {
// const handleCom = utils.getComponentProps(this.$children[this.prevMovedHandleIndex], 'clickFocus')
// console.log('handleCom', handleCom)
// if (handleCom) {
// // handleCom.clickFocus()
// }
// }
if (this.handlesRefs[this.prevMovedHandleIndex]) {
this.handlesRefs[this.prevMovedHandleIndex].clickFocus();
}
},
onMouseMove: function onMouseMove(e) {
if (!this.$refs.sliderRef) {
this.onEnd();
return;
}
var position = utils.getMousePosition(this.vertical, e);
this.onMove(e, position - this.dragOffset);
},
onTouchMove: function onTouchMove(e) {
if (utils.isNotTouchEvent(e) || !this.$refs.sliderRef) {
this.onEnd();
return;
}
var position = utils.getTouchPosition(this.vertical, e);
this.onMove(e, position - this.dragOffset);
},
onKeyDown: function onKeyDown(e) {
if (this.$refs.sliderRef && utils.isEventFromHandle(e, this.handlesRefs)) {
this.onKeyboard(e);
}
},
focus: function focus() {
if (!this.disabled) {
this.handlesRefs[0].focus();
}
},
blur: function blur() {
if (!this.disabled) {
this.handlesRefs[0].blur();
}
},
getSliderStart: function getSliderStart() {
var slider = this.$refs.sliderRef;
var rect = slider.getBoundingClientRect();
return this.vertical ? rect.top : rect.left;
},
getSliderLength: function getSliderLength() {
var slider = this.$refs.sliderRef;
if (!slider) {
return 0;
}
var coords = slider.getBoundingClientRect();
return this.vertical ? coords.height : coords.width;
},
calcValue: function calcValue(offset) {
var vertical = this.vertical,
min = this.min,
max = this.max;
var ratio = Math.abs(Math.max(offset, 0) / this.getSliderLength());
var value = vertical ? (1 - ratio) * (max - min) + min : ratio * (max - min) + min;
return value;
},
calcValueByPos: function calcValueByPos(position) {
var pixelOffset = position - this.getSliderStart();
var nextValue = this.trimAlignValue(this.calcValue(pixelOffset));
return nextValue;
},
calcOffset: function calcOffset(value) {
var min = this.min,
max = this.max;
var ratio = (value - min) / (max - min);
return ratio * 100;
},
onClickMarkLabel: function onClickMarkLabel(e, value) {
e.stopPropagation();
this.onChange({ value: value });
// this.$emit('change', value)
}
},
render: function render(h) {
var _classNames;
var prefixCls = this.prefixCls,
marks = this.marks,
dots = this.dots,
step = this.step,
included = this.included,
disabled = this.disabled,
vertical = this.vertical,
min = this.min,
max = this.max,
maximumTrackStyle = this.maximumTrackStyle,
railStyle = this.railStyle,
dotStyle = this.dotStyle,
activeDotStyle = this.activeDotStyle;
var _renderSlider = this.renderSlider(h),
tracks = _renderSlider.tracks,
handles = _renderSlider.handles;
var sliderClassName = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-with-marks', Object.keys(marks).length), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _defineProperty(_classNames, prefixCls + '-vertical', vertical), _classNames));
var markProps = {
props: {
vertical: vertical,
marks: marks,
included: included,
lowerBound: this.getLowerBound(),
upperBound: this.getUpperBound(),
max: max,
min: min,
className: prefixCls + '-mark'
},
on: {
clickLabel: disabled ? noop : this.onClickMarkLabel
}
};
return h(
'div',
{
ref: 'sliderRef',
'class': sliderClassName,
on: {
'touchstart': disabled ? noop : this.onTouchStart,
'mousedown': disabled ? noop : this.onMouseDown,
'mouseup': disabled ? noop : this.onMouseUp,
'keydown': disabled ? noop : this.onKeyDown,
'focus': disabled ? noop : this.onFocus,
'blur': disabled ? noop : this.onBlur
}
},
[h('div', {
'class': prefixCls + '-rail',
style: _extends({}, maximumTrackStyle, railStyle)
}), tracks, h(Steps, {
attrs: {
prefixCls: prefixCls,
vertical: vertical,
marks: marks,
dots: dots,
step: step,
included: included,
lowerBound: this.getLowerBound(),
upperBound: this.getUpperBound(),
max: max,
min: min,
dotStyle: dotStyle,
activeDotStyle: activeDotStyle
}
}), handles, h(Marks, markProps), this.$slots['default']]
);
}
};
}