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;
44 * Event types dispatched by the print header.
47 PrintHeader
.EventType
= {
48 PRINT_BUTTON_CLICK
: 'print_preview.PrintHeader.PRINT_BUTTON_CLICK',
49 CANCEL_BUTTON_CLICK
: 'print_preview.PrintHeader.CANCEL_BUTTON_CLICK'
53 * CSS classes used by the print header.
57 PrintHeader
.Classes_
= {
58 CANCEL_BUTTON
: 'print-header-cancel-button',
59 PRINT_BUTTON
: 'print-header-print-button',
60 SUMMARY
: 'print-header-summary'
63 PrintHeader
.prototype = {
64 __proto__
: print_preview
.Component
.prototype,
66 set isEnabled(isEnabled
) {
67 this.isEnabled_
= isEnabled
;
68 this.printButton_
.disabled
= !isEnabled
;
69 this.cancelButton_
.disabled
= !isEnabled
;
72 /** @param {string} message Error message to display in the print header. */
73 setErrorMessage: function(message
) {
74 var summaryEl
= this.getElement().getElementsByClassName(
75 PrintHeader
.Classes_
.SUMMARY
)[0];
76 summaryEl
.innerHTML
= '';
77 summaryEl
.textContent
= message
;
81 enterDocument: function() {
82 print_preview
.Component
.prototype.enterDocument
.call(this);
86 this.cancelButton_
, 'click', this.onCancelButtonClick_
.bind(this));
88 this.printButton_
, 'click', this.onPrintButtonClick_
.bind(this));
92 this.printTicketStore_
,
93 print_preview
.PrintTicketStore
.EventType
.INITIALIZE
,
94 this.onTicketChange_
.bind(this));
96 this.printTicketStore_
,
97 print_preview
.PrintTicketStore
.EventType
.DOCUMENT_CHANGE
,
98 this.onTicketChange_
.bind(this));
100 this.printTicketStore_
,
101 print_preview
.PrintTicketStore
.EventType
.TICKET_CHANGE
,
102 this.onTicketChange_
.bind(this));
104 this.destinationStore_
,
105 print_preview
.DestinationStore
.EventType
.DESTINATION_SELECT
,
106 this.onDestinationSelect_
.bind(this));
110 * @return {Element} Print button element.
114 return this.getElement().getElementsByClassName(
115 PrintHeader
.Classes_
.PRINT_BUTTON
)[0];
119 * @return {Element} Cancel button element.
122 get cancelButton_() {
123 return this.getElement().getElementsByClassName(
124 PrintHeader
.Classes_
.CANCEL_BUTTON
)[0];
128 * Updates the summary element based on the currently selected user options.
131 updateSummary_: function() {
132 var summaryEl
= this.getElement().getElementsByClassName(
133 PrintHeader
.Classes_
.SUMMARY
)[0];
134 if (!this.printTicketStore_
.isTicketValid()) {
135 summaryEl
.innerHTML
= '';
140 localStrings
.getString('printPreviewSheetsLabelSingular');
141 var pagesLabel
= localStrings
.getString('printPreviewPageLabelPlural');
143 var saveToPdf
= this.destinationStore_
.selectedDestination
&&
144 this.destinationStore_
.selectedDestination
.id
==
145 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
;
147 summaryLabel
= localStrings
.getString('printPreviewPageLabelSingular');
150 var numPages
= this.printTicketStore_
.getPageNumberSet().size
;
151 var numSheets
= numPages
;
152 if (!saveToPdf
&& this.printTicketStore_
.isDuplexEnabled()) {
153 numSheets
= Math
.ceil(numPages
/ 2);
156 var copies
= this.printTicketStore_
.getCopies();
161 summaryLabel
= saveToPdf
? pagesLabel
:
162 localStrings
.getString('printPreviewSheetsLabelPlural');
166 if (numPages
!= numSheets
) {
167 html
= localStrings
.getStringF('printPreviewSummaryFormatLong',
168 '<b>' + numSheets
+ '</b>',
169 '<b>' + summaryLabel
+ '</b>',
173 html
= localStrings
.getStringF('printPreviewSummaryFormatShort',
174 '<b>' + numSheets
+ '</b>',
175 '<b>' + summaryLabel
+ '</b>');
178 // Removing extra spaces from within the string.
179 html
= html
.replace(/\s{2,}/g, ' ');
180 summaryEl
.innerHTML
= html
;
184 * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT
188 onPrintButtonClick_: function() {
189 if (this.destinationStore_
.selectedDestination
.id
!=
190 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
) {
191 this.printButton_
.classList
.add('loading');
192 this.cancelButton_
.classList
.add('loading');
193 var summaryEl
= this.getElement().getElementsByClassName(
194 PrintHeader
.Classes_
.SUMMARY
)[0];
195 summaryEl
.innerHTML
= localStrings
.getString('printing');
197 cr
.dispatchSimpleEvent(this, PrintHeader
.EventType
.PRINT_BUTTON_CLICK
);
201 * Called when the cancel button is clicked. Dispatches a
202 * CLOSE_PRINT_PREVIEW event.
205 onCancelButtonClick_: function() {
206 cr
.dispatchSimpleEvent(this, PrintHeader
.EventType
.CANCEL_BUTTON_CLICK
);
210 * Called when a new destination is selected. Updates the text on the print
214 onDestinationSelect_: function() {
215 if (this.destinationStore_
.selectedDestination
.id
==
216 print_preview
.Destination
.GooglePromotedId
.SAVE_AS_PDF
||
217 this.destinationStore_
.selectedDestination
.id
==
218 print_preview
.Destination
.GooglePromotedId
.DOCS
) {
219 this.printButton_
.textContent
= localStrings
.getString('saveButton');
221 this.printButton_
.textContent
= localStrings
.getString('printButton');
223 this.printButton_
.focus();
227 * Called when the print ticket has changed. Disables the print button if
228 * any of the settings are invalid.
231 onTicketChange_: function() {
232 this.printButton_
.disabled
=
233 !this.printTicketStore_
.isTicketValid() ||
235 this.updateSummary_();
236 if (document
.activeElement
== null ||
237 document
.activeElement
== document
.body
) {
238 this.printButton_
.focus();
245 PrintHeader
: PrintHeader