UNPKG

@mdsfe/mds-ui

Version:

A set of enterprise-class Vue UI components.

201 lines (183 loc) 6.18 kB
'use strict'; exports.__esModule = true; exports.isFunction = exports.getOffsetWidth = exports.escapeRegexpString = exports.assert = exports.isFirefox = exports.isEdge = exports.isIE = exports.coerceTruthyValueToArray = exports.arrayFind = exports.arrayFindIndex = exports.valueEquals = exports.getOffsetLeft = exports.noop = undefined; exports.getValueByPath = getValueByPath; exports.fixBodyScroll = fixBodyScroll; exports.hasOwn = hasOwn; var _vue = require('vue'); var _vue2 = _interopRequireDefault(_vue); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // import { getScrollBarWidth } from './_popper/dom-helper' var currentBodyOverflow = ''; // let currentBodyPaddingRight = '' var noop = exports.noop = function noop() {}; var getScroll = function getScroll(w, top) { var ret = top ? w.pageYOffset : w.pageXOffset; var method = top ? 'scrollTop' : 'scrollLeft'; if (typeof ret !== 'number') { var d = w.document; // ie6,7,8 standard mode ret = d.documentElement[method]; if (typeof ret !== 'number') { // quirks mode ret = d.body[method]; } } return ret; }; var getClientPosition = function getClientPosition(elem) { var box = void 0; var x = void 0; var y = void 0; var doc = elem.ownerDocument; var body = doc.body; var docElem = doc && doc.documentElement; box = elem.getBoundingClientRect(); x = box.left; y = box.top; x -= docElem.clientLeft || body.clientLeft || 0; y -= docElem.clientTop || body.clientTop || 0; return { left: x, top: y }; }; var getOffsetLeft = exports.getOffsetLeft = function getOffsetLeft(el) { var pos = getClientPosition(el); var doc = el.ownerDocument; var w = doc.defaultView || doc.parentWindow; pos.left += getScroll(w); return pos.left; }; /** * 获取对象深层级的对象,避免循环 * @export * @param {*} sourceObj 源对象 {a:{b:{d:1}}} * @param {*} pathName path组成的数组 例如:'a.b.c' * @param {*} defaultValue 如果没有值默认的值 [] */ function getValueByPath(sourceObj, pathName, defaultValue) { var getValue = function getValue(sourceObj, pathNameArray) { var key = pathNameArray.shift(); var value = sourceObj[key]; if (value === undefined || value === null) { return defaultValue; } else if (pathNameArray.length === 0) { return value; } else if (pathNameArray.length > 0) { return getValue(value, pathNameArray); } }; var pathNameArray = pathName.split('.'); return getValue(sourceObj, pathNameArray); } /** * * 判断两个数是否相等 * @param {*} a * @param {*} b * @returns */ var valueEquals = exports.valueEquals = function valueEquals(a, b) { // see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript if (a === b) return true; if (!(a instanceof Array)) return false; if (!(b instanceof Array)) return false; if (a.length !== b.length) return false; for (var i = 0; i !== a.length; ++i) { if (a[i] !== b[i]) return false; } return true; }; // TODO: use native Array.find, Array.findIndex when IE support is dropped var arrayFindIndex = exports.arrayFindIndex = function arrayFindIndex(arr, pred) { for (var i = 0; i !== arr.length; ++i) { if (pred(arr[i])) { return i; } } return -1; }; var arrayFind = exports.arrayFind = function arrayFind(arr, pred) { var idx = arrayFindIndex(arr, pred); return idx !== -1 ? arr[idx] : undefined; }; // coerce truthy value to array var coerceTruthyValueToArray = exports.coerceTruthyValueToArray = function coerceTruthyValueToArray(val) { if (Array.isArray(val)) { return val; } else if (val) { return [val]; } else { return []; } }; var isIE = exports.isIE = function isIE() { return !_vue2.default.prototype.$isServer && !isNaN(Number(document.documentMode)); }; var isEdge = exports.isEdge = function isEdge() { return !_vue2.default.prototype.$isServer && navigator.userAgent.indexOf('Edge') > -1; }; var isFirefox = exports.isFirefox = function isFirefox() { return !_vue2.default.prototype.$isServer && !!window.navigator.userAgent.match(/firefox/i); }; var assert = exports.assert = function assert(condition, msg) { if (!condition) throw new Error('[mds-ui] ' + msg); }; /** * * 特殊字符转译 * @param {string} [value=''] */ var escapeRegexpString = exports.escapeRegexpString = function escapeRegexpString() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); }; /** * Get element offset width * @param {*} node HTMLDOMElemment * @return {Number} width */ /** * Get element offset width * @param {*} node HTMLDOMElemment * @return {Number} width */ var getOffsetWidth = exports.getOffsetWidth = function getOffsetWidth(node) { return node ? node.offsetWidth : 0; }; /** 判断是否为函数 * @param {*} functionToCheck * @returns */ var isFunction = exports.isFunction = function isFunction(functionToCheck) { var getType = {}; return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; }; /** * 弹窗类组件显现时修正body滚动 * @param {Boolean} visible 弹窗显现状态 */ function fixBodyScroll(visible) { var bodyStyle = document.body.style; if (visible) { currentBodyOverflow = bodyStyle.overflow; // currentBodyPaddingRight = bodyStyle.paddingRight bodyStyle.overflow = 'hidden'; // const basePaddingRight = parseInt(currentBodyPaddingRight || 0) // const baseScrollWidth = parseInt(getScrollBarWidth()) // 防止页面抖动,后续需要自定义滚动条解决 // bodyStyle.paddingRight = basePaddingRight + baseScrollWidth + 'px' } else { var timer = setTimeout(function () { clearTimeout(timer); bodyStyle.overflow = currentBodyOverflow; // bodyStyle.paddingRight = currentBodyPaddingRight // currentBodyPaddingRight = '' }, 180); } } var hasOwnProperty = Object.prototype.hasOwnProperty; function hasOwn(obj, key) { return hasOwnProperty.call(obj, key); }