From f61f97d22f5b39aefd1d7f13a703ba145aaf3f57 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 19 Mar 2018 00:19:52 +0530 Subject: [PATCH] Remove unused functions from utils, Rename promisify to nextTick --- src/body-renderer.js | 4 +- src/datamanager.js | 16 +++++--- src/rowmanager.js | 4 +- src/utils.js | 92 ++++++-------------------------------------- 4 files changed, 26 insertions(+), 90 deletions(-) diff --git a/src/body-renderer.js b/src/body-renderer.js index ad88fe7..a3320f7 100644 --- a/src/body-renderer.js +++ b/src/body-renderer.js @@ -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() { diff --git a/src/datamanager.js b/src/datamanager.js index 186cc83..4a814b5 100644 --- a/src/datamanager.js +++ b/src/datamanager.js @@ -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); } diff --git a/src/rowmanager.js b/src/rowmanager.js index 15f604d..7381b7d 100644 --- a/src/rowmanager.js +++ b/src/rowmanager.js @@ -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() { diff --git a/src/utils.js b/src/utils.js index a73566f..1c196c9 100644 --- a/src/utils.js +++ b/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] = {