Skip to content

Commit

Permalink
Show context menu on image right-click
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh committed Oct 29, 2016
1 parent 4db0f43 commit 363c3cb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/js/lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
this.$overlay = $('#lightboxOverlay');
this.$outerContainer = this.$lightbox.find('.lb-outerContainer');
this.$container = this.$lightbox.find('.lb-container');
this.$nav = this.$lightbox.find('.lb-nav')

// Store css values for future lookup
this.containerTopPadding = parseInt(this.$container.css('padding-top'), 10);
Expand Down Expand Up @@ -144,6 +145,32 @@
return false;
});

/*
Show context menu for image on right-click
There is a div containing the navigation that spans the entire image and lives above of it. If
you right-click, you are right clicking this div and not the image. This prevents users from
saving the image or using other context menu actions with the image.
To fix this, when we detect the right mouse button is pressed down, but not yet clicked, we
set pointer-events to none on the nav div. This is so that the upcoming right-click event on
the next mouseup will bubble down to the image. Once the right-click/contextmenu event occurs
we set the pointer events back to auto for the nav div so it can capture hover and left-click
events as usual.
*/
this.$nav.on('mousedown', function(event) {
if (event.which === 3) {
self.$nav.css('pointer-events', 'none');

self.$lightbox.on('contextmenu', function() {
setTimeout(function() {
this.$nav.css('pointer-events', 'auto');
}.bind(self), 0)
});
}
})


this.$lightbox.find('.lb-loader, .lb-close').on('click', function() {
self.end();
return false;
Expand Down

1 comment on commit 363c3cb

@teo1978
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about touch devices, where the context menu is triggered by a long press instead of a right click?

Also, is it possible that a right-click happens and for whatever reason the "contextmenu" event is then not triggered? This would "leak" the value of "pointer-events" which would remain "none" forever. I'm not sure this can even happen, but you must be very sure that this is 100% guaranteed to be impossible under any circumstances, before you use your hack. (I can think of a case, haven't tested it: an event handler for mousedown from some other code which fires after yours and does preventDefault())

And also: what if someone purposefully sets pointer-events to none (in CSS or javascript) for a reason (can't think of one, but we shouldn't assume something doesn't exist because we can't think of a case)? You may end up "restoring" it to auto. Again, sounds like it can't normally happen (how would the contextmenu event fire if the element can't be target of a mouse event?) but is it guaranteed to be impossible?

Overall, this seems a bad hack. I would rather go to the root of the problem, which is the giant div on top of the image existing in the first place.

Please sign in to comment.