@58fe/p5
Version:
pc端vue组件
99 lines (92 loc) • 2.31 kB
JavaScript
function oneOf(value, validList) {
for (let i = 0; i < validList.length; i++) {
if (value === validList[i]) {
return true;
}
}
return false;
}
function typeOf(obj) {
const toString = Object.prototype.toString;
const map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object'
};
return map[toString.call(obj)];
}
// Find components upward 向上父级元素查找
function findComponentUpward(context, componentName, componentNames) {
if (typeof componentName === 'string') {
componentNames = [componentName];
} else {
componentNames = componentName;
}
let parent = context.$parent;
let name = parent.$options.name;
while (parent && (!name || componentNames.indexOf(name) < 0)) {
parent = parent.$parent;
if (parent) {
name = parent.$options.name;
}
}
return parent;
}
// Find components downward 向下子元素查找
function findComponentsDownward(context, componentName, ignoreComponentNames = []) {
if (!Array.isArray(ignoreComponentNames)) {
ignoreComponentNames = [ignoreComponentNames];
}
return context.$children.reduce((components, child) => {
if (child.$options.name === componentName) {
components.push(child);
}
if (ignoreComponentNames.indexOf(child.$options.name) < 0) {
const foundChilds = findComponentsDownward(child, componentName);
return components.concat(foundChilds);
} else {
return components;
}
}, []);
}
function deepCopy(data) {
const t = typeOf(data);
let o;
if (t === 'array') {
o = [];
} else if (t === 'object') {
o = {};
} else {
return data;
}
if (t === 'array') {
for (let i = 0; i < data.length; i++) {
o.push(deepCopy(data[i]));
}
} else if (t === 'object') {
for (let i in data) {
o[i] = deepCopy(data[i]);
}
}
return o;
}
function calculateNodeStyling(targetDom, property) {
const style = window.getComputedStyle(targetDom);
const value = parseFloat(style.getPropertyValue(property));
return value;
}
export {
oneOf,
findComponentUpward,
findComponentsDownward,
deepCopy,
calculateNodeStyling,
typeOf
};