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

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

Powered by Google App Engine
This is Rietveld 408576698