helpers.html 3.68 KB
<!--
@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>