mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Added keyboard shortcuts (part 1) (#70)
Scrolling in message view (#109)
This commit is contained in:
parent
3936a818b7
commit
8979687f88
37 changed files with 2095 additions and 333 deletions
20
vendors/keymaster/MIT-LICENSE
vendored
Normal file
20
vendors/keymaster/MIT-LICENSE
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
Copyright (c) 2011-2013 Thomas Fuchs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
212
vendors/keymaster/README.markdown
vendored
Normal file
212
vendors/keymaster/README.markdown
vendored
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
# keymaster.js
|
||||
|
||||
Keymaster is a simple micro-library for defining and
|
||||
dispatching keyboard shortcuts in web applications.
|
||||
|
||||
It has no dependencies.
|
||||
|
||||
*It’s a work in progress (e.g. beta), so spare me your nerdrage and instead
|
||||
contribute! Patches are welcome, but they are not guaranteed to make
|
||||
it in.*
|
||||
|
||||
## Usage
|
||||
|
||||
Include `keymaster.js` in your web app*, by loading it as usual:
|
||||
|
||||
```html
|
||||
<script src="keymaster.js"></script>
|
||||
```
|
||||
|
||||
Keymaster has no dependencies and can be used completely standalone.
|
||||
It should not interfere with any JavaScript libraries or frameworks.
|
||||
|
||||
_*Preferably use a minified version that fits your workflow. You can
|
||||
run `make` to have UglifyJS (if you have it installed) create a
|
||||
`keymaster.min.js` file for you._
|
||||
|
||||
## Defining shortcuts
|
||||
|
||||
One global method is exposed, `key` which defines shortcuts when
|
||||
called directly.
|
||||
|
||||
```javascript
|
||||
// define short of 'a'
|
||||
key('a', function(){ alert('you pressed a!') });
|
||||
|
||||
// returning false stops the event and prevents default browser events
|
||||
key('ctrl+r', function(){ alert('stopped reload!'); return false });
|
||||
|
||||
// multiple shortcuts that do the same thing
|
||||
key('⌘+r, ctrl+r', function(){ });
|
||||
```
|
||||
|
||||
The handler method is called with two arguments set, the keydown `event` fired, and
|
||||
an object containing, among others, the following two properties:
|
||||
|
||||
`shortcut`: a string that contains the shortcut used, e.g. `ctrl+r`
|
||||
`scope`: a string describing the scope (or `all`)
|
||||
|
||||
```javascript
|
||||
key('⌘+r, ctrl+r', function(event, handler){
|
||||
console.log(handler.shortcut, handler.scope);
|
||||
});
|
||||
|
||||
// "ctrl+r", "all"
|
||||
```
|
||||
|
||||
|
||||
## Supported keys
|
||||
|
||||
Keymaster understands the following modifiers:
|
||||
`⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, and `⌘`.
|
||||
|
||||
The following special keys can be used for shortcuts:
|
||||
`backspace`, `tab`, `clear`, `enter`, `return`, `esc`, `escape`, `space`,
|
||||
`up`, `down`, `left`, `right`, `home`, `end`, `pageup`, `pagedown`, `del`, `delete`
|
||||
and `f1` through `f19`.
|
||||
|
||||
|
||||
## Modifier key queries
|
||||
|
||||
At any point in time (even in code other than key shortcut handlers),
|
||||
you can query the `key` object for the state of any keys. This
|
||||
allows easy implementation of things like shift+click handlers. For example,
|
||||
`key.shift` is `true` if the shift key is currently pressed.
|
||||
|
||||
```javascript
|
||||
if(key.shift) alert('shift is pressed, OMGZ!');
|
||||
```
|
||||
|
||||
|
||||
## Other key queries
|
||||
|
||||
At any point in time (even in code other than key shortcut handlers),
|
||||
you can query the `key` object for the state of any key. This
|
||||
is very helpful for game development using a game loop. For example,
|
||||
`key.isPressed(77)` is `true` if the M key is currently pressed.
|
||||
|
||||
```javascript
|
||||
if(key.isPressed("M")) alert('M key is pressed, can ya believe it!?');
|
||||
if(key.isPressed(77)) alert('M key is pressed, can ya believe it!?');
|
||||
```
|
||||
|
||||
You can also get these as an array using...
|
||||
```javascript
|
||||
key.getPressedKeyCodes() // returns an array of key codes currently pressed
|
||||
```
|
||||
|
||||
|
||||
## Scopes
|
||||
|
||||
If you want to reuse the same shortcut for separate areas in your single page app,
|
||||
Keymaster supports switching between scopes. Use the `key.setScope` method to set scope.
|
||||
|
||||
```javascript
|
||||
// define shortcuts with a scope
|
||||
key('o, enter', 'issues', function(){ /* do something */ });
|
||||
key('o, enter', 'files', function(){ /* do something else */ });
|
||||
|
||||
// set the scope (only 'all' and 'issues' shortcuts will be honored)
|
||||
key.setScope('issues'); // default scope is 'all'
|
||||
```
|
||||
|
||||
|
||||
## Filter key presses
|
||||
|
||||
By default, when an `INPUT`, `SELECT` or `TEXTAREA` element is focused, Keymaster doesn't process any shortcuts.
|
||||
|
||||
You can change this by overwriting `key.filter` with a new function. This function is called before
|
||||
Keymaster processes shortcuts, with the keydown event as argument.
|
||||
|
||||
If your function returns false, then the no shortcuts will be processed.
|
||||
|
||||
Here's the default implementation for reference:
|
||||
|
||||
```javascript
|
||||
function filter(event){
|
||||
var tagName = (event.target || event.srcElement).tagName;
|
||||
return !(tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
|
||||
}
|
||||
```
|
||||
|
||||
If you only want _some_ shortcuts to work while in an input element, you can change the scope in the
|
||||
`key.filter` function. Here's an example implementation, setting the scope to either `'input'` or `'other'`.
|
||||
Don't forget to return `true` so the any shortcuts get processed.
|
||||
|
||||
```javascript
|
||||
key.filter = function(event){
|
||||
var tagName = (event.target || event.srcElement).tagName;
|
||||
key.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other');
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
However a more robust way to handle this is to use proper
|
||||
focus and blur event handlers on your input element, and change scopes there as you see fit.
|
||||
|
||||
|
||||
## noConflict mode
|
||||
|
||||
You can call ```key.noConflict``` to remove the ```key``` function from global scope and restore whatever ```key``` was defined to before Keymaster was loaded. Calling ```key.noConflict``` will return the Keymaster ```key``` function.
|
||||
|
||||
```javascript
|
||||
var k = key.noConflict();
|
||||
k('a', function() { /* ... */ });
|
||||
|
||||
key()
|
||||
// --> TypeError: 'undefined' is not a function
|
||||
```
|
||||
|
||||
|
||||
## Unbinding shortcuts
|
||||
|
||||
Similar to defining shortcuts, they can be unbound using `key.unbind`.
|
||||
|
||||
```javascript
|
||||
// unbind 'a' handler
|
||||
key.unbind('a');
|
||||
|
||||
// unbind a key only for a single scope
|
||||
// when no scope is specified it defaults to the current scope (key.getScope())
|
||||
key.unbind('o, enter', 'issues');
|
||||
key.unbind('o, enter', 'files');
|
||||
```
|
||||
|
||||
|
||||
## Notes
|
||||
|
||||
Keymaster should work with any browser that fires `keyup` and `keydown` events,
|
||||
and is tested with IE (6+), Safari, Firefox and Chrome.
|
||||
|
||||
See [http://madrobby.github.com/keymaster/](http://madrobby.github.com/keymaster/) for a live demo.
|
||||
|
||||
|
||||
## CoffeeScript
|
||||
|
||||
If you're using CoffeeScript, configuring key shortcuts couldn't be simpler:
|
||||
|
||||
```coffeescript
|
||||
key 'a', -> alert('you pressed a!')
|
||||
|
||||
key '⌘+r, ctrl+r', ->
|
||||
alert 'stopped reload!'
|
||||
off
|
||||
|
||||
key 'o, enter', 'issues', ->
|
||||
whatevs()
|
||||
|
||||
alert 'shift is pressed, OMGZ!' if key.shift
|
||||
```
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
To contribute, please fork Keymaster, add your patch and tests for it (in the `test/` folder) and
|
||||
submit a pull request.
|
||||
|
||||
## TODOs
|
||||
|
||||
* Finish test suite
|
||||
|
||||
Keymaster is (c) 2011-2013 Thomas Fuchs and may be freely distributed under the MIT license.
|
||||
See the `MIT-LICENSE` file.
|
||||
306
vendors/keymaster/keymaster.js
vendored
Normal file
306
vendors/keymaster/keymaster.js
vendored
Normal file
|
|
@ -0,0 +1,306 @@
|
|||
// keymaster.js
|
||||
// (c) 2011-2013 Thomas Fuchs
|
||||
// keymaster.js may be freely distributed under the MIT license.
|
||||
|
||||
;(function(global){
|
||||
var k,
|
||||
_handlers = {},
|
||||
_mods = { 16: false, 18: false, 17: false, 91: false },
|
||||
_scope = 'all',
|
||||
// modifier keys
|
||||
_MODIFIERS = {
|
||||
'⇧': 16, shift: 16,
|
||||
'⌥': 18, alt: 18, option: 18,
|
||||
'⌃': 17, ctrl: 17, control: 17,
|
||||
'⌘': 91, command: 91
|
||||
},
|
||||
// special keys
|
||||
_MAP = {
|
||||
backspace: 8, tab: 9, clear: 12,
|
||||
enter: 13, 'return': 13,
|
||||
esc: 27, escape: 27, space: 32,
|
||||
left: 37, up: 38,
|
||||
right: 39, down: 40,
|
||||
insert: 45,
|
||||
del: 46, 'delete': 46,
|
||||
home: 36, end: 35,
|
||||
pageup: 33, pagedown: 34,
|
||||
',': 188, '.': 190, '/': 191,
|
||||
'`': 192, '-': 189, '=': 187,
|
||||
';': 186, '\'': 222,
|
||||
'[': 219, ']': 221, '\\': 220
|
||||
},
|
||||
code = function(x){
|
||||
return _MAP[x] || x.toUpperCase().charCodeAt(0);
|
||||
},
|
||||
_downKeys = [];
|
||||
|
||||
for(k=1;k<20;k++) _MAP['f'+k] = 111+k;
|
||||
|
||||
// IE doesn't support Array#indexOf, so have a simple replacement
|
||||
function index(array, item){
|
||||
var i = array.length;
|
||||
while(i--) if(array[i]===item) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// for comparing mods before unassignment
|
||||
function compareArray(a1, a2) {
|
||||
if (a1.length != a2.length) return false;
|
||||
for (var i = 0; i < a1.length; i++) {
|
||||
if (a1[i] !== a2[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
var modifierMap = {
|
||||
16:'shiftKey',
|
||||
18:'altKey',
|
||||
17:'ctrlKey',
|
||||
91:'metaKey'
|
||||
};
|
||||
function updateModifierKey(event) {
|
||||
for(k in _mods) _mods[k] = event[modifierMap[k]];
|
||||
};
|
||||
|
||||
// handle keydown event
|
||||
function dispatch(event) {
|
||||
var key, handler, k, i, modifiersMatch, scope;
|
||||
key = event.keyCode;
|
||||
|
||||
if (index(_downKeys, key) == -1) {
|
||||
_downKeys.push(key);
|
||||
}
|
||||
|
||||
// if a modifier key, set the key.<modifierkeyname> property to true and return
|
||||
if(key == 93 || key == 224) key = 91; // right command on webkit, command on Gecko
|
||||
if(key in _mods) {
|
||||
_mods[key] = true;
|
||||
// 'assignKey' from inside this closure is exported to window.key
|
||||
for(k in _MODIFIERS) if(_MODIFIERS[k] == key) assignKey[k] = true;
|
||||
return;
|
||||
}
|
||||
updateModifierKey(event);
|
||||
|
||||
// see if we need to ignore the keypress (filter() can can be overridden)
|
||||
// by default ignore key presses if a select, textarea, or input is focused
|
||||
if(!assignKey.filter.call(this, event)) return;
|
||||
|
||||
// abort if no potentially matching shortcuts found
|
||||
if (!(key in _handlers)) return;
|
||||
|
||||
scope = getScope();
|
||||
|
||||
// for each potential shortcut
|
||||
for (i = 0; i < _handlers[key].length; i++) {
|
||||
handler = _handlers[key][i];
|
||||
|
||||
// see if it's in the current scope
|
||||
if(handler.scope == scope || handler.scope == 'all'){
|
||||
// check if modifiers match if any
|
||||
modifiersMatch = handler.mods.length > 0;
|
||||
for(k in _mods)
|
||||
if((!_mods[k] && index(handler.mods, +k) > -1) ||
|
||||
(_mods[k] && index(handler.mods, +k) == -1)) modifiersMatch = false;
|
||||
// call the handler and stop the event if neccessary
|
||||
if((handler.mods.length == 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91]) || modifiersMatch){
|
||||
if(handler.method(event, handler)===false){
|
||||
if(event.preventDefault) event.preventDefault();
|
||||
else event.returnValue = false;
|
||||
if(event.stopPropagation) event.stopPropagation();
|
||||
if(event.cancelBubble) event.cancelBubble = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// unset modifier keys on keyup
|
||||
function clearModifier(event){
|
||||
var key = event.keyCode, k,
|
||||
i = index(_downKeys, key);
|
||||
|
||||
// remove key from _downKeys
|
||||
if (i >= 0) {
|
||||
_downKeys.splice(i, 1);
|
||||
}
|
||||
|
||||
if(key == 93 || key == 224) key = 91;
|
||||
if(key in _mods) {
|
||||
_mods[key] = false;
|
||||
for(k in _MODIFIERS) if(_MODIFIERS[k] == key) assignKey[k] = false;
|
||||
}
|
||||
};
|
||||
|
||||
function resetModifiers() {
|
||||
for(k in _mods) _mods[k] = false;
|
||||
for(k in _MODIFIERS) assignKey[k] = false;
|
||||
};
|
||||
|
||||
// parse and assign shortcut
|
||||
function assignKey(key, scope, method){
|
||||
var keys, mods, bScopeIsArray = false;
|
||||
keys = getKeys(key);
|
||||
if (method === undefined) {
|
||||
method = scope;
|
||||
scope = 'all';
|
||||
}
|
||||
|
||||
bScopeIsArray = !!(typeof scope !== 'string' && scope.length && typeof scope[0] === 'string');
|
||||
|
||||
// for each shortcut
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
// set modifier keys if any
|
||||
mods = [];
|
||||
key = keys[i].split('+');
|
||||
if (key.length > 1){
|
||||
mods = getMods(key);
|
||||
key = [key[key.length-1]];
|
||||
}
|
||||
// convert to keycode and...
|
||||
key = key[0];
|
||||
key = code(key);
|
||||
// ...store handler
|
||||
if (!(key in _handlers)) _handlers[key] = [];
|
||||
|
||||
if (bScopeIsArray) {
|
||||
for (var j = 0; j < scope.length; j++) {
|
||||
_handlers[key].push({ shortcut: keys[i], scope: scope[j], method: method, key: keys[i], mods: mods });
|
||||
}
|
||||
} else {
|
||||
_handlers[key].push({ shortcut: keys[i], scope: scope, method: method, key: keys[i], mods: mods });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// unbind all handlers for given key in current scope
|
||||
function unbindKey(key, scope) {
|
||||
var multipleKeys, keys,
|
||||
mods = [],
|
||||
i, j, obj;
|
||||
|
||||
multipleKeys = getKeys(key);
|
||||
|
||||
for (j = 0; j < multipleKeys.length; j++) {
|
||||
keys = multipleKeys[j].split('+');
|
||||
|
||||
if (keys.length > 1) {
|
||||
mods = getMods(keys);
|
||||
key = keys[keys.length - 1];
|
||||
}
|
||||
|
||||
key = code(key);
|
||||
|
||||
if (scope === undefined) {
|
||||
scope = getScope();
|
||||
}
|
||||
if (!_handlers[key]) {
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < _handlers[key].length; i++) {
|
||||
obj = _handlers[key][i];
|
||||
// only clear handlers if correct scope and mods match
|
||||
if (obj.scope === scope && compareArray(obj.mods, mods)) {
|
||||
_handlers[key][i] = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Returns true if the key with code 'keyCode' is currently down
|
||||
// Converts strings into key codes.
|
||||
function isPressed(keyCode) {
|
||||
if (typeof(keyCode)=='string') {
|
||||
keyCode = code(keyCode);
|
||||
}
|
||||
return index(_downKeys, keyCode) != -1;
|
||||
}
|
||||
|
||||
function getPressedKeyCodes() {
|
||||
return _downKeys.slice(0);
|
||||
}
|
||||
|
||||
function filter(event){
|
||||
var tagName = (event.target || event.srcElement).tagName;
|
||||
// ignore keypressed in any elements that support keyboard data input
|
||||
return !(tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
|
||||
}
|
||||
|
||||
// initialize key.<modifier> to false
|
||||
for(k in _MODIFIERS) assignKey[k] = false;
|
||||
|
||||
// set current scope (default 'all')
|
||||
function setScope(scope){ _scope = scope || 'all' };
|
||||
function getScope(){ return _scope || 'all' };
|
||||
|
||||
// delete all handlers for a given scope
|
||||
function deleteScope(scope){
|
||||
var key, handlers, i;
|
||||
|
||||
for (key in _handlers) {
|
||||
handlers = _handlers[key];
|
||||
for (i = 0; i < handlers.length; ) {
|
||||
if (handlers[i].scope === scope) handlers.splice(i, 1);
|
||||
else i++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// abstract key logic for assign and unassign
|
||||
function getKeys(key) {
|
||||
var keys;
|
||||
key = key.replace(/\s/g, '');
|
||||
keys = key.split(',');
|
||||
if ((keys[keys.length - 1]) == '') {
|
||||
keys[keys.length - 2] += ',';
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
// abstract mods logic for assign and unassign
|
||||
function getMods(key) {
|
||||
var mods = key.slice(0, key.length - 1);
|
||||
for (var mi = 0; mi < mods.length; mi++)
|
||||
mods[mi] = _MODIFIERS[mods[mi]];
|
||||
return mods;
|
||||
}
|
||||
|
||||
// cross-browser events
|
||||
function addEvent(object, event, method) {
|
||||
if (object.addEventListener)
|
||||
object.addEventListener(event, method, false);
|
||||
else if(object.attachEvent)
|
||||
object.attachEvent('on'+event, function(){ method(window.event) });
|
||||
};
|
||||
|
||||
// set the handlers globally on document
|
||||
addEvent(document, 'keydown', function(event) { dispatch(event) }); // Passing _scope to a callback to ensure it remains the same by execution. Fixes #48
|
||||
addEvent(document, 'keyup', clearModifier);
|
||||
|
||||
// reset modifiers to false whenever the window is (re)focused.
|
||||
addEvent(window, 'focus', resetModifiers);
|
||||
|
||||
// store previously defined key
|
||||
var previousKey = global.key;
|
||||
|
||||
// restore previously defined key and return reference to our key object
|
||||
function noConflict() {
|
||||
var k = global.key;
|
||||
global.key = previousKey;
|
||||
return k;
|
||||
}
|
||||
|
||||
// set window.key and window.key.set/get/deleteScope, and the default filter
|
||||
global.key = assignKey;
|
||||
global.key.setScope = setScope;
|
||||
global.key.getScope = getScope;
|
||||
global.key.deleteScope = deleteScope;
|
||||
global.key.filter = filter;
|
||||
global.key.isPressed = isPressed;
|
||||
global.key.getPressedKeyCodes = getPressedKeyCodes;
|
||||
global.key.noConflict = noConflict;
|
||||
global.key.unbind = unbindKey;
|
||||
|
||||
if(typeof module !== 'undefined') module.exports = key;
|
||||
|
||||
})(this);
|
||||
11
vendors/keymaster/package.json
vendored
Normal file
11
vendors/keymaster/package.json
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "keymaster",
|
||||
"description": "library for defining and dispatching keyboard shortcuts",
|
||||
"version": "1.6.2",
|
||||
"author": "Thomas Fuchs <thomas@slash7.com> (http://mir.aculo.us)",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/madrobby/keymaster"
|
||||
},
|
||||
"main": "./keymaster.js"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue