Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jimmyye/46b2ee89c4c0385f29ed5b737fe70da7 to your computer and use it in GitHub Desktop.
Save jimmyye/46b2ee89c4c0385f29ed5b737fe70da7 to your computer and use it in GitHub Desktop.
Handsontable plugin - Observe dblClick event
function ObserveDblClick() {
var plugin = this;
// public methods
this.init = function() {
bindHeaders.call(this);
};
this.dblclickColHeader = function(hot, col) {
hot.runHooks('dblclickColHeader', col);
};
this.dblclickRowHeader = function(hot, row) {
hot.runHooks('dblclickRowHeader', row);
};
this.dblclickCell = function(hot, row, col) {
hot.runHooks('dblclickCell', row, col);
};
// private methods
var bindHeaders = function() {
const hot = this;
const eventManager = new Handsontable.EventManager(hot);
const hasClass = Handsontable.dom.hasClass;
eventManager.addEventListener(hot.rootElement, 'dblclick', function(e) {
const el = e.target;
const isHeader = hasClass(el, 'relative');
const isRowHeader = hasClass(el, 'rowHeader');
const isColHeader = hasClass(el, 'colHeader');
const isCell = hasClass(el, 'highlight');
// const isCorner = hasClass(el, 'corner');
if (isHeader || isRowHeader || isColHeader || isCell) {
const [row, col] = hot.getSelectedLast();
// use getColumn to get index of the closest th
if (col == 0 && (isHeader || isRowHeader) && getColumn(el) === 0) {
plugin.dblclickRowHeader(hot, row);
} else if (row == 0 && (isHeader || isColHeader) && getColumn(el) === 1) {
plugin.dblclickColHeader(hot, col);
} else {
plugin.dblclickCell(hot, row, col);
}
}
});
};
function getColumn(target) {
var TH = Handsontable.dom.closest(target, 'TH');
return Handsontable.dom.index(TH);
}
// function getRow(target) {
// var TR = Handsontable.dom.closest(target, 'TR');
// return Handsontable.dom.index(TR);
// }
//
// function isCorner(target) {
// var TR = Handsontable.dom.closest(target, 'TR');
// return !TR.querySelector('.rowHeader');
// }
//
// function countRowHeaders(instance) {
// var THs = instance.view.TBODY.querySelector('tr').querySelectorAll('th');
// return THs.length;
// }
};
var observeDblClick = new ObserveDblClick();
Handsontable.hooks.add('afterInit', observeDblClick.init);
Handsontable.hooks.register('dblclickColHeader');
Handsontable.hooks.register('dblclickRowHeader');
Handsontable.hooks.register('dblclickCell');
Handsontable.plugins.ObserveDblClick = ObserveDblClick;
// Handsontable v0.38.1
hot.addHooks('dblclickCell', function(row, cell) {
//...
});
hot.addHooks('dblclickRowHeader', function(row) {
//...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment