gmane page-mode: binding for browser-object-links
[conkeror.git] / modules / scroll.js
blob527984a96e12ed82446ba07c7128e723dfe37ade
1 /**
2  * (C) Copyright 2009 Shawn Betts
3  * (C) Copyright 2009 John J. Foerch <jjfoerch@earthlink.net>
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
10 define_variable("headings_xpath", "//h1 | //h2 | //h3 | //h4 | //h5 | //h6",
11     "The xpath expression used by next-heading and previous-heading to find "+
12     "headings.  Users will rarely need to change the value of this, but it "+
13     "exists especially for page-modes to override with a site-specific "+
14     "xpath expression.");
17 define_variable("scroll_to_heading_wrap", true,
18     "If true, will wrap to the topmost heading when the viewport is at the "+
19     "bottom of the document and the user tries to access the next heading. "+
20     "Does the equivalent thing for \"previous heading\" as well.");
23 define_browser_object_class(
24     "next-heading", null, null,
25     function (I) {
26         let xpr = I.buffer.document.evaluate(
27                 I.buffer.get('headings_xpath'), I.buffer.document, null,
28                 Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null),
29             heading, found = null, foundtop = null,
30             first = null, firsttop = null;
31         while ((heading = xpr.iterateNext())) {
32             let rect = heading.getBoundingClientRect();
33             if (rect.bottom - rect.top < 2)
34                 continue;
35             if (! first || rect.top < firsttop) {
36                 first = heading;
37                 firsttop = rect.top;
38             }
39             if (rect.top > 2 && (! found || rect.top < foundtop)) {
40                 found = heading;
41                 foundtop = rect.top;
42             }
43         }
44         // scrollY can exceed scrollMaxY
45         let eod = I.buffer.scrollY - I.buffer.scrollMaxY >= 0;
46         if ((!found || eod) && scroll_to_heading_wrap)
47             found = first;
48         yield co_return(found);
49     });
52 define_browser_object_class(
53     "previous-heading", null, null,
54     function (I) {
55         let xpr = I.buffer.document.evaluate(
56                 I.buffer.get('headings_xpath'), I.buffer.document, null,
57                 Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null),
58             heading, found = null, foundtop = null,
59             last = null, lasttop = null;
60         while ((heading = xpr.iterateNext())) {
61             let rect = heading.getBoundingClientRect();
62             if (rect.bottom - rect.top < 2)
63                 continue;
64             if (rect.top < -1 && (! found || rect.top > foundtop)) {
65                 found = heading;
66                 foundtop = rect.top;
67             }
68             if (! last || rect.top > lasttop) {
69                 last = heading;
70                 lasttop = rect.top;
71             }
72         }
73         if (! found && scroll_to_heading_wrap)
74             found = last;
75         yield co_return(found);
76     });
79 function scroll (I) {
80     var element = yield read_browser_object(I);
81     // no scrolling and no error if we failed to get an object.
82     if (! element)
83         return;
84     element.scrollIntoView();
85     I.window.minibuffer.message(element.textContent);
89 interactive("scroll",
90     "Generalized scroll command.  The amount of scrolling is determined by "+
91     "the object passed to the command as a browser-object.  If the object "+
92     "is a DOM node, that node will be scrolled to the top of the viewport "+
93     "if possible.",
94     scroll);