Updated drag and drop thumbnails.
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / print_header.js
blob59edff20b6e5e7406f56a3d2416af8a178354782
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() {
6 'use strict';
8 /**
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.
15 * @constructor
16 * @extends {print_preview.Component}
18 function PrintHeader(printTicketStore, destinationStore) {
19 print_preview.Component.call(this);
21 /**
22 * Used to read information about the document.
23 * @type {!print_preview.PrintTicketStore}
24 * @private
26 this.printTicketStore_ = printTicketStore;
28 /**
29 * Used to get the selected destination.
30 * @type {!print_preview.DestinationStore}
31 * @private
33 this.destinationStore_ = destinationStore;
35 /**
36 * Whether the component is enabled.
37 * @type {boolean}
38 * @private
40 this.isEnabled_ = true;
43 /**
44 * Event types dispatched by the print header.
45 * @enum {string}
47 PrintHeader.EventType = {
48 PRINT_BUTTON_CLICK: 'print_preview.PrintHeader.PRINT_BUTTON_CLICK',
49 CANCEL_BUTTON_CLICK: 'print_preview.PrintHeader.CANCEL_BUTTON_CLICK'
52 /**
53 * CSS classes used by the print header.
54 * @enum {string}
55 * @private
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;
80 /** @override */
81 enterDocument: function() {
82 print_preview.Component.prototype.enterDocument.call(this);
84 // User events
85 this.tracker.add(
86 this.cancelButton_, 'click', this.onCancelButtonClick_.bind(this));
87 this.tracker.add(
88 this.printButton_, 'click', this.onPrintButtonClick_.bind(this));
90 // Data events.
91 this.tracker.add(
92 this.printTicketStore_,
93 print_preview.PrintTicketStore.EventType.INITIALIZE,
94 this.onTicketChange_.bind(this));
95 this.tracker.add(
96 this.printTicketStore_,
97 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE,
98 this.onTicketChange_.bind(this));
99 this.tracker.add(
100 this.printTicketStore_,
101 print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
102 this.onTicketChange_.bind(this));
103 this.tracker.add(
104 this.destinationStore_,
105 print_preview.DestinationStore.EventType.DESTINATION_SELECT,
106 this.onDestinationSelect_.bind(this));
110 * @return {Element} Print button element.
111 * @private
113 get printButton_() {
114 return this.getElement().getElementsByClassName(
115 PrintHeader.Classes_.PRINT_BUTTON)[0];
119 * @return {Element} Cancel button element.
120 * @private
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.
129 * @private
131 updateSummary_: function() {
132 var summaryEl = this.getElement().getElementsByClassName(
133 PrintHeader.Classes_.SUMMARY)[0];
134 if (!this.printTicketStore_.isTicketValid()) {
135 summaryEl.innerHTML = '';
136 return;
139 var summaryLabel =
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;
146 if (saveToPdf) {
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();
157 numSheets *= copies;
158 numPages *= copies;
160 if (numSheets > 1) {
161 summaryLabel = saveToPdf ? pagesLabel :
162 localStrings.getString('printPreviewSheetsLabelPlural');
165 var html;
166 if (numPages != numSheets) {
167 html = localStrings.getStringF('printPreviewSummaryFormatLong',
168 '<b>' + numSheets + '</b>',
169 '<b>' + summaryLabel + '</b>',
170 numPages,
171 pagesLabel);
172 } else {
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
185 * common event.
186 * @private
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.
203 * @private
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
211 * button.
212 * @private
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');
220 } else {
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.
229 * @private
231 onTicketChange_: function() {
232 this.printButton_.disabled =
233 !this.printTicketStore_.isTicketValid() ||
234 !this.isEnabled_;
235 this.updateSummary_();
236 if (document.activeElement == null ||
237 document.activeElement == document.body) {
238 this.printButton_.focus();
243 // Export
244 return {
245 PrintHeader: PrintHeader