Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
Use underscore binding and iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
treppo committed Jul 25, 2016
1 parent 0b04cf0 commit 7a50d7d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
41 changes: 25 additions & 16 deletions web-ui/app/js/page/shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -47,61 +47,70 @@ 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) {
var mailCompositionKeyEvents = {};
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]);
}

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;
}

Expand Down
43 changes: 18 additions & 25 deletions web-ui/test/spec/page/shortcuts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describeComponent('page/shortcuts', function () {

$(document).trigger(keydownEvent(this.component.keyCodes.ESC));

expect(eventSpy).toHaveBeenTriggeredOn(document)
expect(eventSpy).toHaveBeenTriggeredOn(document);
});
});

describe('mail list shortcuts', function () {
function shortcutEventAndTriggeredEventSpy() {
return [
{
eventSpy: openComposeBoxEventSpy(),
eventSpy: spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openComposeBox),
shortcutEvent: keydownEvent(this.component.keyCodes.C)
},
{
Expand All @@ -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 () {
Expand All @@ -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('<input />');
_.each(shortcutEventAndTriggeredEventSpy.call(this), function (args) {
this.$node.append('<input />');
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('<textarea></textarea>');
_.each(shortcutEventAndTriggeredEventSpy.call(this), function (args) {
this.$node.append('<textarea></textarea>');
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);
});
});

Expand All @@ -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 () {
Expand All @@ -96,30 +93,26 @@ 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 () {
var eventSpy = spyOnEvent(document, Pixelated.events.ui.mail.send);

$(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 () {
var eventSpy = spyOnEvent(document, Pixelated.events.ui.mail.send);

$(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});
}
Expand Down
2 changes: 1 addition & 1 deletion web-ui/test/spec/search/search_trigger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});

0 comments on commit 7a50d7d

Please sign in to comment.