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

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: Review feedback Created 8 years, 8 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 *
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 &&
132 this.destinationStore_.selectedDestination.isPrintToPdf;
133 if (printToPDF) {
134 summaryLabel = localStrings.getString('printPreviewPageLabelSingular');
135 }
136
137 var numPages = this.printTicketStore_.getPageNumberSet().size;
138 var numSheets = numPages;
139 if (!printToPDF && this.printTicketStore_.isDuplexEnabled()) {
140 numSheets = Math.ceil(numPages / 2);
141 }
142
143 var copies = this.printTicketStore_.getCopies();
144 numSheets *= copies;
145 numPages *= copies;
146
147 if (numSheets > 1) {
148 summaryLabel = printToPDF ? pagesLabel :
149 localStrings.getString('printPreviewSheetsLabelPlural');
150 }
151
152 var html;
153 if (numPages != numSheets) {
154 html = localStrings.getStringF('printPreviewSummaryFormatLong',
155 '<b>' + numSheets + '</b>',
156 '<b>' + summaryLabel + '</b>',
157 numPages,
158 pagesLabel);
159 } else {
160 html = localStrings.getStringF('printPreviewSummaryFormatShort',
161 '<b>' + numSheets + '</b>',
162 '<b>' + summaryLabel + '</b>');
163 }
164
165 // Removing extra spaces from within the string.
166 html = html.replace(/\s{2,}/g, ' ');
167 summaryEl.innerHTML = html;
168 },
169
170 /**
171 * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT
172 * common event.
173 * @private
174 */
175 onPrintButtonClick_: function() {
176 if (!this.destinationStore_.selectedDestination.isPrintToPdf) {
98 this.printButton_.classList.add('loading'); 177 this.printButton_.classList.add('loading');
99 this.cancelButton_.classList.add('loading'); 178 this.cancelButton_.classList.add('loading');
100 this.summary_.innerHTML = localStrings.getString('printing'); 179 var summaryEl = this.getElement().getElementsByClassName(
101 } 180 PrintHeader.Classes_.SUMMARY)[0];
102 this.disableCancelButton(); 181 summaryEl.innerHTML = localStrings.getString('printing');
103 requestToPrintDocument(); 182 }
104 }, 183 cr.dispatchSimpleEvent(this, PrintHeader.Event.PRINT_BUTTON_CLICK);
105 184 },
106 /** 185
107 * Updates the state of |this.printButton_| depending on the user selection. 186 /**
108 * The button is enabled only when the following conditions are true. 187 * Called when the cancel button is clicked. Dispatches a
109 * 1) The selected page ranges are valid. 188 * CLOSE_PRINT_PREVIEW event.
110 * 2) The number of copies is valid (if applicable). 189 * @private
111 * @private 190 */
112 */ 191 onCancelButtonClick_: function() {
113 updatePrintButton_: function() { 192 cr.dispatchSimpleEvent(this, PrintHeader.Event.CANCEL_BUTTON_CLICK);
114 if (showingSystemDialog) 193 },
115 return; 194
116 this.printButton_.disabled = !areSettingsValid(); 195 /**
117 }, 196 * Called when the document has been updated. Updates document statistics.
118 197 * @private
119 /** 198 */
120 * Updates |this.summary_| based on the currently selected user options. 199 onDocumentChange_: function() {
121 * @private 200 this.updateSummary_();
122 */ 201 if (!this.printTicketStore_.isTicketValid()) {
123 updateSummary_: function() { 202 this.printButton_.disabled = true;
124 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; 203 }
125 var copies = printToPDF ? 1 : copiesSettings.numberOfCopies; 204 },
126 205
127 if ((!printToPDF && !copiesSettings.isValid()) || 206 /**
128 !pageSettings.isPageSelectionValid()) { 207 * Called when a new destination is selected. Updates the text on the print
129 this.summary_.innerHTML = ''; 208 * button.
130 return; 209 * @private
131 } 210 */
132 211 onDestinationSelect_: function() {
133 if (!marginSettings.areMarginSettingsValid()) { 212 if (this.destinationStore_.selectedDestination.isPrintToPdf) {
134 this.summary_.innerHTML = ''; 213 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 { 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.updateSummary_();
226 this.printButton_.disabled = !this.printTicketStore_.isTicketValid();
178 } 227 }
179 }; 228 };
180 229
230 // Export
181 return { 231 return {
182 PrintHeader: PrintHeader 232 PrintHeader: PrintHeader
183 }; 233 };
184 }); 234 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698