Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / print_header.js
blob33dcdb8bbbbab0e4d356fbd5893dba30147864f2
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}
17    */
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
25      */
26     this.printTicketStore_ = printTicketStore;
28     /**
29      * Used to get the selected destination.
30      * @type {!print_preview.DestinationStore}
31      * @private
32      */
33     this.destinationStore_ = destinationStore;
35     /**
36      * Whether the component is enabled.
37      * @type {boolean}
38      * @private
39      */
40     this.isEnabled_ = true;
42     /**
43      * Whether the print button is enabled.
44      * @type {boolean}
45      * @private
46      */
47     this.isPrintButtonEnabled_ = true;
48   };
50   /**
51    * Event types dispatched by the print header.
52    * @enum {string}
53    */
54   PrintHeader.EventType = {
55     PRINT_BUTTON_CLICK: 'print_preview.PrintHeader.PRINT_BUTTON_CLICK',
56     CANCEL_BUTTON_CLICK: 'print_preview.PrintHeader.CANCEL_BUTTON_CLICK'
57   };
59   PrintHeader.prototype = {
60     __proto__: print_preview.Component.prototype,
62     set isEnabled(isEnabled) {
63       this.isEnabled_ = isEnabled;
64       this.updatePrintButtonEnabledState_();
65       this.isCancelButtonEnabled = isEnabled;
66     },
68     get isPrintButtonEnabled() {
69       return !this.getChildElement('button.print').disabled;
70     },
72     set isPrintButtonEnabled(isEnabled) {
73       this.isPrintButtonEnabled_ = isEnabled;
74       this.updatePrintButtonEnabledState_();
75     },
77     set isCancelButtonEnabled(isEnabled) {
78       this.getChildElement('button.cancel').disabled = !isEnabled;
79     },
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);
88     },
90     /** @override */
91     decorateInternal: function() {
92       cr.ui.reverseButtonStrips(this.getElement());
93     },
95     /** @override */
96     enterDocument: function() {
97       print_preview.Component.prototype.enterDocument.call(this);
99       // User events
100       this.tracker.add(
101           this.getChildElement('button.cancel'),
102           'click',
103           this.onCancelButtonClick_.bind(this));
104       this.tracker.add(
105           this.getChildElement('button.print'),
106           'click',
107           this.onPrintButtonClick_.bind(this));
109       // Data events.
110       this.tracker.add(
111           this.printTicketStore_,
112           print_preview.PrintTicketStore.EventType.INITIALIZE,
113           this.onTicketChange_.bind(this));
114       this.tracker.add(
115           this.printTicketStore_,
116           print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE,
117           this.onTicketChange_.bind(this));
118       this.tracker.add(
119           this.printTicketStore_,
120           print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
121           this.onTicketChange_.bind(this));
122       this.tracker.add(
123           this.destinationStore_,
124           print_preview.DestinationStore.EventType.DESTINATION_SELECT,
125           this.onDestinationSelect_.bind(this));
126       this.tracker.add(
127           this.printTicketStore_.copies,
128           print_preview.ticket_items.TicketItem.EventType.CHANGE,
129           this.onTicketChange_.bind(this));
130       this.tracker.add(
131           this.printTicketStore_.duplex,
132           print_preview.ticket_items.TicketItem.EventType.CHANGE,
133           this.onTicketChange_.bind(this));
134       this.tracker.add(
135           this.printTicketStore_.pageRange,
136           print_preview.ticket_items.TicketItem.EventType.CHANGE,
137           this.onTicketChange_.bind(this));
138     },
140     /**
141      * Updates Print Button state.
142      * @private
143      */
144     updatePrintButtonEnabledState_: function() {
145       this.getChildElement('button.print').disabled =
146           this.destinationStore_.selectedDestination == null ||
147           !this.isEnabled_ ||
148           !this.isPrintButtonEnabled_ ||
149           !this.printTicketStore_.isTicketValid();
150     },
152     /**
153      * Updates the summary element based on the currently selected user options.
154      * @private
155      */
156     updateSummary_: function() {
157       if (!this.printTicketStore_.isTicketValid()) {
158         this.getChildElement('.summary').innerHTML = '';
159         return;
160       }
162       var summaryLabel =
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;
169       if (saveToPdf) {
170         summaryLabel = loadTimeData.getString('printPreviewPageLabelSingular');
171       }
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);
177       }
179       var copies = this.printTicketStore_.copies.getValueAsNumber();
180       numSheets *= copies;
181       numPages *= copies;
183       if (numSheets > 1) {
184         summaryLabel = saveToPdf ? pagesLabel :
185             loadTimeData.getString('printPreviewSheetsLabelPlural');
186       }
188       var html;
189       var label;
190       if (numPages != numSheets) {
191         html = loadTimeData.getStringF('printPreviewSummaryFormatLong',
192                                        '<b>' + numSheets + '</b>',
193                                        '<b>' + summaryLabel + '</b>',
194                                        numPages,
195                                        pagesLabel);
196         label = loadTimeData.getStringF('printPreviewSummaryFormatLong',
197                                         numSheets, summaryLabel,
198                                         numPages, pagesLabel);
199       } else {
200         html = loadTimeData.getStringF('printPreviewSummaryFormatShort',
201                                        '<b>' + numSheets + '</b>',
202                                        '<b>' + summaryLabel + '</b>');
203         label = loadTimeData.getStringF('printPreviewSummaryFormatShort',
204                                         numSheets, summaryLabel);
205       }
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);
213     },
215     /**
216      * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT
217      * common event.
218      * @private
219      */
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');
227       }
228       cr.dispatchSimpleEvent(this, PrintHeader.EventType.PRINT_BUTTON_CLICK);
229     },
231     /**
232      * Called when the cancel button is clicked. Dispatches a
233      * CLOSE_PRINT_PREVIEW event.
234      * @private
235      */
236     onCancelButtonClick_: function() {
237       cr.dispatchSimpleEvent(this, PrintHeader.EventType.CANCEL_BUTTON_CLICK);
238     },
240     /**
241      * Called when a new destination is selected. Updates the text on the print
242      * button.
243      * @private
244      */
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();
255       }
256     },
258     /**
259      * Called when the print ticket has changed. Disables the print button if
260      * any of the settings are invalid.
261      * @private
262      */
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();
269       }
270     }
271   };
273   // Export
274   return {
275     PrintHeader: PrintHeader
276   };