| 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 // Whether or not the PDF plugin supports all the capabilities needed for | |
| 8 // print preview. | |
| 9 var hasCompatiblePDFPlugin = true; | |
| 10 | |
| 11 // 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 |
| 12 // user has selected. | 8 // user has selected. |
| 13 var totalPageCount = -1; | 9 var totalPageCount = -1; |
| 14 | 10 |
| 15 // The previously selected pages by the user. It is used in | 11 // The previously selected pages by the user. It is used in |
| 16 // onPageSelectionMayHaveChanged() to make sure that a new preview is not | 12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not |
| 17 // requested more often than necessary. | 13 // requested more often than necessary. |
| 18 var previouslySelectedPages = []; | 14 var previouslySelectedPages = []; |
| 19 | 15 |
| 20 // The previously selected layout mode. It is used in order to prevent the | 16 // The previously selected layout mode. It is used in order to prevent the |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 | 37 |
| 42 // Destination list special value constants. | 38 // Destination list special value constants. |
| 43 const PRINT_TO_PDF = 'Print To PDF'; | 39 const PRINT_TO_PDF = 'Print To PDF'; |
| 44 const MANAGE_PRINTERS = 'Manage Printers'; | 40 const MANAGE_PRINTERS = 'Manage Printers'; |
| 45 | 41 |
| 46 /** | 42 /** |
| 47 * Window onload handler, sets up the page and starts print preview by getting | 43 * Window onload handler, sets up the page and starts print preview by getting |
| 48 * the printer list. | 44 * the printer list. |
| 49 */ | 45 */ |
| 50 function onLoad() { | 46 function onLoad() { |
| 47 $('system-dialog-link').addEventListener('click', showSystemDialog); |
| 48 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
| 49 $('dummy-viewer').classList.add('hidden'); |
| 50 |
| 51 if (!checkCompatiblePluginExists()) { |
| 52 displayErrorMessage(localStrings.getString('noPlugin')); |
| 53 return; |
| 54 } |
| 55 |
| 51 $('printer-list').disabled = true; | 56 $('printer-list').disabled = true; |
| 52 $('print-button').disabled = true; | 57 $('print-button').disabled = true; |
| 53 $('print-button').addEventListener('click', printFile); | 58 $('print-button').addEventListener('click', printFile); |
| 54 $('cancel-button').addEventListener('click', handleCancelButtonClick); | |
| 55 $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged); | 59 $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged); |
| 56 $('copies').addEventListener('input', copiesFieldChanged); | 60 $('copies').addEventListener('input', copiesFieldChanged); |
| 57 $('print-pages').addEventListener('click', handleIndividualPagesCheckbox); | 61 $('print-pages').addEventListener('click', handleIndividualPagesCheckbox); |
| 58 $('individual-pages').addEventListener('blur', function() { | 62 $('individual-pages').addEventListener('blur', function() { |
| 59 clearTimeout(timerId); | 63 clearTimeout(timerId); |
| 60 onPageSelectionMayHaveChanged(); | 64 onPageSelectionMayHaveChanged(); |
| 61 }); | 65 }); |
| 62 $('individual-pages').addEventListener('focus', addTimerToPageRangeField); | 66 $('individual-pages').addEventListener('focus', addTimerToPageRangeField); |
| 63 $('individual-pages').addEventListener('input', pageRangesFieldChanged); | 67 $('individual-pages').addEventListener('input', pageRangesFieldChanged); |
| 64 $('two-sided').addEventListener('click', handleTwoSidedClick) | 68 $('two-sided').addEventListener('click', handleTwoSidedClick) |
| 65 $('landscape').addEventListener('click', onLayoutModeToggle); | 69 $('landscape').addEventListener('click', onLayoutModeToggle); |
| 66 $('portrait').addEventListener('click', onLayoutModeToggle); | 70 $('portrait').addEventListener('click', onLayoutModeToggle); |
| 67 $('color').addEventListener('click', function() { setColor(true); }); | 71 $('color').addEventListener('click', function() { setColor(true); }); |
| 68 $('bw').addEventListener('click', function() { setColor(false); }); | 72 $('bw').addEventListener('click', function() { setColor(false); }); |
| 69 $('printer-list').addEventListener( | 73 $('printer-list').addEventListener( |
| 70 'change', updateControlsWithSelectedPrinterCapabilities); | 74 'change', updateControlsWithSelectedPrinterCapabilities); |
| 71 $('system-dialog-link').addEventListener('click', showSystemDialog); | |
| 72 $('increment').addEventListener('click', | 75 $('increment').addEventListener('click', |
| 73 function() { onCopiesButtonsClicked(1); }); | 76 function() { onCopiesButtonsClicked(1); }); |
| 74 $('decrement').addEventListener('click', | 77 $('decrement').addEventListener('click', |
| 75 function() { onCopiesButtonsClicked(-1); }); | 78 function() { onCopiesButtonsClicked(-1); }); |
| 76 $('controls').onsubmit = function() { return false; }; | 79 $('controls').onsubmit = function() { return false; }; |
| 77 chrome.send('getPrinters'); | 80 chrome.send('getPrinters'); |
| 78 } | 81 } |
| 79 | 82 |
| 80 /** | 83 /** |
| 81 * Asks the browser to close the preview tab. | 84 * Asks the browser to close the preview tab. |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 function printFile() { | 319 function printFile() { |
| 317 chrome.send('print', [getSettingsJSON()]); | 320 chrome.send('print', [getSettingsJSON()]); |
| 318 } | 321 } |
| 319 | 322 |
| 320 /** | 323 /** |
| 321 * Asks the browser to generate a preview PDF based on current print settings. | 324 * Asks the browser to generate a preview PDF based on current print settings. |
| 322 */ | 325 */ |
| 323 function requestPrintPreview() { | 326 function requestPrintPreview() { |
| 324 isPreviewStillLoading = true; | 327 isPreviewStillLoading = true; |
| 325 setControlsDisabled(true); | 328 setControlsDisabled(true); |
| 326 $('dancing-dots').classList.remove('hidden'); | |
| 327 $('dancing-dots').classList.remove('invisible'); | 329 $('dancing-dots').classList.remove('invisible'); |
| 328 chrome.send('getPreview', [getSettingsJSON()]); | 330 chrome.send('getPreview', [getSettingsJSON()]); |
| 329 } | 331 } |
| 330 | 332 |
| 331 /** | 333 /** |
| 332 * Fill the printer list drop down. | 334 * Fill the printer list drop down. |
| 333 * Called from PrintPreviewHandler::SendPrinterList(). | 335 * Called from PrintPreviewHandler::SendPrinterList(). |
| 334 * @param {Array} printers Array of printer info objects. | 336 * @param {Array} printers Array of printer info objects. |
| 335 * @param {number} defaultPrinterIndex The index of the default printer. | 337 * @param {number} defaultPrinterIndex The index of the default printer. |
| 336 */ | 338 */ |
| (...skipping 30 matching lines...) Expand all Loading... |
| 367 if (is_default) | 369 if (is_default) |
| 368 option.selected = true; | 370 option.selected = true; |
| 369 } | 371 } |
| 370 | 372 |
| 371 /** | 373 /** |
| 372 * Sets the color mode for the PDF plugin. | 374 * Sets the color mode for the PDF plugin. |
| 373 * Called from PrintPreviewHandler::ProcessColorSetting(). | 375 * Called from PrintPreviewHandler::ProcessColorSetting(). |
| 374 * @param {boolean} color is true if the PDF plugin should display in color. | 376 * @param {boolean} color is true if the PDF plugin should display in color. |
| 375 */ | 377 */ |
| 376 function setColor(color) { | 378 function setColor(color) { |
| 377 if (!hasCompatiblePDFPlugin) { | |
| 378 return; | |
| 379 } | |
| 380 var pdfViewer = $('pdf-viewer'); | 379 var pdfViewer = $('pdf-viewer'); |
| 381 if (!pdfViewer) { | 380 if (!pdfViewer) { |
| 382 return; | 381 return; |
| 383 } | 382 } |
| 384 pdfViewer.grayscale(!color); | 383 pdfViewer.grayscale(!color); |
| 385 } | 384 } |
| 386 | 385 |
| 387 /** | 386 /** |
| 388 * Display an error message in the center of the preview area. | 387 * Display an error message in the center of the preview area. |
| 389 * @param (string) errorMessage The error message to be displayed. | 388 * @param (string) errorMessage The error message to be displayed. |
| 390 */ | 389 */ |
| 391 function displayErrorMessage(errorMessage) { | 390 function displayErrorMessage(errorMessage) { |
| 392 isPreviewStillLoading = false; | 391 isPreviewStillLoading = false; |
| 392 $('dancing-dots').classList.remove('invisible'); |
| 393 $('dancing-dots-text').classList.add('hidden'); | 393 $('dancing-dots-text').classList.add('hidden'); |
| 394 $('error-text').innerHTML = errorMessage; | 394 $('error-text').innerHTML = errorMessage; |
| 395 $('error-text').classList.remove('hidden'); | 395 $('error-text').classList.remove('hidden'); |
| 396 setControlsDisabled(true); | 396 setControlsDisabled(true); |
| 397 | 397 |
| 398 var pdfViewer = $('pdf-viewer'); | 398 var pdfViewer = $('pdf-viewer'); |
| 399 if (pdfViewer) | 399 if (pdfViewer) |
| 400 $('mainview').removeChild(pdfViewer); | 400 $('mainview').removeChild(pdfViewer); |
| 401 } | 401 } |
| 402 | 402 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 | 455 |
| 456 createPDFPlugin(); | 456 createPDFPlugin(); |
| 457 isPreviewStillLoading = false; | 457 isPreviewStillLoading = false; |
| 458 updatePrintSummary(); | 458 updatePrintSummary(); |
| 459 } | 459 } |
| 460 | 460 |
| 461 /** | 461 /** |
| 462 * Create the PDF plugin or reload the existing one. | 462 * Create the PDF plugin or reload the existing one. |
| 463 */ | 463 */ |
| 464 function createPDFPlugin() { | 464 function createPDFPlugin() { |
| 465 if (!hasCompatiblePDFPlugin) { | |
| 466 return; | |
| 467 } | |
| 468 | |
| 469 // Enable the print button. | 465 // Enable the print button. |
| 470 if (!$('printer-list').disabled) { | 466 if (!$('printer-list').disabled) { |
| 471 $('print-button').disabled = false; | 467 $('print-button').disabled = false; |
| 472 } | 468 } |
| 473 | 469 |
| 474 var pdfViewer = $('pdf-viewer'); | 470 var pdfViewer = $('pdf-viewer'); |
| 475 if (pdfViewer) { | 471 if (pdfViewer) { |
| 476 // Older version of the PDF plugin may not have this method. | 472 // Older version of the PDF plugin may not have this method. |
| 477 // TODO(thestig) Eventually remove this check. | 473 // TODO(thestig) Eventually remove this check. |
| 478 if (pdfViewer.goToPage) { | 474 if (pdfViewer.goToPage) { |
| 479 // Need to call this before the reload(), where the plugin resets its | 475 // Need to call this before the reload(), where the plugin resets its |
| 480 // internal page count. | 476 // internal page count. |
| 481 pdfViewer.goToPage('0'); | 477 pdfViewer.goToPage('0'); |
| 482 } | 478 } |
| 483 pdfViewer.reload(); | 479 pdfViewer.reload(); |
| 484 pdfViewer.grayscale(!isColor()); | 480 pdfViewer.grayscale(!isColor()); |
| 485 return; | 481 return; |
| 486 } | 482 } |
| 487 | 483 |
| 488 var pdfPlugin = document.createElement('embed'); | 484 var pdfPlugin = document.createElement('embed'); |
| 489 pdfPlugin.setAttribute('id', 'pdf-viewer'); | 485 pdfPlugin.setAttribute('id', 'pdf-viewer'); |
| 490 pdfPlugin.setAttribute('type', 'application/pdf'); | 486 pdfPlugin.setAttribute('type', 'application/pdf'); |
| 491 pdfPlugin.setAttribute('src', 'chrome://print/print.pdf'); | 487 pdfPlugin.setAttribute('src', 'chrome://print/print.pdf'); |
| 492 var mainView = $('mainview'); | 488 var mainView = $('mainview'); |
| 493 mainView.appendChild(pdfPlugin); | 489 mainView.appendChild(pdfPlugin); |
| 494 | |
| 495 // Check to see if the PDF plugin is our PDF plugin. (or compatible) | |
| 496 if (!pdfPlugin.onload) { | |
| 497 hasCompatiblePDFPlugin = false; | |
| 498 displayErrorMessage(localStrings.getString('noPlugin')); | |
| 499 return; | |
| 500 } | |
| 501 pdfPlugin.onload('onPDFLoad()'); | 490 pdfPlugin.onload('onPDFLoad()'); |
| 502 | 491 |
| 503 // Older version of the PDF plugin may not have this method. | 492 // Older version of the PDF plugin may not have this method. |
| 504 // TODO(thestig) Eventually remove this check. | 493 // TODO(thestig) Eventually remove this check. |
| 505 if (pdfPlugin.removePrintButton) { | 494 if (pdfPlugin.removePrintButton) { |
| 506 pdfPlugin.removePrintButton(); | 495 pdfPlugin.removePrintButton(); |
| 507 } | 496 } |
| 508 | 497 |
| 509 pdfPlugin.grayscale(true); | 498 pdfPlugin.grayscale(true); |
| 510 } | 499 } |
| 511 | 500 |
| 512 /** | 501 /** |
| 502 * Returns true if a compatible pdf plugin exists, false if it doesn't. |
| 503 */ |
| 504 function checkCompatiblePluginExists() { |
| 505 var dummyPlugin = $('dummy-viewer') |
| 506 return !!dummyPlugin.onload; |
| 507 } |
| 508 |
| 509 /** |
| 513 * Updates the state of print button depending on the user selection. | 510 * Updates the state of print button depending on the user selection. |
| 514 * The button is enabled only when the following conditions are true. | 511 * The button is enabled only when the following conditions are true. |
| 515 * 1) The selected page ranges are valid. | 512 * 1) The selected page ranges are valid. |
| 516 * 2) The number of copies is valid. | 513 * 2) The number of copies is valid. |
| 517 */ | 514 */ |
| 518 function updatePrintButtonState() { | 515 function updatePrintButtonState() { |
| 519 $('print-button').disabled = (!isNumberOfCopiesValid() || | 516 $('print-button').disabled = (!isNumberOfCopiesValid() || |
| 520 getSelectedPagesValidityLevel() != 1); | 517 getSelectedPagesValidityLevel() != 1); |
| 521 } | 518 } |
| 522 | 519 |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 /** | 885 /** |
| 889 * Executed when the 'increment' or 'decrement' button is clicked. | 886 * Executed when the 'increment' or 'decrement' button is clicked. |
| 890 */ | 887 */ |
| 891 function onCopiesButtonsClicked(sign) { | 888 function onCopiesButtonsClicked(sign) { |
| 892 if($('copies').value == 1 && (sign == -1)) | 889 if($('copies').value == 1 && (sign == -1)) |
| 893 return; | 890 return; |
| 894 $('copies').value = getCopies() + sign * 1; | 891 $('copies').value = getCopies() + sign * 1; |
| 895 copiesFieldChanged(); | 892 copiesFieldChanged(); |
| 896 } | 893 } |
| 897 | 894 |
| OLD | NEW |