diff --git a/README.md b/README.md index 5f1846d7..c19bde4b 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ be reused with a new model at any time. Particularly, any state that may change as the result of a user interaction with the list item must be bound to the model to avoid view state inconsistency. -__Important:__ `iron-list` must ether be explicitly sized, or delegate scrolling to an +__Important:__ `iron-list` must ether be explicitly sized, or delegate scrolling to an explicitly sized parent. By "explicitly sized", we mean it either has an explicit CSS `height` property set via a class or inline style, or else is sized by other layout means (e.g. the `flex` or `fit` classes). @@ -31,7 +31,7 @@ List item templates should bind to template models of the following structure: } ``` -Alternatively, you can change the property name used as data index by changing the +Alternatively, you can change the property name used as data index by changing the `indexAs` property. The `as` property defines the name of the variable to add to the binding scope for the array. @@ -62,3 +62,17 @@ bound from the model object provided to the template scope): ``` + +### Resizing + +`iron-list` lays out the items when it recives a notification via the `resize` event. +This event is fired by any element that implements `IronResizableBehavior`. + +By default, elements such as `iron-pages`, `paper-tabs` or `paper-dialog` will trigger +this event automatically. If you hide the list manually (e.g. you use `display: none`) +you might want to implement `IronResizableBehavior` or fire this event manually right +after the list became visible again. e.g. + +```js +document.querySelector('iron-list').fire('resize'); +``` diff --git a/iron-list.html b/iron-list.html index 190e484f..b78c3054 100644 --- a/iron-list.html +++ b/iron-list.html @@ -69,6 +69,18 @@ +### Resizing + +`iron-list` lays out the items when it recives a notification via the `resize` event. +This event is fired by any element that implements `IronResizableBehavior`. + +By default, elements such as `iron-pages`, `paper-tabs` or `paper-dialog` will trigger +this event automatically. If you hide the list manually (e.g. you use `display: none`) +you might want to implement `IronResizableBehavior` or fire this event manually right +after the list became visible again. e.g. + + document.querySelector('iron-list').fire('resize'); + @group Iron Element @element iron-list @@ -341,6 +353,13 @@ return this._viewportSize * 3; }, + /** + * True if the current list is visible. + */ + get _isVisible() { + return !!(this.offsetWidth || this.offsetHeight); + }, + /** * Gets the first visible item in the viewport. * @@ -581,7 +600,7 @@ * @return Array */ _increasePoolIfNeeded: function() { - if (this._physicalSize > this._optPhysicalSize) { + if (this._physicalSize >= this._optPhysicalSize || this._physicalAverage === 0) { return null; } @@ -626,11 +645,7 @@ * but it also ensures that only one `update` cycle is created. */ _render: function() { - if (this.isAttached && !this._initRendered && this.items) { - // polymer/issues/2039 - if (window.CustomElements) { - window.CustomElements.takeRecords(); - } + if (this.isAttached && !this._initRendered && this._isVisible && this.items) { this._update(); this._initRendered = true; } @@ -745,6 +760,7 @@ this.debounce('refresh', this._render); + } else if (change.path === 'items.splices') { // render the new set this._initRendered = false; @@ -838,6 +854,10 @@ var prevAvgCount = this._physicalAverageCount; var prevPhysicalAvg = this._physicalAverage; + // Make sure we distributed all the physical items + // so we can measure them + Polymer.dom.flush(); + for (var i = 0; i < this._physicalCount; i++) { this._physicalSizes[i] = this._physicalItems[i].offsetHeight; total += this._physicalSizes[i]; @@ -948,19 +968,26 @@ this._firstVisibleIndexVal = null; }, + /** + * Reset the physical average and the average count. + */ _resetAverage: function() { this._physicalAverage = 0; this._physicalAverageCount = 0; }, + /** + * A handler for the `resize` event triggered by `IronResizableBehavior` + * when the element is resized. + */ _resizeHandler: function() { - if (this._physicalItems) { - this.debounce('resize', function() { + this.debounce('resize', function() { + if (this._physicalItems && this._isVisible) { this._resetAverage(); this.updateViewportBoundaries(); this.scrollToIndex(this.firstVisibleIndex); - }); - } + } + }); } }); diff --git a/test/basic.html b/test/basic.html index 184225b5..cae4f010 100644 --- a/test/basic.html +++ b/test/basic.html @@ -18,49 +18,18 @@ - - + + - + + + + + + + + + + + + + + + + + + + diff --git a/test/index.html b/test/index.html index 72924aab..16b23e5c 100644 --- a/test/index.html +++ b/test/index.html @@ -20,7 +20,8 @@ WCT.loadSuites([ 'basic.html', 'mutations.html', - 'physical-count.html' + 'physical-count.html', + 'hidden-list.html' ]); diff --git a/test/mutations.html b/test/mutations.html index f6d10558..a432fe19 100644 --- a/test/mutations.html +++ b/test/mutations.html @@ -18,45 +18,15 @@ - - + + @@ -67,7 +37,7 @@ setup(function() { container = fixture('trivialList'); - list = findElementInList(container, 'iron-list'); + list = container.list; }); test('update physical item', function(done) { diff --git a/test/physical-count.html b/test/physical-count.html index 4a4f21c1..d9ae50f5 100644 --- a/test/physical-count.html +++ b/test/physical-count.html @@ -18,47 +18,17 @@ - - + + + + @@ -69,7 +39,7 @@ setup(function() { container = fixture('trivialList'); - list = findElementInList(container, 'iron-list'); + list = container.list; }); test('increase pool size', function(done) { @@ -77,7 +47,9 @@ flush(function() { var lastItem = getLastItemFromList(list); var lastItemHeight = lastItem.offsetHeight; - var expectedFinalItem = list.offsetHeight/lastItemHeight - 1; + var expectedFinalItem = list.offsetHeight/lastItemHeight; + + assert.equal(lastItemHeight, 1); assert.equal(getLastItemFromList(list).textContent, expectedFinalItem); done(); }); diff --git a/test/x-list.html b/test/x-list.html new file mode 100644 index 00000000..4ed0ca26 --- /dev/null +++ b/test/x-list.html @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + +