From 988cfcda8d627554018be7862fffc994f27d1c32 Mon Sep 17 00:00:00 2001 From: dwaltz Date: Tue, 27 Jan 2015 10:35:24 -0700 Subject: [PATCH] Placard keyDown events. Enter triggers accept, Esc triggers cancel. --- js/placard.js | 13 +++++++++++++ test/placard-test.js | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/js/placard.js b/js/placard.js index 8a89fb228..18dafda46 100644 --- a/js/placard.js +++ b/js/placard.js @@ -47,7 +47,10 @@ this.options.revertOnCancel = (this.$accept.length>0) ? true : false; } + this.isInput = this.$field.is('input'); + this.$field.on('focus.fu.placard', $.proxy(this.show, this)); + this.$field.on('keydown.fu.placard', $.proxy(this.keyComplete, this)); this.$accept.on('click.fu.placard', $.proxy(this.complete, this, 'accept')); this.$cancel.on('click.fu.placard', function(e){ e.preventDefault(); self.complete('cancel'); }); @@ -72,6 +75,16 @@ } }, + keyComplete: function(e){ + if(this.isInput && e.keyCode === 13){ + this.complete('accept'); + this.$field.blur(); + } else if (e.keyCode === 27) { + this.complete('cancel'); + this.$field.blur(); + } + }, + destroy: function() { this.$element.remove(); // remove any external bindings diff --git a/test/placard-test.js b/test/placard-test.js index cf613eb40..825fb2f3e 100644 --- a/test/placard-test.js +++ b/test/placard-test.js @@ -122,6 +122,26 @@ define(function(require){ $placard.find('.placard-cancel').click(); }); + test('Enter and exit keys should trigger appropriate response', function(){ + var $placard = $(html).find('#placard1'); + var $input = $placard.find('input'); + var e = $.Event("keydown"); + + $placard.placard({ + onAccept: function(){ + ok(1===1, 'onAccept function called when enter keypress'); + }, + onCancel: function(){ + ok(1===1, 'onCancel function called when exit keypress'); + } + }); + + e.keyCode = 13; + $input.trigger(e); + e.keyCode = 27; + $input.trigger(e); + }); + test('externalClickAction option should work as expected', function(){ var $placard = $(html).find('#placard1');