OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Creates a PrintHeader object. This object encapsulates all the elements | 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. | 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. |
11 * @constructor | 15 * @constructor |
| 16 * @extends {print_preview.Component} |
12 */ | 17 */ |
13 function PrintHeader() { | 18 function PrintHeader(printTicketStore, destinationStore) { |
14 this.printButton_ = $('print-button'); | 19 print_preview.Component.call(this); |
15 this.cancelButton_ = $('cancel-button'); | 20 |
16 this.summary_ = $('print-summary'); | 21 /** |
17 this.printButton_.focus(); | 22 * Used to read information about the document. |
18 this.addEventListeners_(); | 23 * @type {!print_preview.PrintTicketStore} |
19 } | 24 * @private |
20 | 25 */ |
21 cr.addSingletonGetter(PrintHeader); | 26 this.printTicketStore_ = printTicketStore; |
| 27 |
| 28 /** |
| 29 * Used to get the selected destination. |
| 30 * @type {!print_preview.DestinationStore} |
| 31 * @private |
| 32 */ |
| 33 this.destinationStore_ = destinationStore; |
| 34 |
| 35 /** |
| 36 * Whether the component is enabled. |
| 37 * @type {boolean} |
| 38 * @private |
| 39 */ |
| 40 this.isEnabled_ = true; |
| 41 }; |
| 42 |
| 43 /** |
| 44 * Event types dispatched by the print header. |
| 45 * @enum {string} |
| 46 */ |
| 47 PrintHeader.EventType = { |
| 48 PRINT_BUTTON_CLICK: 'print_preview.PrintHeader.PRINT_BUTTON_CLICK', |
| 49 CANCEL_BUTTON_CLICK: 'print_preview.PrintHeader.CANCEL_BUTTON_CLICK' |
| 50 }, |
| 51 |
| 52 /** |
| 53 * CSS classes used by the print header. |
| 54 * @enum {string} |
| 55 * @private |
| 56 */ |
| 57 PrintHeader.Classes_ = { |
| 58 CANCEL_BUTTON: 'print-header-cancel-button', |
| 59 PRINT_BUTTON: 'print-header-print-button', |
| 60 SUMMARY: 'print-header-summary' |
| 61 }; |
22 | 62 |
23 PrintHeader.prototype = { | 63 PrintHeader.prototype = { |
24 get printButton() { | 64 __proto__: print_preview.Component.prototype, |
25 return this.printButton_; | 65 |
26 }, | 66 set isEnabled(isEnabled) { |
27 | 67 this.isEnabled_ = isEnabled; |
28 get cancelButton() { | 68 this.printButton_.disabled = !isEnabled; |
29 return this.cancelButton_; | 69 }, |
30 }, | 70 |
31 | 71 setErrorMessage: function(message) { |
32 get summary() { | 72 var summaryEl = this.getElement().getElementsByClassName( |
33 return this.summary_; | 73 PrintHeader.Classes_.SUMMARY)[0]; |
34 }, | 74 summaryEl.innerHTML = ''; |
35 | 75 summaryEl.textContent = message; |
36 /** | 76 }, |
37 * Adding event listeners where necessary. Listeners take care of changing | 77 |
38 * their behavior depending on the current state, no need to remove them. | 78 /** @override */ |
39 * @private | 79 enterDocument: function() { |
40 */ | 80 print_preview.Component.prototype.enterDocument.call(this); |
41 addEventListeners_: function() { | 81 this.printButton_.focus(); |
42 this.cancelButton_.onclick = function() { | 82 |
43 this.disableCancelButton(); | 83 // User events |
44 closePrintPreviewTab(); | 84 this.tracker.add( |
45 }.bind(this); | 85 this.cancelButton_, 'click', this.onCancelButtonClick_.bind(this)); |
46 this.printButton_.onclick = this.onPrintRequested.bind(this); | 86 this.tracker.add( |
47 document.addEventListener(customEvents.UPDATE_SUMMARY, | 87 this.printButton_, 'click', this.onPrintButtonClick_.bind(this)); |
48 this.updateSummary_.bind(this)); | 88 |
49 document.addEventListener(customEvents.UPDATE_PRINT_BUTTON, | 89 // Data events. |
50 this.updatePrintButton_.bind(this)); | 90 this.tracker.add( |
51 document.addEventListener(customEvents.PDF_GENERATION_ERROR, | 91 this.printTicketStore_, |
52 this.onPDFGenerationError_.bind(this)); | 92 print_preview.PrintTicketStore.EventType.INITIALIZE, |
53 document.addEventListener(customEvents.PRINTER_CAPABILITIES_UPDATED, | 93 this.onTicketChange_.bind(this)); |
54 this.onPrinterCapabilitiesUpdated_.bind(this)); | 94 this.tracker.add( |
55 }, | 95 this.printTicketStore_, |
56 | 96 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE, |
57 /** | 97 this.onTicketChange_.bind(this)); |
58 * Enables the cancel button and attaches its keydown event listener. | 98 this.tracker.add( |
59 */ | 99 this.printTicketStore_, |
60 enableCancelButton: function() { | 100 print_preview.PrintTicketStore.EventType.TICKET_CHANGE, |
61 window.onkeydown = onKeyDown; | 101 this.onTicketChange_.bind(this)); |
62 this.cancelButton_.disabled = false; | 102 this.tracker.add( |
63 }, | 103 this.destinationStore_, |
64 | 104 print_preview.DestinationStore.EventType.DESTINATION_SELECT, |
65 /** | 105 this.onDestinationSelect_.bind(this)); |
66 * Executes when a |customEvents.PDF_GENERATION_ERROR| event occurs. | 106 }, |
67 * @private | 107 |
68 */ | 108 /** |
69 onPDFGenerationError_: function() { | 109 * @return {Element} Print button element. |
70 this.printButton_.disabled = true; | 110 * @private |
71 }, | 111 */ |
72 | 112 get printButton_() { |
73 /** | 113 return this.getElement().getElementsByClassName( |
74 * Executes when a |customEvents.PRINTER_CAPABILITIES_UPDATED| event occurs. | 114 PrintHeader.Classes_.PRINT_BUTTON)[0]; |
75 * @private | 115 }, |
76 */ | 116 |
77 onPrinterCapabilitiesUpdated_: function() { | 117 /** |
78 getSelectedPrinterName() == PRINT_TO_PDF ? | 118 * @return {Element} Cancel button element. |
79 this.printButton.textContent = localStrings.getString('saveButton') : | 119 * @private |
80 this.printButton.textContent = localStrings.getString('printButton'); | 120 */ |
81 }, | 121 get cancelButton_() { |
82 | 122 return this.getElement().getElementsByClassName( |
83 /** | 123 PrintHeader.Classes_.CANCEL_BUTTON)[0]; |
84 * Disables the cancel button and removes its keydown event listener. | 124 }, |
85 */ | 125 |
86 disableCancelButton: function() { | 126 /** |
87 window.onkeydown = null; | 127 * Updates the summary element based on the currently selected user options. |
88 this.cancelButton_.disabled = true; | 128 * @private |
89 }, | 129 */ |
90 | 130 updateSummary_: function() { |
91 /** | 131 var summaryEl = this.getElement().getElementsByClassName( |
92 * Listener executing whenever the print button is clicked or user presses | 132 PrintHeader.Classes_.SUMMARY)[0]; |
93 * the enter button while focus is in the pages field. | 133 if (!this.printTicketStore_.isTicketValid()) { |
94 */ | 134 summaryEl.innerHTML = ''; |
95 onPrintRequested: function() { | 135 return; |
96 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; | 136 } |
97 if (!printToPDF) { | 137 |
| 138 var summaryLabel = |
| 139 localStrings.getString('printPreviewSheetsLabelSingular'); |
| 140 var pagesLabel = localStrings.getString('printPreviewPageLabelPlural'); |
| 141 |
| 142 var printToPDF = this.destinationStore_.selectedDestination && |
| 143 this.destinationStore_.selectedDestination.isPrintToPdf; |
| 144 if (printToPDF) { |
| 145 summaryLabel = localStrings.getString('printPreviewPageLabelSingular'); |
| 146 } |
| 147 |
| 148 var numPages = this.printTicketStore_.getPageNumberSet().size; |
| 149 var numSheets = numPages; |
| 150 if (!printToPDF && this.printTicketStore_.isDuplexEnabled()) { |
| 151 numSheets = Math.ceil(numPages / 2); |
| 152 } |
| 153 |
| 154 var copies = this.printTicketStore_.getCopies(); |
| 155 numSheets *= copies; |
| 156 numPages *= copies; |
| 157 |
| 158 if (numSheets > 1) { |
| 159 summaryLabel = printToPDF ? pagesLabel : |
| 160 localStrings.getString('printPreviewSheetsLabelPlural'); |
| 161 } |
| 162 |
| 163 var html; |
| 164 if (numPages != numSheets) { |
| 165 html = localStrings.getStringF('printPreviewSummaryFormatLong', |
| 166 '<b>' + numSheets + '</b>', |
| 167 '<b>' + summaryLabel + '</b>', |
| 168 numPages, |
| 169 pagesLabel); |
| 170 } else { |
| 171 html = localStrings.getStringF('printPreviewSummaryFormatShort', |
| 172 '<b>' + numSheets + '</b>', |
| 173 '<b>' + summaryLabel + '</b>'); |
| 174 } |
| 175 |
| 176 // Removing extra spaces from within the string. |
| 177 html = html.replace(/\s{2,}/g, ' '); |
| 178 summaryEl.innerHTML = html; |
| 179 }, |
| 180 |
| 181 /** |
| 182 * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT |
| 183 * common event. |
| 184 * @private |
| 185 */ |
| 186 onPrintButtonClick_: function() { |
| 187 if (!this.destinationStore_.selectedDestination.isPrintToPdf) { |
98 this.printButton_.classList.add('loading'); | 188 this.printButton_.classList.add('loading'); |
99 this.cancelButton_.classList.add('loading'); | 189 this.cancelButton_.classList.add('loading'); |
100 this.summary_.innerHTML = localStrings.getString('printing'); | 190 var summaryEl = this.getElement().getElementsByClassName( |
101 } | 191 PrintHeader.Classes_.SUMMARY)[0]; |
102 this.disableCancelButton(); | 192 summaryEl.innerHTML = localStrings.getString('printing'); |
103 requestToPrintDocument(); | 193 } |
104 }, | 194 cr.dispatchSimpleEvent(this, PrintHeader.EventType.PRINT_BUTTON_CLICK); |
105 | 195 }, |
106 /** | 196 |
107 * Updates the state of |this.printButton_| depending on the user selection. | 197 /** |
108 * The button is enabled only when the following conditions are true. | 198 * Called when the cancel button is clicked. Dispatches a |
109 * 1) The selected page ranges are valid. | 199 * CLOSE_PRINT_PREVIEW event. |
110 * 2) The number of copies is valid (if applicable). | 200 * @private |
111 * @private | 201 */ |
112 */ | 202 onCancelButtonClick_: function() { |
113 updatePrintButton_: function() { | 203 cr.dispatchSimpleEvent(this, PrintHeader.EventType.CANCEL_BUTTON_CLICK); |
114 if (showingSystemDialog) | 204 }, |
115 return; | 205 |
116 this.printButton_.disabled = !areSettingsValid(); | 206 /** |
117 }, | 207 * Called when a new destination is selected. Updates the text on the print |
118 | 208 * button. |
119 /** | 209 * @private |
120 * Updates |this.summary_| based on the currently selected user options. | 210 */ |
121 * @private | 211 onDestinationSelect_: function() { |
122 */ | 212 if (this.destinationStore_.selectedDestination.isPrintToPdf) { |
123 updateSummary_: function() { | 213 this.printButton_.textContent = localStrings.getString('saveButton'); |
124 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; | |
125 var copies = printToPDF ? 1 : copiesSettings.numberOfCopies; | |
126 | |
127 if ((!printToPDF && !copiesSettings.isValid()) || | |
128 !pageSettings.isPageSelectionValid()) { | |
129 this.summary_.innerHTML = ''; | |
130 return; | |
131 } | |
132 | |
133 if (!marginSettings.areMarginSettingsValid()) { | |
134 this.summary_.innerHTML = ''; | |
135 return; | |
136 } | |
137 | |
138 var pageSet = pageSettings.selectedPagesSet; | |
139 var numOfSheets = pageSet.length; | |
140 if (numOfSheets == 0) | |
141 return; | |
142 | |
143 var summaryLabel = | |
144 localStrings.getString('printPreviewSheetsLabelSingular'); | |
145 var numOfPagesText = ''; | |
146 var pagesLabel = localStrings.getString('printPreviewPageLabelPlural'); | |
147 | |
148 if (printToPDF) | |
149 summaryLabel = localStrings.getString('printPreviewPageLabelSingular'); | |
150 | |
151 if (!printToPDF && | |
152 copiesSettings.duplexMode == print_preview.CopiesSettings.LONG_EDGE) { | |
153 numOfSheets = Math.ceil(numOfSheets / 2); | |
154 } | |
155 numOfSheets *= copies; | |
156 | |
157 if (numOfSheets > 1) { | |
158 summaryLabel = printToPDF ? pagesLabel : | |
159 localStrings.getString('printPreviewSheetsLabelPlural'); | |
160 } | |
161 | |
162 var html = ''; | |
163 if (pageSet.length * copies != numOfSheets) { | |
164 numOfPagesText = pageSet.length * copies; | |
165 html = localStrings.getStringF('printPreviewSummaryFormatLong', | |
166 '<b>' + numOfSheets + '</b>', | |
167 '<b>' + summaryLabel + '</b>', | |
168 numOfPagesText, pagesLabel); | |
169 } else { | 214 } else { |
170 html = localStrings.getStringF('printPreviewSummaryFormatShort', | 215 this.printButton_.textContent = localStrings.getString('printButton'); |
171 '<b>' + numOfSheets + '</b>', | 216 } |
172 '<b>' + summaryLabel + '</b>'); | 217 }, |
173 } | 218 |
174 | 219 /** |
175 // Removing extra spaces from within the string. | 220 * Called when the print ticket has changed. Disables the print button if |
176 html = html.replace(/\s{2,}/g, ' '); | 221 * any of the settings are invalid. |
177 this.summary_.innerHTML = html; | 222 * @private |
| 223 */ |
| 224 onTicketChange_: function() { |
| 225 this.printButton_.disabled = |
| 226 !this.printTicketStore_.isTicketValid() || |
| 227 !this.isEnabled_; |
| 228 this.updateSummary_(); |
178 } | 229 } |
179 }; | 230 }; |
180 | 231 |
| 232 // Export |
181 return { | 233 return { |
182 PrintHeader: PrintHeader | 234 PrintHeader: PrintHeader |
183 }; | 235 }; |
184 }); | 236 }); |
OLD | NEW |