jquery-shortcuts
Advanced tools
Comparing version 0.1.0 to 1.3.0
/*! | ||
* jQuery Shortcuts Plugin v1.2.0 | ||
* jQuery Shortcuts Plugin v1.3.0 | ||
* https://github.com/riga/jquery.shortcuts | ||
@@ -24,2 +24,10 @@ * | ||
/** | ||
* Bookkeeping for mapping "key -> group" for | ||
* faster resolving priorities. | ||
*/ | ||
var keyGroupMap = {}; | ||
/** | ||
* Default options. | ||
@@ -39,3 +47,6 @@ */ | ||
// the target | ||
target: document | ||
target: document, | ||
// apply priorities? | ||
priorities: false | ||
}; | ||
@@ -197,2 +208,5 @@ | ||
// the priority | ||
_priority: 0, | ||
// parent shortcuts | ||
@@ -217,2 +231,11 @@ _parent: null, | ||
// priority getter/setter | ||
priority: function(priority) { | ||
if (priority === undefined) { | ||
return self._priority; | ||
} else { | ||
self._priority = priority; | ||
} | ||
}, | ||
// parent getter | ||
@@ -257,2 +280,18 @@ parent: function() { | ||
// determines for an invoked key event whether the callback can be fired, i.e. | ||
// there's no other group listening for the key event with a higher priority | ||
_hasPrecedence: function(key) { | ||
var groups = keyGroupMap[key]; | ||
if (!groups || groups.length == 0) { | ||
return true; | ||
} | ||
var highestPrio = Math.max.apply(Math, groups.map(function(group) { | ||
return group.priority(); | ||
})); | ||
return self.priority() >= highestPrio; | ||
}, | ||
// adds handlers for a key | ||
@@ -273,2 +312,7 @@ add: function(key) { | ||
// handle priorities | ||
if (options.priorities && !self._hasPrecedence(key)) { | ||
return; | ||
} | ||
// prevent default? | ||
@@ -286,2 +330,8 @@ cbs.fire(event); | ||
// store the info that _this_ group (self) listens to that key | ||
var groups = keyGroupMap[key] = keyGroupMap[key] || []; | ||
if (!~groups.indexOf(self)) { | ||
groups.push(self); | ||
} | ||
return self; | ||
@@ -295,2 +345,11 @@ }, | ||
// remove _this_ group (self) from the key group mapping | ||
var groups = keyGroupMap[key]; | ||
if (groups) { | ||
var idx = groups.indexOf(self); | ||
if (~idx) { | ||
groups.splice(idx, 1); | ||
} | ||
} | ||
if (!self._callbacks[key]) { | ||
@@ -297,0 +356,0 @@ return self; |
{ | ||
"name": "jquery-shortcuts", | ||
"version": "0.1.0", | ||
"version": "1.3.0", | ||
"author": "Marcel Rieger <marcelrieger@icloud.com>", | ||
@@ -5,0 +5,0 @@ "description": "jQuery.Shortcuts lets you easily switch between sets of arbitrary and easy-to-define shortcuts.", |
# jQuery.Shortcuts | ||
jQuery.Shortcuts lets you easily switch between sets of arbitrary and easy-to-define shortcuts. You can even manage the sets of shortcuts via namespaces. | ||
jQuery.Shortcuts lets you easily switch between sets (groups) of arbitrary and easy-to-define shortcuts. You can even manage parentage of shortcut groups via namespaces. | ||
@@ -11,8 +11,32 @@ | ||
## Example | ||
## Examples | ||
##### Configuration | ||
```javascript | ||
// configure $.Shortcuts _before_ you create any group | ||
// (these are the default options) | ||
$.Shortcuts({ | ||
// name of the global namespace | ||
global: "global", | ||
// delimitter that separates namespaces | ||
delimitter: ".", | ||
// the default event type | ||
defaultEvent: "keydown", | ||
// the target | ||
target: document, | ||
// apply priorities? | ||
priorities: false | ||
}); | ||
``` | ||
##### Add and remove (global) shortcuts: | ||
```javascript | ||
// get the (global) shortcuts object | ||
// get the (global) shortcut group | ||
var sc = $.Shortcuts(); | ||
@@ -35,3 +59,3 @@ | ||
```javascript | ||
// register a new shortcuts object | ||
// register a new shortcut group | ||
var myTopSc = $.Shortcuts("top"); | ||
@@ -42,3 +66,3 @@ | ||
// register another shortcuts object that belongs to "top" | ||
// register another group that belongs to "top" | ||
var mySubSc = $.Shortcuts("top.sub"); | ||
@@ -51,24 +75,28 @@ | ||
// => true | ||
``` | ||
##### Configuration | ||
##### Using priorities | ||
```javascript | ||
// configure $.Shortcuts _before_ you create any instance | ||
// (these are the default options) | ||
$.Shortcuts({ | ||
// name of the global namespace | ||
global: "global", | ||
// configure the plugin | ||
$.Shortcuts({ priorities: true }); | ||
// delimitter that separates namespaces | ||
delimitter: ".", | ||
// register two shortcut groups | ||
var sc1 = $.Shortcuts("one"); | ||
var sc2 = $.Shortcuts("two"); | ||
// the default event type | ||
defaultEvent: "keydown", | ||
// create and apply a handler for the same key event | ||
var handler = function(event) { | ||
// do sth here | ||
}; | ||
sc1.add("ctrl+h", handler); | ||
sc2.add("ctrl+h", handler); | ||
// the target | ||
target: document | ||
}); | ||
// assign a higher priority for sc2 (default is 0) | ||
sc2.priority(10); | ||
// | ||
// no, when ctrl+h is pressed, only sc2 fires | ||
// | ||
``` | ||
@@ -80,3 +108,3 @@ | ||
* **`$.Shortcuts([namespace|options])`** | ||
> If `options` are passed, the global options are extended and the `$.Shortcuts` object is returned. If a `namespace` is passed, a new shortcuts instance with that namespace is created and returned. When no argument is given, the global shortcuts object is returned. Parentage is built automatically. A namespace consists of a number of names seperated by a delimitter. Example: namespace `"foo.bar"` => shortcuts `"global"` -> shortcuts `"foo"` -> shortcuts `"bar"`. **Note** that the global namespace (`"global"` in this example) is always prepended. | ||
> If `options` are passed, the global options are extended and the `$.Shortcuts` object is returned. If a `namespace` is passed, a new shortcuts instance/group with that namespace is created and returned. When no argument is given, the global shortcut group is returned. Parentage is built automatically. A namespace consists of a number of names seperated by a delimitter. Example: namespace `"foo.bar"` => shortcuts `"global"` -> shortcuts `"foo"` -> shortcuts `"bar"`. **Note** that the global namespace (`"global"` in this example) is always prepended. | ||
@@ -90,9 +118,9 @@ * `namespace()` | ||
* `parent()` | ||
> Returns the parent shortcuts, or `null` when invoked on the global shortcuts object. | ||
> Returns the parent shortcuts, or `null` when invoked on the global shortcut group. | ||
* `children()` | ||
> Return all child shortcut objects mapped to their names. | ||
> Return all child shortcut groups mapped to their names. | ||
* `child(name)` | ||
> Return a child shortcuts object given by `name`. | ||
> Return a child shortcut group given by `name`. | ||
@@ -108,2 +136,5 @@ * `enabled()` | ||
* `priority([priority])` | ||
> When `priority` is given, the priority is set to that value. Otherwise, the current priority is returned. **Note** that priorities are only applied, when the `priorities` option is set to `true`. | ||
* `add(key, [handler1], [handler2], [...])` | ||
@@ -110,0 +141,0 @@ > Add handlers for a shortcut given by `key`. See [John Resig's hotkeys plugin](https://github.com/jeresig/jquery.hotkeys) for more information on the format. As of version 1.1.0, you can prepend the desired event type to your key using the colon character, e.g. `keydown:ctrl+a`. `keydown` is the default. Other valid events are `keyup` and `keypress`. |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
19904
492
1
155