Skip to content

Added RightClick option to config #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ dragElement.init = function init(options) {
element.onmousedown = onStart;
element.ontouchstart = onStart;

function onContextMenu(e) {
if(gd._context.rightClick) {
return;
}
var e2;
e.preventDefault();
try {
e2 = new MouseEvent('contextmenu', e);
}
catch(err) {
var offset = pointerOffset(e);
e2 = document.createEvent('MouseEvents');
e2.initMouseEvent('contextmenu',
e.bubbles, e.cancelable,
e.view, e.detail,
e.screenX, e.screenY,
offset[0], offset[1],
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
e.button, initialTarget);
}
initialTarget.dispatchEvent(e2);
onDone(e);
}

function onStart(e) {
// make dragging and dragged into properties of gd
// so that others can look at and modify them
Expand Down Expand Up @@ -111,6 +135,7 @@ dragElement.init = function init(options) {
document.documentElement.style.cursor = window.getComputedStyle(element).cursor;
}

document.addEventListener('contextmenu', onContextMenu);
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onDone);
document.addEventListener('touchmove', onMove);
Expand Down Expand Up @@ -138,6 +163,7 @@ dragElement.init = function init(options) {
}

function onDone(e) {
document.removeEventListener('contextmenu', onContextMenu);
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onDone);
document.removeEventListener('touchmove', onMove);
Expand Down
3 changes: 3 additions & 0 deletions src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ module.exports = {
// double click interaction (false, 'reset', 'autosize' or 'reset+autosize')
doubleClick: 'reset+autosize',

// right click interaction, if false browser context menu won't be shown, syntetic contextmenu event instead
rightClick: true,

// new users see some hints about interactivity
showTips: true,

Expand Down
18 changes: 18 additions & 0 deletions test/jasmine/tests/dragelement_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ describe('dragElement', function() {

expect(mockObj.dummy).not.toHaveBeenCalled();
});

it('should fire contextmenu event on element when option rightclick set to false', function() {
var options = { element: this.element, gd: this.gd };
options.gd._context = { rightClick: false };
dragElement.init(options);

var mockObj = {
handleContextMenu: function() {}
};
spyOn(mockObj, 'handleContextMenu');

this.element.oncontextmenu = mockObj.handleContextMenu;

mouseEvent('mousedown', this.x, this.y);
mouseEvent('contextmenu', this.x, this.y);

expect(mockObj.handleContextMenu).toHaveBeenCalled();
});
});

describe('dragElement.getCursor', function() {
Expand Down