Remove unused functions from utils, Rename promisify to nextTick
This commit is contained in:
parent
bc44058580
commit
f61f97d22f
@ -1,7 +1,7 @@
|
||||
import $ from './dom';
|
||||
import Clusterize from 'clusterize.js';
|
||||
import {
|
||||
promisify
|
||||
nextTick
|
||||
} from './utils';
|
||||
|
||||
export default class BodyRenderer {
|
||||
@ -13,7 +13,7 @@ export default class BodyRenderer {
|
||||
this.cellmanager = instance.cellmanager;
|
||||
this.bodyScrollable = instance.bodyScrollable;
|
||||
this.log = instance.log;
|
||||
this.appendRemainingData = promisify(this.appendRemainingData, this);
|
||||
this.appendRemainingData = nextTick(this.appendRemainingData, this);
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import {
|
||||
isNumeric,
|
||||
promisify,
|
||||
nextTick,
|
||||
isNumber,
|
||||
notSet
|
||||
} from './utils';
|
||||
@ -8,10 +8,10 @@ import {
|
||||
export default class DataManager {
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
this.sortRows = promisify(this.sortRows, this);
|
||||
this.switchColumn = promisify(this.switchColumn, this);
|
||||
this.removeColumn = promisify(this.removeColumn, this);
|
||||
this.filterRows = promisify(this.filterRows, this);
|
||||
this.sortRows = nextTick(this.sortRows, this);
|
||||
this.switchColumn = nextTick(this.switchColumn, this);
|
||||
this.removeColumn = nextTick(this.removeColumn, this);
|
||||
this.filterRows = nextTick(this.filterRows, this);
|
||||
}
|
||||
|
||||
init(data, columns) {
|
||||
@ -506,6 +506,12 @@ export default class DataManager {
|
||||
|
||||
getColumn(colIndex) {
|
||||
colIndex = +colIndex;
|
||||
|
||||
if (colIndex < 0) {
|
||||
// negative indexes
|
||||
colIndex = this.columns.length + colIndex;
|
||||
}
|
||||
|
||||
return this.columns.find(col => col.colIndex === colIndex);
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import $ from './dom';
|
||||
import {
|
||||
makeDataAttributeString,
|
||||
promisify,
|
||||
nextTick,
|
||||
ensureArray,
|
||||
linkProperties
|
||||
} from './utils';
|
||||
@ -18,7 +18,7 @@ export default class RowManager {
|
||||
]);
|
||||
|
||||
this.bindEvents();
|
||||
this.refreshRows = promisify(this.refreshRows, this);
|
||||
this.refreshRows = nextTick(this.refreshRows, this);
|
||||
}
|
||||
|
||||
get datamanager() {
|
||||
|
||||
92
src/utils.js
92
src/utils.js
@ -20,78 +20,6 @@ export function makeDataAttributeString(props) {
|
||||
.trim();
|
||||
}
|
||||
|
||||
export function getDefault(a, b) {
|
||||
return a !== undefined ? a : b;
|
||||
}
|
||||
|
||||
export function escapeRegExp(str) {
|
||||
// https://stackoverflow.com/a/6969486
|
||||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
|
||||
}
|
||||
|
||||
export function getCSSString(styleMap) {
|
||||
let style = '';
|
||||
|
||||
for (const prop in styleMap) {
|
||||
if (styleMap.hasOwnProperty(prop)) {
|
||||
style += `${prop}: ${styleMap[prop]}; `;
|
||||
}
|
||||
}
|
||||
|
||||
return style.trim();
|
||||
}
|
||||
|
||||
export function getCSSRuleBlock(rule, styleMap) {
|
||||
return `${rule} { ${getCSSString(styleMap)} }`;
|
||||
}
|
||||
|
||||
export function buildCSSRule(rule, styleMap, cssRulesString = '') {
|
||||
// build css rules efficiently,
|
||||
// append new rule if doesnt exist,
|
||||
// update existing ones
|
||||
|
||||
const rulePatternStr = `${escapeRegExp(rule)} {([^}]*)}`;
|
||||
const rulePattern = new RegExp(rulePatternStr, 'g');
|
||||
|
||||
if (cssRulesString && cssRulesString.match(rulePattern)) {
|
||||
for (const property in styleMap) {
|
||||
const value = styleMap[property];
|
||||
const propPattern = new RegExp(`${escapeRegExp(property)}:([^;]*);`);
|
||||
|
||||
cssRulesString = cssRulesString.replace(rulePattern, function (match, propertyStr) {
|
||||
if (propertyStr.match(propPattern)) {
|
||||
// property exists, replace value with new value
|
||||
propertyStr = propertyStr.replace(propPattern, (match, valueStr) => {
|
||||
return `${property}: ${value};`;
|
||||
});
|
||||
}
|
||||
propertyStr = propertyStr.trim();
|
||||
|
||||
const replacer =
|
||||
`${rule} { ${propertyStr} }`;
|
||||
|
||||
return replacer;
|
||||
});
|
||||
}
|
||||
|
||||
return cssRulesString;
|
||||
}
|
||||
// no match, append new rule block
|
||||
return `${cssRulesString}${getCSSRuleBlock(rule, styleMap)}`;
|
||||
}
|
||||
|
||||
export function removeCSSRule(rule, cssRulesString = '') {
|
||||
const rulePatternStr = `${escapeRegExp(rule)} {([^}]*)}`;
|
||||
const rulePattern = new RegExp(rulePatternStr, 'g');
|
||||
let output = cssRulesString;
|
||||
|
||||
if (cssRulesString && cssRulesString.match(rulePattern)) {
|
||||
output = cssRulesString.replace(rulePattern, '');
|
||||
}
|
||||
|
||||
return output.trim();
|
||||
}
|
||||
|
||||
export function copyTextToClipboard(text) {
|
||||
// https://stackoverflow.com/a/30810322/5353542
|
||||
var textArea = document.createElement('textarea');
|
||||
@ -155,23 +83,25 @@ export let throttle = _throttle;
|
||||
|
||||
export let debounce = _debounce;
|
||||
|
||||
export function promisify(fn, context = null) {
|
||||
export function nextTick(fn, context = null) {
|
||||
return (...args) => {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
const execute = () => {
|
||||
const out = fn.apply(context, args);
|
||||
resolve(out);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
if (window.setImmediate) {
|
||||
setImmediate(execute);
|
||||
} else if (window.requestAnimationFrame) {
|
||||
requestAnimationFrame(execute);
|
||||
} else {
|
||||
setTimeout(execute);
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
export function chainPromises(promises) {
|
||||
return promises.reduce(
|
||||
(prev, cur) => prev.then(cur), Promise.resolve()
|
||||
);
|
||||
};
|
||||
|
||||
export function linkProperties(target, source, properties) {
|
||||
const props = properties.reduce((acc, prop) => {
|
||||
acc[prop] = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user