bee-table/build/utils.js

254 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-01-12 08:53:51 +08:00
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
2018-12-02 16:53:06 +08:00
exports.Event = exports.tryParseInt = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
2017-01-12 08:53:51 +08:00
exports.measureScrollbar = measureScrollbar;
exports.debounce = debounce;
exports.warningOnce = warningOnce;
2018-05-11 09:29:43 +08:00
exports.addClass = addClass;
exports.removeClass = removeClass;
exports.ObjectAssign = ObjectAssign;
exports.closest = closest;
exports.getMaxColChildrenLength = getMaxColChildrenLength;
exports.getColChildrenLength = getColChildrenLength;
2017-01-12 08:53:51 +08:00
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
2018-05-11 09:29:43 +08:00
var _parseInt = require('lodash/parseInt');
var _parseInt2 = _interopRequireDefault(_parseInt);
2017-01-12 08:53:51 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var scrollbarSize = void 0;
2017-01-12 08:53:51 +08:00
// Measure scrollbar width for padding body during modal show/hide
var scrollbarMeasure = {
position: 'absolute',
top: '-9999px',
width: '50px',
height: '50px',
overflow: 'scroll'
};
function measureScrollbar() {
var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'vertical';
2017-01-12 08:53:51 +08:00
if (typeof document === 'undefined' || typeof window === 'undefined') {
return 0;
}
if (scrollbarSize) {
return scrollbarSize;
2017-01-12 08:53:51 +08:00
}
var scrollDiv = document.createElement('div');
Object.keys(scrollbarMeasure).forEach(function (scrollProp) {
scrollDiv.style[scrollProp] = scrollbarMeasure[scrollProp];
});
2017-01-12 08:53:51 +08:00
document.body.appendChild(scrollDiv);
var size = 0;
if (direction === 'vertical') {
size = scrollDiv.offsetWidth - scrollDiv.clientWidth;
} else if (direction === 'horizontal') {
size = scrollDiv.offsetHeight - scrollDiv.clientHeight;
}
2017-01-12 08:53:51 +08:00
document.body.removeChild(scrollDiv);
scrollbarSize = size;
return scrollbarSize;
2017-01-12 08:53:51 +08:00
}
function debounce(func, wait, immediate) {
var timeout = void 0;
return function debounceFunc() {
var context = this;
var args = arguments;
// https://fb.me/react-event-pooling
if (args[0] && args[0].persist) {
args[0].persist();
}
var later = function later() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
}
var warned = {};
function warningOnce(condition, format, args) {
if (!warned[format]) {
(0, _warning2["default"])(condition, format, args);
warned[format] = true;
}
2018-05-11 09:29:43 +08:00
}
var tryParseInt = exports.tryParseInt = function tryParseInt(value) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var resultValue = (0, _parseInt2["default"])(value);
if (isNaN(resultValue)) {
return defaultValue;
}
return resultValue;
};
function addClass(elm, className) {
if (!className) return;
var els = Array.isArray(elm) ? elm : [elm];
els.forEach(function (el) {
if (el.classList) {
el.classList.add(className.split(' '));
} else {
el.className += ' ' + className;
}
});
}
function removeClass(elm, className) {
if (!className) return;
var els = Array.isArray(elm) ? elm : [elm];
els.forEach(function (el) {
if (el.classList) {
el.classList.remove(className.split(' '));
} else {
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
});
}
/**
* 简单数组数据对象拷贝
* @param {*} obj 要拷贝的对象
*/
function ObjectAssign(obj) {
var b = obj instanceof Array;
var tagObj = b ? [] : {};
if (b) {
//数组
obj.forEach(function (da) {
var _da = {};
_extends(_da, da);
tagObj.push(_da);
});
} else {
_extends(tagObj, obj);
}
return tagObj;
}
/**
* 获取某个父元素
* */
function closest(ele, selector) {
var matches = ele.matches || ele.webkitMatchesSelector || ele.mozMatchesSelector || ele.msMatchesSelector;
if (matches) {
while (ele) {
if (matches.call(ele, selector)) {
return ele;
} else {
ele = ele.parentElement;
}
}
}
return null;
}
function getMaxColChildrenLength(columns) {
var arr = [];
arr = columns.map(function (item, index) {
var chilrenLen = 0;
if (item.children) {
chilrenLen = getColChildrenLength(item.children, chilrenLen + 1);
}
return chilrenLen;
});
var max = Math.max.apply(null, arr);
return max;
}
function getColChildrenLength(columns, chilrenLen) {
columns.forEach(function (item, index) {
if (item.children) {
chilrenLen = getColChildrenLength(item.children, chilrenLen + 1);
}
});
return chilrenLen;
2018-12-02 16:53:06 +08:00
}
function addHandler(element, type, handler) {
if (element.addEventListener) {
//检测是否为DOM2级方法
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
//检测是否为IE级方法
element.attachEvent("on" + type, handler);
} else {
//检测是否为DOM0级方法
element["on" + type] = handler;
}
}
function removeHandler(element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent("on" + type, handler);
} else {
element["on" + type] = null;
}
}
//获取事件对象的兼容性写法
function getEvent(event) {
return event ? event : window.event;
}
//获取事件对象目标的兼容性写法
function getTarget(event) {
return event.target || event.srcElement;
}
function preventDefault(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
function stopPropagation(event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
var Event = exports.Event = {
addHandler: addHandler,
removeHandler: removeHandler,
getEvent: getEvent,
getTarget: getTarget,
preventDefault: preventDefault,
stopPropagation: stopPropagation
};