Bug 1545394 - Keep stale rows in the view while receiving new results. r=mak

Differential Revision: https://phabricator.services.mozilla.com/D28049

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Dão Gottwald 2019-04-19 20:09:13 +00:00
Родитель 9d8ae2dabc
Коммит 6cf49d57b6
1 изменённых файлов: 54 добавлений и 27 удалений

Просмотреть файл

@ -108,9 +108,7 @@ class UrlbarView {
if (!this.isOpen || !this._selected) { if (!this.isOpen || !this._selected) {
return null; return null;
} }
return this._selected.result;
let resultIndex = this._selected.getAttribute("resultIndex");
return this._queryContext.results[resultIndex];
} }
/** /**
@ -193,33 +191,22 @@ class UrlbarView {
// UrlbarController listener methods. // UrlbarController listener methods.
onQueryStarted(queryContext) { onQueryStarted(queryContext) {
this._rows.style.minHeight = this._getBoundsWithoutFlushing(this._rows).height + "px"; this._startRemoveStaleRowsTimer();
} }
onQueryCancelled(queryContext) { onQueryCancelled(queryContext) {
// Nothing. this._cancelRemoveStaleRowsTimer();
} }
onQueryFinished(queryContext) { onQueryFinished(queryContext) {
this._rows.style.minHeight = ""; this._cancelRemoveStaleRowsTimer();
this._removeStaleRows();
} }
onQueryResults(queryContext) { onQueryResults(queryContext) {
this._queryContext = queryContext; this._queryContext = queryContext;
while (this._rows.children.length > queryContext.results.length) { this._updateResults(queryContext);
this._rows.lastElementChild.remove();
}
let resultIndex = 0;
for (let row of this._rows.children) {
this._updateRow(row, resultIndex);
resultIndex++;
}
for (; resultIndex < queryContext.results.length; resultIndex++) {
let row = this._createRow();
this._updateRow(row, resultIndex);
this._rows.appendChild(row);
}
let isFirstPreselectedResult = false; let isFirstPreselectedResult = false;
if (queryContext.lastResultCount == 0) { if (queryContext.lastResultCount == 0) {
@ -392,6 +379,24 @@ class UrlbarView {
this.panel.openPopup(this.input.textbox, "after_start"); this.panel.openPopup(this.input.textbox, "after_start");
} }
_updateResults(queryContext) {
let results = queryContext.results;
let i = 0;
for (let row of this._rows.children) {
if (i < results.length) {
this._updateRow(row, results[i]);
} else {
row.setAttribute("stale", "true");
}
i++;
}
for (; i < results.length; i++) {
let row = this._createRow();
this._updateRow(row, results[i]);
this._rows.appendChild(row);
}
}
_createRow() { _createRow() {
let item = this._createElement("div"); let item = this._createElement("div");
item.className = "urlbarView-row"; item.className = "urlbarView-row";
@ -438,8 +443,10 @@ class UrlbarView {
return item; return item;
} }
_updateRow(item, resultIndex) { _updateRow(item, result) {
let result = this._queryContext.results[resultIndex]; let resultIndex = this._queryContext.results.indexOf(result);
item.result = result;
item.removeAttribute("stale");
item.id = "urlbarView-row-" + resultIndex; item.id = "urlbarView-row-" + resultIndex;
item.setAttribute("resultIndex", resultIndex); item.setAttribute("resultIndex", resultIndex);
@ -520,6 +527,31 @@ class UrlbarView {
item._elements.get("action").textContent = action; item._elements.get("action").textContent = action;
} }
_removeStaleRows() {
let row = this._rows.lastElementChild;
while (row) {
let next = row.previousElementSibling;
if (row.hasAttribute("stale")) {
row.remove();
}
row = next;
}
}
_startRemoveStaleRowsTimer() {
this._removeStaleRowsTimer = this.window.setTimeout(() => {
this._removeStaleRowsTimer = null;
this._removeStaleRows();
}, 200);
}
_cancelRemoveStaleRowsTimer() {
if (this._removeStaleRowsTimer) {
this.window.clearTimeout(this._removeStaleRowsTimer);
this._removeStaleRowsTimer = null;
}
}
_selectItem(item, { _selectItem(item, {
updateInput = true, updateInput = true,
setAccessibleFocus = true, setAccessibleFocus = true,
@ -536,12 +568,7 @@ class UrlbarView {
this._selected = item; this._selected = item;
if (updateInput) { if (updateInput) {
let result = null; this.input.setValueFromResult(item && item.result);
if (item) {
let resultIndex = item.getAttribute("resultIndex");
result = this._queryContext.results[resultIndex];
}
this.input.setValueFromResult(result);
} }
} }