]> gitweb @ CieloNegro.org - sugar.git/blob - dot-files/_vimperator/plugin/walk-input_js
Auto commit by The Sugar System.
[sugar.git] / dot-files / _vimperator / plugin / walk-input_js
1 // Vimperator plugin: 'Walk Input'
2 // Last Change: 2008-05-22.
3 // License: BSD
4 // Version: 1.0
5 // Maintainer: Takayama Fumihiko <tekezo@pqrs.org>
6
7 // ------------------------------------------------------------
8 // The focus walks <input> & <textarea> elements.
9 // If you type M-i first, the focus moves to "<input name='search' />".
10 // Then if you type M-i once more, the focus moves to "<input name='name' />".
11 //
12 // <html><body>
13 //     <input name="search" />
14 //     <a href="xxx">xxx</a>
15 //     <a href="yyy">yyy</a>
16 //     <a href="zzz">zzz</a>
17 //     <input name="name" />
18 //     <textarea name="comment" />
19 //  </body></html>
20
21
22 var walkinput = function(forward) {
23     var win = document.commandDispatcher.focusedWindow;
24     var d = win.document;
25     var xpath = '//input[@type="text" or @type="password" or @type="search" or not(@type)] | //textarea';
26     var list = d.evaluate(xpath, d, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
27     if (list.snapshotLength == 0) return;
28
29     var focused = document.commandDispatcher.focusedElement;
30     var current = null;
31     var next = null;
32     var prev = null;
33     for (var i = 0; i < list.snapshotLength; ++i) {
34         var e = list.snapshotItem(i);
35         if (e == focused) {
36             current = e;
37         } else if (current && next == null) {
38             next = e;
39         } else if (current == null) {
40             prev = e;
41         }
42     }
43     if (forward == true && next) {
44         next.focus();
45     } else if (forward == false && prev) {
46         prev.focus();
47     } else {
48         if (forward == true) {
49             list.snapshotItem(0).focus();
50         } else {
51             list.snapshotItem(list.snapshotLength - 1).focus();
52         }
53     }
54 };
55
56 liberator.mappings.add([liberator.modes.NORMAL], ['<M-i>'], 'Walk Input Fields', function() { walkinput(true); });
57 liberator.mappings.add([liberator.modes.INSERT], ['<M-i>'], 'Walk Input Fields', function() { walkinput(true); });
58 liberator.mappings.add([liberator.modes.NORMAL], ['<A-i>'], 'Walk Input Fields', function() { walkinput(true); });
59 liberator.mappings.add([liberator.modes.INSERT], ['<A-i>'], 'Walk Input Fields', function() { walkinput(true); });
60 liberator.mappings.add([liberator.modes.NORMAL], ['<M-I>'], 'Walk Input Fields', function() { walkinput(false); });
61 liberator.mappings.add([liberator.modes.INSERT], ['<M-I>'], 'Walk Input Fields', function() { walkinput(false); });
62 liberator.mappings.add([liberator.modes.NORMAL], ['<A-I>'], 'Walk Input Fields', function() { walkinput(false); });
63 liberator.mappings.add([liberator.modes.INSERT], ['<A-I>'], 'Walk Input Fields', function() { walkinput(false); });