This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
plusastab.joelpurra.js
137 lines (106 loc) · 3.54 KB
/
plusastab.joelpurra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*!
* @license PlusAsTab
* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Swedish Post and Telecom Authority (PTS)
* Developed for PTS by Joel Purra <https://joelpurra.com/>
* Released under the BSD license.
*
* A jQuery plugin to use the numpad plus key as a tab key equivalent.
*/
/* global
jQuery,
*/
// Set up namespace, if needed
var JoelPurra = JoelPurra || {};
(function($, namespace)
{
// Private functions
function performEmulatedTabbing(isTab, isReverse, $target) {
isTab = (isTab === true);
isReverse = (isReverse === true);
if (isTab
&& $target !== undefined
&& $target.length !== 0) {
$target.emulateTab(isReverse ? -1 : +1);
return true;
}
return false;
}
function isChosenTabkey(key) {
if (key === options.key
|| ($.isArray(options.key)
&& $.inArray(key, options.key) !== -1)) {
return true;
}
return false;
}
function isEmulatedTabkey(event) {
// Checked later for reverse tab
//&& !event.shiftKey
if (!event.altKey
&& !event.ctrlKey
&& !event.metaKey
&& isChosenTabkey(event.which)) {
return true;
}
return false;
}
function checkEmulatedTabKeyDown(event) {
if (!isEmulatedTabkey(event)) {
return;
}
var $target = $(event.target),
wasDone = null;
if ($target.is(disablePlusAsTab)
|| $target.parents(disablePlusAsTab).length > 0
|| (!$target.is(enablePlusAsTab)
&& $target.parents(enablePlusAsTab).length === 0)) {
return;
}
wasDone = performEmulatedTabbing(true, event.shiftKey, $target);
if (wasDone) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
return false;
}
return;
}
function initializeAtLoad() {
$(document).on("keydown" + eventNamespace, checkEmulatedTabKeyDown);
}
namespace.PlusAsTab = {};
var eventNamespace = ".PlusAsTab",
// Keys from
// https://api.jquery.com/event.which/
// https://developer.mozilla.org/en/DOM/KeyboardEvent#Virtual_key_codes
KEY_NUM_PLUS = 107,
// Add options defaults here
internalDefaults = {
key: KEY_NUM_PLUS,
},
options = $.extend(true, {}, internalDefaults),
enablePlusAsTab = ".plus-as-tab, [data-plus-as-tab=true]",
disablePlusAsTab = ".disable-plus-as-tab, [data-plus-as-tab=false]";
// Public functions
namespace.PlusAsTab.setOptions = function(userOptions) {
// Merge the options onto the current options (usually the default values)
$.extend(true, options, userOptions);
};
namespace.PlusAsTab.plusAsTab = function($elements, enable) {
enable = (enable === undefined ? true : enable === true);
return $elements.each(function() {
var $this = $(this);
$this
.not(disablePlusAsTab)
.not(enablePlusAsTab)
.attr("data-plus-as-tab", enable ? "true" : "false");
});
};
$.fn.extend({
plusAsTab: function(enable) {
return namespace.PlusAsTab.plusAsTab(this, enable);
},
});
// PlusAsTab initializes listeners when jQuery is ready
$(initializeAtLoad);
}(jQuery, JoelPurra));