forked from GillesVermeulen/virtualKeyboardDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualKeyboardDetector.js
254 lines (214 loc) · 7.22 KB
/
virtualKeyboardDetector.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* The Virtual Keyboard Detector
*/
var virtualKeyboardDetector = function (window, undefined) {
var recentlyFocusedTimeoutDuration = 3000;
var previousViewportWidth, viewportWidthWithoutVirtualKeyboard;
var previousViewportHeight, viewportHeightWithoutVirtualKeyboard;
var currentViewportWidth =
(previousViewportWidth =
viewportWidthWithoutVirtualKeyboard =
window.innerWidth);
var currentViewportHeight =
(previousViewportHeight =
viewportHeightWithoutVirtualKeyboard =
window.innerHeight);
var virtualKeyboardVisible = false;
var recentlyFocused = false;
var recentlyFocusedTimeout = null;
var validFocusableElements = ["INPUT", "TEXTAREA"];
var subscriptions = {};
/**
* Public functions
*/
function init(options) {
if (typeof options !== "undefined") {
if (typeof options.recentlyFocusedTimeoutDuration !== "undefined")
recentlyFocusedTimeoutDuration = options.recentlyFocusedTimeoutDuration;
}
resetViewportSizes();
initFocusListener();
initResizeListener();
}
function isVirtualKeyboardVisible() {
return virtualKeyboardVisible;
}
function getVirtualKeyboardSize() {
if (!virtualKeyboardVisible) return false;
return {
width: currentViewportWidth,
height: viewportHeightWithoutVirtualKeyboard - currentViewportHeight,
};
}
// Subscribe
function on(eventName, fn) {
if (typeof subscriptions[eventName] === "undefined")
subscriptions[eventName] = [];
subscriptions[eventName].push(fn);
}
// Unsubscribe
function off(eventName, fn) {
if (typeof subscriptions[eventName] === "undefined") return;
if (typeof fn === "undefined") {
subscriptions[eventName] = [];
} else {
var i = subscriptions[eventName].length;
while (i--) {
if (subscriptions[eventName][i] == fn)
subscriptions[eventName].splice(i, 1);
}
}
}
// Publish
function trigger(eventName, args) {
for (var i in subscriptions[eventName]) {
if (typeof subscriptions[eventName][i] === "function")
subscriptions[eventName][i](args);
}
}
/**
* Private functions
*/
// Reset all sizes. We presume the virtual keyboard is not visible at this stage.
// We call this function on initialisation, so make sure you initialise the virtualKeyBoardListener at a moment when the virtual keyboard is likely to be invisible.
function resetViewportSizes() {
currentViewportWidth =
previousViewportWidth =
viewportWidthWithoutVirtualKeyboard =
window.innerWidth;
currentViewportHeight =
previousViewportHeight =
viewportHeightWithoutVirtualKeyboard =
window.innerHeight;
}
// Initialise the listener that checks for focus events in the whole document. This way we can also handle dynamically added focusable elements.
function initFocusListener() {
document.addEventListener("focus", documentFocusHandler, true);
}
// Handle the document focus event. We check if the target was a valid focusable element.
function documentFocusHandler(e) {
if (
typeof e.target !== "undefined" &&
typeof e.target.nodeName !== "undefined"
) {
if (validFocusableElements.indexOf(e.target.nodeName) != -1)
elementFocusHandler(e);
}
}
// Handle the case when a valid focusable element is focused. We flag that a valid element was recently focused. This flag expires after recentlyFocusedTimeoutDuration.
function elementFocusHandler(e) {
if (recentlyFocusedTimeout != null) {
window.clearTimeout(recentlyFocusedTimeout);
recentlyFocusedTimeout = null;
}
recentlyFocused = true;
recentlyFocusedTimeout = window.setTimeout(
expireRecentlyFocused,
recentlyFocusedTimeoutDuration
);
}
function expireRecentlyFocused() {
recentlyFocused = false;
}
function initResizeListener() {
window.addEventListener("resize", resizeHandler);
}
function resizeHandler() {
currentViewportWidth = window.innerWidth;
currentViewportHeight = window.innerHeight;
// If the virtual keyboard is tought to be visible, but the viewport height returns to the value before keyboard was visible, we presume the keyboard was hidden.
if (
virtualKeyboardVisible &&
currentViewportWidth == previousViewportWidth &&
currentViewportHeight >= viewportHeightWithoutVirtualKeyboard
) {
virtualKeyboardHiddenHandler();
}
// If the width of the viewport is changed, it's hard to tell wether virtual keyboard is still visible, so we make sure it's not.
if (currentViewportWidth != previousViewportWidth) {
if ("activeElement" in document) document.activeElement.blur();
virtualKeyboardHiddenHandler();
}
// If recently focused and viewport height is smaller then previous height, we presume that the virtual keyboard has appeared.
if (
!virtualKeyboardVisible &&
recentlyFocused &&
currentViewportWidth == previousViewportWidth &&
currentViewportHeight < previousViewportHeight
) {
virtualKeyboardVisibleHandler();
}
// If the keyboard is presumed not visible, we save the current measurements as values before keyboard was shown.
if (virtualKeyboardVisible == false) {
viewportWidthWithoutVirtualKeyboard = currentViewportWidth;
viewportHeightWithoutVirtualKeyboard = currentViewportHeight;
}
previousViewportWidth = currentViewportWidth;
previousViewportHeight = currentViewportHeight;
}
function virtualKeyboardVisibleHandler() {
virtualKeyboardVisible = true;
var eventData = {
virtualKeyboardVisible: virtualKeyboardVisible,
sizes: getSizesData(),
};
trigger("virtualKeyboardVisible", eventData);
}
function virtualKeyboardHiddenHandler() {
virtualKeyboardVisible = false;
var eventData = {
virtualKeyboardVisible: virtualKeyboardVisible,
sizes: getSizesData(),
};
trigger("virtualKeyboardHidden", eventData);
}
function getSizesData() {
return {
viewportWithoutVirtualKeyboard: {
width: viewportWidthWithoutVirtualKeyboard,
height: viewportHeightWithoutVirtualKeyboard,
},
currentViewport: {
width: currentViewportWidth,
height: currentViewportHeight,
},
virtualKeyboard: {
width: currentViewportWidth,
height: viewportHeightWithoutVirtualKeyboard - currentViewportHeight,
},
};
}
// Make public functions available
return {
init: init,
isVirtualKeyboardVisible: isVirtualKeyboardVisible,
getVirtualKeyboardSize: getVirtualKeyboardSize,
on: on,
addEventListener: on,
subscribe: on,
off: off,
removeEventListener: off,
unsubscribe: off,
trigger: trigger,
publish: trigger,
dispatchEvent: trigger,
};
};
var noop = () => {};
// Export an object with noops when not on web.
export default typeof document !== "undefined"
? virtualKeyboardDetector(window)
: {
init: noop,
isVirtualKeyboardVisible: noop,
getVirtualKeyboardSize: noop,
on: noop,
addEventListener: noop,
subscribe: noop,
off: noop,
removeEventListener: noop,
unsubscribe: noop,
trigger: noop,
publish: noop,
dispatchEvent: noop,
};