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