1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr
.define('print_preview', function() {
9 * Creates a PrintHeader object. This object encapsulates all the elements
10 * and logic related to the top part of the left pane in print_preview.html.
11 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read
12 * information about the document.
13 * @param {!print_preview.DestinationStore} destinationStore Used to get the
14 * selected destination.
16 * @extends {print_preview.Component}
18 function PrintHeader(printTicketStore
, destinationStore
) {
19 print_preview
.Component
.call(this);
22 * Used to read information about the document.
23 * @type {!print_preview.PrintTicketStore}
26 this.printTicketStore_
= printTicketStore
;
29 * Used to get the selected destination.
30 * @type {!print_preview.DestinationStore}
33 this.destinationStore_
= destinationStore
;
36 * Whether the component is enabled.
40 this.isEnabled_
= true;
43 * Whether the print button is enabled.
47 this.isPrintButtonEnabled_
= true;
51 * Event types dispatched by the print header.
54 PrintHeader
.EventType
= {
55 PRINT_BUTTON_CLICK
: 'print_preview.PrintHeader.PRINT_BUTTON_CLICK',
56 CANCEL_BUTTON_CLICK
: 'print_preview.PrintHeader.CANCEL_BUTTON_CLICK'
59 PrintHeader
.prototype = {
60 __proto__
: print_preview
.Component
.prototype,
62 set isEnabled(isEnabled
) {
63 this.isEnabled_
= isEnabled
;
64 this.updatePrintButtonEnabledState_();
65 this.isCancelButtonEnabled
= isEnabled
;
68 get isPrintButtonEnabled() {
69 return !this.getChildElement('button.print').disabled
;
72 set isPrintButtonEnabled(isEnabled
) {
73 this.isPrintButtonEnabled_
= isEnabled
;
74 this.updatePrintButtonEnabledState_();
77 set isCancelButtonEnabled(isEnabled
) {
78 this.getChildElement('button.cancel').disabled
= !isEnabled
;
81 /** @param {string} message Error message to display in the print header. */
82 setErrorMessage: function(message
) {
83 var summaryEl
= this.getChildElement('.summary');
84 summaryEl
.innerHTML
= '';
85 summaryEl
.textContent
= message
;
86 this.getChildElement('button.print').classList
.toggle('loading', false);
87 this.getChildElement('button.cancel').classList
.toggle('loading', false);
91 decorateInternal: function() {
92 cr
.ui
.reverseButtonStrips(this.getElement());
96 enterDocument: function() {
97 print_preview
.Component
.prototype.enterDocument
.call(this);
101 this.getChildElement('button.cancel'),
103 this.onCancelButtonClick_
.bind(this));
105 this.getChildElement('button.print'),
107 this.onPrintButtonClick_
.bind(this));
111 this.printTicketStore_
,
112 print_preview
.PrintTicketStore
.EventType
.INITIALIZE
,
113 this.onTicketChange_
.bind(this));
115 this.printTicketStore_
,
116 print_preview
.PrintTicketStore
.EventType
.DOCUMENT_CHANGE
,
117 this.onTicketChange_
.bind(this));
119 this.printTicketStore_
,
120 print_preview
.PrintTicketStore
.EventType
.TICKET_CHANGE
,
121 this.onTicketChange_
.bind(this));
123 this.destinationStore_
,
124 print_preview
.DestinationStore
.EventType
.DESTINATION_SELECT
,
125 this.onDestinationSelect_
.bind(this));
127 this.printTicketStore_
.copies
,
128 print_preview
.ticket_items
.TicketItem
.EventType
.CHANGE
,
129 this.onTicketChange_
.bind(this));
131 this.printTicketStore_
.duplex
,
132 print_preview
.ticket_items
.TicketItem
.EventType
.CHANGE
,
133 this.onTicketChange_
.bind(this));
135 this.printTicketStore_
.pageRange
,
136 print_preview
.ticket_items
.TicketItem
.EventType
.CHANGE
,
137 this.onTicketChange_
.bind(this));
141 * Updates Print Button state.
144 updatePrintButtonEnabledState_: function() {
145 this.getChildElement('button.print').disabled
=
146 this.destinationStore_
.selectedDestination
== null ||
148 !this.isPrintButtonEnabled_
||
149 !this.printTicketStore_
.isTicketValid();
153 * Updates the summary element based on the currently selected user options.
156 updateSummary_: function() {
157 if (!this.printTicketStore_
.isTicketValid()) {
158 this.getChildElement('.summary').innerHTML
= '';
163 loadTimeData
.getString('printPreviewSheetsLabelSingular');
164 var pagesLabel
= loadTimeData
.getString('printPreviewPageLabelPlural');
166 var saveToPdf
= this.destinationStore_
.selectedDestination
&&
167 this.destinationStore_
.selectedDestination
.id
==
168 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
;
170 summaryLabel
= loadTimeData
.getString('printPreviewPageLabelSingular');
173 var numPages
= this.printTicketStore_
.pageRange
.getPageNumberSet().size
;
174 var numSheets
= numPages
;
175 if (!saveToPdf
&& this.printTicketStore_
.duplex
.getValue()) {
176 numSheets
= Math
.ceil(numPages
/ 2);
179 var copies
= this.printTicketStore_
.copies
.getValueAsNumber();
184 summaryLabel
= saveToPdf
? pagesLabel
:
185 loadTimeData
.getString('printPreviewSheetsLabelPlural');
190 if (numPages
!= numSheets
) {
191 html
= loadTimeData
.getStringF('printPreviewSummaryFormatLong',
192 '<b>' + numSheets
+ '</b>',
193 '<b>' + summaryLabel
+ '</b>',
196 label
= loadTimeData
.getStringF('printPreviewSummaryFormatLong',
197 numSheets
, summaryLabel
,
198 numPages
, pagesLabel
);
200 html
= loadTimeData
.getStringF('printPreviewSummaryFormatShort',
201 '<b>' + numSheets
+ '</b>',
202 '<b>' + summaryLabel
+ '</b>');
203 label
= loadTimeData
.getStringF('printPreviewSummaryFormatShort',
204 numSheets
, summaryLabel
);
207 // Removing extra spaces from within the string.
208 html
= html
.replace(/\s{2,}/g, ' ');
210 var summary
= this.getChildElement('.summary');
211 summary
.innerHTML
= html
;
212 summary
.setAttribute('aria-label', label
);
216 * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT
220 onPrintButtonClick_: function() {
221 if (this.destinationStore_
.selectedDestination
.id
!=
222 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
) {
223 this.getChildElement('button.print').classList
.add('loading');
224 this.getChildElement('button.cancel').classList
.add('loading');
225 this.getChildElement('.summary').innerHTML
=
226 loadTimeData
.getString('printing');
228 cr
.dispatchSimpleEvent(this, PrintHeader
.EventType
.PRINT_BUTTON_CLICK
);
232 * Called when the cancel button is clicked. Dispatches a
233 * CLOSE_PRINT_PREVIEW event.
236 onCancelButtonClick_: function() {
237 cr
.dispatchSimpleEvent(this, PrintHeader
.EventType
.CANCEL_BUTTON_CLICK
);
241 * Called when a new destination is selected. Updates the text on the print
245 onDestinationSelect_: function() {
246 var isSaveLabel
= this.destinationStore_
.selectedDestination
&&
247 (this.destinationStore_
.selectedDestination
.id
==
248 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
||
249 this.destinationStore_
.selectedDestination
.id
==
250 print_preview
.Destination
.GooglePromotedId
.DOCS
);
251 this.getChildElement('button.print').textContent
=
252 loadTimeData
.getString(isSaveLabel
? 'saveButton' : 'printButton');
253 if (this.destinationStore_
.selectedDestination
) {
254 this.getChildElement('button.print').focus();
259 * Called when the print ticket has changed. Disables the print button if
260 * any of the settings are invalid.
263 onTicketChange_: function() {
264 this.updatePrintButtonEnabledState_();
265 this.updateSummary_();
266 if (document
.activeElement
== null ||
267 document
.activeElement
== document
.body
) {
268 this.getChildElement('button.print').focus();
275 PrintHeader
: PrintHeader