Bug 1689240 - Move ScrollOffsets from aboutaddons.js to view-controller.js. r=rpl

Differential Revision: https://phabricator.services.mozilla.com/D104190
This commit is contained in:
Tim Nguyen 2021-03-25 11:44:11 +00:00
parent 330da40a65
commit 6015ee0245
2 changed files with 34 additions and 35 deletions

View File

@ -4679,40 +4679,6 @@ function openAmoInTab(el) {
windowRoot.ownerGlobal.openTrustedLinkIn(amoUrl, "tab");
}
/**
* Helper for saving and restoring the scroll offsets when a previously loaded
* view is accessed again.
*/
var ScrollOffsets = {
_key: null,
_offsets: new Map(),
canRestore: true,
setView(historyEntryId) {
this._key = historyEntryId;
this.canRestore = true;
},
getPosition() {
if (!this.canRestore) {
return { top: 0, left: 0 };
}
let { scrollTop: top, scrollLeft: left } = document.documentElement;
return { top, left };
},
save() {
if (this._key) {
this._offsets.set(this._key, this.getPosition());
}
},
restore() {
let { top = 0, left = 0 } = this._offsets.get(this._key) || {};
window.scrollTo({ top, left, behavior: "auto" });
},
};
function sendEMPong(aSubject, aTopic, aData) {
Services.obs.notifyObservers(window, "EM-pong");
}

View File

@ -6,7 +6,6 @@
/* import-globals-from ../../../content/customElements.js */
/* import-globals-from aboutaddonsCommon.js */
/* import-globals-from aboutaddons.js */
/* exported loadView */
const { AddonManager } = ChromeUtils.import(
@ -47,6 +46,40 @@ function loadView(viewId) {
gViewController.loadView(viewId);
}
/**
* Helper for saving and restoring the scroll offsets when a previously loaded
* view is accessed again.
*/
var ScrollOffsets = {
_key: null,
_offsets: new Map(),
canRestore: true,
setView(historyEntryId) {
this._key = historyEntryId;
this.canRestore = true;
},
getPosition() {
if (!this.canRestore) {
return { top: 0, left: 0 };
}
let { scrollTop: top, scrollLeft: left } = document.documentElement;
return { top, left };
},
save() {
if (this._key) {
this._offsets.set(this._key, this.getPosition());
}
},
restore() {
let { top = 0, left = 0 } = this._offsets.get(this._key) || {};
window.scrollTo({ top, left, behavior: "auto" });
},
};
var gViewController = {
currentViewId: null,
get defaultViewId() {