Merge lp:~methanal-developers/methanal/812032-better-scroll-to into lp:methanal

Proposed by Jonathan Jacobs
Status: Merged
Approved by: Tristan Seligmann
Approved revision: 186
Merged at revision: 185
Proposed branch: lp:~methanal-developers/methanal/812032-better-scroll-to
Merge into: lp:methanal
Diff against target: 94 lines (+56/-7)
2 files modified
methanal/js/Methanal/Util.js (+41/-0)
methanal/js/Methanal/View.js (+15/-7)
To merge this branch: bzr merge lp:~methanal-developers/methanal/812032-better-scroll-to
Reviewer Review Type Date Requested Status
Tristan Seligmann Approve
Review via email: mp+68189@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Tristan Seligmann (mithrandi) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'methanal/js/Methanal/Util.js'
2--- methanal/js/Methanal/Util.js 2011-07-13 15:51:04 +0000
3+++ methanal/js/Methanal/Util.js 2011-07-17 22:21:49 +0000
4@@ -1419,3 +1419,44 @@
5 pluralForm = pluralForm || word + 's';
6 return n == 1 ? word : pluralForm;
7 };
8+
9+
10+
11+/**
12+ * Scroll a node into view.
13+ *
14+ * @type node: I{DOM element}
15+ *
16+ * @type scrollPosition: C{String}
17+ * @param scrollPosition: Name of the position to scroll C{node} to. Available
18+ * options are:
19+ *
20+ * - C{'top'}: Scroll the element to the top of the screen.
21+ *
22+ * - C{'bottom'}: Scroll the element to the bottom of the screen.
23+ *
24+ * - C{'middle'}: Scroll the element to the middle of the screen.
25+ *
26+ * - C{null}: Perform no scrolling.
27+ */
28+Methanal.Util.scrollTo = function scrollTo(node, scrollPosition) {
29+ if (scrollPosition === null || !node.scrollIntoView) {
30+ return;
31+ }
32+
33+ if (scrollPosition === 'bottom') {
34+ node.scrollIntoView(false);
35+ } else if (scrollPosition === 'middle') {
36+ node.scrollIntoView(true);
37+ var innerHeight = window.innerHeight;
38+ // For IE < 9.
39+ if (innerHeight === undefined) {
40+ innerHeight = node.ownerDocument.documentElement.clientHeight;
41+ }
42+ // Scroll the view up by half its size, after scrolling the node to the
43+ // top of it, to centre the node.
44+ window.scrollBy(0, -innerHeight / 2);
45+ } else {
46+ node.scrollIntoView(true);
47+ }
48+};
49
50=== modified file 'methanal/js/Methanal/View.js'
51--- methanal/js/Methanal/View.js 2011-07-13 12:59:13 +0000
52+++ methanal/js/Methanal/View.js 2011-07-17 22:21:49 +0000
53@@ -1252,7 +1252,7 @@
54
55 function focusControl(control) {
56 return function () {
57- control.focus(true);
58+ control.focus();
59 return false;
60 }
61 }
62@@ -1596,20 +1596,28 @@
63
64 /**
65 * Give keyboard focus to the form input.
66+ *
67+ * @type scrollPosition: C{String}
68+ * @param scrollPosition: Scroll the input into a view position, defaults
69+ * to C{'middle'}.
70+ *
71+ * @see: L{Methanal.Util.scrollTo}
72 */
73- function focus(self, scrollIntoView/*=true*/) {
74+ function focus(self, scrollPosition/*='middle'*/) {
75 if (!self.inputNode) {
76 return;
77 }
78
79 if (self.inputNode.focus !== undefined) {
80- self.inputNode.focus();
81- }
82- if (scrollIntoView === undefined || scrollIntoView) {
83- if (self.inputNode.scrollIntoView) {
84- self.inputNode.scrollIntoView(true);
85+ try {
86+ self.inputNode.focus();
87+ } catch (e) {
88+ // Internet Explorer throws an exception when it can't focus
89+ // things.
90 }
91 }
92+
93+ Methanal.Util.scrollTo(self.inputNode, scrollPosition || 'middle');
94 },
95
96

Subscribers

People subscribed via source and target branches

to all changes: