Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(717)

Side by Side Diff: chrome/browser/resources/print_preview/print_header.js

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

Powered by Google App Engine
This is Rietveld 408576698