yyzone
Version:
yyzone vue components and utils
216 lines (194 loc) • 5.57 kB
JavaScript
// oneOf
export function oneOf (value, validList) {
for (let i = 0; i < validList.length; i++) {
if (value === validList[i]) {
return true
}
}
return false
}
// typeOf
export function typeOf(obj) {
const toString = Object.prototype.toString
const types = {
'[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 types[toString.call(obj)] || 'undefined'
}
// deepCopy
export function deepCopy(obj, cache) {
if (cache === void 0) cache = []
if (obj === null || typeof obj !== 'object') {
return obj
}
let hit = find(cache, function (c) {
return c.original === obj
})
if (hit) {
return hit.copy
}
let copy = Array.isArray(obj) ? [] : {}
cache.push({
original: obj,
copy: copy
})
Object.keys(obj).forEach(function (key) {
copy[key] = deepCopy(obj[key], cache)
})
return copy
}
// find component
export 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
}
export function findComponentDownward (context, componentName) {
const childrens = context.$children
let children = null
if (childrens.length) {
for (const child of childrens) {
const name = child.$options.name
if (name === componentName) {
children = child
break
} else {
children = findComponentDownward(child, componentName)
if (children) break
}
}
}
return children
}
// find components
export function findComponentsDownward (context, componentName) {
return context.$children.reduce((components, child) => {
if (child.$options.name === componentName) components.push(child)
const foundChilds = findComponentsDownward(child, componentName)
return components.concat(foundChilds)
}, [])
}
export function findComponentsUpward (context, componentName) {
let parents = []
const parent = context.$parent
if (parent) {
if (parent.$options.name === componentName) parents.push(parent)
return parents.concat(findComponentsUpward(parent, componentName))
} else {
return []
}
}
// hasClass
export function hasClass(el, cls) {
if (!el || !cls) return false
if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.')
if (el.classList) {
return el.classList.contains(cls)
}
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1
}
// addClass
export function addClass(el, cls) {
if (!el) return
let curClass = el.className
const classes = (cls || '').split(' ')
for (let i = 0, j = classes.length ;i < j; i++) {
const clsName = classes[i]
if (!clsName) continue
if (el.classList) {
return el.classList.add(clsName)
}
if (!hasClass(el, clsName)) {
return curClass += ' ' + clsName
}
}
if (!el.classList) {
el.className = curClass
}
}
// removeClass
export function removeClass(el, cls) {
if (!el || !cls) return
const classes = cls.split(' ')
let curClass = ' ' + el.className + ' '
for (let i = 0, j = classes.length; i < j; i++) {
const clsName = classes[i]
if (!clsName) continue
if (el.classList) {
return el.classList.remove(clsName)
}
if (hasClass(el, clsName)) {
return curClass = curClass.replace(' ' + clsName + ' ', ' ')
}
}
if (!el.classList) {
el.className = trim(curClass)
}
}
//普通字符转换成转意符
export function htmlEscape(str) {
if (str && typeof str == 'string') {
return str.replace(/[\<\>\&\"\']/g, function (c) {
return {
'<': '<',
'>': '>',
'&': '&',
'"': '"',
'\'': '''
} [c]
})
}
return str
}
//转意符换成普通字符
export function escapeHtml(str) {
if (str && typeof str == 'string') {
let arrEntities = {
'lt': '<',
'gt': '>',
'nbsp': ' ',
'amp': '&',
'quot': '"',
'#60': '<',
'#62': '>',
'#38': '&',
'#34': '"',
'#39': '\''
}
return str.replace(/&(lt|gt|nbsp|amp|quot|\#(:?60|62|38|34|39))?/ig, function (all, t) {
return arrEntities[t]
})
}
return str
}
// 转换URL
export function revertURL() {
let {
protocol,
host,
pathname,
hash,
search
} = window.location
if (search !== '') {
window.location.href = decodeURIComponent([protocol, '//', host, pathname, hash, search].join(''))
}
}