Skip to content

Commit

Permalink
Revert " - support for multiple key-combos for Pseudos:keys - new Pse…
Browse files Browse the repository at this point in the history
…udos:stop , prevents propagation of events directly from the pseudo - updated docs - added more specs for both Pseudos:keys and new Pseudos:stop"

This reverts commit 418800c.
  • Loading branch information
w00fz committed May 26, 2011
1 parent 418800c commit 4a72128
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 87 deletions.
7 changes: 1 addition & 6 deletions Docs/Element/Element.Event.Pseudos.Keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ Defines the `:keys` Element Event Pseudo. It captures key combinations and fires
### Note

- This plugin adds some common used keys to the keys you can use, like `-`, `end` or `=`. See for the complete list below.
- You can have multiple key combinations separated by comma

Pseudo: keys {#Pseudos:keys}
----------------------------

The event will only fire when a key combination is pressed. This only works with the `keydown` and `keyup` events.

### Examples
### Example

myElement.addEvent('keydown:keys(shift+a+b)', function(){
alert('You pressed the following keys: shift, a and b');
});

myElement.addEvent('keydown:keys(shift+a, delete, ,)', function(){
alert('You pressed one of the following keys: shift + a, delete or ,');
});


Element Event Keys
Expand Down
43 changes: 11 additions & 32 deletions Source/Element/Element.Event.Pseudos.Keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
name: Element.Event.Pseudos.Keys
description: Adds the functionality to fire events if certain keycombinations are pressed
description: Adds functionality fire events if certain keycombinations are pressed
license: MIT-style license
authors:
- Arian Stolwijk
- Djamil Legato
requires: [Element.Event.Pseudos]
Expand All @@ -27,39 +26,19 @@ var keysStoreKey = '$moo:keys-pressed',
Event.definePseudo('keys', function(split, fn, args){

var event = args[0],
keyList = {},
pressed = this.retrieve(keysStoreKey, []),
value = split.value;

// all of this is to split by , allowing , as char of the list
// if you have a regexp for that please do share!
value = value.replace(/\s/g, '').replace(/^,,|,,?$/g, function(match, pos){
if (!pos) return '-sep-,';
else return (value.length - 1 == pos) ? '-sep-' : ',-sep-';
}).replace(/,,,?/g, function(match, pos){
if (match.length == 2) return '-sep-,';
else return ',-sep-,';
});

value = (value == ',' ? [value] : value.split(',')).map(function(value){
value = value.replace('-sep-', ',');
keyList[value] = [];

keyList[value].append(value.replace('++', function(){
keyList[value].push('+'); // shift++ and shift+++a
return '';
}).split('+'));

return value;
});
keys = [],
pressed = this.retrieve(keysStoreKey, []);

keys.append(split.value.replace('++', function(){
keys.push('+'); // shift++ and shift+++a
return '';
}).split('+'));

pressed.include(event.key);

Object.each(keyList, function(keys){
if (keys.every(function(key){
return pressed.contains(key);
})) fn.apply(this, args);
});
if (keys.every(function(key){
return pressed.contains(key);
})) fn.apply(this, args);

this.store(keysStoreKey, pressed);

Expand Down
50 changes: 1 addition & 49 deletions Specs/1.3/Element/Element.Event.Pseudos.Keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ if (window.addEventListener) describe('Element.Event.Pseudos.Keys', function(){
it('keys: should fire events for keyboard key combinations', function(){

var callback = jasmine.createSpy(), called = false,
callback2 = jasmine.createSpy(), called2 = false,
callback3 = jasmine.createSpy(), called3 = false,
callback4 = jasmine.createSpy(), called4 = false;
callback2 = jasmine.createSpy(), called2 = false;

document.body.addEvent('keydown:keys(shift+a)', callback);
document.body.addEvent('keydown:keys(shift++)', callback2);
document.body.addEvent('keydown:keys(shift+,)', callback3);
document.body.addEvent('keydown:keys(,)', callback4);

// shift+a
simulateEvent('type', ['[shift]a[shift-up]', document.body], function(){
Expand All @@ -25,51 +21,7 @@ if (window.addEventListener) describe('Element.Event.Pseudos.Keys', function(){
expect(callback2).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

// shift+,
simulateEvent('type', ['[shift],[shift-up]', document.body], function(){
expect(callback3).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

// ,
simulateEvent('type', [',', document.body], function(){
expect(callback4).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

});


it('keys: should support multiple keys separated by comma', function(){
var callback = jasmine.createSpy(), called = false;

document.body.addEvent('keydown:keys(shift++,delete, , , +)', callback);

// shift++
simulateEvent('type', ['[shift]+[shift-up]', document.body], function(){
expect(callback).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

// delete
simulateEvent('type', ['[delete]', document.body], function(){
expect(callback).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

// ,
simulateEvent('type', [',', document.body], function(){
expect(callback).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

// +
simulateEvent('type', ['+', document.body], function(){
expect(callback).toHaveBeenCalled();
document.body.eliminate('$moo:keys-pressed');
});

});

});

0 comments on commit 4a72128

Please sign in to comment.