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

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

Issue 7358002: Print Preview: Refactoring layout mode related logic to LayoutSettings class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing nits Created 9 years, 5 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
« no previous file with comments | « chrome/browser/resources/print_preview/print_preview.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // require: cr/ui/print_preview_cloud.js 5 // require: cr/ui/print_preview_cloud.js
6 6
7 var localStrings = new LocalStrings(); 7 var localStrings = new LocalStrings();
8 8
9 // If useCloudPrint is true we attempt to connect to cloud print 9 // If useCloudPrint is true we attempt to connect to cloud print
10 // and populate the list of printers with cloud print printers. 10 // and populate the list of printers with cloud print printers.
(...skipping 30 matching lines...) Expand all
41 41
42 // True when preview tab is hidden. 42 // True when preview tab is hidden.
43 var isTabHidden = false; 43 var isTabHidden = false;
44 44
45 // Object holding all the pages related settings. 45 // Object holding all the pages related settings.
46 var pageSettings; 46 var pageSettings;
47 47
48 // Object holding all the copies related settings. 48 // Object holding all the copies related settings.
49 var copiesSettings; 49 var copiesSettings;
50 50
51 // Object holding all the layout related settings.
52 var layoutSettings;
53
51 // True if the user has click 'Advanced...' in order to open the system print 54 // True if the user has click 'Advanced...' in order to open the system print
52 // dialog. 55 // dialog.
53 var showingSystemDialog = false; 56 var showingSystemDialog = false;
54 57
55 // The range of options in the printer dropdown controlled by cloud print. 58 // The range of options in the printer dropdown controlled by cloud print.
56 var firstCloudPrintOptionPos = 0 59 var firstCloudPrintOptionPos = 0
57 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; 60 var lastCloudPrintOptionPos = firstCloudPrintOptionPos;
58 61
59 /** 62 /**
60 * Window onload handler, sets up the page and starts print preview by getting 63 * Window onload handler, sets up the page and starts print preview by getting
(...skipping 15 matching lines...) Expand all
76 79
77 $('print-button').focus(); 80 $('print-button').focus();
78 $('system-dialog-link').addEventListener('click', onSystemDialogLinkClicked); 81 $('system-dialog-link').addEventListener('click', onSystemDialogLinkClicked);
79 $('mainview').parentElement.removeChild($('dummy-viewer')); 82 $('mainview').parentElement.removeChild($('dummy-viewer'));
80 83
81 $('printer-list').disabled = true; 84 $('printer-list').disabled = true;
82 $('print-button').onclick = printFile; 85 $('print-button').onclick = printFile;
83 86
84 pageSettings = print_preview.PageSettings.getInstance(); 87 pageSettings = print_preview.PageSettings.getInstance();
85 copiesSettings = print_preview.CopiesSettings.getInstance(); 88 copiesSettings = print_preview.CopiesSettings.getInstance();
89 layoutSettings = print_preview.LayoutSettings.getInstance();
86 pageSettings.addEventListeners(); 90 pageSettings.addEventListeners();
87 copiesSettings.addEventListeners(); 91 copiesSettings.addEventListeners();
92 layoutSettings.addEventListeners();
88 93
89 showLoadingAnimation(); 94 showLoadingAnimation();
90 chrome.send('getDefaultPrinter'); 95 chrome.send('getDefaultPrinter');
91 } 96 }
92 97
93 /** 98 /**
94 * Adds event listeners to the settings controls. 99 * Adds event listeners to the settings controls.
95 */ 100 */
96 function addEventListeners() { 101 function addEventListeners() {
97 // Controls that require preview rendering. 102 // Controls that require preview rendering.
98 $('landscape').onclick = onLayoutModeToggle;
99 $('portrait').onclick = onLayoutModeToggle;
100 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; 103 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities;
101 104
102 // Controls that do not require preview rendering. 105 // Controls that do not require preview rendering.
103 $('color').onclick = function() { setColor(true); }; 106 $('color').onclick = function() { setColor(true); };
104 $('bw').onclick = function() { setColor(false); }; 107 $('bw').onclick = function() { setColor(false); };
105 } 108 }
106 109
107 /** 110 /**
108 * Removes event listeners from the settings controls. 111 * Removes event listeners from the settings controls.
109 */ 112 */
110 function removeEventListeners() { 113 function removeEventListeners() {
111 if (pageSettings) 114 if (pageSettings)
112 clearTimeout(pageSettings.timerId_); 115 clearTimeout(pageSettings.timerId_);
113 116
114 // Controls that require preview rendering 117 // Controls that require preview rendering
115 $('landscape').onclick = null;
116 $('portrait').onclick = null;
117 $('printer-list').onchange = null; 118 $('printer-list').onchange = null;
118 119
119 // Controls that don't require preview rendering. 120 // Controls that don't require preview rendering.
120 $('color').onclick = null; 121 $('color').onclick = null;
121 $('bw').onclick = null; 122 $('bw').onclick = null;
122 } 123 }
123 124
124 /** 125 /**
125 * Disables the input elements in the sidebar. 126 * Disables the input elements in the sidebar.
126 */ 127 */
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 240
240 // Regenerate the preview data based on selected printer settings. 241 // Regenerate the preview data based on selected printer settings.
241 setDefaultValuesAndRegeneratePreview(); 242 setDefaultValuesAndRegeneratePreview();
242 } 243 }
243 244
244 /** 245 /**
245 * Updates the controls with printer capabilities information. 246 * Updates the controls with printer capabilities information.
246 * @param {Object} settingInfo printer setting information. 247 * @param {Object} settingInfo printer setting information.
247 */ 248 */
248 function updateWithPrinterCapabilities(settingInfo) { 249 function updateWithPrinterCapabilities(settingInfo) {
250 var customEvent = new cr.Event("printerCapabilitiesUpdated");
251 customEvent.printerCapabilities = settingInfo;
252 document.dispatchEvent(customEvent);
253
249 var disableColorOption = settingInfo.disableColorOption; 254 var disableColorOption = settingInfo.disableColorOption;
250 var disableCopiesOption = settingInfo.disableCopiesOption;
251 var setColorAsDefault = settingInfo.setColorAsDefault; 255 var setColorAsDefault = settingInfo.setColorAsDefault;
252 var disableLandscapeOption = settingInfo.disableLandscapeOption;
253 var setDuplexAsDefault = settingInfo.setDuplexAsDefault;
254 var color = $('color'); 256 var color = $('color');
255 var bw = $('bw'); 257 var bw = $('bw');
256 var colorOptions = $('color-options'); 258 var colorOptions = $('color-options');
257 259
258 if (disableCopiesOption) {
259 fadeOutElement($('copies-option'));
260 $('hr-before-copies').classList.remove('invisible');
261 } else {
262 fadeInElement($('copies-option'));
263 $('hr-before-copies').classList.add('invisible');
264 }
265
266 if (disableLandscapeOption) {
267 fadeOutElement($('landscape-option'));
268 } else {
269 fadeInElement($('landscape-option'));
270 }
271
272 disableColorOption ? fadeOutElement(colorOptions) : 260 disableColorOption ? fadeOutElement(colorOptions) :
273 fadeInElement(colorOptions); 261 fadeInElement(colorOptions);
274 colorOptions.setAttribute('aria-hidden', disableColorOption); 262 colorOptions.setAttribute('aria-hidden', disableColorOption);
275 263
276 if (color.checked != setColorAsDefault) { 264 if (color.checked != setColorAsDefault) {
277 color.checked = setColorAsDefault; 265 color.checked = setColorAsDefault;
278 bw.checked = !setColorAsDefault; 266 bw.checked = !setColorAsDefault;
279 } 267 }
280 copiesSettings.twoSidedCheckbox.checked = setDuplexAsDefault;
281 } 268 }
282 269
283 /** 270 /**
284 * Turn on the integration of Cloud Print. 271 * Turn on the integration of Cloud Print.
285 * @param {boolean} enable True if cloud print should be used. 272 * @param {boolean} enable True if cloud print should be used.
286 * @param {string} cloudPrintUrl The URL to use for cloud print servers. 273 * @param {string} cloudPrintUrl The URL to use for cloud print servers.
287 */ 274 */
288 function setUseCloudPrint(enable, cloudPrintURL) { 275 function setUseCloudPrint(enable, cloudPrintURL) {
289 useCloudPrint = enable; 276 useCloudPrint = enable;
290 cloudprint.setBaseURL(cloudPrintURL); 277 cloudprint.setBaseURL(cloudPrintURL);
291 } 278 }
292 279
293 /** 280 /**
294 * Take the PDF data handed to us and submit it to the cloud, closing the print 281 * Take the PDF data handed to us and submit it to the cloud, closing the print
295 * preview tab once the upload is successful. 282 * preview tab once the upload is successful.
296 * @param {string} data Data to send as the print job. 283 * @param {string} data Data to send as the print job.
297 */ 284 */
298 function printToCloud(data) { 285 function printToCloud(data) {
299 cloudprint.printToCloud(data, finishedCloudPrinting); 286 cloudprint.printToCloud(data, finishedCloudPrinting);
300 } 287 }
301 288
302 /** 289 /**
303 * Cloud print upload of the PDF file is finished, time to close the dialog. 290 * Cloud print upload of the PDF file is finished, time to close the dialog.
304 */ 291 */
305 function finishedCloudPrinting() { 292 function finishedCloudPrinting() {
306 window.location = cloudprint.getBaseURL(); 293 window.location = cloudprint.getBaseURL();
307 } 294 }
308 295
309 /** 296 /**
310 * Checks whether the preview layout setting is set to 'landscape' or not.
311 *
312 * @return {boolean} true if layout is 'landscape'.
313 */
314 function isLandscape() {
315 return $('landscape').checked;
316 }
317
318 /**
319 * Checks whether the preview color setting is set to 'color' or not. 297 * Checks whether the preview color setting is set to 'color' or not.
320 * 298 *
321 * @return {boolean} true if color is 'color'. 299 * @return {boolean} true if color is 'color'.
322 */ 300 */
323 function isColor() { 301 function isColor() {
324 return $('color').checked; 302 return $('color').checked;
325 } 303 }
326 304
327 /** 305 /**
328 * Checks whether the preview collate setting value is set or not.
329 *
330 * @return {boolean} true if collate setting is enabled and checked.
331 */
332 function isCollated() {
333 return !copiesSettings.collateOption.hidden && $('collate').checked;
334 }
335
336 /**
337 * Gets the duplex mode for printing.
338 * @return {number} duplex mode.
339 */
340 function getDuplexMode() {
341 // Constants values matches printing::DuplexMode enum.
342 const SIMPLEX = 0;
343 const LONG_EDGE = 1;
344 return !copiesSettings.twoSidedCheckbox.checked ? SIMPLEX : LONG_EDGE;
345 }
346
347 /**
348 * Creates an object based on the values in the printer settings. 306 * Creates an object based on the values in the printer settings.
349 * 307 *
350 * @return {Object} Object containing print job settings. 308 * @return {Object} Object containing print job settings.
351 */ 309 */
352 function getSettings() { 310 function getSettings() {
353 var deviceName = getSelectedPrinterName(); 311 var deviceName = getSelectedPrinterName();
354 var printToPDF = (deviceName == PRINT_TO_PDF); 312 var printToPDF = (deviceName == PRINT_TO_PDF);
355 313
356 var settings = 314 var settings =
357 {'deviceName': deviceName, 315 {'deviceName': deviceName,
358 'pageRange': pageSettings.selectedPageRanges, 316 'pageRange': pageSettings.selectedPageRanges,
359 'printAll': pageSettings.allPagesRadioButton.checked, 317 'printAll': pageSettings.allPagesRadioButton.checked,
360 'duplex': getDuplexMode(), 318 'duplex': copiesSettings.duplexMode,
361 'copies': copiesSettings.numberOfCopies, 319 'copies': copiesSettings.numberOfCopies,
362 'collate': isCollated(), 320 'collate': copiesSettings.isCollated(),
363 'landscape': isLandscape(), 321 'landscape': layoutSettings.isLandscape(),
364 'color': isColor(), 322 'color': isColor(),
365 'printToPDF': printToPDF, 323 'printToPDF': printToPDF,
366 'requestID': 0}; 324 'requestID': 0};
367 325
368 var printerList = $('printer-list'); 326 var printerList = $('printer-list');
369 var selectedPrinter = printerList.selectedIndex; 327 var selectedPrinter = printerList.selectedIndex;
370 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { 328 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) {
371 settings['cloudPrintID'] = 329 settings['cloudPrintID'] =
372 printerList.options[selectedPrinter].value; 330 printerList.options[selectedPrinter].value;
373 } 331 }
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 } 733 }
776 734
777 /** 735 /**
778 * Called when the PDF plugin loads its document. 736 * Called when the PDF plugin loads its document.
779 */ 737 */
780 function onPDFLoad() { 738 function onPDFLoad() {
781 $('pdf-viewer').fitToHeight(); 739 $('pdf-viewer').fitToHeight();
782 setColor($('color').checked); 740 setColor($('color').checked);
783 hideLoadingAnimation(); 741 hideLoadingAnimation();
784 742
785 if (!previewModifiable)
786 fadeOutElement($('landscape-option'));
787 cr.dispatchSimpleEvent(document, 'PDFLoaded'); 743 cr.dispatchSimpleEvent(document, 'PDFLoaded');
788 } 744 }
789 745
790 /** 746 /**
791 * Update the page count and check the page range. 747 * Update the page count and check the page range.
792 * Called from PrintPreviewUI::OnDidGetPreviewPageCount(). 748 * Called from PrintPreviewUI::OnDidGetPreviewPageCount().
793 * @param {number} pageCount The number of pages. 749 * @param {number} pageCount The number of pages.
794 */ 750 */
795 function onDidGetPreviewPageCount(pageCount) { 751 function onDidGetPreviewPageCount(pageCount) {
796 pageSettings.updateState(pageCount); 752 pageSettings.updateState(pageCount);
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 html = localStrings.getStringF('printPreviewSummaryFormatShort', 925 html = localStrings.getStringF('printPreviewSummaryFormatShort',
970 '<b>' + numOfSheets + '</b>', 926 '<b>' + numOfSheets + '</b>',
971 '<b>' + summaryLabel + '</b>'); 927 '<b>' + summaryLabel + '</b>');
972 928
973 // Removing extra spaces from within the string. 929 // Removing extra spaces from within the string.
974 html = html.replace(/\s{2,}/g, ' '); 930 html = html.replace(/\s{2,}/g, ' ');
975 printSummary.innerHTML = html; 931 printSummary.innerHTML = html;
976 } 932 }
977 933
978 /** 934 /**
979 * When the user switches printing orientation mode the page field selection is
980 * reset to "all pages selected". After the change the number of pages will be
981 * different and currently selected page numbers might no longer be valid.
982 * Even if they are still valid the content of these pages will be different.
983 */
984 function onLayoutModeToggle() {
985 // If the chosen layout is same as before, nothing needs to be done.
986 if (printSettings.isLandscape == isLandscape())
987 return;
988 setDefaultValuesAndRegeneratePreview();
989 }
990
991 /**
992 * Sets the default values and sends a request to regenerate preview data. 935 * Sets the default values and sends a request to regenerate preview data.
993 */ 936 */
994 function setDefaultValuesAndRegeneratePreview() { 937 function setDefaultValuesAndRegeneratePreview() {
995 pageSettings.resetState(); 938 pageSettings.resetState();
996 requestPrintPreview(); 939 requestPrintPreview();
997 } 940 }
998 941
999 /** 942 /**
1000 * Class that represents the state of the print settings. 943 * Class that represents the state of the print settings.
1001 */ 944 */
1002 function PrintSettings() { 945 function PrintSettings() {
1003 this.deviceName = ''; 946 this.deviceName = '';
1004 this.isLandscape = ''; 947 this.isLandscape = '';
1005 } 948 }
1006 949
1007 /** 950 /**
1008 * Takes a snapshot of the print settings. 951 * Takes a snapshot of the print settings.
1009 */ 952 */
1010 PrintSettings.prototype.save = function() { 953 PrintSettings.prototype.save = function() {
1011 this.deviceName = getSelectedPrinterName(); 954 this.deviceName = getSelectedPrinterName();
1012 this.isLandscape = isLandscape(); 955 this.isLandscape = layoutSettings.isLandscape();
1013 } 956 }
1014 957
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview/print_preview.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698