kingdot
Version:
A UI Components Library For Vue
277 lines (248 loc) • 9.1 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
import { factory } from 'ulid';
export var ulid = function () {
var crypto = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && (window.crypto || window.msCrypto); // IE 11 uses window.msCrypto
if (!crypto || !crypto.getRandomValues) {
return factory(Math.random);
} else {
return factory();
}
}();
function isNullOrUndefined(o) {
return o === null || o === undefined;
}
export function isNumber(o) {
return typeof o === 'number';
}
var pathMap = {};
var i18n = {};
var hasOwn = Object.prototype.hasOwnProperty;
var charCodeOfDot = '.'.charCodeAt(0);
var rePropName = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' + '|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
"([^\"'].*)" + '|' +
// Or match strings (supports escaping characters).
"([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" + ')\\]' + '|' +
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))', 'g');
// eslint-disable-next-line no-useless-escape
var valueRegexp = /\{([^\}\s]+)\}/g;
function castPath(path) {
if (typeof path !== 'string') return path;
if (pathMap[path]) return pathMap[path];
var result = [];
if (path.charCodeAt(0) === charCodeOfDot) {
result.push('');
}
path.replace(rePropName, function (match, expression, quote, subString) {
var key = match;
if (quote) {
// eslint-disable-next-line no-undef
key = subString.replace(reEscapeChar, '$1');
} else if (expression) {
key = expression;
}
result.push(key);
});
pathMap[path] = result;
return result;
}
function get(object, path, defaultValue) {
if (hasOwn.call(object, path)) return object[path];
path = castPath(path);
var index = 0;
var length = path.length;
while (!isNullOrUndefined(object) && index < length) {
object = object[path[index++]];
}
return index && index === length && object !== undefined ? object : defaultValue;
}
export function _$(key, data) {
var value = get(i18n, key);
if (isNullOrUndefined(value)) {
value = key;
}
if (data) {
value = value.replace(valueRegexp, function (nouse, variable) {
var suffix = void 0;
var index = variable.indexOf(':');
if (index > 0) {
suffix = variable.substr(0, index);
suffix = suffix.split('|');
variable = variable.substr(index + 1);
variable = get(data, variable);
if (variable > 1) {
return suffix.length > 1 ? suffix[1] : suffix[0];
} else {
return suffix.length > 1 ? suffix[0] : '';
}
} else {
variable = get(data, variable);
return isNullOrUndefined(variable) ? nouse : variable;
}
});
}
return value;
}
export function getElementInScroll(e, eqValue) {
if (typeof eqValue !== 'number') {
eqValue = eqValue.offsetHeight;
}
var data = e.getBoundingClientRect();
var clientHeight = document.body.clientHeight;
var selfHeight = e.offsetHeight;
var result = void 0;
if (clientHeight - data.y - selfHeight - eqValue > 0) {
result = true;
} else if (data.y - eqValue > 0) {
result = false;
} else {
result = true;
}
return result;
}
var _scrollbarWdith = null;
var _getScrollbarWidth = function _getScrollbarWidth() {
var odiv = document.createElement('div'); // 创建一个div
var styles = {
width: '100px',
height: '100px',
overflowY: 'scroll' // 让他有滚动条
};
var i;
var scrollbarWidth;
for (i in styles) {
odiv.style[i] = styles[i];
}odiv.setAttribute('id', 'tet_scroll');
document.body.appendChild(odiv); // 把div添加到body中
scrollbarWidth = odiv.offsetWidth - odiv.clientWidth; // 相减
_removeNode(odiv); // 移除创建的div
return scrollbarWidth; // 返回滚动条宽度
};
function _removeNode(node) {
node.remove ? node.remove() : node.parentElement.removeChild(node);
}
export function getScrollbarWidth() {
if (!_scrollbarWdith) {
_scrollbarWdith = _getScrollbarWidth();
}
return _scrollbarWdith;
}
export function getStyle(obj, attr, getNumber) {
var _result;
if (obj.currentStyle) {
_result = obj.currentStyle[attr];
} else {
_result = document.defaultView.getComputedStyle(obj, null)[attr];
}
return getNumber ? Number.parseInt(_result) : _result;
}
var windowPaddingMin = 1;
export function mouseDragBegin(_ref, dragFn, endFn) {
var x = _ref.x,
y = _ref.y,
target = _ref.target;
var outBraking = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var startPosition = {
x: x,
y: y
};
var prePosition = {
x: x,
y: y
};
var _target$getClientRect = target.getClientRects()[0],
top = _target$getClientRect.top,
left = _target$getClientRect.left,
width = _target$getClientRect.width,
height = _target$getClientRect.height;
var _document$body = document.body,
offsetHeight = _document$body.offsetHeight,
offsetWidth = _document$body.offsetWidth;
var minLeft = (outBraking.left || windowPaddingMin) + (startPosition.x - left);
var minTop = (outBraking.top || windowPaddingMin) + (startPosition.y - top);
var maxTop = offsetHeight - height;
var maxLeft = offsetWidth - (outBraking.right || windowPaddingMin) - (width - (startPosition.x - left));
var mouseMove = function mouseMove(e) {
e = window.event || e;
var _x, _y;
if (minLeft > e.x) {
_x = minLeft;
} else if (e.x > maxLeft) {
_x = maxLeft;
} else {
_x = e.x;
}
if (e.y < minTop) {
_y = minTop;
} else if (e.y > maxTop) {
_y = maxTop;
} else {
_y = e.y;
}
prePosition = { x: _x, y: _y };
dragFn(startPosition, prePosition);
e.preventDefault();
};
var mouseUp = function mouseUp() {
endFn && endFn(startPosition, prePosition);
document.removeEventListener('mousemove', mouseMove);
document.removeEventListener('mouseup', mouseUp);
};
document.addEventListener('mousemove', mouseMove);
document.addEventListener('mouseup', mouseUp);
}
var _requestAnimationFrame = window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : setTimeout;
export function animationFrame(fn) {
return _requestAnimationFrame(fn);
}
export function stopPropagation(event) {
window.event ? window.event.cancelBubble = true : event.stopPropagation();
}
var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
// eslint-disable-next-line no-unused-vars
var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1;
export function isIe() {
var result = false;
var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1; // 判断是否IE<11浏览器
var isEdge = userAgent.indexOf('Edge') > -1 && !isIE; // 判断是否IE的Edge浏览器
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;
if (isIE || isEdge || isIE11) {
result = true;
} else {
result = false;
}
return result;
}
export function isEmpty(val) {
// val 为 null、undefined 时
if (val == null) return true;
if (typeof val === 'number') return isNaN(val);
if (typeof val === 'boolean' || val instanceof RegExp) return false;
if (typeof val === 'string') return val.trim().length === 0;
if ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object') {
if (Object.prototype.toString.call(val) === '[object Array]') return !val.length;
if (Object.prototype.toString.call(val) === '[object Object]') return !Object.keys(val).length;
}
return false;
}
export function isFunction(fn) {
return fn && Object.toString.call(fn) === '[object Function]';
}
export function arrayEquals(arr1, arr2) {
if (!(arr1 || arr2)) return false;
if (arr1.length !== arr2.length) return false;
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] instanceof Array && arr2[i] instanceof Array) {
return arrayEquals(arr1[i], arr2[i]);
} else {
if (arr1[i] !== arr2[i]) return false;
}
}
return true;
}