combokeys-context
Advanced tools
Comparing version 1.0.2 to 2.0.0-beta1
{ | ||
"name": "combokeys-context", | ||
"version": "1.0.2", | ||
"version": "2.0.0-beta1", | ||
"author": "Tim Oram <mitmaro@gmail.com> (http://www.mitmaro.ca)", | ||
@@ -15,3 +15,3 @@ "contributors": [], | ||
}, | ||
"main": "src/Combokeys-Context.js", | ||
"main": "index.js", | ||
"scripts": { | ||
@@ -31,10 +31,16 @@ "test": "istanbul cover -- ./node_modules/.bin/_mocha" | ||
"devDependencies": { | ||
"chai": "^2.1.1", | ||
"chai": "^2.1.2", | ||
"inject-loader": "^1.0.0", | ||
"istanbul": "^0.3.7", | ||
"mocha": "^2.1.0", | ||
"shelljs": "^0.4.0", | ||
"sinon": "^1.13.0", | ||
"rewire": "^2.3.1", | ||
"rewire-webpack": "^1.0.0", | ||
"sinon": "^1.14.1", | ||
"sinon-chai": "^2.7.0" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"cross-browser": "git+https://github.com/MitMaro/CrossBrowser", | ||
"lodash": "^3.5.0" | ||
} | ||
} |
@@ -11,5 +11,11 @@ # ComboKeys Context | ||
## Version 1.x Info | ||
For the current stable version, please see the [v1.x branch](https://github.com/MitMaro/combokeys-context/tree/v1.x). The | ||
master branch is for the work in the upcoming version 2 which will have plugin support as well as a more powerful context | ||
system. | ||
## Compatibility | ||
Tested against the latest version of Google Chrome, latest Firefox, and Internet Explorer 10 and 11. Should work with all | ||
Tested against the latest version of Google Chrome, latest Firefox, and Internet Explorer 9, 10 and 11. Should work with all | ||
browsers that works with ComboKeys as long as `Object.keys` is polyfilled where needed. | ||
@@ -23,4 +29,7 @@ | ||
### Full Usage | ||
### Full API Docs | ||
[ComboKeys Context JSDocs](http://www.mitmaro.ca/combokeys-context/documentation/latest/) | ||
### Basic Usage | ||
```javascript | ||
@@ -33,2 +42,5 @@ var ComboKeys = require('combokeys'); | ||
// register a plugin | ||
comboKeysContext.registerPlugin(new ComboKeysContext.plugins.TagCallbackFilter(['input']); | ||
// define callbacks | ||
@@ -71,7 +83,21 @@ var callbackGlobal = function(evt, key) { | ||
### Full API Docs | ||
[ComboKeys Context JSDocs](http://www.mitmaro.ca/combokeys-context/documentation/latest/) | ||
### ComboKeys Compatibility | ||
This library modifies `ComboKeys.stopCallback` to add support for | ||
plugins. By default this library will not stop a callback in input | ||
tags or respond to the `combokeys` class on an element. To add this | ||
support you can use the `TagCallbackFilter`, `ElementAttributeFilter` | ||
and the `ClassNameFilter` plugins. | ||
var comboKeysContext = new ComboKeysContext(new ComboKeys()); | ||
comboKeysContext.registerPlugin(new TagCallbackFilter(['input', 'select', 'textarea'])); | ||
comboKeysContext.registerPlugin(new ClassNameFilter(['combokeys'], false); | ||
comboKeysContext.registerPlugin(new ElementAttributeFilter({ | ||
isContentEditable: 'true' | ||
}, { | ||
StopPropagationReturn: ComboKeysContext.STOP_CALLBACK | ||
}); | ||
## License | ||
Combokeys Context is released under the MIT license. See LICENSE. | ||
Combokeys Context is released under the ISC license. See LICENSE. |
@@ -1,4 +0,21 @@ | ||
var isArray = require('./util/isArray'); | ||
var _ = { | ||
isArray: require('lodash/lang/isArray') | ||
}; | ||
var eventTarget = require('cross-browser/Event/target'); | ||
var preventDefault = require('cross-browser/Event/preventDefault'); | ||
var stopPropagation = require('cross-browser/Event/stopPropagation'); | ||
var yesForce = 'yes_force'; | ||
var yes = 'yes'; | ||
var no = 'no'; | ||
var noForce = 'no_force'; | ||
function wrapHandler(key, evt, combo) { | ||
var i; | ||
var target = eventTarget(evt); | ||
var eventDefault = no; | ||
var eventPropagation = no; | ||
var result; | ||
if (this._context && this._bindings[key].contexts[this._context]) { | ||
@@ -9,2 +26,31 @@ this._bindings[key].contexts[this._context].call(this._comboKeys, evt, combo); | ||
} | ||
for (i = 0; i < this._activePlugins.length; i++) { | ||
if ( | ||
eventDefault !== yesForce && eventDefault !== noForce && | ||
typeof this._activePlugins[i].preventDefault === 'function' | ||
) { | ||
result = this._activePlugins[i].preventDefault(evt, target, combo, this._context); | ||
if (typeof result !== 'undefined') { | ||
eventDefault = result; | ||
} | ||
} | ||
if ( | ||
eventPropagation !== yesForce && eventPropagation !== noForce && | ||
typeof this._activePlugins[i].stopPropagation === 'function' | ||
) { | ||
result = this._activePlugins[i].stopPropagation(evt, target, combo, this._context); | ||
if (typeof result !== 'undefined') { | ||
eventPropagation = result; | ||
} | ||
} | ||
} | ||
if (eventDefault === yes || eventDefault === yesForce) { | ||
preventDefault(evt); | ||
} | ||
if (eventPropagation === yes || eventPropagation === yesForce) { | ||
stopPropagation(evt); | ||
} | ||
} | ||
@@ -24,5 +70,73 @@ | ||
this._comboKeys = comboKeys; | ||
this._plugins = { | ||
global: [], | ||
contexts: {} | ||
}; | ||
this._activePlugins = []; | ||
this._comboKeys.stopCallback = this._stopCallback.bind(this); | ||
}; | ||
/** | ||
* Force preventDefault to be called | ||
* @constant | ||
*/ | ||
Context.PREVENT_DEFAULT_FORCE = yesForce; | ||
/** | ||
* preventDefault may be called | ||
* @constant | ||
*/ | ||
Context.PREVENT_DEFAULT = yes; | ||
/** | ||
* preventDefault may not be called | ||
* @constant | ||
*/ | ||
Context.ALLOW_DEFAULT = no; | ||
/** | ||
* Force preventDefault to be called | ||
* @constant | ||
*/ | ||
Context.ALLOW_DEFAULT_FORCE = noForce; | ||
/** | ||
* Force stopPropagation to be called | ||
* @constant | ||
*/ | ||
Context.STOP_PROPAGATION_FORCE = yesForce; | ||
/** | ||
* stopPropagation may be called | ||
* @constant | ||
*/ | ||
Context.STOP_PROPAGATION = yes; | ||
/** | ||
* stopPropagation may not be called | ||
* @constant | ||
*/ | ||
Context.ALLOW_PROPAGATION = no; | ||
/** | ||
* Force stopPropagation not to be called | ||
* @constant | ||
*/ | ||
Context.ALLOW_PROPAGATION_FORCE = noForce; | ||
/** | ||
* Force callback to not be called | ||
* @constant | ||
*/ | ||
Context.STOP_CALLBACK_FORCE = yesForce; | ||
/** | ||
* Callback may not be called | ||
* @constant | ||
*/ | ||
Context.STOP_CALLBACK = yes; | ||
/** | ||
* Callback may be called | ||
* @constant | ||
*/ | ||
Context.ALLOW_CALLBACK = no; | ||
/** | ||
* Force callback to be called | ||
* @constant | ||
*/ | ||
Context.ALLOW_CALLBACK_FORCE = noForce; | ||
/** | ||
* Bind keys to an action with an optional context | ||
@@ -36,3 +150,3 @@ * @param key The key combination, see [Mousetrap.bind]{@link http://craig.is/killing/mice#api.bind} | ||
var i; | ||
var keys = isArray(key) ? key : [key]; | ||
var keys = _.isArray(key) ? key : [key]; | ||
@@ -59,3 +173,3 @@ // if context is a function we assume it's the callback and shift params | ||
var binding; | ||
var keys = isArray(key) ? key : [key]; | ||
var keys = _.isArray(key) ? key : [key]; | ||
@@ -90,3 +204,3 @@ for (i = 0; i < keys.length; ++i) { | ||
var i; | ||
var keys = isArray(key) ? key : [key]; | ||
var keys = _.isArray(key) ? key : [key]; | ||
@@ -111,2 +225,5 @@ for (i = 0; i < keys.length; ++i) { | ||
this._context = null; | ||
this._plugins.global = []; | ||
this._plugins.contexts = {}; | ||
this._activePlugins = []; | ||
}; | ||
@@ -123,2 +240,3 @@ | ||
this._context = context; | ||
this._combinePlugins(); | ||
}; | ||
@@ -131,4 +249,23 @@ | ||
this._context = null; | ||
this._combinePlugins(); | ||
}; | ||
/** | ||
* Register a plugin that follows the {@link Plugin} interface | ||
* | ||
* @param {ContextPlugin} plugin A plugin to be registered | ||
*/ | ||
Context.prototype.registerPlugin = function(plugin, context) { | ||
if (typeof context === 'undefined') { | ||
this._plugins.global.push(plugin); | ||
} | ||
else { | ||
if (typeof this._plugins.contexts[context] === 'undefined') { | ||
this._plugins.contexts[context] = []; | ||
} | ||
this._plugins.contexts[context].push(plugin); | ||
} | ||
this._combinePlugins(); | ||
}; | ||
Context.prototype._deleteBinding = function(key) { | ||
@@ -156,2 +293,47 @@ this._comboKeys.unbind(key); | ||
Context.prototype._stopCallback = function(evt, element, combo) { | ||
var i; | ||
var result; | ||
var stopCallback = no; | ||
for (i = 0; i < this._activePlugins.length; i++) { | ||
if (typeof this._activePlugins[i].stopCallback === 'function') { | ||
result = this._activePlugins[i].stopCallback(evt, eventTarget(evt), combo, this._context); | ||
// if force yes or no return now | ||
if (result === yesForce) { | ||
return true; | ||
} | ||
else if (result === noForce) { | ||
return false; | ||
} | ||
if (typeof result !== 'undefined') { | ||
stopCallback = result; | ||
} | ||
} | ||
} | ||
return stopCallback === yes; | ||
}; | ||
Context.prototype._combinePlugins = function() { | ||
var globalPluginLength = this._plugins.global.length; | ||
var contextPluginLength = 0; | ||
var i = 0; | ||
if (this._context && typeof this._plugins.contexts[this._context] !== 'undefined') { | ||
contextPluginLength = this._plugins.contexts[this._context].length; | ||
} | ||
var length = Math.max(globalPluginLength, contextPluginLength); | ||
this._activePlugins = []; | ||
while (i < length) { | ||
if (i < globalPluginLength) { | ||
this._activePlugins.push(this._plugins.global[i]); | ||
} | ||
if (i < contextPluginLength) { | ||
this._activePlugins.push(this._plugins.contexts[this._context][i]); | ||
} | ||
i++; | ||
} | ||
}; | ||
module.exports = Context; |
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable and can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
100
24185
2
9
12
513
2
1
+ Addedcross-browser@git+https://github.com/MitMaro/CrossBrowser
+ Addedlodash@^3.5.0
+ Addedlodash@3.10.1(transitive)