From 7a50d7d38aa9e5e1037c095ef3ae5d15013ed418 Mon Sep 17 00:00:00 2001 From: Christian Treppo Date: Mon, 25 Jul 2016 12:25:44 +0200 Subject: [PATCH] Use underscore binding and iterators --- web-ui/app/js/page/shortcuts.js | 41 +++++++++++------- web-ui/test/spec/page/shortcuts.spec.js | 43 ++++++++----------- .../test/spec/search/search_trigger.spec.js | 2 +- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/web-ui/app/js/page/shortcuts.js b/web-ui/app/js/page/shortcuts.js index b3b9c408e..48be57b48 100644 --- a/web-ui/app/js/page/shortcuts.js +++ b/web-ui/app/js/page/shortcuts.js @@ -26,7 +26,7 @@ define([ function shortcuts() { this.after('initialize', function () { - this.on(document, 'keydown', $.proxy(onKeydown, this)); + this.on(document, 'keydown', _.bind(onKeydown, this)); }); var composeBoxId = 'compose-box'; @@ -47,30 +47,29 @@ define([ this.composeBoxId = composeBoxId; function onKeydown(event) { - tryGlobalKeyEvents(event, triggerOnDocument.call(this)); + tryGlobalKeyEvents(event, _.bind(triggerOnDocument, this)); if (composeBoxIsShown()) { - tryMailCompositionKeyEvents(event, triggerOnDocument.call(this)); + tryMailCompositionKeyEvents(event, _.bind(triggerOnDocument, this)); } else { - tryMailHandlingKeyEvents(event, triggerOnDocument.call(this)); + tryMailHandlingKeyEvents(event, _.bind(triggerOnDocument, this)); } } - function triggerOnDocument() { - var self = this; - return function (event) { - self.trigger(document, event); - } + function triggerOnDocument(event) { + this.trigger(document, event); } function tryGlobalKeyEvents(event, triggerFunc) { var globalKeyEvents = {}; globalKeyEvents[keyCodes.ESC] = events.dispatchers.rightPane.openNoMessageSelected; - if (!globalKeyEvents.hasOwnProperty(event.which)) return; + if (!globalKeyEvents.hasOwnProperty(event.which)) { + return; + } - triggerFunc(globalKeyEvents[event.which]); event.preventDefault(); + return triggerFunc(globalKeyEvents[event.which]); } function tryMailCompositionKeyEvents(event, triggerFunc) { @@ -78,21 +77,27 @@ define([ mailCompositionKeyEvents[modifierKeys.CTRL + keyCodes.ENTER] = events.ui.mail.send; mailCompositionKeyEvents[modifierKeys.META + keyCodes.ENTER] = events.ui.mail.send; - if (!mailCompositionKeyEvents.hasOwnProperty(modifierKey(event) + event.which)) return; + if (!mailCompositionKeyEvents.hasOwnProperty(modifierKey(event) + event.which)) { + return; + } event.preventDefault(); return triggerFunc(mailCompositionKeyEvents[modifierKey(event) + event.which]); } function tryMailHandlingKeyEvents(event, triggerFunc) { - if (isTriggeredOnInputField(event.target)) return; + if (isTriggeredOnInputField(event.target)) { + return; + } var mailHandlingKeyEvents = {}; mailHandlingKeyEvents[keyCodes.S] = events.search.focus; mailHandlingKeyEvents[keyCodes.FORWARD_SLASH] = events.search.focus; mailHandlingKeyEvents[keyCodes.C] = events.dispatchers.rightPane.openComposeBox; - if (!mailHandlingKeyEvents.hasOwnProperty(event.which)) return; + if (!mailHandlingKeyEvents.hasOwnProperty(event.which)) { + return; + } event.preventDefault(); return triggerFunc(mailHandlingKeyEvents[event.which]); @@ -100,8 +105,12 @@ define([ function modifierKey(event) { var modifierKey = ""; - if (event.ctrlKey === true) modifierKey = modifierKeys.CTRL; - if (event.metaKey === true) modifierKey = modifierKeys.META; + if (event.ctrlKey === true) { + modifierKey = modifierKeys.CTRL; + } + if (event.metaKey === true) { + modifierKey = modifierKeys.META; + } return modifierKey; } diff --git a/web-ui/test/spec/page/shortcuts.spec.js b/web-ui/test/spec/page/shortcuts.spec.js index d621f122a..284079ec4 100644 --- a/web-ui/test/spec/page/shortcuts.spec.js +++ b/web-ui/test/spec/page/shortcuts.spec.js @@ -11,7 +11,7 @@ describeComponent('page/shortcuts', function () { $(document).trigger(keydownEvent(this.component.keyCodes.ESC)); - expect(eventSpy).toHaveBeenTriggeredOn(document) + expect(eventSpy).toHaveBeenTriggeredOn(document); }); }); @@ -19,7 +19,7 @@ describeComponent('page/shortcuts', function () { function shortcutEventAndTriggeredEventSpy() { return [ { - eventSpy: openComposeBoxEventSpy(), + eventSpy: spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openComposeBox), shortcutEvent: keydownEvent(this.component.keyCodes.C) }, { @@ -30,7 +30,7 @@ describeComponent('page/shortcuts', function () { eventSpy: spyOnEvent(document, Pixelated.events.search.focus), shortcutEvent: keydownEvent(this.component.keyCodes.S) } - ] + ]; } it('are triggered when no input or textarea is focused', function () { @@ -44,39 +44,36 @@ describeComponent('page/shortcuts', function () { }); it('are not triggered when an input is focused', function () { - var self = this; - shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { - self.$node.append(''); + _.each(shortcutEventAndTriggeredEventSpy.call(this), function (args) { + this.$node.append(''); var eventSpy = args.eventSpy; - self.$node.find('input').trigger(args.shortcutEvent); + this.$node.find('input').trigger(args.shortcutEvent); expect(eventSpy).not.toHaveBeenTriggeredOn(document); - }); + }, this); }); it('are not triggered when a textarea is focused', function () { - var self = this; - shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { - self.$node.append(''); + _.each(shortcutEventAndTriggeredEventSpy.call(this), function (args) { + this.$node.append(''); var eventSpy = args.eventSpy; - self.$node.find('textarea').trigger(args.shortcutEvent); + this.$node.find('textarea').trigger(args.shortcutEvent); expect(eventSpy).not.toHaveBeenTriggeredOn(document); - }); + }, this); }); it('are not triggered when the composeBox is opened', function () { - var self = this; - shortcutEventAndTriggeredEventSpy.call(this).forEach(function (args) { - addComposeBox.call(self); + _.each(shortcutEventAndTriggeredEventSpy.call(this), function (args) { + addComposeBox.call(this); var eventSpy = args.eventSpy; $(document).trigger(args.shortcutEvent); expect(eventSpy).not.toHaveBeenTriggeredOn(document); - }); + }, this); }); }); @@ -87,7 +84,7 @@ describeComponent('page/shortcuts', function () { $(document).trigger(jQuery.Event('keydown', {ctrlKey: true, which: this.component.keyCodes.ENTER})); - expect(eventSpy).toHaveBeenTriggeredOn(document) + expect(eventSpy).toHaveBeenTriggeredOn(document); }); it('triggers ui.mail.send when [Meta] + [Enter] is pressed and compose box is open', function () { @@ -96,7 +93,7 @@ describeComponent('page/shortcuts', function () { $(document).trigger(jQuery.Event('keydown', {metaKey: true, which: this.component.keyCodes.ENTER})); - expect(eventSpy).toHaveBeenTriggeredOn(document) + expect(eventSpy).toHaveBeenTriggeredOn(document); }); it('does not trigger ui.mail.send when [Ctrl] + [Enter] is pressed and compose box is closed', function () { @@ -104,7 +101,7 @@ describeComponent('page/shortcuts', function () { $(document).trigger(jQuery.Event('keydown', {ctrlKey: true, which: this.component.keyCodes.ENTER})); - expect(eventSpy).not.toHaveBeenTriggeredOn(document) + expect(eventSpy).not.toHaveBeenTriggeredOn(document); }); it('does not trigger ui.mail.send when [Meta] + [Enter] is pressed and compose box is closed', function () { @@ -112,14 +109,10 @@ describeComponent('page/shortcuts', function () { $(document).trigger(jQuery.Event('keydown', {metaKey: true, which: this.component.keyCodes.ENTER})); - expect(eventSpy).not.toHaveBeenTriggeredOn(document) + expect(eventSpy).not.toHaveBeenTriggeredOn(document); }); }); - function openComposeBoxEventSpy() { - return spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openComposeBox); - } - function keydownEvent(code) { return jQuery.Event('keydown', {which: code}); } diff --git a/web-ui/test/spec/search/search_trigger.spec.js b/web-ui/test/spec/search/search_trigger.spec.js index 6895f00ed..12026966d 100644 --- a/web-ui/test/spec/search/search_trigger.spec.js +++ b/web-ui/test/spec/search/search_trigger.spec.js @@ -52,6 +52,6 @@ describeComponent('search/search_trigger', function () { it('should focus search input field on focus event', function () { $(document).trigger(Pixelated.events.search.focus); - expect($(document.activeElement)).toEqual(this.component.select('input')) + expect($(document.activeElement)).toEqual(this.component.select('input')); }); });