Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Work on responsive, change how flip bounds are calculated.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Oct 24, 2014
1 parent 988b406 commit f4075cc
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 10 deletions.
1 change: 1 addition & 0 deletions core-list.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ polyfill-next-selector { content: '.core-list-viewport > *'; }
position: absolute;
top: 0;
box-sizing: border-box;
opacity: 0;
}

polyfill-next-selector { content: ':host(:not([grid])) .core-list-viewport > *'; }
Expand Down
30 changes: 22 additions & 8 deletions core-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@
throw 'When using groups data, the sum of group[n].length\'s and data.length must agree!';
}
}
var g = this.groupForVirtualIndex(this._virtualStart);
this._groupStart = g.group;
this._groupStartIndex = g.groupIndex;
} else {
this._grouped = false;
this._nestedGroups = false;
Expand All @@ -610,7 +613,7 @@

// Add physical items up to a max based on data length, viewport size, and extra item overhang
var currentCount = this._physicalCount || 0;
this._physicalCount = Math.min(Math.ceil(this._target.offsetHeight / this.sizingHeight) * this.runwayFactor * this._rowFactor, this._virtualCount);
this._physicalCount = Math.min(Math.ceil(this._target.offsetHeight / (this._physicalAverage || this.sizingHeight)) * this.runwayFactor * this._rowFactor, this._virtualCount);
this._physicalCount = Math.max(currentCount, this._physicalCount);
this._physicalData = this._physicalData || new Array(this._physicalCount);
var needItemInit = false;
Expand Down Expand Up @@ -660,7 +663,7 @@
var needsReposition;
if (force || physicalDatum.model != virtualDatum) {
// Hide items that are getting new models until positioned
physicalItem.style.opacity = force ? 1 : 0;
// physicalItem.style.opacity = force ? 1 : 0;
// Set model, index, and selected fields
physicalDatum.model = virtualDatum;
physicalDatum.index = virtualIndex;
Expand Down Expand Up @@ -878,7 +881,7 @@
* @method refresh
*/
refresh: function() {
var i, virtualIndex, physicalIndex, physicalItem, deltaCount;
var i, deltaCount;

// Determine scroll position & any scrollDelta that may have occurred
var lastScrollTop = this._scrollTop;
Expand All @@ -895,10 +898,9 @@
this._physicalOffset += Math.max(scrollDelta, -this._physicalOffset);
this.changeStartIndex(deltaCount);
console.log(this._scrollTop, 'Random access to ' + this._virtualStart, this._physicalOffset);
} else {
} else {
// Incremental movement: adjust index by flipping items
var flipBound = this._aboveSize + this._physicalOffset +
(this._dir > 0 ? 0.4 : 0.6) * Math.max((this._physicalSize - this._targetSize, this._physicalSize));
var flipBound = this._dir > 0 ? this._upperBound : this._lowerBound;
if (((this._dir > 0 && this._scrollTop > flipBound) ||
(this._dir < 0 && this._scrollTop < flipBound))) {
var flipSize = Math.abs(this._scrollTop - flipBound);
Expand Down Expand Up @@ -986,7 +988,7 @@
}

// Position items
var divider;
var divider, upperBound, lowerBound;
var rowx = 0;
var x = this._rowMargin;
var y = this._physicalOffset;
Expand All @@ -998,11 +1000,12 @@
physicalItem = this._physicalItems[physicalIndex];
// Position divider
if (physicalItem._isDivider) {
if (rowx != 0) {
if (rowx !== 0) {
y += lastHeight;
rowx = 0;
}
divider = this._physicalDividers[physicalIndex];
divider.style.opacity = 1;
x = this._rowMargin;
if (divider && (divider._translateX != x || divider._translateY != y)) {
if (this.grid) {
Expand All @@ -1028,14 +1031,25 @@
if (this.grid) {
rowx++;
if (rowx >= this._rowFactor) {
lowerBound = y;
rowx = 0;
y += lastHeight;
if (!upperBound) {
upperBound = y;
}
}
x = this._rowMargin + rowx * this.width;
} else {
lowerBound = y;
y += lastHeight;
if (!upperBound) {
upperBound = y;
}
}
}
this._upperBound = this._aboveSize + upperBound;
this._lowerBound = this._aboveSize + lowerBound - this._targetSize;
// console.log('lower', lowerBound, 'upper', upperBound);
},

_updateViewportHeight: function() {
Expand Down
3 changes: 1 addition & 2 deletions demo-debug-grid.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<head>
<title>core-list</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<script>Object.observe = null;</script>
<script src="../webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="core-list.html">
<link rel="import" href="../core-ajax/core-ajax.html">
Expand Down Expand Up @@ -110,7 +109,7 @@

<core-ajax url="demo-data-grouped-short.json" response="{{data}}" auto></core-ajax>

<core-list id="list" data="{{data.data || data}}" groups="{{data.groups}}" runwayFactor="3" grid width="100">
<core-list id="list" data="{{data.data || data}}" groups="{{data.groups}}" grid width="100">
<template>
<div class="divider" divider hidden>
{{groupModel.letter}}
Expand Down
203 changes: 203 additions & 0 deletions demo-flickr-responsive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<!doctype html>
<!--
Copyright (c) 2014 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.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
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.txt
-->
<html>
<head>
<title>core-list</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<script src="../webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="core-list.html">
<link rel="import" href="../core-ajax/core-ajax.html">
<link rel="import" href="../core-toolbar/core-toolbar.html">
<link rel="import" href="../core-scroll-header-panel/core-scroll-header-panel.html">
<link rel="import" href="../core-icon/core-icon.html">
<link rel="import" href="../core-icons/core-icons.html">
<link rel="import" href="../core-icons/device-icons.html">
<link rel="import" href="../core-icons/image-icons.html">
<link rel="import" href="../core-icon-button/core-icon-button.html">
<link rel="import" href="../core-media-query/core-media-query.html">
<link rel="import" href="../paper-shadow/paper-shadow.html">
<link rel="import" href="../paper-spinner/paper-spinner.html">
<link rel="import" href="../paper-menu-button/paper-menu-button.html">
<link rel="import" href="../core-image/core-image.html">
<link rel="import" href="../core-selector/core-selector.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-slider/paper-slider.html">
<link rel="import" href="../core-scroll-threshold/core-scroll-threshold.html">
<link rel="import" href="../core-scroll-threshold/core-scroll-threshold.html">
<style>
html {
margin: 0;
height: 100%;
position: relative;
}
body {
margin: 0;
height: 100%;
overflow: auto;
position: relative;

-webkit-tap-highlight-color: transparent;
user-select: none;
-webkit-user-select: none;
font-family: sans-serif;
}
</style>
</head>
<body>

<list-test fit></list-test>

<polymer-element name="list-test" layout vertical>
<template>
<style>
.item {
box-sizing: border-box;
padding: 10px;
border-bottom: 1px solid lightgray;
}
.item.grid {
padding: 1px;
border-bottom: 0;
}
.photo {
box-sizing: border-box;
background-color: lightgray;
background-size: cover;
position: relative;
}
.toolbar {
background: #E91E63;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
color: white;
}
.divider {
font-size: 20px;
color: gray;
padding: 24px 8px 8px 8px;
}
.title {
padding: 12px 8px 8px 8px;
}
.title.grid {
position: absolute;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.3);
color: white;
}
.pad {
padding: 16px;
align-items: center;
}
.pad-left {
padding-left: 8px;
}
#scroller {
overflow: auto;
}
</style>

<core-media-query query="min-width: 640px" queryMatches="{{wide}}"></core-media-query>
<core-toolbar class="toolbar">
<paper-menu-button icon="menu">
<template repeat="{{group, idx in groups}}">
<paper-item on-tap="{{jumpToGroup}}" group="{{idx}}">{{group.data.name}}</paper-item>
</template>
</paper-menu-button>
<paper-menu-button icon="{{modeEl.icon}}">
<core-selector selected="{{mode}}" selectedItem="{{modeEl}}">
<paper-item name="grid" icon="view-module">Grid</paper-item>
<paper-item name="list" icon="view-list">List</paper-item>
<paper-item name="responsive" icon="device:devices">Responsive</paper-item>
</core-selector>
</paper-menu-button>
<paper-menu-button icon="{{groupedEl.icon}}">
<core-selector selected="{{grouped}}" selectedItem="{{groupedEl}}">
<paper-item name="yes" icon="image:filter-none">Show Groups</paper-item>
<paper-item name="no" icon="image:crop-din">No Groups</paper-item>
</core-selector>
</paper-menu-button>
<paper-slider id="slider" min="150" max="400" step="50" value="{{size}}"></paper-slider>
</core-toolbar>
<core-scroll-threshold id="threshold" scrollTarget="{{$.scroller}}"
lowerThreshold="1500" on-lower-trigger="{{loadMore}}">
</core-scroll-threshold>
<div id="scroller" flex>
<core-list id="list" data="{{data}}" groups="{{grouped=='yes' && groups}}" grid?="{{mode=='grid' || (mode=='responsive' && wide) }}" width="{{size+2}}" flex scrollTarget="{{$.scroller}}">
<template>
<div class="divider" divider>{{groupModel.name}}</div>
<div class="item {{ {selected:selected, grid:$.list.grid} | tokenList }}" layout horizontal>
<core-image class="photo" height="{{size}}" width="{{size}}" sizing="cover" load?="{{!!model}}" src="https://farm{{model.farm}}.staticflickr.com/{{model.server}}/{{model.id}}_{{model.secret}}_m.jpg" preload relative>
<div class="title grid" hidden?="{{!$.list.grid}}">{{model.title}}</div>
</core-image>
<div class="title" hidden?="{{$.list.grid}}" flex>{{model.title}}</div>
<paper-shadow z="{{$.list.grid ? 0 : 1}}"></paper-shadow>
</div>
</template>
</core-list>
<div class="pad" hidden?="{{!$.threshold.lowerTriggered}}" layout horizontal center-justified>
<paper-spinner active></paper-spinner>
<div class="pad-left">Loading...</div>
</div>
</div>
</template>
<script>
(function() {

Polymer('list-test', {
ready: function() {
window.list = this.$.list;
this.mode = 'grid';
this.grouped = 'yes';
this.$.slider.value = 200;
this.data = [];
this.groups = [];
this.apiKey = 'c304f1096a06486d3c1e7ab271bf7f3f';
this.load(4);
},
load: function(moreCount) {
if (this.countries.length) {
var country = this.countries.shift();
var ajax = document.createElement('core-ajax');
var count = Math.floor(50 + Math.random() * 20);
ajax.url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=' +
this.apiKey + '&safe_search=1&sort=interestingness-desc&text=' +
encodeURIComponent(country) + '&format=json&per_page=' + count;
ajax.addEventListener('core-response', function(e) {
var resp = JSON.parse(e.detail.response.match('jsonFlickrApi\\((.*)\\)')[1]);
if (resp.stat == 'ok') {
resp.photos.photo.forEach(function(p) { this.data.push(p); }.bind(this));
this.groups.push({length: resp.photos.photo.length, data: {name: country}});
// this.data.push(resp.photos.photo);
// this.groups.push({name: country});
this.$.threshold.clearLower(true);
if (moreCount) {
this.load(--moreCount);
}
}
}.bind(this));
ajax.go();
}
},
loadMore: function() {
this.load(2);
},
jumpToGroup: function(e) {
this.$.list.scrollToGroup(e.target.getAttribute('group'));
},
countries: ["Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Chad", "Chile", "China", "Christmas Island", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Ivory Coast", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "France, Metropolitan", "French Guiana", "French Polynesia", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "North Korea", "South Korea", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Sandwich Isls.", "Saint Lucia", "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovak Republic", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Viet Nam", "Virgin Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zaire", "Zambia", "Zimbabwe"]
});
})();
</script>
</polymer-element>

</body>
</html>

0 comments on commit f4075cc

Please sign in to comment.