Skip to content

Commit

Permalink
Merge pull request #513 from PolymerElements/fix-512
Browse files Browse the repository at this point in the history
Get physical index by key in Polymer 1.x
  • Loading branch information
keanulee authored Mar 29, 2018
2 parents 0c9a669 + 8e779d5 commit cd3a68d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
50 changes: 30 additions & 20 deletions iron-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -1109,32 +1109,42 @@

_forwardItemPath: function(path, value) {
path = path.slice(6); // 'items.'.length == 6
var dot = path.indexOf('.') + 1;
if (dot === 0) {
var dot = path.indexOf('.');
if (dot === -1) {
dot = path.length;
}
var vidx = IS_V2
? parseInt(path.substring(0, dot), 10)
// Extract `#` from `path`.
: parseInt(path.substring(1, dot), 10);
var offscreenItem = this._offscreenFocusedItem;
var isIndexRendered = this._isIndexRendered(vidx);
var inst;
var isIndexRendered;
var pidx;
var inst;
var offscreenInstance = this.modelForElement(this._offscreenFocusedItem);
if (IS_V2) {
var vidx = parseInt(path.substring(0, dot), 10);
isIndexRendered = this._isIndexRendered(vidx);
if (isIndexRendered) {
pidx = this._getPhysicalIndex(vidx);
inst = this.modelForElement(this._physicalItems[pidx]);
} else if (offscreenInstance) {
inst = offscreenInstance;
}

if (isIndexRendered) {
pidx = this._getPhysicalIndex(vidx);
inst = this.modelForElement(this._physicalItems[pidx]);
} else if (offscreenItem) {
inst = this.modelForElement(offscreenItem);
}
if (!inst || inst[this.indexAs] !== vidx) {
// NOTE: consider valid instance if it has correct __key__ (Polymer 1.x).
if (!inst || inst.__key__ !== '#' + vidx) {
if (!inst || inst[this.indexAs] !== vidx) {
return;
}
} else {
// Polymer 1.x - get physical instance by key (`#1`), not index.
var key = path.substring(0, dot);
if (offscreenInstance && offscreenInstance.__key__ === key) {
inst = offscreenInstance;
} else {
pidx = this._physicalIndexForKey[key];
inst = this.modelForElement(this._physicalItems[pidx]);

if (!inst || inst.__key__ !== key) {
return;
}
}
}
path = path.substring(dot);
path = path.substring(dot + 1);
path = this.as + (path ? '.' + path : '');
IS_V2
? inst._setPendingPropertyOrPath(path, value, false, true)
Expand Down Expand Up @@ -1709,7 +1719,7 @@
_getPhysicalIndex: function(vidx) {
return IS_V2
? (this._physicalStart + (vidx - this._virtualStart)) % this._physicalCount
: this._physicalIndexForKey['#' + vidx];
: this._physicalIndexForKey[this._collection.getKey(this.items[vidx])];
},

focusItem: function(idx) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/helpers.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
var root = document;
while (root && root.elementFromPoint) {
el = root.elementFromPoint(x, y);
// In Safari, root.elementFromPoint() will return the host if nothing matches
// in the shadow root (https://bugs.webkit.org/show_bug.cgi?id=170743).
if (el === r) break;
r = el || r;
root = el ? el.shadowRoot : null;
}
Expand Down
19 changes: 19 additions & 0 deletions test/focus.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@
});
});

test('list focus change with down arrow after removing item (#512)', function(done) {
container.useTabIndex = true;
list.items = buildDataSet(100);
list.splice('items', 0, 1);

flush(function() {
var initialItem = getFirstItemFromList(list);
var itemToFocus = getNthItemFromList(list, 1);
initialItem.focus();
flush(function() {
MockInteractions.pressAndReleaseKeyOn(list, 40); // down
flush(function() {
assert.notEqual(itemToFocus.tabIndex, -1);
done();
});
});
});
});

test('list focus change with up arrow', function(done) {
list.items = buildDataSet(100);

Expand Down

0 comments on commit cd3a68d

Please sign in to comment.