| 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 // require: cr/ui/print_preview_cloud.js | 5 // require: cr/ui/print_preview_cloud.js |
| 6 | 6 |
| 7 var localStrings = new LocalStrings(); | 7 var localStrings = new LocalStrings(); |
| 8 | 8 |
| 9 // If useCloudPrint is true we attempt to connect to cloud print | 9 // If useCloudPrint is true we attempt to connect to cloud print |
| 10 // and populate the list of printers with cloud print printers. | 10 // and populate the list of printers with cloud print printers. |
| 11 var useCloudPrint = false; | 11 var useCloudPrint = false; |
| 12 | 12 |
| 13 // Store the last selected printer index. | 13 // Store the last selected printer index. |
| 14 var lastSelectedPrinterIndex = 0; | 14 var lastSelectedPrinterIndex = 0; |
| 15 | 15 |
| 16 // Used to disable some printing options when the preview is not modifiable. | 16 // Used to disable some printing options when the preview is not modifiable. |
| 17 var previewModifiable = false; | 17 var previewModifiable = false; |
| 18 | 18 |
| 19 // Destination list special value constants. | 19 // Destination list special value constants. |
| 20 const ADD_CLOUD_PRINTER = 'addCloudPrinter'; | 20 const ADD_CLOUD_PRINTER = 'addCloudPrinter'; |
| 21 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; | 21 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; |
| 22 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; | 22 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; |
| 23 const MORE_PRINTERS = 'morePrinters'; | 23 const MORE_PRINTERS = 'morePrinters'; |
| 24 const SIGN_IN = 'signIn'; | 24 const SIGN_IN = 'signIn'; |
| 25 const PRINT_TO_PDF = 'Print to PDF'; | 25 const PRINT_TO_PDF = 'Print to PDF'; |
| 26 | 26 |
| 27 // Print ready data index. |
| 28 const PRINT_READY_DATA_INDEX = -1; |
| 29 |
| 27 // State of the print preview settings. | 30 // State of the print preview settings. |
| 28 var printSettings = new PrintSettings(); | 31 var printSettings = new PrintSettings(); |
| 29 | 32 |
| 30 // The name of the default or last used printer. | 33 // The name of the default or last used printer. |
| 31 var defaultOrLastUsedPrinterName = ''; | 34 var defaultOrLastUsedPrinterName = ''; |
| 32 | 35 |
| 33 // True when a pending print preview request exists. | 36 // True when a pending print preview request exists. |
| 34 var hasPendingPreviewRequest = false; | 37 var hasPendingPreviewRequest = false; |
| 35 | 38 |
| 36 // The ID of the last preview request. | 39 // The ID of the last preview request. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 55 var colorSettings; | 58 var colorSettings; |
| 56 | 59 |
| 57 // True if the user has click 'Advanced...' in order to open the system print | 60 // True if the user has click 'Advanced...' in order to open the system print |
| 58 // dialog. | 61 // dialog. |
| 59 var showingSystemDialog = false; | 62 var showingSystemDialog = false; |
| 60 | 63 |
| 61 // The range of options in the printer dropdown controlled by cloud print. | 64 // The range of options in the printer dropdown controlled by cloud print. |
| 62 var firstCloudPrintOptionPos = 0; | 65 var firstCloudPrintOptionPos = 0; |
| 63 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; | 66 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; |
| 64 | 67 |
| 68 // Store the current previewUid. |
| 69 var currentPreviewUid = ''; |
| 70 |
| 71 // True if we need to generate draft preview data. |
| 72 var generateDraftData = true; |
| 65 | 73 |
| 66 // TODO(abodenha@chromium.org) A lot of cloud print specific logic has | 74 // TODO(abodenha@chromium.org) A lot of cloud print specific logic has |
| 67 // made its way into this file. Refactor to create a cleaner boundary | 75 // made its way into this file. Refactor to create a cleaner boundary |
| 68 // between print preview and GCP code. Reference bug 88098 when fixing. | 76 // between print preview and GCP code. Reference bug 88098 when fixing. |
| 69 | 77 |
| 70 // A dictionary of cloud printers that have been added to the printer | 78 // A dictionary of cloud printers that have been added to the printer |
| 71 // dropdown. | 79 // dropdown. |
| 72 var addedCloudPrinters = {}; | 80 var addedCloudPrinters = {}; |
| 73 | 81 |
| 74 // The maximum number of cloud printers to allow in the dropdown. | 82 // The maximum number of cloud printers to allow in the dropdown. |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 var settings = | 312 var settings = |
| 305 {'deviceName': deviceName, | 313 {'deviceName': deviceName, |
| 306 'pageRange': pageSettings.selectedPageRanges, | 314 'pageRange': pageSettings.selectedPageRanges, |
| 307 'printAll': pageSettings.allPagesRadioButton.checked, | 315 'printAll': pageSettings.allPagesRadioButton.checked, |
| 308 'duplex': copiesSettings.duplexMode, | 316 'duplex': copiesSettings.duplexMode, |
| 309 'copies': copiesSettings.numberOfCopies, | 317 'copies': copiesSettings.numberOfCopies, |
| 310 'collate': copiesSettings.isCollated(), | 318 'collate': copiesSettings.isCollated(), |
| 311 'landscape': layoutSettings.isLandscape(), | 319 'landscape': layoutSettings.isLandscape(), |
| 312 'color': colorSettings.isColor(), | 320 'color': colorSettings.isColor(), |
| 313 'printToPDF': printToPDF, | 321 'printToPDF': printToPDF, |
| 314 'requestID': 0}; | 322 'requestID': 0, |
| 323 'generateDraftData': generateDraftData}; |
| 315 | 324 |
| 316 var printerList = $('printer-list'); | 325 var printerList = $('printer-list'); |
| 317 var selectedPrinter = printerList.selectedIndex; | 326 var selectedPrinter = printerList.selectedIndex; |
| 318 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { | 327 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { |
| 319 settings['cloudPrintID'] = | 328 settings['cloudPrintID'] = |
| 320 printerList.options[selectedPrinter].value; | 329 printerList.options[selectedPrinter].value; |
| 321 } | 330 } |
| 322 return settings; | 331 return settings; |
| 323 } | 332 } |
| 324 | 333 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 function sendPrintDocumentRequest() { | 423 function sendPrintDocumentRequest() { |
| 415 var printerList = $('printer-list'); | 424 var printerList = $('printer-list'); |
| 416 var printer = printerList[printerList.selectedIndex]; | 425 var printer = printerList[printerList.selectedIndex]; |
| 417 chrome.send('saveLastPrinter', [printer.textContent, | 426 chrome.send('saveLastPrinter', [printer.textContent, |
| 418 cloudprint.getData(printer)]); | 427 cloudprint.getData(printer)]); |
| 419 chrome.send('print', [JSON.stringify(getSettings()), | 428 chrome.send('print', [JSON.stringify(getSettings()), |
| 420 cloudprint.getPrintTicketJSON(printer)]); | 429 cloudprint.getPrintTicketJSON(printer)]); |
| 421 } | 430 } |
| 422 | 431 |
| 423 /** | 432 /** |
| 433 * Loads the selected preview pages. |
| 434 */ |
| 435 function loadSelectedPages() { |
| 436 hasPendingPreviewRequest = false; |
| 437 pageSettings.updatePageSelection(); |
| 438 var pageSet = pageSettings.previouslySelectedPages; |
| 439 var pageCount = pageSet.length; |
| 440 if (pageCount == 0 || currentPreviewUid == '') |
| 441 return; |
| 442 |
| 443 for (var i = 0; i < pageCount; i++) |
| 444 onDidPreviewPage(pageSet[i] - 1, currentPreviewUid); |
| 445 addEventListeners(); |
| 446 } |
| 447 |
| 448 /** |
| 424 * Asks the browser to generate a preview PDF based on current print settings. | 449 * Asks the browser to generate a preview PDF based on current print settings. |
| 425 */ | 450 */ |
| 426 function requestPrintPreview() { | 451 function requestPrintPreview() { |
| 427 hasPendingPreviewRequest = true; | 452 hasPendingPreviewRequest = true; |
| 428 removeEventListeners(); | 453 removeEventListeners(); |
| 429 printSettings.save(); | 454 printSettings.save(); |
| 455 generateDraftData = true; |
| 430 if (!isTabHidden) | 456 if (!isTabHidden) |
| 431 showLoadingAnimation(); | 457 showLoadingAnimation(); |
| 432 | 458 |
| 459 if (previewModifiable && hasOnlyPageSettingsChanged()) { |
| 460 loadSelectedPages(); |
| 461 generateDraftData = false; |
| 462 } else { |
| 463 pageSettings.updatePageSelection(); |
| 464 } |
| 465 |
| 466 if (!previewModifiable && pageSettings.totalPageCount > 0) |
| 467 generateDraftData = false; |
| 468 |
| 433 var settings = getSettings(); | 469 var settings = getSettings(); |
| 434 settings.requestID = generatePreviewRequestID(); | 470 settings.requestID = generatePreviewRequestID(); |
| 435 chrome.send('getPreview', [JSON.stringify(settings)]); | 471 chrome.send('getPreview', [JSON.stringify(settings)]); |
| 436 } | 472 } |
| 437 | 473 |
| 438 /** | 474 /** |
| 439 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print | 475 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print |
| 440 * preview tab regarding the file selection cancel event. | 476 * preview tab regarding the file selection cancel event. |
| 441 */ | 477 */ |
| 442 function fileSelectionCancelled() { | 478 function fileSelectionCancelled() { |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 * Notification that a print preview page has been rendered. | 858 * Notification that a print preview page has been rendered. |
| 823 * Check if the settings have changed and request a regeneration if needed. | 859 * Check if the settings have changed and request a regeneration if needed. |
| 824 * Called from PrintPreviewUI::OnDidPreviewPage(). | 860 * Called from PrintPreviewUI::OnDidPreviewPage(). |
| 825 * @param {number} pageNumber The page number, 0-based. | 861 * @param {number} pageNumber The page number, 0-based. |
| 826 */ | 862 */ |
| 827 function onDidPreviewPage(pageNumber, previewUid) { | 863 function onDidPreviewPage(pageNumber, previewUid) { |
| 828 // Refactor | 864 // Refactor |
| 829 if (!previewModifiable) | 865 if (!previewModifiable) |
| 830 return; | 866 return; |
| 831 | 867 |
| 832 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); | |
| 833 | |
| 834 if (checkIfSettingsChangedAndRegeneratePreview()) | 868 if (checkIfSettingsChangedAndRegeneratePreview()) |
| 835 return; | 869 return; |
| 870 |
| 871 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); |
| 872 if (pageIndex == -1) |
| 873 return; |
| 874 |
| 875 currentPreviewUid = previewUid; |
| 836 if (pageIndex == 0) | 876 if (pageIndex == 0) |
| 837 createPDFPlugin(previewUid); | 877 createPDFPlugin(pageNumber); |
| 838 | 878 |
| 839 $('pdf-viewer').loadPreviewPage( | 879 $('pdf-viewer').loadPreviewPage( |
| 840 getPageSrcURL(previewUid, pageNumber), pageIndex); | 880 getPageSrcURL(previewUid, pageNumber), pageIndex); |
| 841 } | 881 } |
| 842 | 882 |
| 843 /** | 883 /** |
| 844 * Update the print preview when new preview data is available. | 884 * Update the print preview when new preview data is available. |
| 845 * Create the PDF plugin as needed. | 885 * Create the PDF plugin as needed. |
| 846 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 886 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
| 847 * @param {string} jobTitle The print job title. | 887 * @param {string} jobTitle The print job title. |
| 848 * @param {boolean} modifiable If the preview is modifiable. | 888 * @param {boolean} modifiable If the preview is modifiable. |
| 849 * @param {string} previewUid Preview unique identifier. | 889 * @param {string} previewUid Preview unique identifier. |
| 850 * @param {number} previewResponseId The preview request id that resulted in | 890 * @param {number} previewResponseId The preview request id that resulted in |
| 851 * this response. | 891 * this response. |
| 852 */ | 892 */ |
| 853 function updatePrintPreview(jobTitle, | 893 function updatePrintPreview(jobTitle, |
| 854 previewUid, | 894 previewUid, |
| 855 previewResponseId) { | 895 previewResponseId) { |
| 856 if (!isExpectedPreviewResponse(previewResponseId)) | 896 if (!isExpectedPreviewResponse(previewResponseId)) |
| 857 return; | 897 return; |
| 858 hasPendingPreviewRequest = false; | 898 hasPendingPreviewRequest = false; |
| 859 | 899 |
| 860 if (checkIfSettingsChangedAndRegeneratePreview()) | 900 if (checkIfSettingsChangedAndRegeneratePreview()) |
| 861 return; | 901 return; |
| 862 | 902 |
| 863 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 903 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
| 864 | 904 |
| 865 if (!previewModifiable) { | 905 if (!previewModifiable) { |
| 866 // If the preview is not modifiable the plugin has not been created yet. | 906 // If the preview is not modifiable the plugin has not been created yet. |
| 867 createPDFPlugin(previewUid); | 907 currentPreviewUid = previewUid; |
| 908 createPDFPlugin(PRINT_READY_DATA_INDEX); |
| 868 } | 909 } |
| 869 | 910 |
| 870 cr.dispatchSimpleEvent(document, 'updateSummary'); | 911 cr.dispatchSimpleEvent(document, 'updateSummary'); |
| 871 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 912 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
| 872 addEventListeners(); | 913 addEventListeners(); |
| 873 | 914 |
| 874 if (hasPendingPrintDocumentRequest) | 915 if (hasPendingPrintDocumentRequest) |
| 875 requestToPrintPendingDocument(); | 916 requestToPrintPendingDocument(); |
| 876 } | 917 } |
| 877 | 918 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 891 setDefaultValuesAndRegeneratePreview(); | 932 setDefaultValuesAndRegeneratePreview(); |
| 892 return true; | 933 return true; |
| 893 } | 934 } |
| 894 if (pageSettings.requestPrintPreviewIfNeeded()) | 935 if (pageSettings.requestPrintPreviewIfNeeded()) |
| 895 return true; | 936 return true; |
| 896 | 937 |
| 897 return false; | 938 return false; |
| 898 } | 939 } |
| 899 | 940 |
| 900 /** | 941 /** |
| 942 * Check if only page selection has been changed since the last preview request |
| 943 * and is valid. |
| 944 * @return {boolean} true if the new page selection is valid. |
| 945 */ |
| 946 function hasOnlyPageSettingsChanged() { |
| 947 var tempPrintSettings = new PrintSettings(); |
| 948 tempPrintSettings.save(); |
| 949 |
| 950 return !!(printSettings.deviceName == tempPrintSettings.deviceName && |
| 951 printSettings.isLandscape == tempPrintSettings.isLandscape && |
| 952 pageSettings.hasPageSelectionChangedAndIsValid()); |
| 953 } |
| 954 |
| 955 /** |
| 901 * Create the PDF plugin or reload the existing one. | 956 * Create the PDF plugin or reload the existing one. |
| 902 * @param {string} previewUid Preview unique identifier. | 957 * @param {number} srcDataIndex Preview data source index. |
| 903 */ | 958 */ |
| 904 function createPDFPlugin(previewUid) { | 959 function createPDFPlugin(srcDataIndex) { |
| 905 var pdfViewer = $('pdf-viewer'); | 960 var pdfViewer = $('pdf-viewer'); |
| 961 var srcURL = getPageSrcURL(currentPreviewUid, srcDataIndex); |
| 906 if (pdfViewer) { | 962 if (pdfViewer) { |
| 907 // Need to call this before the reload(), where the plugin resets its | 963 // Need to call this before the reload(), where the plugin resets its |
| 908 // internal page count. | 964 // internal page count. |
| 909 pdfViewer.goToPage('0'); | 965 pdfViewer.goToPage('0'); |
| 966 pdfViewer.resetPrintPreviewUrl(srcURL); |
| 910 pdfViewer.reload(); | 967 pdfViewer.reload(); |
| 911 pdfViewer.grayscale(!colorSettings.isColor()); | 968 pdfViewer.grayscale(!colorSettings.isColor()); |
| 912 return; | 969 return; |
| 913 } | 970 } |
| 914 | 971 |
| 915 // Get the complete preview document. | |
| 916 var dataIndex = previewModifiable ? '0' : '-1'; | |
| 917 | |
| 918 pdfViewer = document.createElement('embed'); | 972 pdfViewer = document.createElement('embed'); |
| 919 pdfViewer.setAttribute('id', 'pdf-viewer'); | 973 pdfViewer.setAttribute('id', 'pdf-viewer'); |
| 920 pdfViewer.setAttribute('type', | 974 pdfViewer.setAttribute('type', |
| 921 'application/x-google-chrome-print-preview-pdf'); | 975 'application/x-google-chrome-print-preview-pdf'); |
| 922 pdfViewer.setAttribute('src', getPageSrcURL(previewUid, dataIndex)); | 976 pdfViewer.setAttribute('src', srcURL); |
| 923 pdfViewer.setAttribute('aria-live', 'polite'); | 977 pdfViewer.setAttribute('aria-live', 'polite'); |
| 924 pdfViewer.setAttribute('aria-atomic', 'true'); | 978 pdfViewer.setAttribute('aria-atomic', 'true'); |
| 925 $('mainview').appendChild(pdfViewer); | 979 $('mainview').appendChild(pdfViewer); |
| 926 pdfViewer.onload('onPDFLoad()'); | 980 pdfViewer.onload('onPDFLoad()'); |
| 927 pdfViewer.removePrintButton(); | 981 pdfViewer.removePrintButton(); |
| 928 pdfViewer.grayscale(true); | 982 pdfViewer.grayscale(true); |
| 929 } | 983 } |
| 930 | 984 |
| 931 /** | 985 /** |
| 932 * @return {boolean} true if a compatible pdf plugin exists. | 986 * @return {boolean} true if a compatible pdf plugin exists. |
| 933 */ | 987 */ |
| 934 function checkCompatiblePluginExists() { | 988 function checkCompatiblePluginExists() { |
| 935 var dummyPlugin = $('dummy-viewer') | 989 var dummyPlugin = $('dummy-viewer') |
| 936 return !!(dummyPlugin.onload && | 990 return !!(dummyPlugin.onload && |
| 937 dummyPlugin.goToPage && | 991 dummyPlugin.goToPage && |
| 938 dummyPlugin.removePrintButton && | 992 dummyPlugin.removePrintButton && |
| 939 dummyPlugin.loadPreviewPage && | 993 dummyPlugin.loadPreviewPage && |
| 940 dummyPlugin.printPreviewPageCount); | 994 dummyPlugin.printPreviewPageCount && |
| 995 dummyPlugin.resetPrintPreviewUrl); |
| 941 } | 996 } |
| 942 | 997 |
| 943 window.addEventListener('DOMContentLoaded', onLoad); | 998 window.addEventListener('DOMContentLoaded', onLoad); |
| 944 | 999 |
| 945 /** | 1000 /** |
| 946 * Sets the default values and sends a request to regenerate preview data. | 1001 * Sets the default values and sends a request to regenerate preview data. |
| 947 */ | 1002 */ |
| 948 function setDefaultValuesAndRegeneratePreview() { | 1003 function setDefaultValuesAndRegeneratePreview() { |
| 949 pageSettings.resetState(); | 1004 pageSettings.resetState(); |
| 950 requestPrintPreview(); | 1005 requestPrintPreview(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 968 | 1023 |
| 969 /// Pull in all other scripts in a single shot. | 1024 /// Pull in all other scripts in a single shot. |
| 970 <include src="print_preview_animations.js"/> | 1025 <include src="print_preview_animations.js"/> |
| 971 <include src="print_preview_cloud.js"/> | 1026 <include src="print_preview_cloud.js"/> |
| 972 <include src="print_preview_utils.js"/> | 1027 <include src="print_preview_utils.js"/> |
| 973 <include src="print_header.js"/> | 1028 <include src="print_header.js"/> |
| 974 <include src="page_settings.js"/> | 1029 <include src="page_settings.js"/> |
| 975 <include src="copies_settings.js"/> | 1030 <include src="copies_settings.js"/> |
| 976 <include src="layout_settings.js"/> | 1031 <include src="layout_settings.js"/> |
| 977 <include src="color_settings.js"/> | 1032 <include src="color_settings.js"/> |
| OLD | NEW |