instance-js
Version:
50 lines (38 loc) • 1.18 kB
JavaScript
I.on("require#bindKeys", function(done) {
function bindKeys(node, config) {
var keysDown = [],
copyKeyBindings = {},
actions = config.Actions,
keyBindings = config.KeyBindings||config,
action;
for( var key in keyBindings ) {
if( key === "Actions" || key === "KeyBindings" || key === "ComboDelimiter" ) {
continue;
}
action = actions && !(typeof keyBindings[key] === "function") ? actions[ keyBindings[key] ] : keyBindings[key];
copyKeyBindings[key] = action;
}
node.addEventListener("keydown", function(e) {
if( keysDown[ keysDown.length - 1 ] !== e.key ) {
keysDown.push(e.key);
}
var def = keysDown.join(config.ComboDelimiter || "+");
if( copyKeyBindings[ def ] ) {
copyKeyBindings[ def ].call(this, e);
}
});
node.addEventListener("keyup", function(e) {
var index = keysDown.indexOf(e.key);
if( ~index ) {
keysDown = keysDown.slice(0, index).concat( keysDown.slice(1+index) );
}
});
}
I.proto.bindKeys = function(config) {
for(var i=0; i<this.length; i++) {
bindKeys(this[i], config);
}
}
I.bindKeys = bindKeys;
done();
});