-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathwalk-input.js
144 lines (131 loc) · 4.22 KB
/
walk-input.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
// Vimperator plugin: 'Walk Input'
// License: BSD
// Version: 1.3.0
// Maintainer: Takayama Fumihiko <[email protected]>
// anekos <[email protected]>
// ------------------------------------------------------------
// The focus walks <input> & <textarea> elements.
// If you type M-i first, the focus moves to "<input name='search' />".
// Then if you type M-i once more, the focus moves to "<input name='name' />".
//
// <html>
// <input name="search" />
// <a href="xxx">xxx</a>
// <a href="yyy">yyy</a>
// <a href="zzz">zzz</a>
// <input name="name" />
// <textarea name="comment"></textarea>
// </html>
// PLUGIN_INFO {{{
var INFO = xml`
<plugin name="Walk-Input" version="1.3.0"
href="http://github.com/vimpr/vimperator-plugins/blob/master/walk-input.js"
summary="The focus walks 'input' and 'textarea' element."
xmlns="http://vimperator.org/namespaces/liberator">
<author email="[email protected]">Takayama Fumihiko</author>
<author email="[email protected]">anekos</author>
<license>BSD</license>
<project name="Vimperator" minVersion="2.2"/>
<p>
The focus walks <input> & <textarea> element.
If you type <k name="M-i"/> first, the focus moves to "<input name='search' />".
Then if you type <k name="M-i"/> once more, the focus moves to "<input name='name' />".
</p>
<code><![CDATA[
<html>
<input name="search" />
<a href="xxx">xxx</a>
<a href="yyy">yyy</a>
<a href="zzz">zzz</a>
<input name="name" />
<textarea name="comment"></textarea>
</html>
]]></code>
<item>
<tags><![CDATA[i_<M-i> i_<A-i> <M-i> <A-i>]]></tags>
<spec><M-i></spec>
<spec><A-i></spec>
<description>
<p>Move focus forward</p>
</description>
</item>
<item>
<tags><![CDATA[i_<M-S-i> i_<A-S-i> <M-S-i> <A-S-i>]]></tags>
<spec><M-S-i></spec>
<spec><A-S-i></spec>
<description>
<p>Move focus backward</p>
</description>
</item>
</plugin>`;
// }}}
(function () {
var types = [
"text",
"password",
"search",
"datetime",
"datetime-local",
"date",
"month",
"time",
"week",
"number",
"range",
"email",
"url",
"tel",
"color",
].map(function(type) "@type=" + JSON.stringify(type)).join(" or ");
var xpath = '//input[(' + types + ' or not(@type)) and not(@disabled)] | //textarea | //*[@contenteditable="true"]';
function isVisible (elem) {
while (elem && !(elem instanceof HTMLDocument)) {
if (/^none$/i.test(getComputedStyle(elem, '').display))
return false;
elem = elem.parentNode;
}
return true;
}
var walkinput = function (forward) {
var focused = document.commandDispatcher.focusedElement;
var current = null;
var next = null;
var prev = null;
var list = [];
(function (frame) {
var doc = frame.document;
if (doc.body.localName.toLowerCase() == 'body') {
let r = doc.evaluate(xpath, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0, l = r.snapshotLength; i < l; ++i) {
let e = r.snapshotItem(i);
if (!isVisible(e))
continue;
let ef = {element: e, frame: frame};
list.push(ef);
if (e == focused) {
current = ef;
} else if (current && !next) {
next = ef;
} else if (!current) {
prev = ef;
}
}
}
for (let i = 0; i < frame.frames.length; i++)
arguments.callee(frame.frames[i]);
})(content);
if (list.length <= 0)
return;
var elem = forward ? (next || list[0])
: (prev || list[list.length - 1]);
if (!current || current.frame != elem.frame)
elem.frame.focus();
elem.element.focus();
};
let mapForward = liberator.globalVariables.walk_input_map_forward || '<M-i> <A-i>'
let mapBackward = liberator.globalVariables.walk_input_map_backward || '<M-S-i> <A-S-i>'
mappings.addUserMap([modes.NORMAL, modes.INSERT], mapForward.split(/\s+/),
'Walk Input Fields (Forward)', function () walkinput(true));
mappings.addUserMap([modes.NORMAL, modes.INSERT], mapBackward.split(/\s+/),
'Walk Input Fields (Backward)', function () walkinput(false));
})();