ng-keyboard
Advanced tools
Comparing version 0.1.0 to 0.2.1
{ | ||
"name": "ng-keyboard", | ||
"version": "0.1.0", | ||
"version": "0.2.1", | ||
"description": "Keyboard shortcuts for angular apps", | ||
@@ -5,0 +5,0 @@ "main": "src/module.js", |
@@ -38,2 +38,9 @@ var _ = require('lodash'); | ||
/** | ||
* A counter which is used to prioretize bindings if their priority is identical | ||
* | ||
* @type {Number} | ||
*/ | ||
var bindingCounter = 0; | ||
$window = angular.element($window); | ||
@@ -63,3 +70,2 @@ | ||
var satisfiableBindings = _.filter(bindings, isSatisfiable); | ||
@@ -158,17 +164,40 @@ | ||
function executeFirstMatch(event) { | ||
var callbacks = currentSequences | ||
.filter(function (binding) { | ||
return binding.sequence.length === 1 | ||
&& isSatisfiable(binding) | ||
&& binding.action === event.type | ||
&& !angular.element(event.target).is(binding.ignore) | ||
&& isSatisfiedCombo(binding.sequence[0]); | ||
}) | ||
.map(function (match, idx, matches) { | ||
return match.callback; | ||
}); | ||
var firstMatch = _.find(currentSequences, function(binding) { | ||
return binding.sequence.length === 1 | ||
&& isSatisfiable(binding) | ||
&& binding.action === event.type | ||
&& !angular.element(event.target).is(binding.ignore); | ||
$rootScope.$apply(function() { | ||
if (createWrappedCallback(callbacks, 0, event)() === false) { | ||
stopPropagation(event); | ||
preventDefault(event); | ||
} | ||
}); | ||
} | ||
if (firstMatch && isSatisfiedCombo(firstMatch.sequence[0])) { | ||
$rootScope.$apply(function() { | ||
if (firstMatch.callback(event) === false) { | ||
stopPropagation(event); | ||
preventDefault(event); | ||
} | ||
}); | ||
/** | ||
* Return a function which wraps the callback at the given index. | ||
* | ||
* This function will be called recursivly for all further callbacks in the array and will provide the wrapper | ||
* function of the *next* callback as parameter to the *current* callback function. | ||
* | ||
* @param {Function[]} callbacks - Array of callback functions | ||
* @param {Number} idx - Index of the callback function that should be wrapped | ||
* @param {Object} event - The keyboard event that should be given to the callbacks | ||
* | ||
* @returns {Function} wrapper function which will call the original callback | ||
*/ | ||
function createWrappedCallback(callbacks, idx, event) { | ||
var callback = callbacks[idx]; | ||
return function () { | ||
if (callback) { | ||
return callback(event, createWrappedCallback(callbacks, idx+1, event)); | ||
} | ||
} | ||
@@ -184,3 +213,9 @@ } | ||
function sortByPriority(a, b) { | ||
return b.priority - a.priority; | ||
var priorityDiff = b.priority - a.priority; | ||
if (priorityDiff === 0) { | ||
return b.counter - a.counter; | ||
} | ||
return priorityDiff; | ||
} | ||
@@ -269,3 +304,4 @@ | ||
action: options.action || 'keydown', | ||
ignore: options.ignore || 'input, select, textarea' | ||
ignore: options.ignore || 'input, select, textarea', | ||
counter: bindingCounter++ | ||
}; | ||
@@ -272,0 +308,0 @@ // Last come first serve |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
694256
1134