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