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.getChildElement('button.cancel').disabled = !isEnabled;
68 set isPrintButtonEnabled(isEnabled) {
69 this.isPrintButtonEnabled_ = isEnabled;
70 this.updatePrintButtonEnabledState_();
73 /** @param {string} message Error message to display in the print header. */
74 setErrorMessage: function(message) {
75 var summaryEl = this.getChildElement('.summary');
76 summaryEl.innerHTML = '';
77 summaryEl.textContent = message;
81 enterDocument: function() {
82 print_preview.Component.prototype.enterDocument.call(this);
86 this.getChildElement('button.cancel'),
88 this.onCancelButtonClick_.bind(this));
90 this.getChildElement('button.print'),
92 this.onPrintButtonClick_.bind(this));
96 this.printTicketStore_,
97 print_preview.PrintTicketStore.EventType.INITIALIZE,
98 this.onTicketChange_.bind(this));
100 this.printTicketStore_,
101 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE,
102 this.onTicketChange_.bind(this));
104 this.printTicketStore_,
105 print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
106 this.onTicketChange_.bind(this));
108 this.destinationStore_,
109 print_preview.DestinationStore.EventType.DESTINATION_SELECT,
110 this.onDestinationSelect_.bind(this));
112 this.printTicketStore_.copies,
113 print_preview.ticket_items.TicketItem.EventType.CHANGE,
114 this.onTicketChange_.bind(this));
116 this.printTicketStore_.duplex,
117 print_preview.ticket_items.TicketItem.EventType.CHANGE,
118 this.onTicketChange_.bind(this));
120 this.printTicketStore_.pageRange,
121 print_preview.ticket_items.TicketItem.EventType.CHANGE,
122 this.onTicketChange_.bind(this));
126 * Updates Print Button state.
129 updatePrintButtonEnabledState_: function() {
130 this.getChildElement('button.print').disabled =
132 !this.isPrintButtonEnabled_ ||
133 !this.printTicketStore_.isTicketValid();
137 * Updates the summary element based on the currently selected user options.
140 updateSummary_: function() {
141 if (!this.printTicketStore_.isTicketValid()) {
142 this.getChildElement('.summary').innerHTML = '';
147 localStrings.getString('printPreviewSheetsLabelSingular');
148 var pagesLabel = localStrings.getString('printPreviewPageLabelPlural');
150 var saveToPdf = this.destinationStore_.selectedDestination &&
151 this.destinationStore_.selectedDestination.id ==
152 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF;
154 summaryLabel = localStrings.getString('printPreviewPageLabelSingular');
157 var numPages = this.printTicketStore_.pageRange.getPageNumberSet().size;
158 var numSheets = numPages;
159 if (!saveToPdf && this.printTicketStore_.duplex.getValue()) {
160 numSheets = Math.ceil(numPages / 2);
163 var copies = this.printTicketStore_.copies.getValueAsNumber();
168 summaryLabel = saveToPdf ? pagesLabel :
169 localStrings.getString('printPreviewSheetsLabelPlural');
173 if (numPages != numSheets) {
174 html = localStrings.getStringF('printPreviewSummaryFormatLong',
175 '<b>' + numSheets + '</b>',
176 '<b>' + summaryLabel + '</b>',
180 html = localStrings.getStringF('printPreviewSummaryFormatShort',
181 '<b>' + numSheets + '</b>',
182 '<b>' + summaryLabel + '</b>');
185 // Removing extra spaces from within the string.
186 html = html.replace(/\s{2,}/g, ' ');
187 this.getChildElement('.summary').innerHTML = html;
191 * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT
195 onPrintButtonClick_: function() {
196 if (this.destinationStore_.selectedDestination.id !=
197 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
198 this.getChildElement('button.print').classList.add('loading');
199 this.getChildElement('button.cancel').classList.add('loading');
200 this.getChildElement('.summary').innerHTML =
201 localStrings.getString('printing');
203 cr.dispatchSimpleEvent(this, PrintHeader.EventType.PRINT_BUTTON_CLICK);
207 * Called when the cancel button is clicked. Dispatches a
208 * CLOSE_PRINT_PREVIEW event.
211 onCancelButtonClick_: function() {
212 cr.dispatchSimpleEvent(this, PrintHeader.EventType.CANCEL_BUTTON_CLICK);
216 * Called when a new destination is selected. Updates the text on the print
220 onDestinationSelect_: function() {
221 var isSaveLabel = this.destinationStore_.selectedDestination.id ==
222 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ||
223 this.destinationStore_.selectedDestination.id ==
224 print_preview.Destination.GooglePromotedId.DOCS;
225 this.getChildElement('button.print').textContent = isSaveLabel ?
226 localStrings.getString('saveButton') :
227 localStrings.getString('printButton');
228 this.getChildElement('button.print').focus();
232 * Called when the print ticket has changed. Disables the print button if
233 * any of the settings are invalid.
236 onTicketChange_: function() {
237 this.updatePrintButtonEnabledState_();
238 this.updateSummary_();
239 if (document.activeElement == null ||
240 document.activeElement == document.body) {
241 this.getChildElement('button.print').focus();
248 PrintHeader: PrintHeader