ant-design-vue/components/vc-slider/src/utils.js

140 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-03-13 02:16:06 +00:00
import keyCode from '../../_util/KeyCode'
2018-12-15 14:11:21 +00:00
export function isDev () {
return (process.env.NODE_ENV !== 'production')
}
2018-03-13 02:16:06 +00:00
export function isEventFromHandle (e, handles) {
2018-12-15 14:11:21 +00:00
try {
return Object.keys(handles)
.some(key => e.target === handles[key].$el)
} catch (error) {
return false
}
2018-03-13 02:16:06 +00:00
}
export function isValueOutOfRange (value, { min, max }) {
return value < min || value > max
}
export function isNotTouchEvent (e) {
return e.touches.length > 1 ||
(e.type.toLowerCase() === 'touchend' && e.touches.length > 0)
}
export function getClosestPoint (val, { marks, step, min }) {
const points = Object.keys(marks).map(parseFloat)
if (step !== null) {
const closestStep =
Math.round((val - min) / step) * step + min
points.push(closestStep)
}
const diffs = points.map(point => Math.abs(val - point))
return points[diffs.indexOf(Math.min(...diffs))]
}
export function getPrecision (step) {
const stepString = step.toString()
let precision = 0
if (stepString.indexOf('.') >= 0) {
precision = stepString.length - stepString.indexOf('.') - 1
}
return precision
}
export function getMousePosition (vertical, e) {
let zoom = 1
if (window.visualViewport) {
zoom = +(window.visualViewport.width / document.body.getBoundingClientRect().width).toFixed(2)
}
return (vertical ? e.clientY : e.pageX) / zoom
2018-03-13 02:16:06 +00:00
}
export function getTouchPosition (vertical, e) {
let zoom = 1
if (window.visualViewport) {
zoom = +(window.visualViewport.width / document.body.getBoundingClientRect().width).toFixed(2)
}
return (vertical ? e.touches[0].clientY : e.touches[0].pageX) / zoom
2018-03-13 02:16:06 +00:00
}
export function getHandleCenterPosition (vertical, handle) {
const coords = handle.getBoundingClientRect()
return vertical
? coords.top + (coords.height * 0.5)
2018-12-15 14:11:21 +00:00
: window.pageXOffset + coords.left + (coords.width * 0.5)
2018-03-13 02:16:06 +00:00
}
export function ensureValueInRange (val, { max, min }) {
if (val <= min) {
return min
}
if (val >= max) {
return max
}
return val
}
export function ensureValuePrecision (val, props) {
const { step } = props
2018-12-15 14:11:21 +00:00
const closestPoint = isFinite(getClosestPoint(val, props)) ? getClosestPoint(val, props) : 0; // eslint-disable-line
2018-03-13 02:16:06 +00:00
return step === null ? closestPoint
: parseFloat(closestPoint.toFixed(getPrecision(step)))
}
export function pauseEvent (e) {
e.stopPropagation()
e.preventDefault()
}
2018-12-15 14:11:21 +00:00
export function calculateNextValue (func, value, props) {
const operations = {
increase: (a, b) => a + b,
decrease: (a, b) => a - b,
}
const indexToGet = operations[func](Object.keys(props.marks).indexOf(JSON.stringify(value)), 1)
const keyToGet = Object.keys(props.marks)[indexToGet]
if (props.step) {
return operations[func](value, props.step)
} else if (!!Object.keys(props.marks).length && !!props.marks[keyToGet]) {
return props.marks[keyToGet]
}
return value
}
2018-03-13 02:16:06 +00:00
export function getKeyboardValueMutator (e) {
switch (e.keyCode) {
case keyCode.UP:
case keyCode.RIGHT:
2018-12-15 14:11:21 +00:00
return (value, props) => calculateNextValue('increase', value, props)
2018-03-13 02:16:06 +00:00
case keyCode.DOWN:
case keyCode.LEFT:
2018-12-15 14:11:21 +00:00
return (value, props) => calculateNextValue('decrease', value, props)
2018-03-13 02:16:06 +00:00
case keyCode.END: return (value, props) => props.max
case keyCode.HOME: return (value, props) => props.min
case keyCode.PAGE_UP: return (value, props) => value + props.step * 2
case keyCode.PAGE_DOWN: return (value, props) => value - props.step * 2
default: return undefined
}
}
2018-03-28 03:51:27 +00:00
export function getComponentProps (obj, prop) {
if (obj[prop]) {
return obj
} else if (obj.$children.length) {
const len = obj.$children.length
for (let i = 0; i < len; i++) {
if (obj.$children[i][prop]) {
return obj.$children[i]
} else if (obj.$children[i].$children.length) {
return getComponentProps(obj.$children[i], prop)
}
}
}
}