Skip to content

Commit

Permalink
Added middle click mouse down managment (#3764)
Browse files Browse the repository at this point in the history
* added test
* fixed test
  • Loading branch information
asturur committed Mar 14, 2017
1 parent a816742 commit 45f102b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@
*/
fireRightClick: false,

/**
* Indicates if the canvas can fire middle click events
* @type Boolean
* @since 1.7.8
* @default
*/
fireMiddleClick: false,

/**
* @private
*/
Expand Down
8 changes: 8 additions & 0 deletions src/mixins/canvas_events.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@
return;
}

var isMiddleClick = 'which' in e ? e.which === 2 : e.button === 1;
if (isMiddleClick) {
if (this.fireMiddleClick) {
this._handleEvent(e, 'down', target ? target : null);
}
return;
}

if (this.isDrawingMode) {
this._onMouseDownInDrawingMode(e);
return;
Expand Down
25 changes: 25 additions & 0 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -1726,4 +1726,29 @@
ok(typeof InheritedCanvasClass === 'function');
});

test('mouse:down with different buttons', function() {
var clickCount = 0;
function mouseHandler() {
clickCount++;
}
canvas.on('mouse:down', mouseHandler);
canvas.fireMiddleClick = false;
canvas.fireRightClick = false;
canvas._currentTransform = false;
canvas.isDrawingMode = false;
canvas.__onMouseDown({ which: 1 });
equal(clickCount, 1, 'mouse down fired');
clickCount = 0;
canvas.__onMouseDown({ which: 3 });
equal(clickCount, 0, 'rightclick did not fire a mouse:down event');
canvas.fireRightClick = true;
canvas.__onMouseDown({ which: 3 });
equal(clickCount, 1, 'rightclick did fire a mouse:down event');
clickCount = 0;
canvas.__onMouseDown({ which: 2 });
equal(clickCount, 0, 'middleClick did not fire a mouse:down event');
canvas.fireMiddleClick = true;
canvas.__onMouseDown({ which: 2 });
equal(clickCount, 1, 'middleClick did fire a mouse:down event');
});
})();

0 comments on commit 45f102b

Please sign in to comment.