1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
// This file is loaded into the browser window scope.
/* eslint-env mozilla/browser-window */
// -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
customElements.define(
"printpreview-pagination",
class PrintPreviewPagination extends HTMLElement {
static get markup() {
return `
<html:link rel="stylesheet" href="chrome://global/content/printPagination.css" />
<html:div class="container">
<html:button id="navigateHome" class="toolbarButton startItem" data-l10n-id="printpreview-homearrow-button"></html:button>
<html:button id="navigatePrevious" class="toolbarButton" data-l10n-id="printpreview-previousarrow-button"></html:button>
<html:div class="toolbarCenter"><html:span id="sheetIndicator" data-l10n-id="printpreview-sheet-of-sheets" data-l10n-args='{ "sheetNum": 1, "sheetCount": 1 }'></html:span></html:div>
<html:button id="navigateNext" class="toolbarButton" data-l10n-id="printpreview-nextarrow-button"></html:button>
<html:button id="navigateEnd" class="toolbarButton endItem" data-l10n-id="printpreview-endarrow-button"></html:button>
</html:div>
`;
}
static get defaultProperties() {
return {
currentPage: 1,
sheetCount: 1,
};
}
get previewBrowser() {
return this._previewBrowser;
}
set previewBrowser(aBrowser) {
this._previewBrowser = aBrowser;
}
observePreviewBrowser(browser) {
if (browser == this.previewBrowser || !this.isConnected) {
return;
}
this.previewBrowser = browser;
this.mutationObserver.disconnect();
this.mutationObserver.observe(browser, {
attributes: ["current-page", "sheet-count"],
});
this.updateFromBrowser();
}
connectedCallback() {
MozXULElement.insertFTLIfNeeded("toolkit/printing/printPreview.ftl");
const shadowRoot = this.attachShadow({ mode: "open" });
document.l10n.connectRoot(shadowRoot);
let fragment = MozXULElement.parseXULToFragment(this.constructor.markup);
this.shadowRoot.append(fragment);
this.elements = {
sheetIndicator: shadowRoot.querySelector("#sheetIndicator"),
homeButton: shadowRoot.querySelector("#navigateHome"),
previousButton: shadowRoot.querySelector("#navigatePrevious"),
nextButton: shadowRoot.querySelector("#navigateNext"),
endButton: shadowRoot.querySelector("#navigateEnd"),
};
this.shadowRoot.addEventListener("click", this);
this.mutationObserver = new MutationObserver(() =>
this.updateFromBrowser()
);
// Initial render with some default values
// We'll be updated with real values when available
this.update(this.constructor.defaultProperties);
}
disconnectedCallback() {
document.l10n.disconnectRoot(this.shadowRoot);
this.shadowRoot.textContent = "";
this.mutationObserver?.disconnect();
delete this.mutationObserver;
this.currentPreviewBrowserObserver?.disconnect();
delete this.currentPreviewBrowserObserver;
}
handleEvent(event) {
if (event.type == "click" && event.button != 0) {
return;
}
event.stopPropagation();
switch (event.target) {
case this.elements.homeButton:
this.navigate(0, 0, "home");
break;
case this.elements.previousButton:
this.navigate(-1, 0, 0);
break;
case this.elements.nextButton:
this.navigate(1, 0, 0);
break;
case this.elements.endButton:
this.navigate(0, 0, "end");
break;
}
}
navigate(aDirection, aPageNum, aHomeOrEnd) {
const nsIWebBrowserPrint = Ci.nsIWebBrowserPrint;
let targetNum;
let navType;
// we use only one of aHomeOrEnd, aDirection, or aPageNum
if (aHomeOrEnd) {
// We're going to either the very first page ("home"), or the
// very last page ("end").
if (aHomeOrEnd == "home") {
targetNum = 1;
navType = nsIWebBrowserPrint.PRINTPREVIEW_HOME;
} else {
targetNum = this.sheetCount;
navType = nsIWebBrowserPrint.PRINTPREVIEW_END;
}
} else if (aPageNum) {
// We're going to a specific page (aPageNum)
targetNum = Math.min(Math.max(1, aPageNum), this.sheetCount);
navType = nsIWebBrowserPrint.PRINTPREVIEW_GOTO_PAGENUM;
} else {
// aDirection is either +1 or -1, and allows us to increment
// or decrement our currently viewed page.
targetNum = Math.min(
Math.max(1, this.currentSheet + aDirection),
this.sheetCount
);
navType = nsIWebBrowserPrint.PRINTPREVIEW_GOTO_PAGENUM;
}
// Preemptively update our own state, rather than waiting for the message from the child process
// This allows subsequent clicks of next/back to advance 1 page per click if possible
// and keeps the UI feeling more responsive
this.update({ currentPage: targetNum });
this.previewBrowser.sendMessageToActor(
"Printing:Preview:Navigate",
{
navType,
pageNum: targetNum,
},
"Printing"
);
}
update(data = {}) {
if (data.sheetCount) {
this.sheetCount = data.sheetCount;
}
if (data.currentPage) {
this.currentSheet = data.currentPage;
}
document.l10n.setAttributes(
this.elements.sheetIndicator,
this.elements.sheetIndicator.dataset.l10nId,
{
sheetNum: this.currentSheet,
sheetCount: this.sheetCount,
}
);
}
updateFromBrowser() {
let sheetCount = parseInt(
this.previewBrowser.getAttribute("sheet-count"),
10
);
let currentPage = parseInt(
this.previewBrowser.getAttribute("current-page"),
10
);
this.update({ sheetCount, currentPage });
}
}
);
|