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. | |
|
dpapad
2011/06/06 20:47:43
This variable is being decremented in updatePrintP
kmadhusu
2011/06/06 21:23:56
Done.
| |
| 34 var previewRequestCount = 0; | |
| 35 | |
| 36 // True when a pending print file request exists. | |
| 37 var hasPendingPrintFileRequest = false; | |
|
dpapad
2011/06/06 20:47:43
This variable is calculated based on previewReques
kmadhusu
2011/06/06 21:23:56
(just repeating our in-person conversation) I need
| |
| 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); | |
|
dpapad
2011/06/06 20:47:43
I think for now it is better to add this listener
kmadhusu
2011/06/06 21:23:56
Done.
| |
| 44 return; | 50 return; |
| 45 } | 51 } |
| 52 $('system-dialog-link').addEventListener('click', showSystemDialog); | |
| 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 addEventListenersToCopiesControls(); | |
| 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 addEventListenersToCopiesControls() { | |
| 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 hasPendingPrintFileRequest = previewRequestCount > 0; | |
| 340 | |
| 341 if (hasPendingPrintFileRequest) { | |
| 342 if (getSelectedPrinterName() != PRINT_TO_PDF) | |
| 343 chrome.send('hidePreview'); | |
| 344 return; | |
| 345 } | |
| 346 | |
| 329 if (getSelectedPrinterName() != PRINT_TO_PDF) { | 347 if (getSelectedPrinterName() != PRINT_TO_PDF) { |
| 330 $('print-button').classList.add('loading'); | 348 $('print-button').classList.add('loading'); |
| 331 $('cancel-button').classList.add('loading'); | 349 $('cancel-button').classList.add('loading'); |
| 332 $('print-summary').innerHTML = localStrings.getString('printing'); | 350 $('print-summary').innerHTML = localStrings.getString('printing'); |
| 333 removeEventListeners(); | 351 removeEventListeners(); |
| 334 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, | 352 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, |
| 335 1000); | 353 1000); |
| 336 } else | 354 } else { |
| 337 chrome.send('print', [getSettingsJSON()]); | 355 chrome.send('print', [getSettingsJSON()]); |
| 356 } | |
| 338 } | 357 } |
| 339 | 358 |
| 340 /** | 359 /** |
| 341 * Asks the browser to generate a preview PDF based on current print settings. | 360 * Asks the browser to generate a preview PDF based on current print settings. |
| 342 */ | 361 */ |
| 343 function requestPrintPreview() { | 362 function requestPrintPreview() { |
| 363 previewRequestCount++; | |
| 344 removeEventListeners(); | 364 removeEventListeners(); |
| 345 printSettings.save(); | 365 printSettings.save(); |
| 346 showLoadingAnimation(); | 366 showLoadingAnimation(); |
| 347 chrome.send('getPreview', [getSettingsJSON()]); | 367 chrome.send('getPreview', [getSettingsJSON()]); |
| 348 } | 368 } |
| 349 | 369 |
| 350 /** | 370 /** |
| 351 * Set the default printer. If there is one, generate a print preview. | 371 * Set the default printer. If there is one, generate a print preview. |
| 352 * @param {string} printer Name of the default printer. Empty if none. | 372 * @param {string} printer Name of the default printer. Empty if none. |
| 353 */ | 373 */ |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 * @param {number} pageCount The expected total pages count. | 509 * @param {number} pageCount The expected total pages count. |
| 490 * @param {string} jobTitle The print job title. | 510 * @param {string} jobTitle The print job title. |
| 491 * @param {boolean} modifiable If the preview is modifiable. | 511 * @param {boolean} modifiable If the preview is modifiable. |
| 492 * @param {string} previewUid Preview unique identifier. | 512 * @param {string} previewUid Preview unique identifier. |
| 493 */ | 513 */ |
| 494 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { | 514 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { |
| 495 var tempPrintSettings = new PrintSettings(); | 515 var tempPrintSettings = new PrintSettings(); |
| 496 tempPrintSettings.save(); | 516 tempPrintSettings.save(); |
| 497 | 517 |
| 498 previewModifiable = modifiable; | 518 previewModifiable = modifiable; |
| 499 | 519 |
|
dpapad
2011/06/06 20:47:43
Add "var hasPendingPrintFileRequest = previewReque
kmadhusu
2011/06/06 21:23:56
Please refer to my reply for your earlier comment.
| |
| 520 if (previewRequestCount > 0) | |
| 521 previewRequestCount--; | |
| 522 | |
| 500 if (totalPageCount == -1) | 523 if (totalPageCount == -1) |
| 501 totalPageCount = pageCount; | 524 totalPageCount = pageCount; |
| 502 | 525 |
| 503 if (previouslySelectedPages.length == 0) | 526 if (previouslySelectedPages.length == 0) |
| 504 for (var i = 0; i < totalPageCount; i++) | 527 for (var i = 0; i < totalPageCount; i++) |
| 505 previouslySelectedPages.push(i+1); | 528 previouslySelectedPages.push(i+1); |
| 506 | 529 |
| 507 if (printSettings.deviceName != tempPrintSettings.deviceName) { | 530 if (printSettings.deviceName != tempPrintSettings.deviceName) { |
| 508 updateControlsWithSelectedPrinterCapabilities(); | 531 updateControlsWithSelectedPrinterCapabilities(); |
| 509 return; | 532 return; |
| 510 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { | 533 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { |
| 511 setDefaultValuesAndRegeneratePreview(); | 534 setDefaultValuesAndRegeneratePreview(); |
| 512 return; | 535 return; |
| 513 } else if (getSelectedPagesValidityLevel() == 1) { | 536 } else if (getSelectedPagesValidityLevel() == 1) { |
| 514 var currentlySelectedPages = getSelectedPagesSet(); | 537 var currentlySelectedPages = getSelectedPagesSet(); |
| 515 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { | 538 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { |
| 516 previouslySelectedPages = currentlySelectedPages; | 539 previouslySelectedPages = currentlySelectedPages; |
| 517 requestPrintPreview(); | 540 requestPrintPreview(); |
| 518 return; | 541 return; |
| 519 } | 542 } |
| 520 } | 543 } |
| 521 | 544 |
| 522 if (getSelectedPagesValidityLevel() != 1) | 545 if (getSelectedPagesValidityLevel() != 1) |
| 523 pageRangesFieldChanged(); | 546 pageRangesFieldChanged(); |
| 524 | 547 |
| 525 // Update the current tab title. | 548 // Update the current tab title. |
| 526 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 549 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
| 527 | 550 |
| 528 createPDFPlugin(previewUid); | 551 createPDFPlugin(previewUid); |
| 552 | |
| 553 if (previewRequestCount == 0 && hasPendingPrintFileRequest) { | |
| 554 printFile(); | |
| 555 return; | |
| 556 } | |
| 557 | |
| 529 updatePrintSummary(); | 558 updatePrintSummary(); |
| 530 updatePrintButtonState(); | 559 updatePrintButtonState(); |
| 531 addEventListeners(); | 560 addEventListeners(); |
| 532 } | 561 } |
| 533 | 562 |
| 534 /** | 563 /** |
| 535 * Create the PDF plugin or reload the existing one. | 564 * Create the PDF plugin or reload the existing one. |
| 536 * @param {string} previewUid Preview unique identifier. | 565 * @param {string} previewUid Preview unique identifier. |
| 537 */ | 566 */ |
| 538 function createPDFPlugin(previewUid) { | 567 function createPDFPlugin(previewUid) { |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 964 this.isLandscape = ''; | 993 this.isLandscape = ''; |
| 965 } | 994 } |
| 966 | 995 |
| 967 /** | 996 /** |
| 968 * Takes a snapshot of the print settings. | 997 * Takes a snapshot of the print settings. |
| 969 */ | 998 */ |
| 970 PrintSettings.prototype.save = function() { | 999 PrintSettings.prototype.save = function() { |
| 971 this.deviceName = getSelectedPrinterName(); | 1000 this.deviceName = getSelectedPrinterName(); |
| 972 this.isLandscape = isLandscape(); | 1001 this.isLandscape = isLandscape(); |
| 973 } | 1002 } |
| OLD | NEW |