ng-keyboard
Advanced tools
Comparing version 0.2.1 to 0.3.0
{ | ||
"name": "ng-keyboard", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Keyboard shortcuts for angular apps", | ||
@@ -5,0 +5,0 @@ "main": "src/module.js", |
@@ -45,2 +45,9 @@ var _ = require('lodash'); | ||
/** | ||
* Set the instanceCounter for the initial instance | ||
* | ||
* @type {Number} | ||
*/ | ||
this.instanceCounter = 0; | ||
$window = angular.element($window); | ||
@@ -97,3 +104,3 @@ | ||
currentSequences.forEach(function(binding) { | ||
if (isSatisfiedCombo(binding.sequence[0])) { | ||
if (binding.sequence && isSatisfiedCombo(binding.sequence[0])) { | ||
binding.sequence.shift(); | ||
@@ -107,3 +114,3 @@ } | ||
currentSequences = _.filter(currentSequences, function(binding) { | ||
return binding.sequence.length > 0; | ||
return binding.sequence === null || binding.sequence.length > 0; | ||
}); | ||
@@ -113,2 +120,7 @@ | ||
currentSequences = _.filter(currentSequences, isSatisfiable); | ||
// Remove all remaining bindings if only catchAlls are left | ||
if (_.all(currentSequences, {sequence: null})) { | ||
currentSequences.length = 0; | ||
} | ||
} | ||
@@ -140,4 +152,7 @@ | ||
function isSatisfiable(binding) { | ||
var combo = binding.sequence[0]; | ||
var combo; | ||
if (binding.sequence === null) return true; | ||
combo = binding.sequence[0]; | ||
return _.all(activeKeys, function(keyCode) { | ||
@@ -169,2 +184,4 @@ return combo.indexOf(keyCode) !== -1; | ||
.filter(function (binding) { | ||
if (binding.sequence === null) return true; | ||
return binding.sequence.length === 1 | ||
@@ -216,9 +233,15 @@ && isSatisfiable(binding) | ||
function sortByPriority(a, b) { | ||
var parentPriortityDiff = b.parent.instanceCounter - a.parent.instanceCounter; | ||
var bindingCounterDiff = b.counter - a.counter; | ||
var priorityDiff = b.priority - a.priority; | ||
if (priorityDiff === 0) { | ||
return b.counter - a.counter; | ||
if (parentPriortityDiff !== 0) { | ||
return parentPriortityDiff; | ||
} | ||
return priorityDiff; | ||
if (priorityDiff !== 0) { | ||
return priorityDiff; | ||
} | ||
return bindingCounterDiff; | ||
} | ||
@@ -308,3 +331,4 @@ | ||
ignore: options.ignore || 'input, select, textarea', | ||
counter: bindingCounter++ | ||
counter: bindingCounter++, | ||
combo: combo | ||
}; | ||
@@ -330,2 +354,3 @@ // Last come first serve | ||
var newInst = Object.create(this); | ||
newInst.instanceCounter = this.instanceCounter + 1; | ||
@@ -340,2 +365,35 @@ scope.$on('$destroy', function() { | ||
}; | ||
/** | ||
* Add a new callback that binds to all keybindings that were not added on the current instance (see bindTo). | ||
* | ||
* @param {function?} callback | ||
* @return {KeyboardService} | ||
*/ | ||
this.catchAll = function registerCatchAll(callback) { | ||
var options = {}; | ||
// Fallback if an object with further options was passed | ||
if (typeof callback === "object") { | ||
options = callback; | ||
callback = options.callback; | ||
} | ||
// Parse combos and add binding for each combo | ||
var binding = { | ||
parent: this, | ||
sequence: null, | ||
callback: callback, | ||
priority: options.priority || -Infinity, | ||
action: options.action || 'keydown', | ||
ignore: options.ignore || 'input, select, textarea', | ||
counter: bindingCounter++ | ||
}; | ||
// Last come first serve | ||
bindings.unshift(binding); | ||
bindings.sort(sortByPriority); | ||
return this; | ||
}; | ||
}]; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
699440
1185