-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathkeydownAction.js
More file actions
54 lines (49 loc) 路 1.69 KB
/
Copy pathkeydownAction.js
File metadata and controls
54 lines (49 loc) 路 1.69 KB
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
/**
* DataFormsJS [keydownAction] Plugin
*
* This plugin uses the attribute [data-enter-key-click-selector] and allows
* the [enter] key of the element to trigger a [click()] event on the element
* specified in the selector.
*
* Common usage would be for Entry Forms where the {enter} key should perform a
* default action. For example usage see the Places Demo Search Screen.
*
* There is a corresponding Web Component for this plugin:
* js/web-components/keydown-action-service.js
*
* Example:
* <input type="text" data-enter-key-click-selector="button">
*/
/* Validates with both [jshint] and [eslint] */
/* global app */
/* jshint strict: true */
/* eslint-env browser */
/* eslint quotes: ["error", "single", { "avoidEscape": true }] */
/* eslint strict: ["error", "function"] */
/* eslint spaced-comment: ["error", "always"] */
/* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
(function () {
'use strict';
function enterToClick(e) {
if (e.keyCode !== 13) {
return;
}
var selector = e.target.getAttribute('data-enter-key-click-selector');
if (!selector) {
return;
}
var clickEl = document.querySelector(selector);
if (!clickEl) {
return;
}
e.preventDefault();
clickEl.click();
}
app.addPlugin('keydownAction', function (rootElement) {
rootElement = (rootElement === undefined ? document : rootElement);
var elements = rootElement.querySelectorAll('[data-enter-key-click-selector]');
Array.prototype.forEach.call(elements, function(el) {
el.addEventListener('keydown', enterToClick);
});
});
})();