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 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 // 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. |
| 24 var previewModifiable = false; | 24 var previewModifiable = false; |
| 25 | 25 |
| 26 // Destination list special value constants. | 26 // Destination list special value constants. |
| 27 const PRINT_TO_PDF = 'Print To PDF'; | 27 const PRINT_TO_PDF = 'Print To PDF'; |
| 28 const MANAGE_PRINTERS = 'Manage Printers'; | 28 const MANAGE_PRINTERS = 'Manage Printers'; |
| 29 | 29 |
| 30 // State of the print preview settings. | 30 // State of the print preview settings. |
| 31 var printSettings = new PrintSettings(); | 31 var printSettings = new PrintSettings(); |
| 32 | 32 |
| 33 // Total number of print preview requests submitted. | |
| 34 var previewRequestCount = 0; | |
| 35 | |
| 36 // True when a pending print file request exists. | |
| 37 var hasPendingPrintFileRequest = false; | |
| 38 | |
| 33 /** | 39 /** |
| 34 * Window onload handler, sets up the page and starts print preview by getting | 40 * Window onload handler, sets up the page and starts print preview by getting |
| 35 * the printer list. | 41 * the printer list. |
| 36 */ | 42 */ |
| 37 function onLoad() { | 43 function onLoad() { |
| 38 $('system-dialog-link').addEventListener('click', showSystemDialog); | |
| 39 $('cancel-button').addEventListener('click', handleCancelButtonClick); | 44 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
| 40 | 45 |
| 41 if (!checkCompatiblePluginExists()) { | 46 if (!checkCompatiblePluginExists()) { |
| 42 displayErrorMessage(localStrings.getString('noPlugin'), false); | 47 displayErrorMessage(localStrings.getString('noPlugin'), false); |
| 43 $('mainview').parentElement.removeChild($('dummy-viewer')); | 48 $('mainview').parentElement.removeChild($('dummy-viewer')); |
| 49 $('print-button').addEventListener('click', showSystemDialog); | |
| 44 return; | 50 return; |
| 45 } | 51 } |
| 52 $('system-dialog-link').addEventListener('click', showSystemDialog); | |
|
dpapad
2011/06/06 18:07:35
Why move this line? If the there is no compatible
kmadhusu
2011/06/06 18:44:08
Print button is now enabled. In line #49, I have a
| |
| 46 $('mainview').parentElement.removeChild($('dummy-viewer')); | 53 $('mainview').parentElement.removeChild($('dummy-viewer')); |
| 47 | 54 |
| 48 $('printer-list').disabled = true; | 55 $('printer-list').disabled = true; |
| 49 $('print-button').disabled = true; | 56 $('print-button').onclick = printFile; |
| 57 | |
| 58 addEventListenersToPagesControls(); | |
| 59 addEventListenersToCopiesControl(); | |
| 50 showLoadingAnimation(); | 60 showLoadingAnimation(); |
| 51 chrome.send('getDefaultPrinter'); | 61 chrome.send('getDefaultPrinter'); |
| 52 } | 62 } |
| 53 | 63 |
| 54 /** | 64 /** |
| 55 * Adds event listeners to the settings controls. | 65 * Adds event listeners to the pages section controls. |
| 56 */ | 66 */ |
| 57 function addEventListeners() { | 67 function addEventListenersToPagesControls() { |
| 58 $('print-button').onclick = printFile; | |
| 59 | |
| 60 // Controls that require preview rendering. | |
| 61 $('all-pages').onclick = onPageSelectionMayHaveChanged; | 68 $('all-pages').onclick = onPageSelectionMayHaveChanged; |
| 62 $('print-pages').onclick = handleIndividualPagesCheckbox; | 69 $('print-pages').onclick = handleIndividualPagesCheckbox; |
| 63 var individualPages = $('individual-pages'); | 70 var individualPages = $('individual-pages'); |
| 64 individualPages.onblur = function() { | 71 individualPages.onblur = function() { |
| 65 clearTimeout(timerId); | 72 clearTimeout(timerId); |
| 66 onPageSelectionMayHaveChanged(); | 73 onPageSelectionMayHaveChanged(); |
| 67 }; | 74 }; |
| 68 individualPages.onfocus = addTimerToPageRangeField; | 75 individualPages.onfocus = addTimerToPageRangeField; |
| 69 individualPages.oninput = resetPageRangeFieldTimer; | 76 individualPages.oninput = resetPageRangeFieldTimer; |
| 70 $('landscape').onclick = onLayoutModeToggle; | 77 } |
| 71 $('portrait').onclick = onLayoutModeToggle; | |
| 72 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | |
| 73 | 78 |
| 74 // Controls that dont require preview rendering. | 79 /** |
| 80 * Adds event listeners to the copies controls. | |
| 81 */ | |
| 82 function addEventListenersToCopiesControl() { | |
|
dpapad
2011/06/06 18:07:35
rename to addEventListenersToCopiesControls for co
kmadhusu
2011/06/06 18:44:08
Done.
| |
| 75 $('copies').oninput = function() { | 83 $('copies').oninput = function() { |
| 76 copiesFieldChanged(); | 84 copiesFieldChanged(); |
| 77 updatePrintButtonState(); | 85 updatePrintButtonState(); |
| 78 updatePrintSummary(); | 86 updatePrintSummary(); |
| 79 }; | 87 }; |
| 80 $('two-sided').onclick = handleTwoSidedClick; | |
| 81 $('color').onclick = function() { setColor(true); }; | |
| 82 $('bw').onclick = function() { setColor(false); }; | |
| 83 $('increment').onclick = function() { | 88 $('increment').onclick = function() { |
| 84 onCopiesButtonsClicked(1); | 89 onCopiesButtonsClicked(1); |
| 85 updatePrintButtonState(); | 90 updatePrintButtonState(); |
| 86 updatePrintSummary(); | 91 updatePrintSummary(); |
| 87 }; | 92 }; |
| 88 $('decrement').onclick = function() { | 93 $('decrement').onclick = function() { |
| 89 onCopiesButtonsClicked(-1); | 94 onCopiesButtonsClicked(-1); |
| 90 updatePrintButtonState(); | 95 updatePrintButtonState(); |
| 91 updatePrintSummary(); | 96 updatePrintSummary(); |
| 92 }; | 97 }; |
| 93 } | 98 } |
| 94 | 99 |
| 95 /** | 100 /** |
| 101 * Adds event listeners to the settings controls. | |
| 102 */ | |
| 103 function addEventListeners() { | |
| 104 // Controls that require preview rendering. | |
| 105 $('landscape').onclick = onLayoutModeToggle; | |
| 106 $('portrait').onclick = onLayoutModeToggle; | |
| 107 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | |
| 108 | |
| 109 // Controls that dont require preview rendering. | |
| 110 $('two-sided').onclick = handleTwoSidedClick; | |
| 111 $('color').onclick = function() { setColor(true); }; | |
| 112 $('bw').onclick = function() { setColor(false); }; | |
| 113 } | |
| 114 | |
| 115 /** | |
| 96 * Removes event listeners from the settings controls. | 116 * Removes event listeners from the settings controls. |
| 97 */ | 117 */ |
| 98 function removeEventListeners() { | 118 function removeEventListeners() { |
| 99 // Controls that require preview rendering. | 119 // Controls that require preview rendering. |
| 100 $('print-button').disabled = true; | |
| 101 $('all-pages').onclick = null; | |
| 102 $('print-pages').onclick = null; | |
| 103 var individualPages = $('individual-pages'); | |
| 104 individualPages.onblur = null; | |
| 105 individualPages.onfocus = null; | |
| 106 individualPages.oninput = null; | |
| 107 clearTimeout(timerId); | |
| 108 $('landscape').onclick = null; | 120 $('landscape').onclick = null; |
| 109 $('portrait').onclick = null; | 121 $('portrait').onclick = null; |
| 110 $('printer-list').onchange = null; | 122 $('printer-list').onchange = null; |
| 111 | 123 |
| 112 // Controls that dont require preview rendering. | 124 // Controls that dont require preview rendering. |
| 113 $('copies').oninput = copiesFieldChanged; | |
| 114 $('two-sided').onclick = null; | 125 $('two-sided').onclick = null; |
| 115 $('color').onclick = null; | 126 $('color').onclick = null; |
| 116 $('bw').onclick = null; | 127 $('bw').onclick = null; |
| 117 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; | |
| 118 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; | |
| 119 } | 128 } |
| 120 | 129 |
| 121 /** | 130 /** |
| 122 * Asks the browser to close the preview tab. | 131 * Asks the browser to close the preview tab. |
| 123 */ | 132 */ |
| 124 function handleCancelButtonClick() { | 133 function handleCancelButtonClick() { |
| 125 chrome.send('closePrintPreviewTab'); | 134 chrome.send('closePrintPreviewTab'); |
| 126 } | 135 } |
| 127 | 136 |
| 128 /** | 137 /** |
| 129 * Asks the browser to show the native print dialog for printing. | 138 * Asks the browser to show the native print dialog for printing. |
| 130 */ | 139 */ |
| 131 function showSystemDialog() { | 140 function showSystemDialog() { |
| 132 chrome.send('showSystemDialog'); | 141 chrome.send('showSystemDialog'); |
| 133 } | 142 } |
| 134 | 143 |
| 135 /** | 144 /** |
| 136 * Disables the controls which need the initiator tab to generate preview | 145 * Disables the controls which need the initiator tab to generate preview |
| 137 * data. This function is called when the initiator tab is closed. | 146 * data. This function is called when the initiator tab is closed. |
| 138 * @param {string} initiatorTabURL The URL of the initiator tab. | 147 * @param {string} initiatorTabURL The URL of the initiator tab. |
| 139 */ | 148 */ |
| 140 function onInitiatorTabClosed(initiatorTabURL) { | 149 function onInitiatorTabClosed(initiatorTabURL) { |
| 141 $('reopen-page').addEventListener('click', function() { | 150 $('reopen-page').addEventListener('click', function() { |
| 142 window.location = initiatorTabURL; | 151 window.location = initiatorTabURL; |
| 143 }); | 152 }); |
| 144 displayErrorMessage(localStrings.getString('initiatorTabClosed'), true); | 153 displayErrorMessage(localStrings.getString('initiatorTabClosed'), true); |
| 154 $('print-button').disabled = true; | |
| 145 } | 155 } |
| 146 | 156 |
| 147 /** | 157 /** |
| 148 * Gets the selected printer capabilities and updates the controls accordingly. | 158 * Gets the selected printer capabilities and updates the controls accordingly. |
| 149 */ | 159 */ |
| 150 function updateControlsWithSelectedPrinterCapabilities() { | 160 function updateControlsWithSelectedPrinterCapabilities() { |
| 151 var printerList = $('printer-list'); | 161 var printerList = $('printer-list'); |
| 152 var selectedIndex = printerList.selectedIndex; | 162 var selectedIndex = printerList.selectedIndex; |
| 153 if (selectedIndex < 0) | 163 if (selectedIndex < 0) |
| 154 return; | 164 return; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 var deviceName = ''; | 329 var deviceName = ''; |
| 320 if (selectedPrinter >= 0) | 330 if (selectedPrinter >= 0) |
| 321 deviceName = printerList.options[selectedPrinter].value; | 331 deviceName = printerList.options[selectedPrinter].value; |
| 322 return deviceName; | 332 return deviceName; |
| 323 } | 333 } |
| 324 | 334 |
| 325 /** | 335 /** |
| 326 * Asks the browser to print the preview PDF based on current print settings. | 336 * Asks the browser to print the preview PDF based on current print settings. |
| 327 */ | 337 */ |
| 328 function printFile() { | 338 function printFile() { |
| 339 $('print-summary').innerHTML = localStrings.getString('printing'); | |
|
dpapad
2011/06/06 18:07:35
I suspect that if you select Print to PDF, the hit
kmadhusu
2011/06/06 18:44:08
Done.
| |
| 340 | |
| 341 if (previewRequestCount > 0) | |
| 342 hasPendingPrintFileRequest = true; | |
| 343 else | |
| 344 hasPendingPrintFileRequest = false; | |
|
dpapad
2011/06/06 18:07:35
Replace if/else with
hasPendingPrintFileRequest =
kmadhusu
2011/06/06 18:44:08
Done.
| |
| 345 | |
| 346 if (hasPendingPrintFileRequest) { | |
| 347 if (getSelectedPrinterName() != PRINT_TO_PDF) | |
| 348 chrome.send('hidePreview'); | |
| 349 return; | |
| 350 } | |
| 351 | |
| 329 if (getSelectedPrinterName() != PRINT_TO_PDF) { | 352 if (getSelectedPrinterName() != PRINT_TO_PDF) { |
| 330 $('print-button').classList.add('loading'); | 353 $('print-button').classList.add('loading'); |
| 331 $('cancel-button').classList.add('loading'); | 354 $('cancel-button').classList.add('loading'); |
| 332 $('print-summary').innerHTML = localStrings.getString('printing'); | 355 |
| 333 removeEventListeners(); | 356 removeEventListeners(); |
| 334 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, | 357 window.setTimeout(function() {chrome.send('print', [getSettingsJSON()]); }, |
| 335 1000); | 358 1000); |
| 336 } else | 359 } else { |
| 337 chrome.send('print', [getSettingsJSON()]); | 360 chrome.send('print', [getSettingsJSON()]); |
| 361 } | |
| 338 } | 362 } |
| 339 | 363 |
| 340 /** | 364 /** |
| 341 * Asks the browser to generate a preview PDF based on current print settings. | 365 * Asks the browser to generate a preview PDF based on current print settings. |
| 342 */ | 366 */ |
| 343 function requestPrintPreview() { | 367 function requestPrintPreview() { |
| 368 previewRequestCount++; | |
| 344 removeEventListeners(); | 369 removeEventListeners(); |
| 345 printSettings.save(); | 370 printSettings.save(); |
| 346 showLoadingAnimation(); | 371 showLoadingAnimation(); |
| 347 chrome.send('getPreview', [getSettingsJSON()]); | 372 chrome.send('getPreview', [getSettingsJSON()]); |
| 348 } | 373 } |
| 349 | 374 |
| 350 /** | 375 /** |
| 351 * Set the default printer. If there is one, generate a print preview. | 376 * Set the default printer. If there is one, generate a print preview. |
| 352 * @param {string} printer Name of the default printer. Empty if none. | 377 * @param {string} printer Name of the default printer. Empty if none. |
| 353 */ | 378 */ |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 * @param {string} jobTitle The print job title. | 515 * @param {string} jobTitle The print job title. |
| 491 * @param {boolean} modifiable If the preview is modifiable. | 516 * @param {boolean} modifiable If the preview is modifiable. |
| 492 * @param {string} previewUid Preview unique identifier. | 517 * @param {string} previewUid Preview unique identifier. |
| 493 */ | 518 */ |
| 494 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { | 519 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { |
| 495 var tempPrintSettings = new PrintSettings(); | 520 var tempPrintSettings = new PrintSettings(); |
| 496 tempPrintSettings.save(); | 521 tempPrintSettings.save(); |
| 497 | 522 |
| 498 previewModifiable = modifiable; | 523 previewModifiable = modifiable; |
| 499 | 524 |
| 525 if (previewRequestCount > 0) | |
| 526 previewRequestCount--; | |
| 527 | |
| 500 if (totalPageCount == -1) | 528 if (totalPageCount == -1) |
| 501 totalPageCount = pageCount; | 529 totalPageCount = pageCount; |
| 502 | 530 |
| 503 if (previouslySelectedPages.length == 0) | 531 if (previouslySelectedPages.length == 0) |
| 504 for (var i = 0; i < totalPageCount; i++) | 532 for (var i = 0; i < totalPageCount; i++) |
| 505 previouslySelectedPages.push(i+1); | 533 previouslySelectedPages.push(i+1); |
| 506 | 534 |
| 507 if (printSettings.deviceName != tempPrintSettings.deviceName) { | 535 if (printSettings.deviceName != tempPrintSettings.deviceName) { |
| 508 updateControlsWithSelectedPrinterCapabilities(); | 536 updateControlsWithSelectedPrinterCapabilities(); |
| 509 return; | 537 return; |
| 510 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { | 538 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { |
| 511 setDefaultValuesAndRegeneratePreview(); | 539 setDefaultValuesAndRegeneratePreview(); |
| 512 return; | 540 return; |
| 513 } else if (getSelectedPagesValidityLevel() == 1) { | 541 } else if (getSelectedPagesValidityLevel() == 1) { |
| 514 var currentlySelectedPages = getSelectedPagesSet(); | 542 var currentlySelectedPages = getSelectedPagesSet(); |
| 515 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { | 543 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { |
| 516 previouslySelectedPages = currentlySelectedPages; | 544 previouslySelectedPages = currentlySelectedPages; |
| 517 requestPrintPreview(); | 545 requestPrintPreview(); |
| 518 return; | 546 return; |
| 519 } | 547 } |
| 520 } | 548 } |
| 521 | 549 |
| 522 if (getSelectedPagesValidityLevel() != 1) | 550 if (getSelectedPagesValidityLevel() != 1) |
| 523 pageRangesFieldChanged(); | 551 pageRangesFieldChanged(); |
| 524 | 552 |
| 525 // Update the current tab title. | 553 // Update the current tab title. |
| 526 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 554 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
| 527 | 555 |
| 528 createPDFPlugin(previewUid); | 556 createPDFPlugin(previewUid); |
| 557 | |
| 558 if (previewRequestCount == 0 && hasPendingPrintFileRequest) { | |
| 559 printFile(); | |
| 560 return; | |
| 561 } | |
| 562 | |
| 529 updatePrintSummary(); | 563 updatePrintSummary(); |
| 530 updatePrintButtonState(); | 564 updatePrintButtonState(); |
| 531 addEventListeners(); | 565 addEventListeners(); |
| 532 } | 566 } |
| 533 | 567 |
| 534 /** | 568 /** |
| 535 * Create the PDF plugin or reload the existing one. | 569 * Create the PDF plugin or reload the existing one. |
| 536 * @param {string} previewUid Preview unique identifier. | 570 * @param {string} previewUid Preview unique identifier. |
| 537 */ | 571 */ |
| 538 function createPDFPlugin(previewUid) { | 572 function createPDFPlugin(previewUid) { |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 964 this.isLandscape = ''; | 998 this.isLandscape = ''; |
| 965 } | 999 } |
| 966 | 1000 |
| 967 /** | 1001 /** |
| 968 * Takes a snapshot of the print settings. | 1002 * Takes a snapshot of the print settings. |
| 969 */ | 1003 */ |
| 970 PrintSettings.prototype.save = function() { | 1004 PrintSettings.prototype.save = function() { |
| 971 this.deviceName = getSelectedPrinterName(); | 1005 this.deviceName = getSelectedPrinterName(); |
| 972 this.isLandscape = isLandscape(); | 1006 this.isLandscape = isLandscape(); |
| 973 } | 1007 } |
| OLD | NEW |