-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-selection-scroll.html
More file actions
75 lines (58 loc) · 1.93 KB
/
basic-selection-scroll.html
File metadata and controls
75 lines (58 loc) · 1.93 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!--
Aspect which scrolls a container to keep the selected item visible.
@element basic-selection-scroll
-->
<link rel="import" href="../basic-aspect/basic-aspect.html">
<script>
window.Basic = window.Basic || {};
window.Basic.SelectionScroll = {
contribute: {
set selectedItem(item) {
if (item) {
// Keep the selected item in view.
this.collective.scrollItemIntoView(item);
}
},
/**
* Scroll the given element completely into view, minimizing the degree of
* scrolling performed.
*
* Blink has a scrollIntoViewIfNeeded() function that almost the same thing,
* but unfortunately it's non-standard, and in any event often ends up
* scrolling more than is absolutely necessary.
*
* @method scrollItemIntoView
*/
scrollItemIntoView: function(item) {
// Get the relative position of the item with respect to the top of the
// list's scrollable canvas. An item at the top of the list will have a
// elementTop of 0.
var innermost = this.collective.innermostAttached;
if (!innermost) {
return;
}
var elementTop = item.offsetTop - innermost.offsetTop - innermost.clientTop;
var elementBottom = elementTop + item.offsetHeight;
// Determine the bottom of the scrollable canvas.
var scrollBottom = innermost.scrollTop + innermost.clientHeight;
if (elementBottom > scrollBottom) {
// Scroll up until item is entirely visible.
innermost.scrollTop += elementBottom - scrollBottom;
}
else if (elementTop < innermost.scrollTop) {
// Scroll down until item is entirely visible.
innermost.scrollTop = elementTop;
}
}
},
name: 'SelectionScroll'
};
Polymer({
aspects: [Basic.SelectionScroll],
behaviors: [Basic.Aspect],
is: 'basic-selection-scroll',
scrollItemIntoView: function(item) {
this.collective.scrollItemIntoView(item);
}
});
</script>