Blame view

bower_components/iron-list/test/helpers.html 3.68 KB
e619a3b0   Luigi Serra   Controllet cross ...
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
  <!--
  @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;
    }
  
a53fbbed   Renato De Donato   select-dataset ne...
39
    function simulateScroll(config) {
e619a3b0   Luigi Serra   Controllet cross ...
40
41
42
      var list = config.list;
      var target = config.target;
      var delay = config.delay || 1;
a53fbbed   Renato De Donato   select-dataset ne...
43
44
45
46
      var contribution = Math.abs(config.contribution) || 10;
      // scroll back up
      if (target < list.scrollTop) {
        contribution = -contribution;
e619a3b0   Luigi Serra   Controllet cross ...
47
48
      }
  
a53fbbed   Renato De Donato   select-dataset ne...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
      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);
e619a3b0   Luigi Serra   Controllet cross ...
68
      }
a53fbbed   Renato De Donato   select-dataset ne...
69
70
      list.addEventListener('scroll', scrollHandler);
      scrollHandler();
e619a3b0   Luigi Serra   Controllet cross ...
71
72
73
74
    }
  
    function getFirstItemFromList(list) {
      var listRect = list.getBoundingClientRect();
a53fbbed   Renato De Donato   select-dataset ne...
75
      return document.elementFromPoint(listRect.left + 100, listRect.top + 1);
e619a3b0   Luigi Serra   Controllet cross ...
76
77
78
79
    }
  
    function getLastItemFromList(list) {
      var listRect = list.getBoundingClientRect();
a53fbbed   Renato De Donato   select-dataset ne...
80
      return document.elementFromPoint(listRect.left + 100, listRect.top + listRect.height - 1);
e619a3b0   Luigi Serra   Controllet cross ...
81
    }
f748e9cf   Luigi Serra   new controllet an...
82
83
84
85
86
87
88
89
  
    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) {
a53fbbed   Renato De Donato   select-dataset ne...
90
91
        item = document.elementFromPoint(listRect.left + 100, y);
        if (!item || (item.parentNode && !item.parentNode._templateInstance)) {
f748e9cf   Luigi Serra   new controllet an...
92
93
          badPixels++;
        }
a53fbbed   Renato De Donato   select-dataset ne...
94
95
        y++;
        if (badPixels > 2) {
f748e9cf   Luigi Serra   new controllet an...
96
97
          return false;
        }
f748e9cf   Luigi Serra   new controllet an...
98
99
100
      }
      return true;
    }
a53fbbed   Renato De Donato   select-dataset ne...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  
    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;
      };
    }
e619a3b0   Luigi Serra   Controllet cross ...
125
  </script>