autonumeric
Version:
autoNumeric is a standalone Javascript library that provides live *as-you-type* formatting for international numbers and currencies. It supports most international numeric formats and currencies including those used in Europe, Asia, and North and South Am
417 lines (398 loc) • 11.8 kB
JavaScript
/**
* Enumerations for AutoNumeric.js
* @author Alexandre Bonneau <alexandre.bonneau@linuxfr.eu>
* @copyright © 2023 Alexandre Bonneau
*
* The MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* 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, sub license, 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.
*/
/**
* Object that store the helper enumerations
* @type {{ allowedTagList: [string], keyCode: {}, fromCharCodeKeyCode: [string], keyName: {} }}
*/
const AutoNumericEnum = {};
/**
* List of allowed tag on which autoNumeric can be used.
*/
AutoNumericEnum.allowedTagList = [
'b',
'caption',
'cite',
'code',
'const',
'dd',
'del',
'div',
'dfn',
'dt',
'em',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'input',
'ins',
'kdb',
'label',
'li',
'option',
'output',
'p',
'q',
's',
'sample',
'span',
'strong',
'td',
'th',
'u',
];
Object.freeze(AutoNumericEnum.allowedTagList);
Object.defineProperty(AutoNumericEnum, 'allowedTagList', { configurable: false, writable: false });
/**
* This object is the reverse of `keyCode`, and is used to translate the key code to named keys when no valid characters can be obtained by `String.fromCharCode`.
* This object keys correspond to the `event.keyCode` number, and returns the corresponding key name (à la event.key)
*/
AutoNumericEnum.fromCharCodeKeyCode = {
0 : 'LaunchCalculator',
8 : 'Backspace',
9 : 'Tab',
13 : 'Enter',
16 : 'Shift',
17 : 'Ctrl',
18 : 'Alt',
19 : 'Pause',
20 : 'CapsLock',
27 : 'Escape',
32 : ' ',
33 : 'PageUp',
34 : 'PageDown',
35 : 'End',
36 : 'Home',
37 : 'ArrowLeft',
38 : 'ArrowUp',
39 : 'ArrowRight',
40 : 'ArrowDown',
45 : 'Insert',
46 : 'Delete',
48 : '0',
49 : '1',
50 : '2',
51 : '3',
52 : '4',
53 : '5',
54 : '6',
55 : '7',
56 : '8',
57 : '9',
// 65: 'a',
// 66: 'b',
// 67: 'c',
// 68: 'd',
// 69: 'e',
// 70: 'f',
// 71: 'g',
// 72: 'h',
// 73: 'i',
// 74: 'j',
// 75: 'k',
// 76: 'l',
// 77: 'm',
// 78: 'n',
// 79: 'o',
// 80: 'p',
// 81: 'q',
// 82: 'r',
// 83: 's',
// 84: 't',
// 85: 'u',
// 86: 'v',
// 87: 'w',
// 88: 'x',
// 89: 'y',
// 90: 'z',
91 : 'OS', // Note: Firefox and Chrome reports 'OS' instead of 'OSLeft'
92 : 'OSRight',
93 : 'ContextMenu',
96 : '0',
97 : '1',
98 : '2',
99 : '3',
100: '4',
101: '5',
102: '6',
103: '7',
104: '8',
105: '9',
106: '*',
107: '+',
109: '-', // The 'NumpadSubtract' code
110: '.',
111: '/',
112: 'F1',
113: 'F2',
114: 'F3',
115: 'F4',
116: 'F5',
117: 'F6',
118: 'F7',
119: 'F8',
120: 'F9',
121: 'F10',
122: 'F11',
123: 'F12',
144: 'NumLock',
145: 'ScrollLock',
173: '-', // The 'Minus' sign on Firefox. This is only needed when using the Selenium bot though.
182: 'MyComputer',
183: 'MyCalculator',
186: ';',
187: '=',
188: ',',
189: '-', // The 'Minus' sign on all other browsers
190: '.',
191: '/',
192: '`',
219: '[',
220: '\\',
221: ']',
222: '\'',
224: 'Meta',
225: 'AltGraph',
};
Object.freeze(AutoNumericEnum.fromCharCodeKeyCode);
Object.defineProperty(AutoNumericEnum, 'fromCharCodeKeyCode', { configurable: false, writable: false });
/**
* Wrapper variable that hold named keyboard keys with their respective key name (as set in KeyboardEvent.key).
* Those names are listed here :
* @link https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
*/
AutoNumericEnum.keyName = {
// Special values
Unidentified : 'Unidentified',
AndroidDefault: 'AndroidDefault',
// Modifier keys
Alt : 'Alt',
AltGr : 'AltGraph',
CapsLock : 'CapsLock', // Under Chrome, e.key is empty for CapsLock
Ctrl : 'Control',
Fn : 'Fn',
FnLock : 'FnLock',
Hyper : 'Hyper', // 'OS' under Firefox
Meta : 'Meta',
OSLeft : 'OS',
OSRight : 'OS',
Command : 'OS',
NumLock : 'NumLock',
ScrollLock: 'ScrollLock',
Shift : 'Shift',
Super : 'Super', // 'OS' under Firefox
Symbol : 'Symbol',
SymbolLock: 'SymbolLock',
// Whitespace keys
Enter: 'Enter',
Tab : 'Tab',
Space: ' ', // 'Spacebar' for Firefox <37, and IE9
// Navigation keys
LeftArrow : 'ArrowLeft', // 'Left' for Firefox <=36, and IE9
UpArrow : 'ArrowUp', // 'Up' for Firefox <=36, and IE9
RightArrow: 'ArrowRight', // 'Right' for Firefox <=36, and IE9
DownArrow : 'ArrowDown', // 'Down' for Firefox <=36, and IE9
End : 'End',
Home : 'Home',
PageUp : 'PageUp',
PageDown : 'PageDown',
// Editing keys
Backspace: 'Backspace',
Clear : 'Clear',
Copy : 'Copy',
CrSel : 'CrSel', // 'Crsel' for Firefox <=36, and IE9
Cut : 'Cut',
Delete : 'Delete', // 'Del' for Firefox <=36, and IE9
EraseEof : 'EraseEof',
ExSel : 'ExSel', // 'Exsel' for Firefox <=36, and IE9
Insert : 'Insert',
Paste : 'Paste',
Redo : 'Redo',
Undo : 'Undo',
// UI keys
Accept : 'Accept',
Again : 'Again',
Attn : 'Attn', // 'Unidentified' for Firefox, Chrome, and IE9 ('KanaMode' when using the Japanese keyboard layout)
Cancel : 'Cancel',
ContextMenu: 'ContextMenu', // 'Apps' for Firefox <=36, and IE9
Esc : 'Escape', // 'Esc' for Firefox <=36, and IE9
Execute : 'Execute',
Find : 'Find',
Finish : 'Finish', // 'Unidentified' for Firefox, Chrome, and IE9 ('Katakana' when using the Japanese keyboard layout)
Help : 'Help',
Pause : 'Pause',
Play : 'Play',
Props : 'Props',
Select : 'Select',
ZoomIn : 'ZoomIn',
ZoomOut : 'ZoomOut',
// Device keys
BrightnessDown: 'BrightnessDown',
BrightnessUp : 'BrightnessUp',
Eject : 'Eject',
LogOff : 'LogOff',
Power : 'Power',
PowerOff : 'PowerOff',
PrintScreen : 'PrintScreen',
Hibernate : 'Hibernate', // 'Unidentified' for Firefox <=37
Standby : 'Standby', // 'Unidentified' for Firefox <=36, and IE9
WakeUp : 'WakeUp',
// IME and composition keys
Compose: 'Compose',
Dead : 'Dead',
// Function keys
F1 : 'F1',
F2 : 'F2',
F3 : 'F3',
F4 : 'F4',
F5 : 'F5',
F6 : 'F6',
F7 : 'F7',
F8 : 'F8',
F9 : 'F9',
F10: 'F10',
F11: 'F11',
F12: 'F12',
// Document keys
Print: 'Print',
// 'Normal' keys
num0 : '0',
num1 : '1',
num2 : '2',
num3 : '3',
num4 : '4',
num5 : '5',
num6 : '6',
num7 : '7',
num8 : '8',
num9 : '9',
a : 'a',
b : 'b',
c : 'c',
d : 'd',
e : 'e',
f : 'f',
g : 'g',
h : 'h',
i : 'i',
j : 'j',
k : 'k',
l : 'l',
m : 'm',
n : 'n',
o : 'o',
p : 'p',
q : 'q',
r : 'r',
s : 's',
t : 't',
u : 'u',
v : 'v',
w : 'w',
x : 'x',
y : 'y',
z : 'z',
A : 'A',
B : 'B',
C : 'C',
D : 'D',
E : 'E',
F : 'F',
G : 'G',
H : 'H',
I : 'I',
J : 'J',
K : 'K',
L : 'L',
M : 'M',
N : 'N',
O : 'O',
P : 'P',
Q : 'Q',
R : 'R',
S : 'S',
T : 'T',
U : 'U',
V : 'V',
W : 'W',
X : 'X',
Y : 'Y',
Z : 'Z',
Semicolon : ';',
Equal : '=',
Comma : ',',
Hyphen : '-',
Minus : '-',
Plus : '+',
Dot : '.',
Slash : '/',
Backquote : '`',
LeftParenthesis : '(',
RightParenthesis: ')',
LeftBracket : '[',
RightBracket : ']',
Backslash : '\\',
Quote : '\'',
// Numeric keypad keys
numpad0 : '0',
numpad1 : '1',
numpad2 : '2',
numpad3 : '3',
numpad4 : '4',
numpad5 : '5',
numpad6 : '6',
numpad7 : '7',
numpad8 : '8',
numpad9 : '9',
NumpadDot : '.',
NumpadDotAlt : ',', // Modern browsers automatically adapt the character sent by this key to the decimal character of the current language
NumpadMultiply : '*',
NumpadPlus : '+',
NumpadMinus : '-',
NumpadSubtract : '-',
NumpadSlash : '/',
NumpadDotObsoleteBrowsers : 'Decimal',
NumpadMultiplyObsoleteBrowsers: 'Multiply',
NumpadPlusObsoleteBrowsers : 'Add',
NumpadMinusObsoleteBrowsers : 'Subtract',
NumpadSlashObsoleteBrowsers : 'Divide',
// Special arrays for quicker tests
_allFnKeys : ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12'],
_someNonPrintableKeys: ['Tab', 'Enter', 'Shift', 'ShiftLeft', 'ShiftRight', 'Control', 'ControlLeft', 'ControlRight', 'Alt', 'AltLeft', 'AltRight', 'Pause', 'CapsLock', 'Escape'],
_directionKeys : ['PageUp', 'PageDown', 'End', 'Home', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp'],
};
Object.freeze(AutoNumericEnum.keyName._allFnKeys);
Object.freeze(AutoNumericEnum.keyName._someNonPrintableKeys);
Object.freeze(AutoNumericEnum.keyName._directionKeys);
Object.freeze(AutoNumericEnum.keyName);
Object.defineProperty(AutoNumericEnum, 'keyName', { configurable: false, writable: false });
Object.freeze(AutoNumericEnum);
export default AutoNumericEnum;