Files
smart-table/lib/core/utils.js

94 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-01-20 11:28:19 +08:00
export function createTabelWrapper(className, vm, type, content) {
2020-01-13 16:25:20 +08:00
let wrapper = document.createElement("div");
wrapper.className = className;
let table = document.createElement("table");
table.className = "smart-table_" + type;
2020-01-20 11:28:19 +08:00
table.style.width = (vm.size.tabelWidth - 1) + "px";
2020-01-13 16:25:20 +08:00
table.appendChild(createColgroup(vm.colgroup));
table.appendChild(content);
wrapper.appendChild(table);
return wrapper;
}
2020-01-20 11:28:19 +08:00
export function replaceColGroup(vm, wrapper) {
2020-01-16 14:25:41 +08:00
let colgroup = createColgroup(vm.colgroup);
wrapper.replaceChild(colgroup, wrapper.querySelector('colgroup'));
}
2020-01-20 11:28:19 +08:00
export function getIntByAttr(el, key, def) {
2020-01-13 16:25:20 +08:00
return Number.parseInt(el.getAttribute(key) || def)
}
2020-01-20 11:28:19 +08:00
export function getEmptyIndexInArray(array) {
2020-01-13 16:25:20 +08:00
for (let i = 0, len = array.length; i < len; i++) {
if (array[i] === undefined) {
return i
}
}
}
2020-01-20 11:28:19 +08:00
export function throttle(delay, callback) {
2020-01-13 16:25:20 +08:00
let timeoutID;
let lastExec = 0;
2020-01-20 11:28:19 +08:00
function wrapper() {
2020-01-13 16:25:20 +08:00
let self = this;
let elapsed = new Date().getTime() - lastExec;
let args = arguments;
2020-01-20 11:28:19 +08:00
function exec() {
2020-01-13 16:25:20 +08:00
lastExec = new Date().getTime();
callback.apply(self, args)
}
2020-01-20 11:28:19 +08:00
function clear() {
2020-01-13 16:25:20 +08:00
timeoutID = undefined;
}
if (timeoutID) {
clearTimeout(timeoutID)
}
if (elapsed > delay) {
exec()
} else {
timeoutID = setTimeout(exec, delay - elapsed)
}
}
return wrapper;
}
2020-01-20 11:28:19 +08:00
export function debounce(delay, callback) {
2020-01-16 14:25:41 +08:00
let timeout = null;
return function () {
if (timeout !== null) clearTimeout(timeout);
timeout = setTimeout(callback, delay);
}
}
2020-01-20 11:28:19 +08:00
export function isWindows() {
2020-01-13 16:25:20 +08:00
var agent = navigator.userAgent.toLowerCase();
if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
return true;
}
if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
return true;
}
return false;
}
//创建colgroup节点
2020-01-20 11:28:19 +08:00
function createColgroup(arr) {
2020-01-13 16:25:20 +08:00
if (!arr) return;
let colgroup = document.createElement("colgroup");
arr.forEach(item => {
let col = document.createElement("col");
col.setAttribute("width", item);
colgroup.appendChild(col)
})
return colgroup;
}