Skip to content

Commit

Permalink
allow closing of contextmenus when escape key pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
sinetbot committed Nov 22, 2014
1 parent 0a9ab0e commit 79b9b91
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion bootstrap-contextmenu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*!
* Bootstrap Context Menu
* Version: 0.2.0
* Author: @sydcanem
* https://github.com/sydcanem/bootstrap-contextmenu
*
Expand Down Expand Up @@ -93,6 +92,10 @@
return false;
}

,keydown: function(e) {
if (e.which == 27) this.closemenu(e);
}

,before: function(e) {
return true;
}
Expand All @@ -104,6 +107,7 @@
,listen: function () {
this.$element.on('contextmenu.context.data-api', this.scopes, $.proxy(this.show, this));
$('html').on('click.context.data-api', $.proxy(this.closemenu, this));
$('html').on('keydown.context.data-api', $.proxy(this.keydown, this));
}

,destroy: function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bootstrap-contextmenu",
"version": "0.2.0",
"version": "0.3.0",
"description": "Context-menu extension for the Bootstrap framework",
"main": "bootstrap-contextmenu.js",
"directories": {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/bootstrap-contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,30 @@ $(function () {
contextmenu.trigger('contextmenu');
$(document.body).click();
});

test('should remove open class if keyboard escape key is pressed', function () {
var contextHTML = '<div>' +
'<div id="main" data-toggle="context" data-target="#context-menu">' +
' <div id="context-menu">' +
' <ul class="dropdown-menu">' +
' <li><a href="#">Action</a></li>' +
' <li><a href="#">Something else here</a></li>' +
' <li class="divider"></li>' +
' <li><a href="#">Another link</a></li>' +
' <ul>' +
' </div>' +
'</div>' +
'</div>',
contextmenu = $(contextHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="context"]')
.contextmenu()
.trigger('contextmenu');


ok(contextmenu.find('#context-menu').hasClass('open'), 'open class added on right click');
$('body').trigger($.Event('keydown', { which: 27 }));
ok(!contextmenu.find('#context-menu').hasClass('open'), 'open class removed');
contextmenu.remove();
});
});

0 comments on commit 79b9b91

Please sign in to comment.