~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/iron-list/test/helpers.html

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
@license
 
3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
 
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
 
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS
 
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
 
7
Code distributed by Google as part of the polymer project is also
 
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
 
9
-->
 
10
 
 
11
<script>
 
12
  function findElementInList(container, selector) {
 
13
    var i = 0;
 
14
    var children = container._children;
 
15
    var ms = Polymer.DomApi.matchesSelector;
 
16
 
 
17
    for (; i < children.length; i++) {
 
18
      if (children[i].nodeType === Node.ELEMENT_NODE && ms.call(children[i], selector)) {
 
19
        return children[i];
 
20
      }
 
21
    }
 
22
    return null;
 
23
  }
 
24
 
 
25
  function buildItem(index) {
 
26
    return {
 
27
      index: index
 
28
    };
 
29
  }
 
30
 
 
31
  function buildDataSet(size) {
 
32
    var data = [];
 
33
    while (data.length < size) {
 
34
      data.push(buildItem(data.length));
 
35
    }
 
36
    return data;
 
37
  }
 
38
 
 
39
  function simulateScroll(config) {
 
40
    var list = config.list;
 
41
    var target = config.target;
 
42
    var delay = config.delay || 1;
 
43
    var contribution = Math.abs(config.contribution) || 10;
 
44
    // scroll back up
 
45
    if (target < list.scrollTop) {
 
46
      contribution = -contribution;
 
47
    }
 
48
 
 
49
    function scrollHandler() {
 
50
      setTimeout(function() {
 
51
        var minScrollTop = 0;
 
52
        var maxScrollTop = list.scrollHeight - list.offsetHeight;
 
53
 
 
54
        config.onScroll && config.onScroll();
 
55
 
 
56
        if (list.scrollTop < target && contribution > 0 && list.scrollTop < maxScrollTop) {
 
57
          list.scrollTop = Math.min(maxScrollTop, list.scrollTop + contribution);
 
58
 
 
59
        } else if (list.scrollTop > target && contribution < 0 && list.scrollTop > minScrollTop) {
 
60
          list.scrollTop = Math.max(minScrollTop, list.scrollTop + contribution);
 
61
 
 
62
        } else {
 
63
          list.removeEventListener('scroll', scrollHandler);
 
64
          list.scrollTop = target;
 
65
          config.onScrollEnd && config.onScrollEnd();
 
66
        }
 
67
      }, delay);
 
68
    }
 
69
    list.addEventListener('scroll', scrollHandler);
 
70
    scrollHandler();
 
71
  }
 
72
 
 
73
  function getFirstItemFromList(list) {
 
74
    var listRect = list.getBoundingClientRect();
 
75
    return document.elementFromPoint(listRect.left + 100, listRect.top + 1);
 
76
  }
 
77
 
 
78
  function getLastItemFromList(list) {
 
79
    var listRect = list.getBoundingClientRect();
 
80
    return document.elementFromPoint(listRect.left + 100, listRect.top + listRect.height - 1);
 
81
  }
 
82
 
 
83
  function isFullOfItems(list) {
 
84
    var listRect = list.getBoundingClientRect();
 
85
    var listHeight = listRect.height - 1;
 
86
    var item, y = listRect.top + 1;
 
87
    // IE 10 & 11 doesn't render propertly :(
 
88
    var badPixels = 0;
 
89
    while (y < listHeight) {
 
90
      item = document.elementFromPoint(listRect.left + 100, y);
 
91
      if (!item || (item.parentNode && !item.parentNode._templateInstance)) {
 
92
        badPixels++;
 
93
      }
 
94
      y++;
 
95
      if (badPixels > 2) {
 
96
        return false;
 
97
      }
 
98
    }
 
99
    return true;
 
100
  }
 
101
 
 
102
  function checkRepeatedItems(list) {
 
103
    var listRect = list.getBoundingClientRect();
 
104
    var listHeight = list.offsetHeight;
 
105
    var listItems = {};
 
106
 
 
107
    return function() {
 
108
      var itemKey;
 
109
      var y = listRect.top;
 
110
      while (y < listHeight) {
 
111
        item = document.elementFromPoint(listRect.left + 100, y + 2);
 
112
        itemKey = item.textContent || item.innerText;
 
113
 
 
114
        if (item.parentNode && item.parentNode._templateInstance) {
 
115
          if (listItems[itemKey] && listItems[itemKey] != item) {
 
116
            return true;
 
117
          }
 
118
          listItems[itemKey] = item;
 
119
        }
 
120
        y += item.offsetHeight;
 
121
      }
 
122
      return false;
 
123
    };
 
124
  }
 
125
</script>