Skip to content

Commit

Permalink
Added: Dialog escape support. Closes visionmedia#36
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 23, 2012
1 parent 8ce09f1 commit ac01c85
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
32 changes: 28 additions & 4 deletions build/ui.js
Expand Up @@ -260,6 +260,21 @@ Dialog.prototype.overlay = function(){
return this;
};

/**
* Close the dialog when the escape key is pressed.
*
* @api private
*/

Dialog.prototype.escapable = function(){
var self = this;
$(document).bind('keydown.dialog', function(e){
if (27 != e.which) return;
$(this).unbind('keydown.dialog');
self.hide();
});
};

/**
* Show the dialog.
*
Expand All @@ -270,11 +285,18 @@ Dialog.prototype.overlay = function(){
*/

Dialog.prototype.show = function(){
var overlay = this._overlay;

this.emit('show');
if (this._overlay) {
this._overlay.show();

if (overlay) {
overlay.show();
this.el.addClass('modal');
}

// escape
if (!overlay || overlay.closable) this.escapable();

this.el.appendTo('body');
this.el.css({ marginLeft: -(this.el.width() / 2) + 'px' });
return this;
Expand Down Expand Up @@ -534,13 +556,15 @@ Confirmation.prototype.render = function(options){
self.callback(false);
});

actions.find('.cancel').click(function(){
actions.find('.cancel').click(function(e){
e.preventDefault();
self.emit('cancel');
self.callback(false);
self.hide();
});

actions.find('.ok').click(function(){
actions.find('.ok').click(function(e){
e.preventDefault();
self.emit('ok');
self.callback(true);
self.hide();
Expand Down
6 changes: 4 additions & 2 deletions lib/components/confirmation/confirmation.js
Expand Up @@ -117,13 +117,15 @@ Confirmation.prototype.render = function(options){
self.callback(false);
});

actions.find('.cancel').click(function(){
actions.find('.cancel').click(function(e){
e.preventDefault();
self.emit('cancel');
self.callback(false);
self.hide();
});

actions.find('.ok').click(function(){
actions.find('.ok').click(function(e){
e.preventDefault();
self.emit('ok');
self.callback(true);
self.hide();
Expand Down
26 changes: 24 additions & 2 deletions lib/components/dialog/dialog.js
Expand Up @@ -154,6 +154,21 @@ Dialog.prototype.overlay = function(){
return this;
};

/**
* Close the dialog when the escape key is pressed.
*
* @api private
*/

Dialog.prototype.escapable = function(){
var self = this;
$(document).bind('keydown.dialog', function(e){
if (27 != e.which) return;
$(this).unbind('keydown.dialog');
self.hide();
});
};

/**
* Show the dialog.
*
Expand All @@ -164,11 +179,18 @@ Dialog.prototype.overlay = function(){
*/

Dialog.prototype.show = function(){
var overlay = this._overlay;

this.emit('show');
if (this._overlay) {
this._overlay.show();

if (overlay) {
overlay.show();
this.el.addClass('modal');
}

// escape
if (!overlay || overlay.closable) this.escapable();

this.el.appendTo('body');
this.el.css({ marginLeft: -(this.el.width() / 2) + 'px' });
return this;
Expand Down

0 comments on commit ac01c85

Please sign in to comment.