helpers.html
3.68 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
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
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
The complete set of authors may be found at http://polymer.github.io/AUTHORS
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
-->
<script>
function findElementInList(container, selector) {
var i = 0;
var children = container._children;
var ms = Polymer.DomApi.matchesSelector;
for (; i < children.length; i++) {
if (children[i].nodeType === Node.ELEMENT_NODE && ms.call(children[i], selector)) {
return children[i];
}
}
return null;
}
function buildItem(index) {
return {
index: index
};
}
function buildDataSet(size) {
var data = [];
while (data.length < size) {
data.push(buildItem(data.length));
}
return data;
}
function simulateScroll(config) {
var list = config.list;
var target = config.target;
var delay = config.delay || 1;
var contribution = Math.abs(config.contribution) || 10;
// scroll back up
if (target < list.scrollTop) {
contribution = -contribution;
}
function scrollHandler() {
setTimeout(function() {
var minScrollTop = 0;
var maxScrollTop = list.scrollHeight - list.offsetHeight;
config.onScroll && config.onScroll();
if (list.scrollTop < target && contribution > 0 && list.scrollTop < maxScrollTop) {
list.scrollTop = Math.min(maxScrollTop, list.scrollTop + contribution);
} else if (list.scrollTop > target && contribution < 0 && list.scrollTop > minScrollTop) {
list.scrollTop = Math.max(minScrollTop, list.scrollTop + contribution);
} else {
list.removeEventListener('scroll', scrollHandler);
list.scrollTop = target;
config.onScrollEnd && config.onScrollEnd();
}
}, delay);
}
list.addEventListener('scroll', scrollHandler);
scrollHandler();
}
function getFirstItemFromList(list) {
var listRect = list.getBoundingClientRect();
return document.elementFromPoint(listRect.left + 100, listRect.top + 1);
}
function getLastItemFromList(list) {
var listRect = list.getBoundingClientRect();
return document.elementFromPoint(listRect.left + 100, listRect.top + listRect.height - 1);
}
function isFullOfItems(list) {
var listRect = list.getBoundingClientRect();
var listHeight = listRect.height - 1;
var item, y = listRect.top + 1;
// IE 10 & 11 doesn't render propertly :(
var badPixels = 0;
while (y < listHeight) {
item = document.elementFromPoint(listRect.left + 100, y);
if (!item || (item.parentNode && !item.parentNode._templateInstance)) {
badPixels++;
}
y++;
if (badPixels > 2) {
return false;
}
}
return true;
}
function checkRepeatedItems(list) {
var listRect = list.getBoundingClientRect();
var listHeight = list.offsetHeight;
var listItems = {};
return function() {
var itemKey;
var y = listRect.top;
while (y < listHeight) {
item = document.elementFromPoint(listRect.left + 100, y + 2);
itemKey = item.textContent || item.innerText;
if (item.parentNode && item.parentNode._templateInstance) {
if (listItems[itemKey] && listItems[itemKey] != item) {
return true;
}
listItems[itemKey] = item;
}
y += item.offsetHeight;
}
return false;
};
}
</script>