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 printSettings = new PrintSettings(); | |
| 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'), false); | 42 displayErrorMessage(localStrings.getString('noPlugin'), false); |
| 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'); | |
|
James Hawkins
2011/05/23 21:24:06
Move this var to where it's first used.
dpapad
2011/05/23 23:04:07
Done.
| |
| 60 $('print-button').onclick = printFile; | |
| 61 | |
| 62 // Controls that require preview rendering. | |
| 63 $('all-pages').onclick = onPageSelectionMayHaveChanged; | |
| 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'); | |
|
James Hawkins
2011/05/23 21:24:06
Same here.
dpapad
2011/05/23 23:04:07
Done.
| |
| 89 | |
| 90 // Controls that require preview rendering. | |
| 91 $('print-button').disabled = true; | |
| 92 $('all-pages').onclick = null; | |
| 93 $('print-pages').onclick = null; | |
| 94 individualPages.onblur = null; | |
| 95 individualPages.onfocus = null; | |
| 96 individualPages.oninput = null; | |
| 97 clearTimeout(timerId); | |
| 98 $('landscape').onclick = null; | |
| 99 $('portrait').onclick = null; | |
| 100 $('printer-list').onchange = null; | |
| 101 | |
| 102 // Controls that dont require preview rendering. | |
| 103 $('copies').oninput = null; | |
| 104 $('two-sided').onclick = null; | |
| 105 $('color').onclick = null; | |
| 106 $('bw').onclick = null; | |
| 107 $('increment').onclick = null; | |
| 108 $('decrement').onclick = null; | |
| 109 } | |
| 110 | |
| 111 /** | |
| 85 * Asks the browser to close the preview tab. | 112 * Asks the browser to close the preview tab. |
| 86 */ | 113 */ |
| 87 function handleCancelButtonClick() { | 114 function handleCancelButtonClick() { |
| 88 chrome.send('closePrintPreviewTab'); | 115 chrome.send('closePrintPreviewTab'); |
| 89 } | 116 } |
| 90 | 117 |
| 91 /** | 118 /** |
| 92 * Asks the browser to show the native print dialog for printing. | 119 * Asks the browser to show the native print dialog for printing. |
| 93 */ | 120 */ |
| 94 function showSystemDialog() { | 121 function showSystemDialog() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 | 161 |
| 135 // Regenerate the preview data based on selected printer settings. | 162 // Regenerate the preview data based on selected printer settings. |
| 136 setDefaultValuesAndRegeneratePreview(); | 163 setDefaultValuesAndRegeneratePreview(); |
| 137 } | 164 } |
| 138 | 165 |
| 139 /** | 166 /** |
| 140 * Updates the controls with printer capabilities information. | 167 * Updates the controls with printer capabilities information. |
| 141 * @param {Object} settingInfo printer setting information. | 168 * @param {Object} settingInfo printer setting information. |
| 142 */ | 169 */ |
| 143 function updateWithPrinterCapabilities(settingInfo) { | 170 function updateWithPrinterCapabilities(settingInfo) { |
| 144 printerCapabilities = settingInfo; | |
| 145 | |
| 146 if (isPreviewStillLoading) | |
| 147 return; | |
| 148 | |
| 149 var disableColorOption = settingInfo.disableColorOption; | 171 var disableColorOption = settingInfo.disableColorOption; |
| 150 var setColorAsDefault = settingInfo.setColorAsDefault; | 172 var setColorAsDefault = settingInfo.setColorAsDefault; |
| 151 var colorOption = $('color'); | 173 var colorOption = $('color'); |
| 152 var bwOption = $('bw'); | 174 var bwOption = $('bw'); |
| 153 | 175 |
| 154 if (disableColorOption) | 176 if (disableColorOption) |
| 155 $('color-options').classList.add("hidden"); | 177 $('color-options').classList.add("hidden"); |
| 156 else | 178 else |
| 157 $('color-options').classList.remove("hidden"); | 179 $('color-options').classList.remove("hidden"); |
| 158 | 180 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 * Asks the browser to print the preview PDF based on current print settings. | 335 * Asks the browser to print the preview PDF based on current print settings. |
| 314 */ | 336 */ |
| 315 function printFile() { | 337 function printFile() { |
| 316 chrome.send('print', [getSettingsJSON()]); | 338 chrome.send('print', [getSettingsJSON()]); |
| 317 } | 339 } |
| 318 | 340 |
| 319 /** | 341 /** |
| 320 * Asks the browser to generate a preview PDF based on current print settings. | 342 * Asks the browser to generate a preview PDF based on current print settings. |
| 321 */ | 343 */ |
| 322 function requestPrintPreview() { | 344 function requestPrintPreview() { |
| 323 isPreviewStillLoading = true; | 345 removeEventListeners(); |
| 324 setControlsDisabled(true); | 346 printSettings.save(); |
| 325 $('dancing-dots').classList.remove('invisible'); | 347 $('dancing-dots').classList.remove('invisible'); |
| 326 chrome.send('getPreview', [getSettingsJSON()]); | 348 chrome.send('getPreview', [getSettingsJSON()]); |
| 327 } | 349 } |
| 328 | 350 |
| 329 /** | 351 /** |
| 330 * Fill the printer list drop down. | 352 * Fill the printer list drop down. |
| 331 * Called from PrintPreviewHandler::SendPrinterList(). | 353 * Called from PrintPreviewHandler::SendPrinterList(). |
| 332 * @param {Array} printers Array of printer info objects. | 354 * @param {Array} printers Array of printer info objects. |
| 333 * @param {number} defaultPrinterIndex The index of the default printer. | 355 * @param {number} defaultPrinterIndex The index of the default printer. |
| 334 */ | 356 */ |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 pdfViewer.grayscale(!color); | 405 pdfViewer.grayscale(!color); |
| 384 } | 406 } |
| 385 | 407 |
| 386 /** | 408 /** |
| 387 * Display an error message in the center of the preview area. | 409 * Display an error message in the center of the preview area. |
| 388 * @param {string} errorMessage The error message to be displayed. | 410 * @param {string} errorMessage The error message to be displayed. |
| 389 * @param {boolean} showButton Indivates whether the "Reopen the page" button | 411 * @param {boolean} showButton Indivates whether the "Reopen the page" button |
| 390 * should be displayed. | 412 * should be displayed. |
| 391 */ | 413 */ |
| 392 function displayErrorMessage(errorMessage, showButton) { | 414 function displayErrorMessage(errorMessage, showButton) { |
| 393 isPreviewStillLoading = false; | |
| 394 $('dancing-dots').classList.remove('invisible'); | 415 $('dancing-dots').classList.remove('invisible'); |
| 395 $('dancing-dots-text').classList.add('hidden'); | 416 $('dancing-dots-text').classList.add('hidden'); |
| 396 $('error-text').innerHTML = errorMessage; | 417 $('error-text').innerHTML = errorMessage; |
| 397 $('error-text').classList.remove('hidden'); | 418 $('error-text').classList.remove('hidden'); |
| 398 if (showButton) | 419 if (showButton) |
| 399 $('reopen-page').classList.remove('hidden'); | 420 $('reopen-page').classList.remove('hidden'); |
| 400 else | 421 else |
| 401 $('reopen-page').classList.add('hidden'); | 422 $('reopen-page').classList.add('hidden'); |
| 402 | 423 |
| 403 setControlsDisabled(true); | 424 setControlsDisabled(true); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 418 /** | 439 /** |
| 419 * Called when the PDF plugin loads its document. | 440 * Called when the PDF plugin loads its document. |
| 420 */ | 441 */ |
| 421 function onPDFLoad() { | 442 function onPDFLoad() { |
| 422 if (isLandscape()) | 443 if (isLandscape()) |
| 423 $('pdf-viewer').fitToWidth(); | 444 $('pdf-viewer').fitToWidth(); |
| 424 else | 445 else |
| 425 $('pdf-viewer').fitToHeight(); | 446 $('pdf-viewer').fitToHeight(); |
| 426 | 447 |
| 427 $('dancing-dots').classList.add('invisible'); | 448 $('dancing-dots').classList.add('invisible'); |
| 428 setControlsDisabled(false); | |
| 429 | 449 |
| 430 if (!previewModifiable) { | 450 if (!previewModifiable) { |
| 431 $('landscape').disabled = true; | 451 $('landscape').disabled = true; |
| 432 $('portrait').disabled = true; | 452 $('portrait').disabled = true; |
| 433 } | 453 } |
| 434 | 454 |
| 435 updateCopiesButtonsState(); | 455 updateCopiesButtonsState(); |
| 436 updateWithPrinterCapabilities(printerCapabilities); | |
| 437 } | 456 } |
| 438 | 457 |
| 439 /** | 458 /** |
| 440 * Update the print preview when new preview data is available. | 459 * Update the print preview when new preview data is available. |
| 441 * Create the PDF plugin as needed. | 460 * Create the PDF plugin as needed. |
| 442 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 461 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
| 443 * @param {number} pageCount The expected total pages count. | 462 * @param {number} pageCount The expected total pages count. |
| 444 * @param {string} jobTitle The print job title. | 463 * @param {string} jobTitle The print job title. |
| 445 * @param {boolean} modifiable If the preview is modifiable. | 464 * @param {boolean} modifiable If the preview is modifiable. |
| 446 * | 465 * |
| 447 */ | 466 */ |
| 448 function updatePrintPreview(pageCount, jobTitle, modifiable) { | 467 function updatePrintPreview(pageCount, jobTitle, modifiable) { |
| 468 var tempPrintSettings = new PrintSettings(); | |
| 469 tempPrintSettings.save(); | |
| 470 | |
| 449 previewModifiable = modifiable; | 471 previewModifiable = modifiable; |
| 450 | 472 |
| 451 if (totalPageCount == -1) | 473 if (totalPageCount == -1) |
| 452 totalPageCount = pageCount; | 474 totalPageCount = pageCount; |
| 453 | 475 |
| 454 if (previouslySelectedPages.length == 0) | 476 if (previouslySelectedPages.length == 0) |
| 455 for (var i = 0; i < totalPageCount; i++) | 477 for (var i = 0; i < totalPageCount; i++) |
| 456 previouslySelectedPages.push(i+1); | 478 previouslySelectedPages.push(i+1); |
| 457 | 479 |
| 458 if (previouslySelectedLayout == null) | 480 if (printSettings.deviceName != tempPrintSettings.deviceName) { |
| 459 previouslySelectedLayout = $('portrait'); | 481 updateControlsWithSelectedPrinterCapabilities(); |
| 482 return; | |
| 483 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { | |
| 484 requestPrintPreview(); | |
| 485 return; | |
| 486 } else if (getSelectedPagesValidityLevel() == 1) { | |
| 487 var currentlySelectedPages = getSelectedPagesSet(); | |
| 488 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { | |
| 489 previouslySelectedPages = currentlySelectedPages; | |
| 490 requestPrintPreview(); | |
| 491 return; | |
| 492 } | |
| 493 } | |
| 494 | |
| 495 if (getSelectedPagesValidityLevel() != 1) | |
| 496 pageRangesFieldChanged(); | |
|
dpapad
2011/05/23 21:17:06
I think it is more readable now.
| |
| 460 | 497 |
| 461 // Update the current tab title. | 498 // Update the current tab title. |
| 462 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 499 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
| 463 | 500 |
| 464 createPDFPlugin(); | 501 createPDFPlugin(); |
| 465 isPreviewStillLoading = false; | |
| 466 updatePrintSummary(); | 502 updatePrintSummary(); |
| 503 addEventListeners(); | |
| 467 } | 504 } |
| 468 | 505 |
| 469 /** | 506 /** |
| 470 * Create the PDF plugin or reload the existing one. | 507 * Create the PDF plugin or reload the existing one. |
| 471 */ | 508 */ |
| 472 function createPDFPlugin() { | 509 function createPDFPlugin() { |
| 473 // Enable the print button. | 510 // Enable the print button. |
| 474 if (!$('printer-list').disabled) { | 511 if (!$('printer-list').disabled) { |
| 475 $('print-button').disabled = false; | 512 $('print-button').disabled = false; |
| 476 } | 513 } |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 666 $('individual-pages').focus(); | 703 $('individual-pages').focus(); |
| 667 } | 704 } |
| 668 | 705 |
| 669 /** | 706 /** |
| 670 * When the user switches printing orientation mode the page field selection is | 707 * When the user switches printing orientation mode the page field selection is |
| 671 * reset to "all pages selected". After the change the number of pages will be | 708 * reset to "all pages selected". After the change the number of pages will be |
| 672 * different and currently selected page numbers might no longer be valid. | 709 * different and currently selected page numbers might no longer be valid. |
| 673 * Even if they are still valid the content of these pages will be different. | 710 * Even if they are still valid the content of these pages will be different. |
| 674 */ | 711 */ |
| 675 function onLayoutModeToggle() { | 712 function onLayoutModeToggle() { |
| 676 var currentlySelectedLayout = $('portrait').checked ? $('portrait') : | |
| 677 $('landscape'); | |
| 678 | |
| 679 // If the chosen layout is same as before, nothing needs to be done. | 713 // If the chosen layout is same as before, nothing needs to be done. |
| 680 if (previouslySelectedLayout == currentlySelectedLayout) | 714 if (printSettings.isLandscape == isLandscape()) |
| 681 return; | 715 return; |
| 682 | 716 |
| 683 previouslySelectedLayout = currentlySelectedLayout; | |
| 684 $('individual-pages').classList.remove('invalid'); | 717 $('individual-pages').classList.remove('invalid'); |
| 685 setDefaultValuesAndRegeneratePreview(); | 718 setDefaultValuesAndRegeneratePreview(); |
| 686 } | 719 } |
| 687 | 720 |
| 688 /** | 721 /** |
| 689 * Sets the default values and sends a request to regenerate preview data. | 722 * Sets the default values and sends a request to regenerate preview data. |
| 690 */ | 723 */ |
| 691 function setDefaultValuesAndRegeneratePreview() { | 724 function setDefaultValuesAndRegeneratePreview() { |
| 692 $('individual-pages').value = ''; | 725 $('individual-pages').value = ''; |
| 693 hideInvalidHint($('individual-pages-hint')); | 726 hideInvalidHint($('individual-pages-hint')); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 901 copiesField.value = 1; | 934 copiesField.value = 1; |
| 902 else { | 935 else { |
| 903 var newValue = getCopies() + sign * 1; | 936 var newValue = getCopies() + sign * 1; |
| 904 if (newValue < copiesField.min || newValue > copiesField.max) | 937 if (newValue < copiesField.min || newValue > copiesField.max) |
| 905 return; | 938 return; |
| 906 copiesField.value = newValue; | 939 copiesField.value = newValue; |
| 907 } | 940 } |
| 908 copiesFieldChanged(); | 941 copiesFieldChanged(); |
| 909 } | 942 } |
| 910 | 943 |
| 944 /** | |
| 945 * Class that represents the state of the print settings. | |
| 946 */ | |
| 947 function PrintSettings() { | |
| 948 this.deviceName = ''; | |
| 949 this.isLandscape = ''; | |
| 950 } | |
| 951 | |
| 952 /** | |
| 953 * Takes a snapshot of the print settings. | |
| 954 */ | |
| 955 PrintSettings.prototype.save = function() { | |
| 956 var printerList = $('printer-list') | |
| 957 var selectedPrinter = printerList.selectedIndex; | |
| 958 if (selectedPrinter >= 0) | |
| 959 this.deviceName = printerList.options[selectedPrinter].value; | |
| 960 else | |
| 961 this.deviceName = ''; | |
| 962 this.isLandscape = isLandscape(); | |
| 963 } | |
| OLD | NEW |