Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves #7 don't intercept key events sent to editable inputs #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions dist/bespoke-keys.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* bespoke-keys v1.0.0
* bespoke-keys v1.1.0
*
* Copyright 2015, Mark Dalgleish
* This content is released under the MIT license
Expand All @@ -9,20 +9,25 @@
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self);var o=n;o=o.bespoke||(o.bespoke={}),o=o.plugins||(o.plugins={}),o.keys=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
module.exports = function(options) {
return function(deck) {
var isHorizontal = options !== 'vertical';
var isHorizontal = options !== 'vertical',
elementIsReadOnly = function(element) {
return !(element.readOnly === false || element.isContentEditable);
};

document.addEventListener('keydown', function(e) {
if (e.which == 34 || // PAGE DOWN
(e.which == 32 && !e.shiftKey) || // SPACE WITHOUT SHIFT
(isHorizontal && e.which == 39) || // RIGHT
(!isHorizontal && e.which == 40) // DOWN
) { deck.next(); }
if (elementIsReadOnly(e.target)) {
if (e.which == 34 || // PAGE DOWN
(e.which == 32 && !e.shiftKey) || // SPACE WITHOUT SHIFT
(isHorizontal && e.which == 39) || // RIGHT
(!isHorizontal && e.which == 40) // DOWN
) { deck.next(); }

if (e.which == 33 || // PAGE UP
(e.which == 32 && e.shiftKey) || // SPACE + SHIFT
(isHorizontal && e.which == 37) || // LEFT
(!isHorizontal && e.which == 38) // UP
) { deck.prev(); }
if (e.which == 33 || // PAGE UP
(e.which == 32 && e.shiftKey) || // SPACE + SHIFT
(isHorizontal && e.which == 37) || // LEFT
(!isHorizontal && e.which == 38) // UP
) { deck.prev(); }
}
});
};
};
Expand Down
4 changes: 2 additions & 2 deletions dist/bespoke-keys.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions lib/bespoke-keys.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
module.exports = function(options) {
return function(deck) {
var isHorizontal = options !== 'vertical';
var isHorizontal = options !== 'vertical',
elementIsReadOnly = function(element) {
return !(element.readOnly === false || element.isContentEditable);
};

document.addEventListener('keydown', function(e) {
if (e.which == 34 || // PAGE DOWN
(e.which == 32 && !e.shiftKey) || // SPACE WITHOUT SHIFT
(isHorizontal && e.which == 39) || // RIGHT
(!isHorizontal && e.which == 40) // DOWN
) { deck.next(); }
if (elementIsReadOnly(e.target)) {
if (e.which == 34 || // PAGE DOWN
(e.which == 32 && !e.shiftKey) || // SPACE WITHOUT SHIFT
(isHorizontal && e.which == 39) || // RIGHT
(!isHorizontal && e.which == 40) // DOWN
) { deck.next(); }

if (e.which == 33 || // PAGE UP
(e.which == 32 && e.shiftKey) || // SPACE + SHIFT
(isHorizontal && e.which == 37) || // LEFT
(!isHorizontal && e.which == 38) // UP
) { deck.prev(); }
if (e.which == 33 || // PAGE UP
(e.which == 32 && e.shiftKey) || // SPACE + SHIFT
(isHorizontal && e.which == 37) || // LEFT
(!isHorizontal && e.which == 38) // UP
) { deck.prev(); }
}
});
};
};
17 changes: 11 additions & 6 deletions test/spec/bespoke-keysSpec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
describe("bespoke-keys", function() {

Function.prototype.bind = Function.prototype.bind || require('function-bind');
require('simulant/simulant');

var bespoke = require('bespoke'),
var simulant = require('simulant'),
bespoke = require('bespoke'),
keys = require('../../lib-instrumented/bespoke-keys.js');

var deck,

inputBox = null,
createDeck = function(optionValue) {
var parent = document.createElement('article');
parent.innerHTML = '<section></section><section></section>';
parent.innerHTML = '<section><input type="text"></section><section></section>';
inputBox = parent.querySelector('input');

deck = bespoke.from(parent, [
keys(optionValue)
]);
},

pressKey = function(which, isShift) {
simulant.fire(document, 'keydown', { which: which, shiftKey: !!isShift });
pressKey = function(which, isShift, element) {
simulant.fire((element || document), 'keydown', { which: which, shiftKey: !!isShift });
};

describe("horizontal deck", function() {
Expand Down Expand Up @@ -46,6 +47,10 @@ describe("bespoke-keys", function() {
expect(deck.slide()).toBe(1);
});

it("should not go to the next slide when pressing the space bar in an input field", function() {
pressKey(32, false, inputBox);
expect(deck.slide()).toBe(0);
});
});

describe("previous slide", function() {
Expand Down