2018-02-24 10:12:24 +00:00
|
|
|
import { getPropsData, getSlotOptions, getKey, getAttrs, getComponentFromProp } from '../_util/props-util'
|
2018-02-22 09:19:47 +00:00
|
|
|
import { cloneVNodes } from '../_util/vnode'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
export function toTitle (title) {
|
|
|
|
if (typeof title === 'string') {
|
|
|
|
return title
|
|
|
|
}
|
2019-01-04 15:00:09 +00:00
|
|
|
return ''
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2018-02-06 11:00:34 +00:00
|
|
|
export function getValuePropValue (child) {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!child) {
|
|
|
|
return null
|
|
|
|
}
|
2018-02-07 10:56:58 +00:00
|
|
|
const props = getPropsData(child)
|
2018-02-06 11:00:34 +00:00
|
|
|
if ('value' in props) {
|
|
|
|
return props.value
|
|
|
|
}
|
2018-02-22 04:07:37 +00:00
|
|
|
if (getKey(child) !== undefined) {
|
2018-02-07 15:03:47 +00:00
|
|
|
return getKey(child)
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
2018-02-24 10:12:24 +00:00
|
|
|
if (getSlotOptions(child).isSelectOptGroup) {
|
|
|
|
const label = getComponentFromProp(child, 'label')
|
|
|
|
if (label) {
|
|
|
|
return label
|
|
|
|
}
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
2019-01-04 15:00:09 +00:00
|
|
|
throw new Error(`Need at least a key or a value or a label (only for OptGroup) for ${child}`)
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getPropValue (child, prop) {
|
|
|
|
if (prop === 'value') {
|
|
|
|
return getValuePropValue(child)
|
|
|
|
}
|
2018-02-11 10:04:31 +00:00
|
|
|
if (prop === 'children') {
|
2018-02-28 11:07:04 +00:00
|
|
|
const newChild = child.$slots ? cloneVNodes(child.$slots.default, true) : cloneVNodes(child.componentOptions.children, true)
|
|
|
|
if (newChild.length === 1 && !newChild[0].tag) {
|
|
|
|
return newChild[0].text
|
2018-02-11 10:04:31 +00:00
|
|
|
}
|
2018-02-28 11:07:04 +00:00
|
|
|
return newChild
|
2018-02-11 10:04:31 +00:00
|
|
|
}
|
2018-02-12 10:10:51 +00:00
|
|
|
const data = getPropsData(child)
|
|
|
|
if (prop in data) {
|
|
|
|
return data[prop]
|
|
|
|
} else {
|
|
|
|
return getAttrs(child)[prop]
|
|
|
|
}
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isMultiple (props) {
|
|
|
|
return props.multiple
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCombobox (props) {
|
|
|
|
return props.combobox
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isMultipleOrTags (props) {
|
|
|
|
return props.multiple || props.tags
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isMultipleOrTagsOrCombobox (props) {
|
|
|
|
return isMultipleOrTags(props) || isCombobox(props)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isSingleMode (props) {
|
|
|
|
return !isMultipleOrTagsOrCombobox(props)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function toArray (value) {
|
|
|
|
let ret = value
|
|
|
|
if (value === undefined) {
|
|
|
|
ret = []
|
|
|
|
} else if (!Array.isArray(value)) {
|
|
|
|
ret = [value]
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
export function getMapKey (value) {
|
|
|
|
return `${typeof value}-${value}`
|
|
|
|
}
|
|
|
|
|
2018-02-06 11:00:34 +00:00
|
|
|
export function preventDefaultEvent (e) {
|
|
|
|
e.preventDefault()
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
export function findIndexInValueBySingleValue (value, singleValue) {
|
2018-02-06 11:00:34 +00:00
|
|
|
let index = -1
|
2019-01-04 15:00:09 +00:00
|
|
|
if (value) {
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
if (value[i] === singleValue) {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
export function getLabelFromPropsValue (value, key) {
|
|
|
|
let label
|
|
|
|
value = toArray(value)
|
2019-01-04 15:00:09 +00:00
|
|
|
if (value) {
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
if (value[i].key === key) {
|
|
|
|
label = value[i].label
|
|
|
|
break
|
|
|
|
}
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
return label
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getSelectKeys (menuItems, value) {
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let selectedKeys = []
|
|
|
|
menuItems.forEach(item => {
|
2018-02-07 15:03:47 +00:00
|
|
|
if (getSlotOptions(item).isMenuItemGroup) {
|
2018-02-06 11:00:34 +00:00
|
|
|
selectedKeys = selectedKeys.concat(
|
2018-02-07 15:03:47 +00:00
|
|
|
getSelectKeys(item.componentOptions.children, value)
|
2018-02-06 11:00:34 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
const itemValue = getValuePropValue(item)
|
|
|
|
const itemKey = item.key
|
2018-09-19 05:14:46 +00:00
|
|
|
if (findIndexInValueBySingleValue(value, itemValue) !== -1 && itemKey !== undefined) {
|
2018-02-06 11:00:34 +00:00
|
|
|
selectedKeys.push(itemKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return selectedKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
export const UNSELECTABLE_STYLE = {
|
|
|
|
userSelect: 'none',
|
|
|
|
WebkitUserSelect: 'none',
|
|
|
|
}
|
|
|
|
|
|
|
|
export const UNSELECTABLE_ATTRIBUTE = {
|
2018-09-05 13:28:54 +00:00
|
|
|
unselectable: 'on',
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function findFirstMenuItem (children) {
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
const child = children[i]
|
2018-02-12 03:47:57 +00:00
|
|
|
const props = getPropsData(child)
|
|
|
|
if (getSlotOptions(child).isMenuItemGroup) {
|
2018-02-07 15:03:47 +00:00
|
|
|
const found = findFirstMenuItem(child.componentOptions.children)
|
2018-02-06 11:00:34 +00:00
|
|
|
if (found) {
|
|
|
|
return found
|
|
|
|
}
|
2018-02-07 15:03:47 +00:00
|
|
|
} else if (!props.disabled) {
|
2018-02-06 11:00:34 +00:00
|
|
|
return child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2019-01-04 15:00:09 +00:00
|
|
|
export function includesSeparators (str, separators) {
|
2018-02-06 11:00:34 +00:00
|
|
|
for (let i = 0; i < separators.length; ++i) {
|
2019-01-04 15:00:09 +00:00
|
|
|
if (str.lastIndexOf(separators[i]) > 0) {
|
2018-02-06 11:00:34 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-04 15:00:09 +00:00
|
|
|
export function splitBySeparators (str, separators) {
|
2018-02-06 11:00:34 +00:00
|
|
|
const reg = new RegExp(`[${separators.join()}]`)
|
2019-01-04 15:00:09 +00:00
|
|
|
return str.split(reg).filter(token => token)
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function defaultFilterFn (input, child) {
|
2018-02-12 03:47:57 +00:00
|
|
|
const props = getPropsData(child)
|
2018-02-07 15:03:47 +00:00
|
|
|
if (props.disabled) {
|
2018-02-06 11:00:34 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-02-12 10:10:51 +00:00
|
|
|
let value = getPropValue(child, this.optionFilterProp)
|
|
|
|
if (value.length && value[0].text) {
|
|
|
|
value = value[0].text
|
|
|
|
} else {
|
|
|
|
value = String(value)
|
|
|
|
}
|
2019-01-04 15:00:09 +00:00
|
|
|
return value.toLowerCase().indexOf(input.toLowerCase()) > -1
|
2018-02-06 11:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validateOptionValue (value, props) {
|
|
|
|
if (isSingleMode(props) || isMultiple(props)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid \`value\` of type \`${typeof value}\` supplied to Option, ` +
|
2019-01-04 15:00:09 +00:00
|
|
|
`expected \`string\` when \`tags/combobox\` is \`true\`.`,
|
2018-02-06 11:00:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
export function saveRef (instance, name) {
|
|
|
|
return (node) => {
|
|
|
|
instance[name] = node
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 15:00:09 +00:00
|
|
|
|
|
|
|
export function generateUUID () {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
return 'test-uuid'
|
|
|
|
}
|
|
|
|
let d = new Date().getTime()
|
|
|
|
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
|
|
const r = (d + Math.random() * 16) % 16 | 0
|
|
|
|
d = Math.floor(d / 16)
|
|
|
|
return (c === 'x' ? r : (r & 0x7) | 0x8).toString(16)
|
|
|
|
})
|
|
|
|
return uuid
|
|
|
|
}
|