2016-09-09 14:29:19 +08:00
|
|
|
// 判断参数是否是其中之一
|
2016-09-19 14:50:32 +08:00
|
|
|
export function oneOf (value, validList) {
|
2016-09-09 14:29:19 +08:00
|
|
|
for (let i = 0; i < validList.length; i++) {
|
|
|
|
if (value === validList[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-19 14:50:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function camelcaseToHyphen (str) {
|
|
|
|
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
2016-09-29 15:47:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For Modal scrollBar hidden
|
|
|
|
let cached;
|
|
|
|
export function getScrollBarSize (fresh) {
|
|
|
|
if (fresh || cached === undefined) {
|
|
|
|
const inner = document.createElement('div');
|
|
|
|
inner.style.width = '100%';
|
|
|
|
inner.style.height = '200px';
|
|
|
|
|
|
|
|
const outer = document.createElement('div');
|
|
|
|
const outerStyle = outer.style;
|
|
|
|
|
|
|
|
outerStyle.position = 'absolute';
|
|
|
|
outerStyle.top = 0;
|
|
|
|
outerStyle.left = 0;
|
|
|
|
outerStyle.pointerEvents = 'none';
|
|
|
|
outerStyle.visibility = 'hidden';
|
|
|
|
outerStyle.width = '200px';
|
|
|
|
outerStyle.height = '150px';
|
|
|
|
outerStyle.overflow = 'hidden';
|
|
|
|
|
|
|
|
outer.appendChild(inner);
|
|
|
|
|
|
|
|
document.body.appendChild(outer);
|
|
|
|
|
|
|
|
const widthContained = inner.offsetWidth;
|
|
|
|
outer.style.overflow = 'scroll';
|
|
|
|
let widthScroll = inner.offsetWidth;
|
|
|
|
|
|
|
|
if (widthContained === widthScroll) {
|
|
|
|
widthScroll = outer.clientWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.removeChild(outer);
|
|
|
|
|
|
|
|
cached = widthContained - widthScroll;
|
|
|
|
}
|
|
|
|
return cached;
|
2016-11-03 13:54:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// watch DOM change
|
2016-11-11 15:08:12 +08:00
|
|
|
export const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver || false;
|
|
|
|
|
|
|
|
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
|
|
|
|
const MOZ_HACK_REGEXP = /^moz([A-Z])/;
|
|
|
|
|
|
|
|
function camelCase(name) {
|
|
|
|
return name.replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
|
|
|
|
return offset ? letter.toUpperCase() : letter;
|
|
|
|
}).replace(MOZ_HACK_REGEXP, 'Moz$1');
|
|
|
|
}
|
|
|
|
// getStyle
|
|
|
|
export function getStyle (element, styleName) {
|
|
|
|
if (!element || !styleName) return null;
|
|
|
|
styleName = camelCase(styleName);
|
|
|
|
if (styleName === 'float') {
|
|
|
|
styleName = 'cssFloat';
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var computed = document.defaultView.getComputedStyle(element, '');
|
|
|
|
return element.style[styleName] || computed ? computed[styleName] : null;
|
|
|
|
} catch(e) {
|
|
|
|
return element.style[styleName];
|
|
|
|
}
|
|
|
|
}
|