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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('print_preview', function() {
'use strict';
/**
* Creates a PrintHeader object. This object encapsulates all the elements
* and logic related to the top part of the left pane in print_preview.html.
* @constructor
*/
function PrintHeader() {
this.printButton_ = $('print-button');
this.cancelButton_ = $('cancel-button');
this.summary_ = $('print-summary');
this.printButton_.focus();
}
cr.addSingletonGetter(PrintHeader);
PrintHeader.prototype = {
get printButton() {
return this.printButton_;
},
get cancelButton() {
return this.cancelButton_;
},
get summary() {
return this.summary_;
},
/**
* Adding event listeners where necessary. Listeners take care of changing
* their behavior depending on the current state, no need to remove them.
*/
addEventListeners: function() {
this.cancelButton_.onclick = function() {
chrome.send('closePrintPreviewTab');
};
this.printButton_.onclick = this.onPrintButtonClicked_.bind(this);
document.addEventListener('updateSummary',
this.updateSummary_.bind(this));
document.addEventListener('updatePrintButton',
this.updatePrintButton_.bind(this));
},
/**
* Listener executing whenever |this.printButton_| is clicked.
* @private
*/
onPrintButtonClicked_: function() {
var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF;
if (!printToPDF) {
this.printButton_.classList.add('loading');
this.cancelButton_.classList.add('loading');
this.summary_.innerHTML = localStrings.getString('printing');
}
requestToPrintDocument();
},
/**
* Updates the state of |this.printButton_| depending on the user selection.
* The button is enabled only when the following conditions are true.
* 1) The selected page ranges are valid.
* 2) The number of copies is valid (if applicable).
* @private
*/
updatePrintButton_: function() {
if (showingSystemDialog)
return;
this.printButton_.disabled = !areSettingsValid();
},
/**
* Updates |this.summary_| based on the currently selected user options.
* @private
*/
updateSummary_: function() {
var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF;
var copies = printToPDF ? 1 : copiesSettings.numberOfCopies;
if (!printToPDF && !copiesSettings.isValid()) {
this.summary_.innerHTML =
localStrings.getString('invalidNumberOfCopies');
return;
}
if (!pageSettings.isPageSelectionValid()) {
this.summary_.innerHTML = '';
return;
}
var pageSet = pageSettings.selectedPagesSet;
var numOfSheets = pageSet.length;
var summaryLabel =
localStrings.getString('printPreviewSheetsLabelSingular');
var numOfPagesText = '';
var pagesLabel = localStrings.getString('printPreviewPageLabelPlural');
if (printToPDF)
summaryLabel = localStrings.getString('printPreviewPageLabelSingular');
if (!printToPDF && copiesSettings.twoSidedCheckbox.checked)
numOfSheets = Math.ceil(numOfSheets / 2);
numOfSheets *= copies;
if (numOfSheets > 1) {
summaryLabel = printToPDF ? pagesLabel :
localStrings.getString('printPreviewSheetsLabelPlural');
}
var html = '';
if (pageSet.length * copies != numOfSheets) {
numOfPagesText = pageSet.length * copies;
html = localStrings.getStringF('printPreviewSummaryFormatLong',
'<b>' + numOfSheets + '</b>',
'<b>' + summaryLabel + '</b>',
numOfPagesText, pagesLabel);
} else {
html = localStrings.getStringF('printPreviewSummaryFormatShort',
'<b>' + numOfSheets + '</b>',
'<b>' + summaryLabel + '</b>');
}
// Removing extra spaces from within the string.
html = html.replace(/\s{2,}/g, ' ');
this.summary_.innerHTML = html;
},
};
return {
PrintHeader: PrintHeader,
};
});
|