Repository: couchdb-fauxton Updated Branches: refs/heads/master 7e101d889 -> 592001668
ESC key closes any open notification This PR just lets users use their keyboard to close notifications. We discussed doing the same with mouse clicks and mouse overs, but they were a bit more contentious so I figure we can add them later if we want. Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/59200166 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/59200166 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/59200166 Branch: refs/heads/master Commit: 592001668ec3d766a49debb9fd21cc1909c1c111 Parents: 7e101d8 Author: Ben Keen <[email protected]> Authored: Wed Apr 15 11:53:01 2015 -0700 Committer: Ben Keen <[email protected]> Committed: Wed Apr 15 13:03:44 2015 -0700 ---------------------------------------------------------------------- app/addons/fauxton/base.js | 19 ++++++++++++++++++- app/addons/fauxton/tests/baseSpec.js | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/59200166/app/addons/fauxton/base.js ---------------------------------------------------------------------- diff --git a/app/addons/fauxton/base.js b/app/addons/fauxton/base.js index f2944f8..a5e0755 100644 --- a/app/addons/fauxton/base.js +++ b/app/addons/fauxton/base.js @@ -154,12 +154,28 @@ function (app, FauxtonAPI, Components, NavbarReactComponents, NavigationActions, this.removeWithAnimation(); }, - removeWithAnimation: function (event) { + removeWithAnimation: function () { this.$el.velocity('reverse', FauxtonAPI.constants.MISC.TRAY_TOGGLE_SPEED, function () { this.$el.remove(); + this.removeCloseListener(); }.bind(this)); }, + addCloseListener: function () { + $(document).on('keydown.notificationClose', this.onKeyDown.bind(this)); + }, + + onKeyDown: function (e) { + var code = e.keyCode || e.which; + if (code === 27) { // ESC key + this.removeWithAnimation(); + } + }, + + removeCloseListener: function () { + $(document).off('keydown.notificationClose', this.removeWithAnimation); + }, + delayedRemoval: function () { this.timeout = setTimeout(function () { this.removeWithAnimation(); @@ -174,6 +190,7 @@ function (app, FauxtonAPI, Components, NavbarReactComponents, NavigationActions, this.render().$el.appendTo(selector); this.$el.velocity('transition.slideDownIn', FauxtonAPI.constants.MISC.TRAY_TOGGLE_SPEED); this.delayedRemoval(); + this.addCloseListener(); return this; } }); http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/59200166/app/addons/fauxton/tests/baseSpec.js ---------------------------------------------------------------------- diff --git a/app/addons/fauxton/tests/baseSpec.js b/app/addons/fauxton/tests/baseSpec.js index 63b6669..da49273 100644 --- a/app/addons/fauxton/tests/baseSpec.js +++ b/app/addons/fauxton/tests/baseSpec.js @@ -121,5 +121,21 @@ define([ delete window.fauxton_xss_test2_escaped; }); + it('should close notification when ESCAPE key used', function () { + var notification = FauxtonAPI.addNotification({ + msg: 'Close me!', + selector: 'body' + }); + var removeWithAnimationSpy = sinon.spy(notification, 'removeWithAnimation'); + + notification.render(); + + // manually trigger an ESCAPE key click + $(document).trigger($.Event("keydown", { keyCode: 27 })); + + // confirm the remove method has now been called + assert.ok(removeWithAnimationSpy.calledOnce); + }); + }); });
