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 $ from './dom';
|
||||||
import Clusterize from 'clusterize.js';
|
import Clusterize from 'clusterize.js';
|
||||||
import {
|
import {
|
||||||
promisify
|
nextTick
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
|
||||||
export default class BodyRenderer {
|
export default class BodyRenderer {
|
||||||
@ -13,7 +13,7 @@ export default class BodyRenderer {
|
|||||||
this.cellmanager = instance.cellmanager;
|
this.cellmanager = instance.cellmanager;
|
||||||
this.bodyScrollable = instance.bodyScrollable;
|
this.bodyScrollable = instance.bodyScrollable;
|
||||||
this.log = instance.log;
|
this.log = instance.log;
|
||||||
this.appendRemainingData = promisify(this.appendRemainingData, this);
|
this.appendRemainingData = nextTick(this.appendRemainingData, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
isNumeric,
|
isNumeric,
|
||||||
promisify,
|
nextTick,
|
||||||
isNumber,
|
isNumber,
|
||||||
notSet
|
notSet
|
||||||
} from './utils';
|
} from './utils';
|
||||||
@ -8,10 +8,10 @@ import {
|
|||||||
export default class DataManager {
|
export default class DataManager {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.sortRows = promisify(this.sortRows, this);
|
this.sortRows = nextTick(this.sortRows, this);
|
||||||
this.switchColumn = promisify(this.switchColumn, this);
|
this.switchColumn = nextTick(this.switchColumn, this);
|
||||||
this.removeColumn = promisify(this.removeColumn, this);
|
this.removeColumn = nextTick(this.removeColumn, this);
|
||||||
this.filterRows = promisify(this.filterRows, this);
|
this.filterRows = nextTick(this.filterRows, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
init(data, columns) {
|
init(data, columns) {
|
||||||
@ -506,6 +506,12 @@ export default class DataManager {
|
|||||||
|
|
||||||
getColumn(colIndex) {
|
getColumn(colIndex) {
|
||||||
colIndex = +colIndex;
|
colIndex = +colIndex;
|
||||||
|
|
||||||
|
if (colIndex < 0) {
|
||||||
|
// negative indexes
|
||||||
|
colIndex = this.columns.length + colIndex;
|
||||||
|
}
|
||||||
|
|
||||||
return this.columns.find(col => col.colIndex === colIndex);
|
return this.columns.find(col => col.colIndex === colIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import $ from './dom';
|
import $ from './dom';
|
||||||
import {
|
import {
|
||||||
makeDataAttributeString,
|
makeDataAttributeString,
|
||||||
promisify,
|
nextTick,
|
||||||
ensureArray,
|
ensureArray,
|
||||||
linkProperties
|
linkProperties
|
||||||
} from './utils';
|
} from './utils';
|
||||||
@ -18,7 +18,7 @@ export default class RowManager {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
this.bindEvents();
|
this.bindEvents();
|
||||||
this.refreshRows = promisify(this.refreshRows, this);
|
this.refreshRows = nextTick(this.refreshRows, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
get datamanager() {
|
get datamanager() {
|
||||||
|
|||||||
92
src/utils.js
92
src/utils.js
@ -20,78 +20,6 @@ export function makeDataAttributeString(props) {
|
|||||||
.trim();
|
.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) {
|
export function copyTextToClipboard(text) {
|
||||||
// https://stackoverflow.com/a/30810322/5353542
|
// https://stackoverflow.com/a/30810322/5353542
|
||||||
var textArea = document.createElement('textarea');
|
var textArea = document.createElement('textarea');
|
||||||
@ -155,23 +83,25 @@ export let throttle = _throttle;
|
|||||||
|
|
||||||
export let debounce = _debounce;
|
export let debounce = _debounce;
|
||||||
|
|
||||||
export function promisify(fn, context = null) {
|
export function nextTick(fn, context = null) {
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
setTimeout(() => {
|
const execute = () => {
|
||||||
const out = fn.apply(context, args);
|
const out = fn.apply(context, args);
|
||||||
resolve(out);
|
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) {
|
export function linkProperties(target, source, properties) {
|
||||||
const props = properties.reduce((acc, prop) => {
|
const props = properties.reduce((acc, prop) => {
|
||||||
acc[prop] = {
|
acc[prop] = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user