Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["ScrollPosition"]; |
michael@0 | 8 | |
michael@0 | 9 | const Ci = Components.interfaces; |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * It provides methods to collect and restore scroll positions for single |
michael@0 | 13 | * frames and frame trees. |
michael@0 | 14 | * |
michael@0 | 15 | * This is a child process module. |
michael@0 | 16 | */ |
michael@0 | 17 | this.ScrollPosition = Object.freeze({ |
michael@0 | 18 | /** |
michael@0 | 19 | * Collects scroll position data for any given |frame| in the frame hierarchy. |
michael@0 | 20 | * |
michael@0 | 21 | * @param frame (DOMWindow) |
michael@0 | 22 | * |
michael@0 | 23 | * @return {scroll: "x,y"} e.g. {scroll: "100,200"} |
michael@0 | 24 | * Returns null when there is no scroll data we want to store for the |
michael@0 | 25 | * given |frame|. |
michael@0 | 26 | */ |
michael@0 | 27 | collect: function (frame) { |
michael@0 | 28 | let ifreq = frame.QueryInterface(Ci.nsIInterfaceRequestor); |
michael@0 | 29 | let utils = ifreq.getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 30 | let scrollX = {}, scrollY = {}; |
michael@0 | 31 | utils.getScrollXY(false /* no layout flush */, scrollX, scrollY); |
michael@0 | 32 | |
michael@0 | 33 | if (scrollX.value || scrollY.value) { |
michael@0 | 34 | return {scroll: scrollX.value + "," + scrollY.value}; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | return null; |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * Restores scroll position data for any given |frame| in the frame hierarchy. |
michael@0 | 42 | * |
michael@0 | 43 | * @param frame (DOMWindow) |
michael@0 | 44 | * @param value (object, see collect()) |
michael@0 | 45 | */ |
michael@0 | 46 | restore: function (frame, value) { |
michael@0 | 47 | let match; |
michael@0 | 48 | |
michael@0 | 49 | if (value && (match = /(\d+),(\d+)/.exec(value))) { |
michael@0 | 50 | frame.scrollTo(match[1], match[2]); |
michael@0 | 51 | } |
michael@0 | 52 | }, |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Restores scroll position data for the current frame hierarchy starting at |
michael@0 | 56 | * |root| using the given scroll position |data|. |
michael@0 | 57 | * |
michael@0 | 58 | * If the given |root| frame's hierarchy doesn't match that of the given |
michael@0 | 59 | * |data| object we will silently discard data for unreachable frames. We |
michael@0 | 60 | * may as well assign scroll positions to the wrong frames if some were |
michael@0 | 61 | * reordered or removed. |
michael@0 | 62 | * |
michael@0 | 63 | * @param root (DOMWindow) |
michael@0 | 64 | * @param data (object) |
michael@0 | 65 | * { |
michael@0 | 66 | * scroll: "100,200", |
michael@0 | 67 | * children: [ |
michael@0 | 68 | * {scroll: "100,200"}, |
michael@0 | 69 | * null, |
michael@0 | 70 | * {scroll: "200,300", children: [ ... ]} |
michael@0 | 71 | * ] |
michael@0 | 72 | * } |
michael@0 | 73 | */ |
michael@0 | 74 | restoreTree: function (root, data) { |
michael@0 | 75 | if (data.hasOwnProperty("scroll")) { |
michael@0 | 76 | this.restore(root, data.scroll); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | if (!data.hasOwnProperty("children")) { |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | let frames = root.frames; |
michael@0 | 84 | data.children.forEach((child, index) => { |
michael@0 | 85 | if (child && index < frames.length) { |
michael@0 | 86 | this.restoreTree(frames[index], child); |
michael@0 | 87 | } |
michael@0 | 88 | }); |
michael@0 | 89 | } |
michael@0 | 90 | }); |