2020-01-30 20:59:37 +08:00
|
|
|
import {
|
|
|
|
|
appendChild,
|
2020-01-31 16:30:30 +08:00
|
|
|
createElement,
|
|
|
|
|
querySelector,
|
|
|
|
|
appendChildren,
|
2020-02-05 17:10:11 +08:00
|
|
|
querySelectorAll,
|
2020-01-30 20:59:37 +08:00
|
|
|
} 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");
|
2020-01-30 20:59:37 +08:00
|
|
|
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])
|
2020-01-30 20:59:37 +08:00
|
|
|
appendChild(wrapper, table)
|
2020-01-13 16:25:20 +08:00
|
|
|
return wrapper;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 17:10:11 +08:00
|
|
|
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-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
|
|
|
|
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;
|
2020-01-30 20:59:37 +08:00
|
|
|
let colgroup = createElement("colgroup");
|
2020-01-13 16:25:20 +08:00
|
|
|
arr.forEach(item => {
|
2020-01-30 20:59:37 +08:00
|
|
|
let col = createElement("col");
|
2020-01-13 16:25:20 +08:00
|
|
|
col.setAttribute("width", item);
|
2020-01-30 20:59:37 +08:00
|
|
|
appendChild(colgroup, col)
|
2020-01-13 16:25:20 +08:00
|
|
|
})
|
|
|
|
|
return colgroup;
|
2020-01-30 20:59:37 +08:00
|
|
|
}
|