throttle selectArea on mousemove

This commit is contained in:
Faris Ansari 2017-11-21 19:02:57 +05:30
parent 86f9bba5d2
commit 752e53ec7b
4 changed files with 82 additions and 6 deletions

View File

@ -277,6 +277,7 @@ exports.buildCSSRule = buildCSSRule;
exports.removeCSSRule = removeCSSRule;
exports.copyTextToClipboard = copyTextToClipboard;
exports.isNumeric = isNumeric;
exports.throttle = throttle;
function camelCaseToDash(str) {
return str.replace(/([A-Z])/g, function (g) {
return '-' + g[0].toLowerCase();
@ -433,6 +434,41 @@ function isNumeric(val) {
return !isNaN(val);
}
// https://stackoverflow.com/a/27078401
function throttle(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function later() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function () {
var now = Date.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
@ -663,10 +699,12 @@ var CellManager = function () {
mouseDown = false;
});
_dom2.default.on(this.bodyScrollable, 'mousemove', '.data-table-col', function (e) {
var selectArea = function selectArea(e) {
if (!mouseDown) return;
_this5.selectArea((0, _dom2.default)(e.delegatedTarget));
});
};
_dom2.default.on(this.bodyScrollable, 'mousemove', '.data-table-col', (0, _utils.throttle)(selectArea, 100));
}
}, {
key: 'focusCell',

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
import {
copyTextToClipboard,
makeDataAttributeString
makeDataAttributeString,
throttle
} from './utils';
import keyboard from './keyboard';
import $ from './dom';
@ -169,10 +170,12 @@ export default class CellManager {
mouseDown = false;
});
$.on(this.bodyScrollable, 'mousemove', '.data-table-col', (e) => {
const selectArea = (e) => {
if (!mouseDown) return;
this.selectArea($(e.delegatedTarget));
});
};
$.on(this.bodyScrollable, 'mousemove', '.data-table-col', throttle(selectArea, 100));
}
focusCell($cell, { skipClearSelection = 0 } = {}) {

View File

@ -147,3 +147,38 @@ export function copyTextToClipboard(text) {
export function isNumeric(val) {
return !isNaN(val);
}
// https://stackoverflow.com/a/27078401
export function throttle(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
let later = function () {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function () {
var now = Date.now();
if (!previous && options.leading === false) previous = now;
let remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};