Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 var localStrings = new LocalStrings(); | 5 var localStrings = new LocalStrings(); |
| 6 | 6 |
| 7 // The total page count of the previewed document regardless of which pages the | 7 // The total page count of the previewed document regardless of which pages the |
| 8 // user has selected. | 8 // user has selected. |
| 9 var totalPageCount = -1; | 9 var totalPageCount = -1; |
| 10 | 10 |
| 11 // The previously selected pages by the user. It is used in | 11 // The previously selected pages by the user. It is used in |
| 12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not | 12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not |
| 13 // requested more often than necessary. | 13 // requested more often than necessary. |
| 14 var previouslySelectedPages = []; | 14 var previouslySelectedPages = []; |
| 15 | 15 |
| 16 // The previously selected layout mode. It is used in order to prevent the | |
| 17 // preview from updating when the user clicks on the already selected layout | |
| 18 // mode. | |
| 19 var previouslySelectedLayout = null; | |
| 20 | |
| 21 // Timer id of the page range textfield. It is used to reset the timer whenever | 16 // Timer id of the page range textfield. It is used to reset the timer whenever |
| 22 // needed. | 17 // needed. |
| 23 var timerId; | 18 var timerId; |
| 24 | 19 |
| 25 // Store the last selected printer index. | 20 // Store the last selected printer index. |
| 26 var lastSelectedPrinterIndex = 0; | 21 var lastSelectedPrinterIndex = 0; |
| 27 | 22 |
| 28 // Indicates whether a preview has been requested but not received yet. | |
| 29 var isPreviewStillLoading = true; | |
| 30 | |
| 31 // Currently selected printer capabilities. | |
| 32 var printerCapabilities; | |
| 33 | |
| 34 // Used to disable some printing options when the preview is not modifiable. | 23 // Used to disable some printing options when the preview is not modifiable. |
| 35 var previewModifiable = false; | 24 var previewModifiable = false; |
| 36 | 25 |
| 37 // Destination list special value constants. | 26 // Destination list special value constants. |
| 38 const PRINT_TO_PDF = 'Print To PDF'; | 27 const PRINT_TO_PDF = 'Print To PDF'; |
| 39 const MANAGE_PRINTERS = 'Manage Printers'; | 28 const MANAGE_PRINTERS = 'Manage Printers'; |
| 40 | 29 |
| 30 // State of the print preview settings. | |
| 31 var settingsState = new SettingsState(); | |
| 32 | |
| 41 /** | 33 /** |
| 42 * Window onload handler, sets up the page and starts print preview by getting | 34 * Window onload handler, sets up the page and starts print preview by getting |
| 43 * the printer list. | 35 * the printer list. |
| 44 */ | 36 */ |
| 45 function onLoad() { | 37 function onLoad() { |
| 46 $('system-dialog-link').addEventListener('click', showSystemDialog); | 38 $('system-dialog-link').addEventListener('click', showSystemDialog); |
| 47 $('cancel-button').addEventListener('click', handleCancelButtonClick); | 39 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
| 48 | 40 |
| 49 if (!checkCompatiblePluginExists()) { | 41 if (!checkCompatiblePluginExists()) { |
| 50 displayErrorMessage(localStrings.getString('noPlugin')); | 42 displayErrorMessage(localStrings.getString('noPlugin')); |
| 51 $('mainview').parentElement.removeChild($('dummy-viewer')); | 43 $('mainview').parentElement.removeChild($('dummy-viewer')); |
| 52 return; | 44 return; |
| 53 } | 45 } |
| 54 $('mainview').parentElement.removeChild($('dummy-viewer')); | 46 $('mainview').parentElement.removeChild($('dummy-viewer')); |
| 55 | 47 |
| 56 $('printer-list').disabled = true; | 48 $('printer-list').disabled = true; |
| 57 $('print-button').disabled = true; | 49 $('print-button').disabled = true; |
| 58 $('print-button').addEventListener('click', printFile); | |
| 59 $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged); | |
| 60 $('copies').addEventListener('input', copiesFieldChanged); | |
| 61 $('print-pages').addEventListener('click', handleIndividualPagesCheckbox); | |
| 62 $('individual-pages').addEventListener('blur', function() { | |
| 63 clearTimeout(timerId); | |
| 64 onPageSelectionMayHaveChanged(); | |
| 65 }); | |
| 66 $('individual-pages').addEventListener('focus', addTimerToPageRangeField); | |
| 67 $('individual-pages').addEventListener('input', resetPageRangeFieldTimer); | |
| 68 $('two-sided').addEventListener('click', handleTwoSidedClick) | |
| 69 $('landscape').addEventListener('click', onLayoutModeToggle); | |
| 70 $('portrait').addEventListener('click', onLayoutModeToggle); | |
| 71 $('color').addEventListener('click', function() { setColor(true); }); | |
| 72 $('bw').addEventListener('click', function() { setColor(false); }); | |
| 73 $('printer-list').addEventListener( | |
| 74 'change', updateControlsWithSelectedPrinterCapabilities); | |
| 75 $('increment').addEventListener('click', | |
| 76 function() { onCopiesButtonsClicked(1); }); | |
| 77 $('decrement').addEventListener('click', | |
| 78 function() { onCopiesButtonsClicked(-1); }); | |
| 79 $('controls').onsubmit = function() { return false; }; | 50 $('controls').onsubmit = function() { return false; }; |
| 80 $('dancing-dots').classList.remove('invisible'); | 51 $('dancing-dots').classList.remove('invisible'); |
| 81 chrome.send('getPrinters'); | 52 chrome.send('getPrinters'); |
| 82 } | 53 } |
| 83 | 54 |
| 84 /** | 55 /** |
| 56 * Adds event listeners to the settings controls. | |
| 57 */ | |
| 58 function addEventListeners() { | |
| 59 var individualPages = $('individual-pages'); | |
| 60 $('print-button').onclick = printFile; | |
| 61 $('all-pages').onclick = onPageSelectionMayHaveChanged; | |
| 62 | |
| 63 // Controls that require preview rendering. | |
| 64 $('print-pages').onclick = handleIndividualPagesCheckbox; | |
| 65 individualPages.onblur = function() { | |
| 66 clearTimeout(timerId); | |
| 67 onPageSelectionMayHaveChanged(); | |
| 68 }; | |
| 69 individualPages.onfocus = addTimerToPageRangeField; | |
| 70 individualPages.oninput = resetPageRangeFieldTimer; | |
| 71 $('landscape').onclick = onLayoutModeToggle; | |
| 72 $('portrait').onclick = onLayoutModeToggle; | |
| 73 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | |
| 74 | |
| 75 // Controls that dont require preview rendering. | |
| 76 $('copies').oninput = copiesFieldChanged; | |
| 77 $('two-sided').onclick = handleTwoSidedClick; | |
| 78 $('color').onclick = function() { setColor(true); }; | |
| 79 $('bw').onclick = function() { setColor(false); }; | |
| 80 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; | |
| 81 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; | |
| 82 } | |
| 83 | |
| 84 /** | |
| 85 * Removes event listeners from the settings controls. | |
| 86 */ | |
| 87 function removeEventListeners() { | |
| 88 var individualPages = $('individual-pages'); | |
| 89 | |
| 90 // Controls that require preview rendering. | |
| 91 $('print-pages').onclick = null; | |
| 92 individualPages.onblur = null; | |
| 93 individualPages.onfocus = null; | |
| 94 individualPages.oninput = null; | |
| 95 clearTimeout(timerId); | |
| 96 $('landscape').onclick = null; | |
| 97 $('portrait').onclick = null; | |
| 98 $('printer-list').onchange = null; | |
| 99 | |
| 100 // Controls that dont require preview rendering. | |
| 101 $('copies').oninput = null; | |
| 102 $('two-sided').onclick = null; | |
| 103 $('color').onclick = null; | |
| 104 $('bw').onclick = null; | |
| 105 $('increment').onclick = null; | |
| 106 $('decrement').onclick = null; | |
| 107 } | |
| 108 | |
| 109 /** | |
| 85 * Asks the browser to close the preview tab. | 110 * Asks the browser to close the preview tab. |
| 86 */ | 111 */ |
| 87 function handleCancelButtonClick() { | 112 function handleCancelButtonClick() { |
| 88 chrome.send('closePrintPreviewTab'); | 113 chrome.send('closePrintPreviewTab'); |
| 89 } | 114 } |
| 90 | 115 |
| 91 /** | 116 /** |
| 92 * Asks the browser to show the native print dialog for printing. | 117 * Asks the browser to show the native print dialog for printing. |
| 93 */ | 118 */ |
| 94 function showSystemDialog() { | 119 function showSystemDialog() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 | 157 |
| 133 // Regenerate the preview data based on selected printer settings. | 158 // Regenerate the preview data based on selected printer settings. |
| 134 setDefaultValuesAndRegeneratePreview(); | 159 setDefaultValuesAndRegeneratePreview(); |
| 135 } | 160 } |
| 136 | 161 |
| 137 /** | 162 /** |
| 138 * Updates the controls with printer capabilities information. | 163 * Updates the controls with printer capabilities information. |
| 139 * @param {Object} settingInfo printer setting information. | 164 * @param {Object} settingInfo printer setting information. |
| 140 */ | 165 */ |
| 141 function updateWithPrinterCapabilities(settingInfo) { | 166 function updateWithPrinterCapabilities(settingInfo) { |
| 142 printerCapabilities = settingInfo; | |
| 143 | |
| 144 if (isPreviewStillLoading) | |
| 145 return; | |
| 146 | |
| 147 var disableColorOption = settingInfo.disableColorOption; | 167 var disableColorOption = settingInfo.disableColorOption; |
| 148 var setColorAsDefault = settingInfo.setColorAsDefault; | 168 var setColorAsDefault = settingInfo.setColorAsDefault; |
| 149 var colorOption = $('color'); | 169 var colorOption = $('color'); |
| 150 var bwOption = $('bw'); | 170 var bwOption = $('bw'); |
| 151 | 171 |
| 152 if (disableColorOption) | 172 if (disableColorOption) |
| 153 $('color-options').classList.add("hidden"); | 173 $('color-options').classList.add("hidden"); |
| 154 else | 174 else |
| 155 $('color-options').classList.remove("hidden"); | 175 $('color-options').classList.remove("hidden"); |
| 156 | 176 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 * Asks the browser to print the preview PDF based on current print settings. | 331 * Asks the browser to print the preview PDF based on current print settings. |
| 312 */ | 332 */ |
| 313 function printFile() { | 333 function printFile() { |
| 314 chrome.send('print', [getSettingsJSON()]); | 334 chrome.send('print', [getSettingsJSON()]); |
| 315 } | 335 } |
| 316 | 336 |
| 317 /** | 337 /** |
| 318 * Asks the browser to generate a preview PDF based on current print settings. | 338 * Asks the browser to generate a preview PDF based on current print settings. |
| 319 */ | 339 */ |
| 320 function requestPrintPreview() { | 340 function requestPrintPreview() { |
| 321 isPreviewStillLoading = true; | 341 removeEventListeners(); |
| 322 setControlsDisabled(true); | 342 settingsState.save(); |
| 323 $('dancing-dots').classList.remove('invisible'); | 343 $('dancing-dots').classList.remove('invisible'); |
| 324 chrome.send('getPreview', [getSettingsJSON()]); | 344 chrome.send('getPreview', [getSettingsJSON()]); |
| 325 } | 345 } |
| 326 | 346 |
| 327 /** | 347 /** |
| 328 * Fill the printer list drop down. | 348 * Fill the printer list drop down. |
| 329 * Called from PrintPreviewHandler::SendPrinterList(). | 349 * Called from PrintPreviewHandler::SendPrinterList(). |
| 330 * @param {Array} printers Array of printer info objects. | 350 * @param {Array} printers Array of printer info objects. |
| 331 * @param {number} defaultPrinterIndex The index of the default printer. | 351 * @param {number} defaultPrinterIndex The index of the default printer. |
| 332 */ | 352 */ |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 return; | 399 return; |
| 380 } | 400 } |
| 381 pdfViewer.grayscale(!color); | 401 pdfViewer.grayscale(!color); |
| 382 } | 402 } |
| 383 | 403 |
| 384 /** | 404 /** |
| 385 * Display an error message in the center of the preview area. | 405 * Display an error message in the center of the preview area. |
| 386 * @param (string) errorMessage The error message to be displayed. | 406 * @param (string) errorMessage The error message to be displayed. |
| 387 */ | 407 */ |
| 388 function displayErrorMessage(errorMessage) { | 408 function displayErrorMessage(errorMessage) { |
| 389 isPreviewStillLoading = false; | |
| 390 $('dancing-dots').classList.remove('invisible'); | 409 $('dancing-dots').classList.remove('invisible'); |
| 391 $('dancing-dots-text').classList.add('hidden'); | 410 $('dancing-dots-text').classList.add('hidden'); |
| 392 $('error-text').innerHTML = errorMessage; | 411 $('error-text').innerHTML = errorMessage; |
| 393 $('error-text').classList.remove('hidden'); | 412 $('error-text').classList.remove('hidden'); |
| 394 setControlsDisabled(true); | 413 setControlsDisabled(true); |
| 395 | 414 |
| 396 var pdfViewer = $('pdf-viewer'); | 415 var pdfViewer = $('pdf-viewer'); |
| 397 if (pdfViewer) | 416 if (pdfViewer) |
| 398 $('mainview').removeChild(pdfViewer); | 417 $('mainview').removeChild(pdfViewer); |
| 399 } | 418 } |
| 400 | 419 |
| 401 /** | 420 /** |
| 402 * Display an error message when print preview fails. | 421 * Display an error message when print preview fails. |
| 403 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed(). | 422 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed(). |
| 404 */ | 423 */ |
| 405 function printPreviewFailed() { | 424 function printPreviewFailed() { |
| 406 displayErrorMessage(localStrings.getString('previewFailed')); | 425 displayErrorMessage(localStrings.getString('previewFailed')); |
| 407 } | 426 } |
| 408 | 427 |
| 409 /** | 428 /** |
| 410 * Called when the PDF plugin loads its document. | 429 * Called when the PDF plugin loads its document. |
| 411 */ | 430 */ |
| 412 function onPDFLoad() { | 431 function onPDFLoad() { |
| 413 if (isLandscape()) | 432 if (isLandscape()) |
| 414 $('pdf-viewer').fitToWidth(); | 433 $('pdf-viewer').fitToWidth(); |
| 415 else | 434 else |
| 416 $('pdf-viewer').fitToHeight(); | 435 $('pdf-viewer').fitToHeight(); |
| 417 | 436 |
| 418 $('dancing-dots').classList.add('invisible'); | 437 $('dancing-dots').classList.add('invisible'); |
| 419 setControlsDisabled(false); | |
| 420 | 438 |
| 421 if (!previewModifiable) { | 439 if (!previewModifiable) { |
| 422 $('landscape').disabled = true; | 440 $('landscape').disabled = true; |
| 423 $('portrait').disabled = true; | 441 $('portrait').disabled = true; |
| 424 } | 442 } |
| 425 | 443 |
| 426 updateCopiesButtonsState(); | 444 updateCopiesButtonsState(); |
| 427 updateWithPrinterCapabilities(printerCapabilities); | |
| 428 } | 445 } |
| 429 | 446 |
| 430 /** | 447 /** |
| 431 * Update the print preview when new preview data is available. | 448 * Update the print preview when new preview data is available. |
| 432 * Create the PDF plugin as needed. | 449 * Create the PDF plugin as needed. |
| 433 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 450 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
| 434 * @param {number} pageCount The expected total pages count. | 451 * @param {number} pageCount The expected total pages count. |
| 435 * @param {string} jobTitle The print job title. | 452 * @param {string} jobTitle The print job title. |
| 436 * @param {boolean} modifiable If the preview is modifiable. | 453 * @param {boolean} modifiable If the preview is modifiable. |
| 437 * | 454 * |
| 438 */ | 455 */ |
| 439 function updatePrintPreview(pageCount, jobTitle, modifiable) { | 456 function updatePrintPreview(pageCount, jobTitle, modifiable) { |
| 457 var tempSettingsState = new SettingsState(); | |
| 458 tempSettingsState.save(); | |
| 459 | |
| 440 previewModifiable = modifiable; | 460 previewModifiable = modifiable; |
| 441 | 461 |
| 442 if (totalPageCount == -1) | 462 if (totalPageCount == -1) |
| 443 totalPageCount = pageCount; | 463 totalPageCount = pageCount; |
| 444 | 464 |
| 445 if (previouslySelectedPages.length == 0) | 465 if (previouslySelectedPages.length == 0) |
| 446 for (var i = 0; i < totalPageCount; i++) | 466 for (var i = 0; i < totalPageCount; i++) |
| 447 previouslySelectedPages.push(i+1); | 467 previouslySelectedPages.push(i+1); |
| 448 | 468 |
| 449 if (previouslySelectedLayout == null) | 469 if (settingsState.deviceName != tempSettingsState.deviceName) { |
| 450 previouslySelectedLayout = $('portrait'); | 470 updateControlsWithSelectedPrinterCapabilities(); |
| 471 return; | |
|
James Hawkins
2011/05/23 20:49:45
You have returns on three of these cases, but not
dpapad
2011/05/23 21:05:50
This is intentional though. In case the page range
James Hawkins
2011/05/23 21:12:30
Ah sorry, thought the code below was in a separate
| |
| 472 } else if (settingsState.isLandscape != tempSettingsState.isLandscape) { | |
| 473 requestPrintPreview(); | |
| 474 return; | |
| 475 } else if (getSelectedPagesValidityLevel() != 1) { | |
| 476 pageRangesFieldChanged(); | |
| 477 } else if (getSelectedPagesValidityLevel() == 1) { | |
| 478 var currentlySelectedPages = getSelectedPagesSet(); | |
| 479 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { | |
| 480 previouslySelectedPages = currentlySelectedPages; | |
| 481 requestPrintPreview(); | |
| 482 return; | |
| 483 } | |
| 484 } | |
| 451 | 485 |
| 452 // Update the current tab title. | 486 // Update the current tab title. |
| 453 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 487 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
| 454 | 488 |
| 455 createPDFPlugin(); | 489 createPDFPlugin(); |
| 456 isPreviewStillLoading = false; | |
| 457 updatePrintSummary(); | 490 updatePrintSummary(); |
| 491 addEventListeners(); | |
| 458 } | 492 } |
| 459 | 493 |
| 460 /** | 494 /** |
| 461 * Create the PDF plugin or reload the existing one. | 495 * Create the PDF plugin or reload the existing one. |
| 462 */ | 496 */ |
| 463 function createPDFPlugin() { | 497 function createPDFPlugin() { |
| 464 // Enable the print button. | 498 // Enable the print button. |
| 465 if (!$('printer-list').disabled) { | 499 if (!$('printer-list').disabled) { |
| 466 $('print-button').disabled = false; | 500 $('print-button').disabled = false; |
| 467 } | 501 } |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 657 $('individual-pages').focus(); | 691 $('individual-pages').focus(); |
| 658 } | 692 } |
| 659 | 693 |
| 660 /** | 694 /** |
| 661 * When the user switches printing orientation mode the page field selection is | 695 * When the user switches printing orientation mode the page field selection is |
| 662 * reset to "all pages selected". After the change the number of pages will be | 696 * reset to "all pages selected". After the change the number of pages will be |
| 663 * different and currently selected page numbers might no longer be valid. | 697 * different and currently selected page numbers might no longer be valid. |
| 664 * Even if they are still valid the content of these pages will be different. | 698 * Even if they are still valid the content of these pages will be different. |
| 665 */ | 699 */ |
| 666 function onLayoutModeToggle() { | 700 function onLayoutModeToggle() { |
| 667 var currentlySelectedLayout = $('portrait').checked ? $('portrait') : | |
| 668 $('landscape'); | |
| 669 | |
| 670 // If the chosen layout is same as before, nothing needs to be done. | 701 // If the chosen layout is same as before, nothing needs to be done. |
| 671 if (previouslySelectedLayout == currentlySelectedLayout) | 702 if (settingsState.isLandscape == isLandscape()) |
| 672 return; | 703 return; |
| 673 | 704 |
| 674 previouslySelectedLayout = currentlySelectedLayout; | |
| 675 $('individual-pages').classList.remove('invalid'); | 705 $('individual-pages').classList.remove('invalid'); |
| 676 setDefaultValuesAndRegeneratePreview(); | 706 setDefaultValuesAndRegeneratePreview(); |
| 677 } | 707 } |
| 678 | 708 |
| 679 /** | 709 /** |
| 680 * Sets the default values and sends a request to regenerate preview data. | 710 * Sets the default values and sends a request to regenerate preview data. |
| 681 */ | 711 */ |
| 682 function setDefaultValuesAndRegeneratePreview() { | 712 function setDefaultValuesAndRegeneratePreview() { |
| 683 $('individual-pages').value = ''; | 713 $('individual-pages').value = ''; |
| 684 hideInvalidHint($('individual-pages-hint')); | 714 hideInvalidHint($('individual-pages-hint')); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 892 copiesField.value = 1; | 922 copiesField.value = 1; |
| 893 else { | 923 else { |
| 894 var newValue = getCopies() + sign * 1; | 924 var newValue = getCopies() + sign * 1; |
| 895 if (newValue < copiesField.min || newValue > copiesField.max) | 925 if (newValue < copiesField.min || newValue > copiesField.max) |
| 896 return; | 926 return; |
| 897 copiesField.value = newValue; | 927 copiesField.value = newValue; |
| 898 } | 928 } |
| 899 copiesFieldChanged(); | 929 copiesFieldChanged(); |
| 900 } | 930 } |
| 901 | 931 |
| 932 /** | |
| 933 * Class that represents the state of the print settings. | |
| 934 */ | |
| 935 function SettingsState() { | |
|
James Hawkins
2011/05/23 20:49:45
s/SettingsState/PrintSettings/
SettingsState is t
dpapad
2011/05/23 21:05:50
Done.
| |
| 936 this.deviceName = ''; | |
| 937 this.isLandscape = ''; | |
| 938 } | |
| 939 | |
| 940 /** | |
| 941 * Takes a snapshot of the print settings. | |
| 942 */ | |
| 943 SettingsState.prototype.save = function() { | |
| 944 var printerList = $('printer-list') | |
| 945 var selectedPrinter = printerList.selectedIndex; | |
| 946 if (selectedPrinter >= 0) | |
| 947 this.deviceName = printerList.options[selectedPrinter].value; | |
| 948 else | |
| 949 this.deviceName = ''; | |
| 950 this.isLandscape = isLandscape(); | |
| 951 } | |
| OLD | NEW |