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

103 lines
2.2 KiB
JavaScript
Raw Normal View History

import {
appendChild,
2020-02-06 18:51:14 +08:00
getAttribute,
createElement,
querySelector,
appendChildren,
querySelectorAll,
} from './node-ops';
export function refactorCell(cell) {
let nodes = cell.childNodes;
2020-02-04 11:59:05 +08:00
let wrapper = createElement("div", "stb_cell");
while (nodes.length) {
appendChild(wrapper, nodes[0])
}
appendChild(cell, wrapper)
}
2020-02-05 11:12:40 +08:00
export function createTableWrapper(className, vm, type, content) {
2020-01-31 13:23:47 +08:00
let wrapper = createElement("div", className);
2020-02-04 11:59:05 +08:00
let table = createElement("table", "stb_" + type);
2020-02-05 11:12:40 +08:00
table.style.width = vm.size.tableWidth + "px";
appendChildren(table, [createColgroup(vm.colgroup), content])
appendChild(wrapper, table)
2020-01-13 16:25:20 +08:00
return wrapper;
}
export function replaceColGroup(vm) {
querySelectorAll(vm.$root, 'table').forEach(table => {
table.style.width = vm.size.tableWidth + "px";
table.replaceChild(createColgroup(vm.colgroup), querySelector(table, 'colgroup'));
})
2020-01-16 14:25:41 +08:00
}
2020-02-06 18:51:14 +08:00
export function getAttrNumber(el, key, def) {
return Number.parseInt(getAttribute(el, key) || def)
2020-01-13 16:25:20 +08:00
}
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
2020-02-04 11:59:05 +08:00
const self = this;
const elapsed = new Date().getTime() - lastExec;
const args = arguments;
2020-01-13 16:25:20 +08:00
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-02-04 11:59:05 +08:00
timeoutID && clearTimeout(timeoutID)
2020-01-13 16:25:20 +08:00
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-02-04 11:59:05 +08:00
let timeoutID;
function wrapper() {
const self = this;
const args = arguments;
function exec() {
callback.apply(self, args)
}
timeoutID && clearTimeout(timeoutID)
timeoutID = setTimeout(exec, delay)
2020-01-16 14:25:41 +08:00
}
2020-02-04 11:59:05 +08:00
return wrapper;
2020-01-16 14:25:41 +08:00
}
2020-02-05 11:12:40 +08:00
function createColgroup(arr) {
2020-01-13 16:25:20 +08:00
if (!arr) return;
let colgroup = createElement("colgroup");
2020-01-13 16:25:20 +08:00
arr.forEach(item => {
let col = createElement("col");
2020-01-13 16:25:20 +08:00
col.setAttribute("width", item);
appendChild(colgroup, col)
2020-01-13 16:25:20 +08:00
})
return colgroup;
}