N°5621 Move mousetrap 1.6.5 to NPM

This commit is contained in:
Pierre Goiffon
2024-02-16 16:37:46 +01:00
parent d4c54b6e1c
commit ff079f7d01
33 changed files with 2947 additions and 2 deletions

View File

@@ -1,2 +1,6 @@
/**
* @deprecated 3.2.0 N°5621 Moved to NPM
*/
(function(d){function n(b,a,h){if(this.recording)if("keydown"==h.type){1===b.length&&g&&k();for(i=0;i<a.length;++i)l(a[i]);l(b)}else"keyup"==h.type&&0<c.length&&k();else p.apply(this,arguments)}function l(b){var a;for(a=0;a<c.length;++a)if(c[a]===b)return;c.push(b);1===b.length&&(g=!0)}function k(){e.push(c);c=[];g=!1;clearTimeout(m);m=setTimeout(q,1E3)}function r(b){var a;for(a=0;a<b.length;++a)b[a].sort(function(a,b){return 1<a.length&&1===b.length?-1:1===a.length&&1<b.length?1:a>b?1:-1}),b[a]=
b[a].join("+")}function q(){f&&(r(e),f(e));e=[];f=null;c=[]}var e=[],f=null,c=[],g=!1,m=null,p=d.prototype.handleKey;d.prototype.record=function(b){var a=this;a.recording=!0;f=function(){a.recording=!1;b.apply(a,arguments)}};d.prototype.handleKey=function(){n.apply(this,arguments)};d.init()})(Mousetrap);

View File

@@ -1,3 +1,7 @@
/**
* @deprecated 3.2.0 N°5621 Moved to NPM
*/
/* mousetrap v1.6.5 craig.is/killing/mice */
(function(q,u,c){function v(a,b,g){a.addEventListener?a.addEventListener(b,g,!1):a.attachEvent("on"+b,g)}function z(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return n[a.which]?n[a.which]:r[a.which]?r[a.which]:String.fromCharCode(a.which).toLowerCase()}function F(a){var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");return b}function w(a){return"shift"==a||"ctrl"==a||"alt"==a||
"meta"==a}function A(a,b){var g,d=[];var e=a;"+"===e?e=["+"]:(e=e.replace(/\+{2}/g,"+plus"),e=e.split("+"));for(g=0;g<e.length;++g){var m=e[g];B[m]&&(m=B[m]);b&&"keypress"!=b&&C[m]&&(m=C[m],d.push("shift"));w(m)&&d.push(m)}e=m;g=b;if(!g){if(!p){p={};for(var c in n)95<c&&112>c||n.hasOwnProperty(c)&&(p[n[c]]=c)}g=p[e]?"keydown":"keypress"}"keypress"==g&&d.length&&(g="keydown");return{key:m,modifiers:d,action:g}}function D(a,b){return null===a||a===u?!1:a===b?!0:D(a.parentNode,b)}function d(a){function b(a){a=

5
node_modules/.package-lock.json generated vendored
View File

@@ -80,6 +80,11 @@
"resolved": "https://registry.npmjs.org/jquery-ui-dist/-/jquery-ui-dist-1.12.1.tgz",
"integrity": "sha512-UnQQWIvxaP7s13FLx37WufEwDYLSXKpp9JHrqEYTP6GqQNM+RMVEwsJMoc163QlzEaAV5YVfWlgQU5FPjKBtSw=="
},
"node_modules/mousetrap": {
"version": "1.6.5",
"resolved": "https://registry.npmjs.org/mousetrap/-/mousetrap-1.6.5.tgz",
"integrity": "sha512-QNo4kEepaIBwiT8CDhP98umTetp+JNfQYBWvC1pc6/OAibuXtRcxZ58Qz8skvEHYvURne/7R8T5VoOI7rDsEUA=="
},
"node_modules/scrollmagic": {
"version": "2.0.8",
"license": "(MIT OR GPL-3.0+)",

35
node_modules/mousetrap/Gruntfile.js generated vendored Normal file
View File

@@ -0,0 +1,35 @@
/*jshint node:true */
module.exports = function(grunt) {
'use strict';
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
complexity: {
options: {
errorsOnly: false,
cyclomatic: 10,
halstead: 30,
maintainability: 85
},
generic: {
src: [
'mousetrap.js'
]
},
plugins: {
src: [
'plugins/**/*.js',
'!plugins/**/tests/**',
'!plugins/**/*.min.js'
]
}
}
});
grunt.loadNpmTasks('grunt-complexity');
grunt.registerTask('default', [
'complexity'
]);
};

193
node_modules/mousetrap/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,193 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
--- Exceptions to the Apache 2.0 License ----
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into an Object form of such source code, you
may redistribute such embedded portions in such Object form without complying
with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
In addition, if you combine or link compiled forms of this Software with
software that is licensed under the GPLv2 ("Combined Software") and if a
court of competent jurisdiction determines that the patent provision (Section
3), the indemnity provision (Section 9) or other Section of the License
conflicts with the conditions of the GPLv2, you may retroactively and
prospectively choose to deem waived or otherwise exclude such Section(s) of
the License, but only in their entirety and only with respect to the Combined
Software.

101
node_modules/mousetrap/README.md generated vendored Normal file
View File

@@ -0,0 +1,101 @@
# Mousetrap
[![CDNJS](https://img.shields.io/cdnjs/v/mousetrap.svg)](https://cdnjs.com/libraries/mousetrap)
Mousetrap is a simple library for handling keyboard shortcuts in Javascript.
It is licensed under the Apache 2.0 license.
It is around **2kb** minified and gzipped and **4.5kb** minified, has no external dependencies, and has been tested in the following browsers:
- Internet Explorer 6+
- Safari
- Firefox
- Chrome
It has support for `keypress`, `keydown`, and `keyup` events on specific keys, keyboard combinations, or key sequences.
## Getting started
1. Include mousetrap on your page before the closing `</body>` tag
```html
<script src="/path/to/mousetrap.min.js"></script>
```
or install `mousetrap` from `npm` and require it
```js
var Mousetrap = require('mousetrap');
```
2. Add some keyboard events to listen for
```html
<script>
// single keys
Mousetrap.bind('4', function() { console.log('4'); });
Mousetrap.bind("?", function() { console.log('show shortcuts!'); });
Mousetrap.bind('esc', function() { console.log('escape'); }, 'keyup');
// combinations
Mousetrap.bind('command+shift+k', function() { console.log('command shift k'); });
// map multiple combinations to the same callback
Mousetrap.bind(['command+k', 'ctrl+k'], function() {
console.log('command k or control k');
// return false to prevent default browser behavior
// and stop event from bubbling
return false;
});
// gmail style sequences
Mousetrap.bind('g i', function() { console.log('go to inbox'); });
Mousetrap.bind('* a', function() { console.log('select all'); });
// konami code!
Mousetrap.bind('up up down down left right left right b a enter', function() {
console.log('konami code');
});
</script>
```
## Why Mousetrap?
There are a number of other similar libraries out there so what makes this one different?
- There are no external dependencies, no framework is required
- You are not limited to `keydown` events (You can specify `keypress`, `keydown`, or `keyup` or let Mousetrap choose for you).
- You can bind key events directly to special keys such as `?` or `*` without having to specify `shift+/` or `shift+8` which are not consistent across all keyboards
- It works with international keyboard layouts
- You can bind Gmail like key sequences in addition to regular keys and key combinations
- You can programatically trigger key events with the `trigger()` method
- It works with the numeric keypad on your keyboard
- The code is well documented/commented
## Tests
Unit tests are run with <a href="https://mochajs.org/">mocha</a>.
### Running in browser
[View it online](http://rawgit.com/ccampbell/mousetrap/master/tests/mousetrap.html) to check your browser compatibility. You may also download the repo and open `tests/mousetrap.html` in your browser.
### Running with Node.js
1. Install development dependencies
```sh
cd /path/to/repo
npm install
```
3. Run tests
```sh
npm test
```
## Documentation
Full documentation can be found at https://craig.is/killing/mice

1058
node_modules/mousetrap/mousetrap.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

11
node_modules/mousetrap/mousetrap.min.js generated vendored Normal file
View File

@@ -0,0 +1,11 @@
/* mousetrap v1.6.5 craig.is/killing/mice */
(function(q,u,c){function v(a,b,g){a.addEventListener?a.addEventListener(b,g,!1):a.attachEvent("on"+b,g)}function z(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return n[a.which]?n[a.which]:r[a.which]?r[a.which]:String.fromCharCode(a.which).toLowerCase()}function F(a){var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");return b}function w(a){return"shift"==a||"ctrl"==a||"alt"==a||
"meta"==a}function A(a,b){var g,d=[];var e=a;"+"===e?e=["+"]:(e=e.replace(/\+{2}/g,"+plus"),e=e.split("+"));for(g=0;g<e.length;++g){var m=e[g];B[m]&&(m=B[m]);b&&"keypress"!=b&&C[m]&&(m=C[m],d.push("shift"));w(m)&&d.push(m)}e=m;g=b;if(!g){if(!p){p={};for(var c in n)95<c&&112>c||n.hasOwnProperty(c)&&(p[n[c]]=c)}g=p[e]?"keydown":"keypress"}"keypress"==g&&d.length&&(g="keydown");return{key:m,modifiers:d,action:g}}function D(a,b){return null===a||a===u?!1:a===b?!0:D(a.parentNode,b)}function d(a){function b(a){a=
a||{};var b=!1,l;for(l in p)a[l]?b=!0:p[l]=0;b||(x=!1)}function g(a,b,t,f,g,d){var l,E=[],h=t.type;if(!k._callbacks[a])return[];"keyup"==h&&w(a)&&(b=[a]);for(l=0;l<k._callbacks[a].length;++l){var c=k._callbacks[a][l];if((f||!c.seq||p[c.seq]==c.level)&&h==c.action){var e;(e="keypress"==h&&!t.metaKey&&!t.ctrlKey)||(e=c.modifiers,e=b.sort().join(",")===e.sort().join(","));e&&(e=f&&c.seq==f&&c.level==d,(!f&&c.combo==g||e)&&k._callbacks[a].splice(l,1),E.push(c))}}return E}function c(a,b,c,f){k.stopCallback(b,
b.target||b.srcElement,c,f)||!1!==a(b,c)||(b.preventDefault?b.preventDefault():b.returnValue=!1,b.stopPropagation?b.stopPropagation():b.cancelBubble=!0)}function e(a){"number"!==typeof a.which&&(a.which=a.keyCode);var b=z(a);b&&("keyup"==a.type&&y===b?y=!1:k.handleKey(b,F(a),a))}function m(a,g,t,f){function h(c){return function(){x=c;++p[a];clearTimeout(q);q=setTimeout(b,1E3)}}function l(g){c(t,g,a);"keyup"!==f&&(y=z(g));setTimeout(b,10)}for(var d=p[a]=0;d<g.length;++d){var e=d+1===g.length?l:h(f||
A(g[d+1]).action);n(g[d],e,f,a,d)}}function n(a,b,c,f,d){k._directMap[a+":"+c]=b;a=a.replace(/\s+/g," ");var e=a.split(" ");1<e.length?m(a,e,b,c):(c=A(a,c),k._callbacks[c.key]=k._callbacks[c.key]||[],g(c.key,c.modifiers,{type:c.action},f,a,d),k._callbacks[c.key][f?"unshift":"push"]({callback:b,modifiers:c.modifiers,action:c.action,seq:f,level:d,combo:a}))}var k=this;a=a||u;if(!(k instanceof d))return new d(a);k.target=a;k._callbacks={};k._directMap={};var p={},q,y=!1,r=!1,x=!1;k._handleKey=function(a,
d,e){var f=g(a,d,e),h;d={};var k=0,l=!1;for(h=0;h<f.length;++h)f[h].seq&&(k=Math.max(k,f[h].level));for(h=0;h<f.length;++h)f[h].seq?f[h].level==k&&(l=!0,d[f[h].seq]=1,c(f[h].callback,e,f[h].combo,f[h].seq)):l||c(f[h].callback,e,f[h].combo);f="keypress"==e.type&&r;e.type!=x||w(a)||f||b(d);r=l&&"keydown"==e.type};k._bindMultiple=function(a,b,c){for(var d=0;d<a.length;++d)n(a[d],b,c)};v(a,"keypress",e);v(a,"keydown",e);v(a,"keyup",e)}if(q){var n={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl",
18:"alt",20:"capslock",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"ins",46:"del",91:"meta",93:"meta",224:"meta"},r={106:"*",107:"+",109:"-",110:".",111:"/",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"`",219:"[",220:"\\",221:"]",222:"'"},C={"~":"`","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7","*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},B={option:"alt",command:"meta","return":"enter",
escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},p;for(c=1;20>c;++c)n[111+c]="f"+c;for(c=0;9>=c;++c)n[c+96]=c.toString();d.prototype.bind=function(a,b,c){a=a instanceof Array?a:[a];this._bindMultiple.call(this,a,b,c);return this};d.prototype.unbind=function(a,b){return this.bind.call(this,a,function(){},b)};d.prototype.trigger=function(a,b){if(this._directMap[a+":"+b])this._directMap[a+":"+b]({},a);return this};d.prototype.reset=function(){this._callbacks={};
this._directMap={};return this};d.prototype.stopCallback=function(a,b){if(-1<(" "+b.className+" ").indexOf(" mousetrap ")||D(b,this.target))return!1;if("composedPath"in a&&"function"===typeof a.composedPath){var c=a.composedPath()[0];c!==a.target&&(b=c)}return"INPUT"==b.tagName||"SELECT"==b.tagName||"TEXTAREA"==b.tagName||b.isContentEditable};d.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)};d.addKeycodes=function(a){for(var b in a)a.hasOwnProperty(b)&&(n[b]=a[b]);p=null};
d.init=function(){var a=d(u),b;for(b in a)"_"!==b.charAt(0)&&(d[b]=function(b){return function(){return a[b].apply(a,arguments)}}(b))};d.init();q.Mousetrap=d;"undefined"!==typeof module&&module.exports&&(module.exports=d);"function"===typeof define&&define.amd&&define(function(){return d})}})("undefined"!==typeof window?window:null,"undefined"!==typeof window?document:null);

20
node_modules/mousetrap/mousetrap.sublime-project generated vendored Normal file
View File

@@ -0,0 +1,20 @@
{
"settings":
{
"detect_indentation": true,
"ensure_newline_at_eof_on_save": true,
"tab_size": 4,
"translate_tabs_to_spaces": true,
"trim_automatic_white_space": false,
"trim_trailing_white_space_on_save": true,
},
"folders":
[
{
"path": "./",
"folder_exclude_patterns": ["bin", "coverage", "node_modules"],
"file_exclude_patterns": ["*.sublime-workspace"]
}
]
}

34
node_modules/mousetrap/package.json generated vendored Normal file
View File

@@ -0,0 +1,34 @@
{
"name": "mousetrap",
"version": "1.6.5",
"description": "Simple library for handling keyboard shortcuts",
"main": "mousetrap.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "mocha --reporter=nyan tests/test.mousetrap.js"
},
"repository": {
"type": "git",
"url": "git://github.com/ccampbell/mousetrap.git"
},
"keywords": [
"keyboard",
"shortcuts",
"events"
],
"author": "Craig Campbell",
"license": "Apache-2.0 WITH LLVM-exception",
"gitHead": "c202a0bd4967d5a3064f9cb376db51dec9345336",
"readmeFilename": "README.md",
"devDependencies": {
"chai": "^4.2.0",
"grunt": "~1.0.3",
"grunt-complexity": "~1.1.0",
"jsdom": "^13.1.0",
"jsdom-global": "^3.0.2",
"mocha": "^5.2.0",
"sinon": "^7.2.2"
}
}

24
node_modules/mousetrap/plugins/README.md generated vendored Normal file
View File

@@ -0,0 +1,24 @@
# Plugins
Plugins extend the functionality of Mousetrap. To use a plugin just include the plugin after mousetrap.
```html
<script src="mousetrap.js"></script>
<script src="mousetrap-record.js"></script>
```
## Bind dictionary
Allows you to make multiple bindings in a single ``Mousetrap.bind`` call.
## Global bind
Allows you to set global bindings that work even inside of input fields.
## Pause/unpause
Allows you to temporarily prevent Mousetrap events from firing.
## Record
Allows you to capture a keyboard shortcut or sequence defined by a user.

View File

@@ -0,0 +1,16 @@
# Bind Dictionary
This extension overwrites the default bind behavior and allows you to bind multiple combinations in a single bind call.
Usage looks like:
```javascript
Mousetrap.bind({
'a': function() { console.log('a'); },
'b': function() { console.log('b'); }
});
```
You can optionally pass in ``keypress``, ``keydown`` or ``keyup`` as a second argument.
Other bind calls work the same way as they do by default.

View File

@@ -0,0 +1,39 @@
/**
* Overwrites default Mousetrap.bind method to optionally accept
* an object to bind multiple key events in a single call
*
* You can pass it in like:
*
* Mousetrap.bind({
* 'a': function() { console.log('a'); },
* 'b': function() { console.log('b'); }
* });
*
* And can optionally pass in 'keypress', 'keydown', or 'keyup'
* as a second argument
*
*/
/* global Mousetrap:true */
(function(Mousetrap) {
var _oldBind = Mousetrap.prototype.bind;
var args;
Mousetrap.prototype.bind = function() {
var self = this;
args = arguments;
// normal call
if (typeof args[0] == 'string' || args[0] instanceof Array) {
return _oldBind.call(self, args[0], args[1], args[2]);
}
// object passed in
for (var key in args[0]) {
if (args[0].hasOwnProperty(key)) {
_oldBind.call(self, key, args[0][key], args[1]);
}
}
};
Mousetrap.init();
}) (Mousetrap);

View File

@@ -0,0 +1 @@
(function(b){var c=b.prototype.bind,a;b.prototype.bind=function(){a=arguments;if("string"==typeof a[0]||a[0]instanceof Array)return c.call(this,a[0],a[1],a[2]);for(var b in a[0])a[0].hasOwnProperty(b)&&c.call(this,b,a[0][b],a[1])};b.init()})(Mousetrap);

15
node_modules/mousetrap/plugins/global-bind/README.md generated vendored Normal file
View File

@@ -0,0 +1,15 @@
# Global Bind
This extension allows you to specify keyboard events that will work anywhere including inside textarea/input fields.
Usage looks like:
```javascript
Mousetrap.bindGlobal('ctrl+s', function() {
_save();
});
```
This means that a keyboard event bound using ``Mousetrap.bind`` will only work outside of form input fields, but using ``Moustrap.bindGlobal`` will work in both places.
If you wanted to create keyboard shortcuts that only work when you are inside a specific textarea you can do that too by creating your own extension.

View File

@@ -0,0 +1,46 @@
/**
* adds a bindGlobal method to Mousetrap that allows you to
* bind specific keyboard shortcuts that will still work
* inside a text input field
*
* usage:
* Mousetrap.bindGlobal('ctrl+s', _saveChanges);
*/
/* global Mousetrap:true */
(function(Mousetrap) {
if (! Mousetrap) {
return;
}
var _globalCallbacks = {};
var _originalStopCallback = Mousetrap.prototype.stopCallback;
Mousetrap.prototype.stopCallback = function(e, element, combo, sequence) {
var self = this;
if (self.paused) {
return true;
}
if (_globalCallbacks[combo] || _globalCallbacks[sequence]) {
return false;
}
return _originalStopCallback.call(self, e, element, combo);
};
Mousetrap.prototype.bindGlobal = function(keys, callback, action) {
var self = this;
self.bind(keys, callback, action);
if (keys instanceof Array) {
for (var i = 0; i < keys.length; i++) {
_globalCallbacks[keys[i]] = true;
}
return;
}
_globalCallbacks[keys] = true;
};
Mousetrap.init();
}) (typeof Mousetrap !== "undefined" ? Mousetrap : undefined);

View File

@@ -0,0 +1 @@
(function(a){var c={},d=a.prototype.stopCallback;a.prototype.stopCallback=function(e,b,a,f){return this.paused?!0:c[a]||c[f]?!1:d.call(this,e,b,a)};a.prototype.bindGlobal=function(a,b,d){this.bind(a,b,d);if(a instanceof Array)for(b=0;b<a.length;b++)c[a[b]]=!0;else c[a]=!0};a.init()})(Mousetrap);

13
node_modules/mousetrap/plugins/pause/README.md generated vendored Normal file
View File

@@ -0,0 +1,13 @@
# Pause/unpause
This extension allows Mousetrap to be paused and unpaused without having to reset keyboard shortcuts and rebind them.
Usage looks like:
```javascript
// stop Mousetrap events from firing
Mousetrap.pause();
// allow Mousetrap events to fire again
Mousetrap.unpause();
```

View File

@@ -0,0 +1,31 @@
/**
* adds a pause and unpause method to Mousetrap
* this allows you to enable or disable keyboard shortcuts
* without having to reset Mousetrap and rebind everything
*/
/* global Mousetrap:true */
(function(Mousetrap) {
var _originalStopCallback = Mousetrap.prototype.stopCallback;
Mousetrap.prototype.stopCallback = function(e, element, combo) {
var self = this;
if (self.paused) {
return true;
}
return _originalStopCallback.call(self, e, element, combo);
};
Mousetrap.prototype.pause = function() {
var self = this;
self.paused = true;
};
Mousetrap.prototype.unpause = function() {
var self = this;
self.paused = false;
};
Mousetrap.init();
}) (Mousetrap);

View File

@@ -0,0 +1 @@
(function(a){var b=a.prototype.stopCallback;a.prototype.stopCallback=function(a,c,d){return this.paused?!0:b.call(this,a,c,d)};a.prototype.pause=function(){this.paused=!0};a.prototype.unpause=function(){this.paused=!1};a.init()})(Mousetrap);

16
node_modules/mousetrap/plugins/record/README.md generated vendored Normal file
View File

@@ -0,0 +1,16 @@
# Record
This extension lets you use Mousetrap to record keyboard sequences and play them back:
```html
<button onclick="recordSequence()">Record</button>
<script>
function recordSequence() {
Mousetrap.record(function(sequence) {
// sequence is an array like ['ctrl+k', 'c']
alert('You pressed: ' + sequence.join(' '));
});
}
</script>
```

View File

@@ -0,0 +1,205 @@
/**
* This extension allows you to record a sequence using Mousetrap.
*
* @author Dan Tao <daniel.tao@gmail.com>
*/
(function(Mousetrap) {
/**
* the sequence currently being recorded
*
* @type {Array}
*/
var _recordedSequence = [],
/**
* a callback to invoke after recording a sequence
*
* @type {Function|null}
*/
_recordedSequenceCallback = null,
/**
* a list of all of the keys currently held down
*
* @type {Array}
*/
_currentRecordedKeys = [],
/**
* temporary state where we remember if we've already captured a
* character key in the current combo
*
* @type {boolean}
*/
_recordedCharacterKey = false,
/**
* a handle for the timer of the current recording
*
* @type {null|number}
*/
_recordTimer = null,
/**
* the original handleKey method to override when Mousetrap.record() is
* called
*
* @type {Function}
*/
_origHandleKey = Mousetrap.prototype.handleKey;
/**
* handles a character key event
*
* @param {string} character
* @param {Array} modifiers
* @param {Event} e
* @returns void
*/
function _handleKey(character, modifiers, e) {
var self = this;
if (!self.recording) {
_origHandleKey.apply(self, arguments);
return;
}
// remember this character if we're currently recording a sequence
if (e.type == 'keydown') {
if (character.length === 1 && _recordedCharacterKey) {
_recordCurrentCombo();
}
for (i = 0; i < modifiers.length; ++i) {
_recordKey(modifiers[i]);
}
_recordKey(character);
// once a key is released, all keys that were held down at the time
// count as a keypress
} else if (e.type == 'keyup' && _currentRecordedKeys.length > 0) {
_recordCurrentCombo();
}
}
/**
* marks a character key as held down while recording a sequence
*
* @param {string} key
* @returns void
*/
function _recordKey(key) {
var i;
// one-off implementation of Array.indexOf, since IE6-9 don't support it
for (i = 0; i < _currentRecordedKeys.length; ++i) {
if (_currentRecordedKeys[i] === key) {
return;
}
}
_currentRecordedKeys.push(key);
if (key.length === 1) {
_recordedCharacterKey = true;
}
}
/**
* marks whatever key combination that's been recorded so far as finished
* and gets ready for the next combo
*
* @returns void
*/
function _recordCurrentCombo() {
_recordedSequence.push(_currentRecordedKeys);
_currentRecordedKeys = [];
_recordedCharacterKey = false;
_restartRecordTimer();
}
/**
* ensures each combo in a sequence is in a predictable order and formats
* key combos to be '+'-delimited
*
* modifies the sequence in-place
*
* @param {Array} sequence
* @returns void
*/
function _normalizeSequence(sequence) {
var i;
for (i = 0; i < sequence.length; ++i) {
sequence[i].sort(function(x, y) {
// modifier keys always come first, in alphabetical order
if (x.length > 1 && y.length === 1) {
return -1;
} else if (x.length === 1 && y.length > 1) {
return 1;
}
// character keys come next (list should contain no duplicates,
// so no need for equality check)
return x > y ? 1 : -1;
});
sequence[i] = sequence[i].join('+');
}
}
/**
* finishes the current recording, passes the recorded sequence to the stored
* callback, and sets Mousetrap.handleKey back to its original function
*
* @returns void
*/
function _finishRecording() {
if (_recordedSequenceCallback) {
_normalizeSequence(_recordedSequence);
_recordedSequenceCallback(_recordedSequence);
}
// reset all recorded state
_recordedSequence = [];
_recordedSequenceCallback = null;
_currentRecordedKeys = [];
}
/**
* called to set a 1 second timeout on the current recording
*
* this is so after each key press in the sequence the recording will wait for
* 1 more second before executing the callback
*
* @returns void
*/
function _restartRecordTimer() {
clearTimeout(_recordTimer);
_recordTimer = setTimeout(_finishRecording, 1000);
}
/**
* records the next sequence and passes it to a callback once it's
* completed
*
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.record = function(callback) {
var self = this;
self.recording = true;
_recordedSequenceCallback = function() {
self.recording = false;
callback.apply(self, arguments);
};
};
Mousetrap.prototype.handleKey = function() {
var self = this;
_handleKey.apply(self, arguments);
};
Mousetrap.init();
})(Mousetrap);

View File

@@ -0,0 +1,2 @@
(function(d){function n(b,a,h){if(this.recording)if("keydown"==h.type){1===b.length&&g&&k();for(i=0;i<a.length;++i)l(a[i]);l(b)}else"keyup"==h.type&&0<c.length&&k();else p.apply(this,arguments)}function l(b){var a;for(a=0;a<c.length;++a)if(c[a]===b)return;c.push(b);1===b.length&&(g=!0)}function k(){e.push(c);c=[];g=!1;clearTimeout(m);m=setTimeout(q,1E3)}function r(b){var a;for(a=0;a<b.length;++a)b[a].sort(function(a,b){return 1<a.length&&1===b.length?-1:1===a.length&&1<b.length?1:a>b?1:-1}),b[a]=
b[a].join("+")}function q(){f&&(r(e),f(e));e=[];f=null;c=[]}var e=[],f=null,c=[],g=!1,m=null,p=d.prototype.handleKey;d.prototype.record=function(b){var a=this;a.recording=!0;f=function(){a.recording=!1;b.apply(a,arguments)}};d.prototype.handleKey=function(){n.apply(this,arguments)};d.init()})(Mousetrap);

29
node_modules/mousetrap/plugins/record/tests/index.html generated vendored Normal file
View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>Jelly</title>
<meta charset=utf-8>
<link href="jelly.css" rel="stylesheet">
</head>
<body>
<h1>Jelly</h1>
<h2>For testing the <strong>record</strong> extension</h2>
<p>Click "Record" to test recording a sequence.</p>
<button class="test-record">Record</button>
<div class="test-record-result"></div>
<script type="text/javascript" src="../../../tests/libs/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../../../mousetrap.js"></script>
<script type="text/javascript" src="../mousetrap-record.js"></script>
<script type="text/javascript" src="jelly.js"></script>
<script type="text/javascript">
Jelly.spread();
</script>
</body>
</html>

18
node_modules/mousetrap/plugins/record/tests/jelly.css generated vendored Normal file
View File

@@ -0,0 +1,18 @@
body {
font-family: helvetica, arial, sans-serif;
line-height: 20px;
}
kbd {
background-color: #ccc;
display: inline-block;
padding: 0.5ex 1em;
}
.test-record-result {
margin-top: 20px;
}
.test-record-result span:nth-child(n+2) {
margin-left: 10px;
}

53
node_modules/mousetrap/plugins/record/tests/jelly.js generated vendored Normal file
View File

@@ -0,0 +1,53 @@
/**
* Peanut butter goes great with jelly.
*
* @author Dan Tao <daniel.tao@gmail.com>
*/
var Jelly = (function() {
var recordButton = $("button.test-record"),
recordResult = $("div.test-record-result");
function _formatSequenceAsHtml(sequence) {
var combos = [],
i;
for (i = 0; i < sequence.length; ++i) {
combos.push('<span>' + _formatKeysAsHtml(sequence[i].split('+')) + '</span>');
}
return combos.join(' ');
}
function _formatKeysAsHtml(keys) {
var htmlKeys = [],
i;
for (i = 0; i < keys.length; ++i) {
htmlKeys.push('<kbd>' + keys[i] + '</kbd>');
}
return htmlKeys.join('+');
}
function _prepareRecordTest() {
recordButton.prop('disabled', true);
recordButton.text('Recording');
Mousetrap.record(function(sequence) {
recordResult.html(_formatSequenceAsHtml(sequence));
recordButton.prop('disabled', false);
recordButton.text('Record');
});
// take focus away from the button so that Mousetrap will actually
// capture keystrokes
recordButton.blur();
}
return {
spread: function() {
recordButton.click(_prepareRecordTest);
}
};
})();

File diff suppressed because one or more lines are too long

158
node_modules/mousetrap/tests/libs/key-event.js generated vendored Normal file
View File

@@ -0,0 +1,158 @@
(function(window, document) {
var KeyEvent = function(data, type) {
this.keyCode = 'keyCode' in data ? data.keyCode : 0;
this.charCode = 'charCode' in data ? data.charCode : 0;
var modifiers = 'modifiers' in data ? data.modifiers : [];
this.ctrlKey = false;
this.metaKey = false;
this.altKey = false;
this.shiftKey = false;
for (var i = 0; i < modifiers.length; i++) {
this[modifiers[i] + 'Key'] = true;
}
this.type = type || 'keypress';
};
KeyEvent.prototype.toNative = function() {
var event = document.createEventObject ? document.createEventObject() : document.createEvent('Events');
if (event.initEvent) {
event.initEvent(this.type, true, true);
}
event.keyCode = this.keyCode;
event.which = this.charCode || this.keyCode;
event.shiftKey = this.shiftKey;
event.metaKey = this.metaKey;
event.altKey = this.altKey;
event.ctrlKey = this.ctrlKey;
return event;
};
KeyEvent.prototype.fire = function(element) {
var event = this.toNative();
if (element.dispatchEvent) {
element.dispatchEvent(event);
return;
}
element.fireEvent('on' + this.type, event);
};
// simulates complete key event as if the user pressed the key in the browser
// triggers a keydown, then a keypress, then a keyup
KeyEvent.simulate = function(charCode, keyCode, modifiers, element, repeat, options) {
if (modifiers === undefined) {
modifiers = [];
}
if (element === undefined) {
element = document;
}
if (repeat === undefined) {
repeat = 1;
}
if (options === undefined) {
options = {};
}
// Re-target the element so that `event.target` becomes the shadow host. See:
// https://developers.google.com/web/fundamentals/web-components/shadowdom#events
// This is a bit of a lie because true events would re-target the event target both for
// closed and open shadow trees. `KeyEvent` is not a true event and will fire the event
// directly from the shadow host for closed shadow trees. For open trees, this would make
// the tests fail as the actual event that will be eventually dispatched would have an
// incorrect `Event.composedPath()` starting with the shadow host instead of the
// initial event target.
if (options.shadowHost && options.shadowHost.shadowRoot === null) {
// closed shadow dom
element = options.shadowHost;
}
var modifierToKeyCode = {
'shift': 16,
'ctrl': 17,
'alt': 18,
'meta': 91
};
// if the key is a modifier then take it out of the regular
// keypress/keydown
if (keyCode == 16 || keyCode == 17 || keyCode == 18 || keyCode == 91) {
repeat = 0;
}
var modifiersToInclude = [];
var keyEvents = [];
// modifiers would go down first
for (var i = 0; i < modifiers.length; i++) {
modifiersToInclude.push(modifiers[i]);
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: modifierToKeyCode[modifiers[i]],
modifiers: modifiersToInclude
}, 'keydown'));
}
// @todo factor in duration for these
while (repeat > 0) {
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: keyCode,
modifiers: modifiersToInclude
}, 'keydown'));
keyEvents.push(new KeyEvent({
charCode: charCode,
keyCode: charCode,
modifiers: modifiersToInclude
}, 'keypress'));
repeat--;
}
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: keyCode,
modifiers: modifiersToInclude
}, 'keyup'));
// now lift up the modifier keys
for (i = 0; i < modifiersToInclude.length; i++) {
var modifierKeyCode = modifierToKeyCode[modifiersToInclude[i]];
modifiersToInclude.splice(i, 1);
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: modifierKeyCode,
modifiers: modifiersToInclude
}, 'keyup'));
}
for (i = 0; i < keyEvents.length; i++) {
// console.log('firing', keyEvents[i].type, keyEvents[i].keyCode, keyEvents[i].charCode);
keyEvents[i].fire(element);
}
};
window.KeyEvent = KeyEvent;
// expose as a common js module
if (typeof module !== 'undefined' && module.exports) {
module.exports = KeyEvent;
}
// expose KeyEvent as an AMD module
if (typeof define === 'function' && define.amd) {
define(function() {
return KeyEvent;
});
}
}) (typeof window !== 'undefined' ? window : null, typeof window !== 'undefined' ? document : null);

24
node_modules/mousetrap/tests/mousetrap.html generated vendored Normal file
View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Mousetrap Tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/sinon/pkg/sinon.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../mousetrap.js"></script>
<script src="libs/key-event.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test.mousetrap.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['Mousetrap']);
mocha.run();
</script>
</body>

772
node_modules/mousetrap/tests/test.mousetrap.js generated vendored Normal file
View File

@@ -0,0 +1,772 @@
/**
* The following strategy of importing modules allows the tests to be run in a browser environment.
* Test libraries like `mocha`, `sinon`, etc. are expected to be loaded before this file.
*/
var sinon = sinon || require('sinon');
var chai = chai || require('chai');
var expect = chai.expect;
if (typeof window === 'undefined') {
require('mocha');
require('jsdom-global')();
}
// Load libraries that require access to the DOM after `jsdom-global`
var Mousetrap = Mousetrap || require('./../mousetrap');
var KeyEvent = KeyEvent || require('./libs/key-event');
// Reset Mousetrap after each test
afterEach(function () {
Mousetrap.reset();
});
describe('Mousetrap.bind', function () {
describe('basic', function () {
it('z key fires when pressing z', function () {
var spy = sinon.spy();
Mousetrap.bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90);
// really slow for some reason
// expect(spy).to.have.been.calledOnce;
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key fires from keydown', function () {
var spy = sinon.spy();
Mousetrap.bind('z', spy, 'keydown');
KeyEvent.simulate('Z'.charCodeAt(0), 90);
// really slow for some reason
// expect(spy).to.have.been.calledOnce;
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key does not fire when pressing b', function () {
var spy = sinon.spy();
Mousetrap.bind('z', spy);
KeyEvent.simulate('B'.charCodeAt(0), 66);
expect(spy.callCount).to.equal(0);
});
it('z key does not fire when holding a modifier key', function () {
var spy = sinon.spy();
var modifiers = ['ctrl', 'alt', 'meta', 'shift'];
var charCode;
var modifier;
Mousetrap.bind('z', spy);
for (var i = 0; i < 4; i++) {
modifier = modifiers[i];
charCode = 'Z'.charCodeAt(0);
// character code is different when alt is pressed
if (modifier == 'alt') {
charCode = 'Ω'.charCodeAt(0);
}
spy.resetHistory();
KeyEvent.simulate(charCode, 90, [modifier]);
expect(spy.callCount).to.equal(0);
}
});
it('z key does not fire when inside an input element in an open shadow dom', function() {
var spy = sinon.spy();
var shadowHost = document.createElement('div');
var shadowRoot = shadowHost.attachShadow({ mode: 'open' });
document.body.appendChild(shadowHost);
var inputElement = document.createElement('input');
shadowRoot.appendChild(inputElement);
expect(shadowHost.shadowRoot).to.equal(shadowRoot, 'shadow root accessible');
Mousetrap.bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], inputElement, 1, { shadowHost: shadowHost });
document.body.removeChild(shadowHost);
expect(spy.callCount).to.equal(0, 'callback should not have fired');
});
it('z key does fire when inside an input element in a closed shadow dom', function() {
var spy = sinon.spy();
var shadowHost = document.createElement('div');
var shadowRoot = shadowHost.attachShadow({ mode: 'closed' });
document.body.appendChild(shadowHost);
var inputElement = document.createElement('input');
shadowRoot.appendChild(inputElement);
expect(shadowHost.shadowRoot).to.equal(null, 'shadow root unaccessible');
Mousetrap.bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], inputElement, 1, { shadowHost: shadowHost });
document.body.removeChild(shadowHost);
expect(spy.callCount).to.equal(1, 'callback should have fired once');
});
it('keyup events should fire', function() {
var spy = sinon.spy();
Mousetrap.bind('z', spy, 'keyup');
KeyEvent.simulate('Z'.charCodeAt(0), 90);
expect(spy.callCount).to.equal(1, 'keyup event for "z" should fire');
// for key held down we should only get one key up
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], document, 10);
expect(spy.callCount).to.equal(2, 'keyup event for "z" should fire once for held down key');
});
it('keyup event for 0 should fire', function () {
var spy = sinon.spy();
Mousetrap.bind('0', spy, 'keyup');
KeyEvent.simulate(0, 48);
expect(spy.callCount).to.equal(1, 'keyup event for "0" should fire');
});
it('rebinding a key overwrites the callback for that key', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('x', spy1);
Mousetrap.bind('x', spy2);
KeyEvent.simulate('X'.charCodeAt(0), 88);
expect(spy1.callCount).to.equal(0, 'original callback should not fire');
expect(spy2.callCount).to.equal(1, 'new callback should fire');
});
it('binding an array of keys', function () {
var spy = sinon.spy();
Mousetrap.bind(['a', 'b', 'c'], spy);
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'new callback was called');
expect(spy.args[0][1]).to.equal('a', 'callback should match "a"');
KeyEvent.simulate('B'.charCodeAt(0), 66);
expect(spy.callCount).to.equal(2, 'new callback was called twice');
expect(spy.args[1][1]).to.equal('b', 'callback should match "b"');
KeyEvent.simulate('C'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(3, 'new callback was called three times');
expect(spy.args[2][1]).to.equal('c', 'callback should match "c"');
});
it('return false should prevent default and stop propagation', function () {
var spy = sinon.spy(function () {
return false;
});
Mousetrap.bind('command+s', spy);
KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][0].defaultPrevented).to.be.true;
// cancelBubble is not correctly set to true in webkit/blink
//
// @see https://code.google.com/p/chromium/issues/detail?id=162270
// expect(spy.args[0][0].cancelBubble).to.be.true;
// try without return false
spy = sinon.spy();
Mousetrap.bind('command+s', spy);
KeyEvent.simulate('S'.charCodeAt(0), 83, ['meta']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][0].cancelBubble).to.be.false;
expect(spy.args[0][0].defaultPrevented).to.be.false;
});
it('capslock key is ignored', function () {
var spy = sinon.spy();
Mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback should fire for lowercase a');
spy.resetHistory();
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback should fire for capslock A');
spy.resetHistory();
KeyEvent.simulate('A'.charCodeAt(0), 65, ['shift']);
expect(spy.callCount).to.equal(0, 'callback should not fire fort shift+a');
});
});
describe('special characters', function () {
it('binding special characters', function () {
var spy = sinon.spy();
Mousetrap.bind('*', spy);
KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('*', 'callback should match *');
});
it('binding special characters keyup', function () {
var spy = sinon.spy();
Mousetrap.bind('*', spy, 'keyup');
KeyEvent.simulate('*'.charCodeAt(0), 56, ['shift']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('*', 'callback should match "*"');
});
it('binding keys with no associated charCode', function () {
var spy = sinon.spy();
Mousetrap.bind('left', spy);
KeyEvent.simulate(0, 37);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('left', 'callback should match "left"');
});
it('binding plus key alone should work', function () {
var spy = sinon.spy();
Mousetrap.bind('+', spy);
// fires for regular + character
KeyEvent.simulate('+'.charCodeAt(0), 43);
// and for shift+=
KeyEvent.simulate(43, 187, ['shift']);
expect(spy.callCount).to.equal(2, 'callback should fire');
expect(spy.args[0][1]).to.equal('+', 'callback should match "+"');
});
it('binding plus key as "plus" should work', function () {
var spy = sinon.spy();
Mousetrap.bind('plus', spy);
// fires for regular + character
KeyEvent.simulate('+'.charCodeAt(0), 43);
// and for shift+=
KeyEvent.simulate(43, 187, ['shift']);
expect(spy.callCount).to.equal(2, 'callback should fire');
expect(spy.args[0][1]).to.equal('plus', 'callback should match "plus"');
});
it('binding to alt++ should work', function () {
var spy = sinon.spy();
Mousetrap.bind('alt++', spy);
KeyEvent.simulate('+'.charCodeAt(0), 43, ['alt']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('alt++', 'callback should match "alt++"');
});
it('binding to alt+shift++ should work as well', function () {
var spy = sinon.spy();
Mousetrap.bind('alt+shift++', spy);
KeyEvent.simulate('+'.charCodeAt(0), 43, ['shift', 'alt']);
expect(spy.callCount).to.equal(1, 'callback should fire');
expect(spy.args[0][1]).to.equal('alt+shift++', 'callback should match "alt++"');
});
});
describe('combos with modifiers', function () {
it('binding key combinations', function () {
var spy = sinon.spy();
Mousetrap.bind('command+o', spy);
KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
expect(spy.callCount).to.equal(1, 'command+o callback should fire');
expect(spy.args[0][1]).to.equal('command+o', 'keyboard string returned is correct');
});
it('binding key combos with multiple modifiers', function () {
var spy = sinon.spy();
Mousetrap.bind('command+shift+o', spy);
KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta']);
expect(spy.callCount).to.equal(0, 'command+o callback should not fire');
KeyEvent.simulate('O'.charCodeAt(0), 79, ['meta', 'shift']);
expect(spy.callCount).to.equal(1, 'command+o callback should fire');
});
it('should fire callback when ctrl+numpad 0 is pressed', function () {
var spy = sinon.spy();
Mousetrap.bind('ctrl+0', spy);
// numpad 0 keycode
KeyEvent.simulate(96, 96, ['ctrl']);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('ctrl+0', 'second argument should be key combo');
});
});
describe('sequences', function () {
it('binding sequences', function () {
var spy = sinon.spy();
Mousetrap.bind('g i', spy);
KeyEvent.simulate('G'.charCodeAt(0), 71);
expect(spy.callCount).to.equal(0, 'callback should not fire');
KeyEvent.simulate('I'.charCodeAt(0), 73);
expect(spy.callCount).to.equal(1, 'callback should fire');
});
it('binding sequences with mixed types', function () {
var spy = sinon.spy();
Mousetrap.bind('g o enter', spy);
KeyEvent.simulate('G'.charCodeAt(0), 71);
expect(spy.callCount).to.equal(0, 'callback should not fire');
KeyEvent.simulate('O'.charCodeAt(0), 79);
expect(spy.callCount).to.equal(0, 'callback should not fire');
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
});
it('binding sequences starting with modifier keys', function () {
var spy = sinon.spy();
Mousetrap.bind('option enter', spy);
KeyEvent.simulate(0, 18, ['alt']);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
spy = sinon.spy();
Mousetrap.bind('command enter', spy);
KeyEvent.simulate(0, 91, ['meta']);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
spy = sinon.spy();
Mousetrap.bind('escape enter', spy);
KeyEvent.simulate(0, 27);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'callback should fire');
});
it('key within sequence should not fire', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('a', spy1);
Mousetrap.bind('c a t', spy2);
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy1.callCount).to.equal(1, 'callback 1 should fire');
spy1.resetHistory();
KeyEvent.simulate('C'.charCodeAt(0), 67);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate('T'.charCodeAt(0), 84);
expect(spy1.callCount).to.equal(0, 'callback for "a" key should not fire');
expect(spy2.callCount).to.equal(1, 'callback for "c a t" sequence should fire');
});
it('keyup at end of sequence should not fire', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('t', spy1, 'keyup');
Mousetrap.bind('b a t', spy2);
KeyEvent.simulate('B'.charCodeAt(0), 66);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate('T'.charCodeAt(0), 84);
expect(spy1.callCount).to.equal(0, 'callback for "t" keyup should not fire');
expect(spy2.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
});
it('keyup sequences should work', function () {
var spy = sinon.spy();
Mousetrap.bind('b a t', spy, 'keyup');
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('a'.charCodeAt(0), 65);
// hold the last key down for a while
KeyEvent.simulate('t'.charCodeAt(0), 84, [], document, 10);
expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire on keyup');
});
it('extra spaces in sequences should be ignored', function () {
var spy = sinon.spy();
Mousetrap.bind('b a t', spy);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(1, 'callback for "b a t" sequence should fire');
});
it('modifiers and sequences play nicely', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('ctrl a', spy1);
Mousetrap.bind('ctrl+b', spy2);
KeyEvent.simulate(0, 17, ['ctrl']);
KeyEvent.simulate('A'.charCodeAt(0), 65);
expect(spy1.callCount).to.equal(1, '"ctrl a" should fire');
KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
expect(spy2.callCount).to.equal(1, '"ctrl+b" should fire');
});
it('sequences that start the same work', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('g g l', spy2);
Mousetrap.bind('g g o', spy1);
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('o'.charCodeAt(0), 79);
expect(spy1.callCount).to.equal(1, '"g g o" should fire');
expect(spy2.callCount).to.equal(0, '"g g l" should not fire');
spy1.resetHistory();
spy2.resetHistory();
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('g'.charCodeAt(0), 71);
KeyEvent.simulate('l'.charCodeAt(0), 76);
expect(spy1.callCount).to.equal(0, '"g g o" should not fire');
expect(spy2.callCount).to.equal(1, '"g g l" should fire');
});
it('sequences should not fire subsequences', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('a b c', spy1);
Mousetrap.bind('b c', spy2);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate('B'.charCodeAt(0), 66);
KeyEvent.simulate('C'.charCodeAt(0), 67);
expect(spy1.callCount).to.equal(1, '"a b c" should fire');
expect(spy2.callCount).to.equal(0, '"b c" should not fire');
spy1.resetHistory();
spy2.resetHistory();
Mousetrap.bind('option b', spy1);
Mousetrap.bind('a option b', spy2);
KeyEvent.simulate('A'.charCodeAt(0), 65);
KeyEvent.simulate(0, 18, ['alt']);
KeyEvent.simulate('B'.charCodeAt(0), 66);
expect(spy1.callCount).to.equal(0, '"option b" should not fire');
expect(spy2.callCount).to.equal(1, '"a option b" should fire');
});
it('rebinding same sequence should override previous', function () {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Mousetrap.bind('a b c', spy1);
Mousetrap.bind('a b c', spy2);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy1.callCount).to.equal(0, 'first callback should not fire');
expect(spy2.callCount).to.equal(1, 'second callback should fire');
});
it('broken sequences', function () {
var spy = sinon.spy();
Mousetrap.bind('h a t', spy);
KeyEvent.simulate('h'.charCodeAt(0), 72);
KeyEvent.simulate('e'.charCodeAt(0), 69);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('r'.charCodeAt(0), 82);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(0, 'sequence for "h a t" should not fire for "h e a r t"');
});
it('sequences containing combos should work', function () {
var spy = sinon.spy();
Mousetrap.bind('a ctrl+b', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('B'.charCodeAt(0), 66, ['ctrl']);
expect(spy.callCount).to.equal(1, '"a ctrl+b" should fire');
Mousetrap.unbind('a ctrl+b');
spy = sinon.spy();
Mousetrap.bind('ctrl+b a', spy);
KeyEvent.simulate('b'.charCodeAt(0), 66, ['ctrl']);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, '"ctrl+b a" should fire');
});
it('sequences starting with spacebar should work', function () {
var spy = sinon.spy();
Mousetrap.bind('a space b c', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate(32, 32);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(1, '"a space b c" should fire');
});
it('konami code', function () {
var spy = sinon.spy();
Mousetrap.bind('up up down down left right left right b a enter', spy);
KeyEvent.simulate(0, 38);
KeyEvent.simulate(0, 38);
KeyEvent.simulate(0, 40);
KeyEvent.simulate(0, 40);
KeyEvent.simulate(0, 37);
KeyEvent.simulate(0, 39);
KeyEvent.simulate(0, 37);
KeyEvent.simulate(0, 39);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate(0, 13);
expect(spy.callCount).to.equal(1, 'konami code should fire');
});
it('sequence timer resets', function () {
var spy = sinon.spy();
var clock = sinon.useFakeTimers();
Mousetrap.bind('h a t', spy);
KeyEvent.simulate('h'.charCodeAt(0), 72);
clock.tick(600);
KeyEvent.simulate('a'.charCodeAt(0), 65);
clock.tick(900);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(1, 'sequence should fire after waiting');
clock.restore();
});
it('sequences timeout', function () {
var spy = sinon.spy();
var clock = sinon.useFakeTimers();
Mousetrap.bind('g t', spy);
KeyEvent.simulate('g'.charCodeAt(0), 71);
clock.tick(1000);
KeyEvent.simulate('t'.charCodeAt(0), 84);
expect(spy.callCount).to.equal(0, 'sequence callback should not fire');
clock.restore();
});
});
describe('default actions', function () {
var keys = {
keypress: [
['a', 65],
['A', 65, ['shift']],
['7', 55],
['?', 191],
['*', 56],
['+', 187],
['$', 52],
['[', 219],
['.', 190]
],
keydown: [
['shift+\'', 222, ['shift']],
['shift+a', 65, ['shift']],
['shift+5', 53, ['shift']],
['command+shift+p', 80, ['meta', 'shift']],
['space', 32],
['left', 37]
]
};
function getCallback(key, keyCode, type, modifiers) {
return function () {
var spy = sinon.spy();
Mousetrap.bind(key, spy);
KeyEvent.simulate(key.charCodeAt(0), keyCode, modifiers);
expect(spy.callCount).to.equal(1);
expect(spy.args[0][0].type).to.equal(type);
};
}
for (var type in keys) {
for (var i = 0; i < keys[type].length; i++) {
var key = keys[type][i][0];
var keyCode = keys[type][i][1];
var modifiers = keys[type][i][2] || [];
it('"' + key + '" uses "' + type + '"', getCallback(key, keyCode, type, modifiers));
}
}
});
});
describe('Mousetrap.unbind', function () {
it('unbind works', function () {
var spy = sinon.spy();
Mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback for a should fire');
Mousetrap.unbind('a');
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback for a should not fire after unbind');
});
it('unbind accepts an array', function () {
var spy = sinon.spy();
Mousetrap.bind(['a', 'b', 'c'], spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(3, 'callback should have fired 3 times');
Mousetrap.unbind(['a', 'b', 'c']);
KeyEvent.simulate('a'.charCodeAt(0), 65);
KeyEvent.simulate('b'.charCodeAt(0), 66);
KeyEvent.simulate('c'.charCodeAt(0), 67);
expect(spy.callCount).to.equal(3, 'callback should not fire after unbind');
});
});
describe('wrapping a specific element', function () {
// Prepare the DOM for these tests.
document.body.insertAdjacentHTML('afterbegin', `
<form style="display: none;">
<textarea></textarea>
</form>
`);
var form = document.querySelector('form');
var textarea = form.querySelector('textarea');
it('z key fires when pressing z in the target element', function () {
var spy = sinon.spy();
Mousetrap(form).bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], form);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key fires when pressing z in a child of the target element', function () {
var spy = sinon.spy();
Mousetrap(form).bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90, [], textarea);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('z', 'second argument should be key combo');
});
it('z key does not fire when pressing z outside the target element', function () {
var spy = sinon.spy();
Mousetrap(textarea).bind('z', spy);
KeyEvent.simulate('Z'.charCodeAt(0), 90);
expect(spy.callCount).to.equal(0, 'callback should not have fired');
});
it('should work when constructing a new mousetrap object', function () {
var spy = sinon.spy();
var mousetrap = new Mousetrap(form);
mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65, [], textarea);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
});
it('should allow you to create an empty mousetrap constructor', function () {
var spy = sinon.spy();
var mousetrap = new Mousetrap();
mousetrap.bind('a', spy);
KeyEvent.simulate('a'.charCodeAt(0), 65);
expect(spy.callCount).to.equal(1, 'callback should fire once');
expect(spy.args[0][0]).to.be.an.instanceOf(Event, 'first argument should be Event');
expect(spy.args[0][1]).to.equal('a', 'second argument should be key combo');
});
});
describe('Mouestrap.addKeycodes', function () {
it('should properly recognize non-default mapping', function () {
const spy = sinon.spy();
Mousetrap.addKeycodes({
144: 'num',
});
Mousetrap.bind('num', spy);
KeyEvent.simulate(144, 144);
expect(spy.callCount).to.equal(1, 'callback should fire for num');
spy.resetHistory();
});
});

11
package-lock.json generated
View File

@@ -16,6 +16,7 @@
"datatables.net-select": "^1.2.0",
"jquery": "^3.5.1",
"jquery-ui-dist": "^1.12.1",
"mousetrap": "^1.6.5",
"scrollmagic": "^2.0.8",
"selectize-plugin-a11y": "^1.1.0",
"tippy.js": "^6.2.5",
@@ -99,6 +100,11 @@
"resolved": "https://registry.npmjs.org/jquery-ui-dist/-/jquery-ui-dist-1.12.1.tgz",
"integrity": "sha512-UnQQWIvxaP7s13FLx37WufEwDYLSXKpp9JHrqEYTP6GqQNM+RMVEwsJMoc163QlzEaAV5YVfWlgQU5FPjKBtSw=="
},
"node_modules/mousetrap": {
"version": "1.6.5",
"resolved": "https://registry.npmjs.org/mousetrap/-/mousetrap-1.6.5.tgz",
"integrity": "sha512-QNo4kEepaIBwiT8CDhP98umTetp+JNfQYBWvC1pc6/OAibuXtRcxZ58Qz8skvEHYvURne/7R8T5VoOI7rDsEUA=="
},
"node_modules/scrollmagic": {
"version": "2.0.8",
"license": "(MIT OR GPL-3.0+)",
@@ -197,6 +203,11 @@
"resolved": "https://registry.npmjs.org/jquery-ui-dist/-/jquery-ui-dist-1.12.1.tgz",
"integrity": "sha512-UnQQWIvxaP7s13FLx37WufEwDYLSXKpp9JHrqEYTP6GqQNM+RMVEwsJMoc163QlzEaAV5YVfWlgQU5FPjKBtSw=="
},
"mousetrap": {
"version": "1.6.5",
"resolved": "https://registry.npmjs.org/mousetrap/-/mousetrap-1.6.5.tgz",
"integrity": "sha512-QNo4kEepaIBwiT8CDhP98umTetp+JNfQYBWvC1pc6/OAibuXtRcxZ58Qz8skvEHYvURne/7R8T5VoOI7rDsEUA=="
},
"scrollmagic": {
"version": "2.0.8"
},

View File

@@ -11,6 +11,7 @@
"datatables.net-select": "^1.2.0",
"jquery": "^3.5.1",
"jquery-ui-dist": "^1.12.1",
"mousetrap": "^1.6.5",
"scrollmagic": "^2.0.8",
"selectize-plugin-a11y": "^1.1.0",
"tippy.js": "^6.2.5",

View File

@@ -203,8 +203,8 @@ class iTopWebPage extends NiceWebPage implements iTabbedPage
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'node_modules/toastify-js/src/toastify.js');
// Keyboard shortcuts
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/mousetrap/mousetrap.min.js');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/mousetrap/mousetrap-record.min.js');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'node_modules/mousetrap/mousetrap.min.js');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'node_modules/mousetrap/plugins/record/mousetrap-record.min.js');
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/pages/backoffice/keyboard-shortcuts.js');
// Used throughout the app.