Last active
August 29, 2015 14:01
-
-
Save iambigd/b892a10343be7e25033d to your computer and use it in GitHub Desktop.
Bootstrap context menu (ref: http://stackoverflow.com/questions/18666601/use-bootstrap-3-dropdown-menu-as-context-menu)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, window) { | |
$.fn.contextMenu = function (settings) { | |
return this.each(function () { | |
// Open context menu | |
$(this).on("contextmenu", function (e) { | |
//open menu | |
$(settings.menuSelector) | |
.data("invokedOn", $(e.target)) | |
.show() | |
.css({ | |
position: "absolute", | |
left: getLeftLocation(e), | |
top: getTopLocation(e) | |
}); | |
//add click listener on menu | |
ContextMenuClickHandler(); | |
return false; | |
}); | |
// click handler for context menu | |
function ContextMenuClickHandler() { | |
$(settings.menuSelector) | |
.off('click') | |
.on( 'click', function (e) { | |
$(this).hide(); | |
var $invokedOn = $(this).data("invokedOn"); | |
var $selectedMenu = $(e.target); | |
settings.menuSelected.call($(this), $invokedOn, $selectedMenu); | |
}); | |
} | |
//make sure menu closes on any click | |
$(document).click(function () { | |
$(settings.menuSelector).hide(); | |
}); | |
}); | |
function getLeftLocation(e) { | |
var mouseWidth = e.pageX; | |
var pageWidth = $(window).width(); | |
var menuWidth = $(settings.menuSelector).width(); | |
// opening menu would pass the side of the page | |
if (mouseWidth + menuWidth > pageWidth && | |
menuWidth < mouseWidth) { | |
return mouseWidth - menuWidth; | |
} | |
return mouseWidth; | |
} | |
function getTopLocation(e) { | |
var mouseHeight = e.pageY; | |
var pageHeight = $(window).height(); | |
var menuHeight = $(settings.menuSelector).height(); | |
// opening menu would pass the bottom of the page | |
if (mouseHeight + menuHeight > pageHeight && | |
menuHeight < mouseHeight) { | |
return mouseHeight - menuHeight; | |
} | |
return mouseHeight; | |
} | |
}; | |
})(jQuery, window); | |
$("#myTable td").contextMenu({ | |
menuSelector: "#contextMenu", | |
menuSelected: function (invokedOn, selectedMenu) { | |
var msg = "You selected the menu item '" + selectedMenu.text() + | |
"' on the value '" + invokedOn.text() + "'"; | |
alert(msg); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table id="myTable" class="table table-hover"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>First Name</th> | |
<th>Last Name</th> | |
<th>Username</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td>1</td> | |
<td>Mark</td> | |
<td>Otto</td> | |
<td>@mdo</td> | |
</tr> | |
<tr> | |
<td>2</td> | |
<td>Jacob</td> | |
<td>Thornton</td> | |
<td>@fat</td> | |
</tr> | |
<tr> | |
<td>3</td> | |
<td>Larry</td> | |
<td>the Bird</td> | |
<td>@twitter</td> | |
</tr> | |
</tbody> | |
</table> | |
<ul id="contextMenu" class="dropdown-menu" role="menu" style="display:none" > | |
<li><a tabindex="-1" href="#">Action</a></li> | |
<li><a tabindex="-1" href="#">Another action</a></li> | |
<li><a tabindex="-1" href="#">Something else here</a></li> | |
<li class="divider"></li> | |
<li><a tabindex="-1" href="#">Separated link</a></li> | |
</ul> | |
<!-- Post Info --> | |
<div style='position:fixed;bottom:0;left:0; | |
background:lightgray;width:100%;'> | |
About this SO Question: <a href='http://stackoverflow.com/q/18666601/1366033'>Use Bootstrap 3 dropdown menu as context menu</a><br/> | |
<div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment