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 // The name of the default or last used printer. | 30 // The name of the default or last used printer. |
28 var defaultOrLastUsedPrinterName = ''; | 31 var defaultOrLastUsedPrinterName = ''; |
29 | 32 |
30 // True when a pending print preview request exists. | 33 // True when a pending print preview request exists. |
31 var hasPendingPreviewRequest = false; | 34 var hasPendingPreviewRequest = false; |
32 | 35 |
33 // The ID of the last preview request. | 36 // The ID of the last preview request. |
34 var lastPreviewRequestID = -1; | 37 var lastPreviewRequestID = -1; |
35 | 38 |
36 // The ID of the initial preview request. | 39 // The ID of the initial 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 var settings = | 299 var settings = |
292 {'deviceName': deviceName, | 300 {'deviceName': deviceName, |
293 'pageRange': pageSettings.selectedPageRanges, | 301 'pageRange': pageSettings.selectedPageRanges, |
294 'duplex': copiesSettings.duplexMode, | 302 'duplex': copiesSettings.duplexMode, |
295 'copies': copiesSettings.numberOfCopies, | 303 'copies': copiesSettings.numberOfCopies, |
296 'collate': copiesSettings.isCollated(), | 304 'collate': copiesSettings.isCollated(), |
297 'landscape': layoutSettings.isLandscape(), | 305 'landscape': layoutSettings.isLandscape(), |
298 'color': colorSettings.isColor(), | 306 'color': colorSettings.isColor(), |
299 'printToPDF': printToPDF, | 307 'printToPDF': printToPDF, |
300 'isFirstRequest' : false, | 308 'isFirstRequest' : false, |
301 'requestID': -1}; | 309 'requestID': -1, |
| 310 'generateDraftData': generateDraftData}; |
302 | 311 |
303 var printerList = $('printer-list'); | 312 var printerList = $('printer-list'); |
304 var selectedPrinter = printerList.selectedIndex; | 313 var selectedPrinter = printerList.selectedIndex; |
305 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { | 314 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { |
306 settings['cloudPrintID'] = | 315 settings['cloudPrintID'] = |
307 printerList.options[selectedPrinter].value; | 316 printerList.options[selectedPrinter].value; |
308 } | 317 } |
309 return settings; | 318 return settings; |
310 } | 319 } |
311 | 320 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 function sendPrintDocumentRequest() { | 432 function sendPrintDocumentRequest() { |
424 var printerList = $('printer-list'); | 433 var printerList = $('printer-list'); |
425 var printer = printerList[printerList.selectedIndex]; | 434 var printer = printerList[printerList.selectedIndex]; |
426 chrome.send('saveLastPrinter', [printer.textContent, | 435 chrome.send('saveLastPrinter', [printer.textContent, |
427 cloudprint.getData(printer)]); | 436 cloudprint.getData(printer)]); |
428 chrome.send('print', [JSON.stringify(getSettings()), | 437 chrome.send('print', [JSON.stringify(getSettings()), |
429 cloudprint.getPrintTicketJSON(printer)]); | 438 cloudprint.getPrintTicketJSON(printer)]); |
430 } | 439 } |
431 | 440 |
432 /** | 441 /** |
| 442 * Loads the selected preview pages. |
| 443 */ |
| 444 function loadSelectedPages() { |
| 445 hasPendingPreviewRequest = false; |
| 446 pageSettings.updatePageSelection(); |
| 447 var pageSet = pageSettings.previouslySelectedPages; |
| 448 var pageCount = pageSet.length; |
| 449 if (pageCount == 0 || currentPreviewUid == '') |
| 450 return; |
| 451 |
| 452 for (var i = 0; i < pageCount; i++) |
| 453 onDidPreviewPage(pageSet[i] - 1, currentPreviewUid); |
| 454 addEventListeners(); |
| 455 } |
| 456 |
| 457 /** |
433 * Asks the browser to generate a preview PDF based on current print settings. | 458 * Asks the browser to generate a preview PDF based on current print settings. |
434 */ | 459 */ |
435 function requestPrintPreview() { | 460 function requestPrintPreview() { |
436 hasPendingPreviewRequest = true; | 461 hasPendingPreviewRequest = true; |
437 layoutSettings.updateState(); | 462 layoutSettings.updateState(); |
| 463 generateDraftData = true; |
438 if (!isTabHidden) | 464 if (!isTabHidden) |
439 showLoadingAnimation(); | 465 showLoadingAnimation(); |
440 | 466 |
| 467 if (previewModifiable && hasOnlyPageSettingsChanged()) { |
| 468 loadSelectedPages(); |
| 469 generateDraftData = false; |
| 470 } else { |
| 471 pageSettings.updatePageSelection(); |
| 472 } |
| 473 |
| 474 if (!previewModifiable && pageSettings.totalPageCount > 0) |
| 475 generateDraftData = false; |
| 476 |
441 chrome.send('getPreview', [JSON.stringify(getSettingsWithRequestID())]); | 477 chrome.send('getPreview', [JSON.stringify(getSettingsWithRequestID())]); |
442 } | 478 } |
443 | 479 |
444 /** | 480 /** |
445 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print | 481 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print |
446 * preview tab regarding the file selection cancel event. | 482 * preview tab regarding the file selection cancel event. |
447 */ | 483 */ |
448 function fileSelectionCancelled() { | 484 function fileSelectionCancelled() { |
449 // TODO(thestig) re-enable controls here. | 485 // TODO(thestig) re-enable controls here. |
450 } | 486 } |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 $('system-dialog-throbber').classList.add('hidden'); | 799 $('system-dialog-throbber').classList.add('hidden'); |
764 $('native-print-dialog-throbber').classList.add('hidden'); | 800 $('native-print-dialog-throbber').classList.add('hidden'); |
765 displayErrorMessage(errorMessage); | 801 displayErrorMessage(errorMessage); |
766 } | 802 } |
767 | 803 |
768 /** | 804 /** |
769 * Display an error message when print preview fails. | 805 * Display an error message when print preview fails. |
770 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed(). | 806 * Called from PrintPreviewMessageHandler::OnPrintPreviewFailed(). |
771 */ | 807 */ |
772 function printPreviewFailed() { | 808 function printPreviewFailed() { |
| 809 disableInputElementsInSidebar(); |
773 displayErrorMessage(localStrings.getString('previewFailed')); | 810 displayErrorMessage(localStrings.getString('previewFailed')); |
774 } | 811 } |
775 | 812 |
776 /** | 813 /** |
777 * Called when the PDF plugin loads its document. | 814 * Called when the PDF plugin loads its document. |
778 */ | 815 */ |
779 function onPDFLoad() { | 816 function onPDFLoad() { |
780 if (previewModifiable) { | 817 if (previewModifiable) { |
781 setPluginPreviewPageCount(); | 818 setPluginPreviewPageCount(); |
782 } | 819 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 return; | 877 return; |
841 | 878 |
842 // Refactor | 879 // Refactor |
843 if (!previewModifiable) | 880 if (!previewModifiable) |
844 return; | 881 return; |
845 | 882 |
846 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); | 883 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); |
847 | 884 |
848 if (pageSettings.requestPrintPreviewIfNeeded()) | 885 if (pageSettings.requestPrintPreviewIfNeeded()) |
849 return; | 886 return; |
| 887 |
| 888 if (pageIndex == -1) |
| 889 return; |
| 890 |
| 891 currentPreviewUid = previewUid; |
850 if (pageIndex == 0) | 892 if (pageIndex == 0) |
851 createPDFPlugin(previewUid); | 893 createPDFPlugin(pageNumber); |
852 | 894 |
853 $('pdf-viewer').loadPreviewPage( | 895 $('pdf-viewer').loadPreviewPage( |
854 getPageSrcURL(previewUid, pageNumber), pageIndex); | 896 getPageSrcURL(previewUid, pageNumber), pageIndex); |
855 } | 897 } |
856 | 898 |
857 /** | 899 /** |
858 * Update the print preview when new preview data is available. | 900 * Update the print preview when new preview data is available. |
859 * Create the PDF plugin as needed. | 901 * Create the PDF plugin as needed. |
860 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 902 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
861 * @param {string} jobTitle The print job title. | 903 * @param {string} jobTitle The print job title. |
862 * @param {boolean} modifiable If the preview is modifiable. | 904 * @param {boolean} modifiable If the preview is modifiable. |
863 * @param {string} previewUid Preview unique identifier. | 905 * @param {string} previewUid Preview unique identifier. |
864 * @param {number} previewResponseId The preview request id that resulted in | 906 * @param {number} previewResponseId The preview request id that resulted in |
865 * this response. | 907 * this response. |
866 */ | 908 */ |
867 function updatePrintPreview(jobTitle, | 909 function updatePrintPreview(jobTitle, |
868 previewUid, | 910 previewUid, |
869 previewResponseId) { | 911 previewResponseId) { |
870 if (!isExpectedPreviewResponse(previewResponseId)) | 912 if (!isExpectedPreviewResponse(previewResponseId)) |
871 return; | 913 return; |
872 hasPendingPreviewRequest = false; | 914 hasPendingPreviewRequest = false; |
873 | 915 |
874 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 916 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
875 | 917 |
876 if (!previewModifiable) { | 918 if (!previewModifiable) { |
877 // If the preview is not modifiable the plugin has not been created yet. | 919 // If the preview is not modifiable the plugin has not been created yet. |
878 createPDFPlugin(previewUid); | 920 currentPreviewUid = previewUid; |
| 921 createPDFPlugin(PRINT_READY_DATA_INDEX); |
879 } | 922 } |
880 | 923 |
881 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 924 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
882 | 925 |
883 if (hasPendingPrintDocumentRequest) | 926 if (hasPendingPrintDocumentRequest) |
884 requestToPrintPendingDocument(); | 927 requestToPrintPendingDocument(); |
885 } | 928 } |
886 | 929 |
887 /** | 930 /** |
| 931 * Check if only page selection has been changed since the last preview request |
| 932 * and is valid. |
| 933 * @return {boolean} true if the new page selection is valid. |
| 934 */ |
| 935 function hasOnlyPageSettingsChanged() { |
| 936 var tempPrintSettings = new PrintSettings(); |
| 937 tempPrintSettings.save(); |
| 938 |
| 939 return !!(printSettings.deviceName == tempPrintSettings.deviceName && |
| 940 printSettings.isLandscape == tempPrintSettings.isLandscape && |
| 941 pageSettings.hasPageSelectionChangedAndIsValid()); |
| 942 } |
| 943 |
| 944 /** |
888 * Create the PDF plugin or reload the existing one. | 945 * Create the PDF plugin or reload the existing one. |
889 * @param {string} previewUid Preview unique identifier. | 946 * @param {number} srcDataIndex Preview data source index. |
890 */ | 947 */ |
891 function createPDFPlugin(previewUid) { | 948 function createPDFPlugin(srcDataIndex) { |
892 var pdfViewer = $('pdf-viewer'); | 949 var pdfViewer = $('pdf-viewer'); |
| 950 var srcURL = getPageSrcURL(currentPreviewUid, srcDataIndex); |
893 if (pdfViewer) { | 951 if (pdfViewer) { |
894 // Need to call this before the reload(), where the plugin resets its | 952 // Need to call this before the reload(), where the plugin resets its |
895 // internal page count. | 953 // internal page count. |
896 pdfViewer.goToPage('0'); | 954 pdfViewer.goToPage('0'); |
| 955 pdfViewer.resetPrintPreviewUrl(srcURL); |
897 pdfViewer.reload(); | 956 pdfViewer.reload(); |
898 pdfViewer.grayscale(!colorSettings.isColor()); | 957 pdfViewer.grayscale(!colorSettings.isColor()); |
899 return; | 958 return; |
900 } | 959 } |
901 | 960 |
902 // Get the complete preview document. | |
903 var dataIndex = previewModifiable ? '0' : '-1'; | |
904 | |
905 pdfViewer = document.createElement('embed'); | 961 pdfViewer = document.createElement('embed'); |
906 pdfViewer.setAttribute('id', 'pdf-viewer'); | 962 pdfViewer.setAttribute('id', 'pdf-viewer'); |
907 pdfViewer.setAttribute('type', | 963 pdfViewer.setAttribute('type', |
908 'application/x-google-chrome-print-preview-pdf'); | 964 'application/x-google-chrome-print-preview-pdf'); |
909 pdfViewer.setAttribute('src', getPageSrcURL(previewUid, dataIndex)); | 965 pdfViewer.setAttribute('src', srcURL); |
910 pdfViewer.setAttribute('aria-live', 'polite'); | 966 pdfViewer.setAttribute('aria-live', 'polite'); |
911 pdfViewer.setAttribute('aria-atomic', 'true'); | 967 pdfViewer.setAttribute('aria-atomic', 'true'); |
912 $('mainview').appendChild(pdfViewer); | 968 $('mainview').appendChild(pdfViewer); |
913 pdfViewer.onload('onPDFLoad()'); | 969 pdfViewer.onload('onPDFLoad()'); |
914 pdfViewer.removePrintButton(); | 970 pdfViewer.removePrintButton(); |
915 pdfViewer.grayscale(true); | 971 pdfViewer.grayscale(true); |
916 } | 972 } |
917 | 973 |
918 /** | 974 /** |
919 * @return {boolean} true if a compatible pdf plugin exists. | 975 * @return {boolean} true if a compatible pdf plugin exists. |
920 */ | 976 */ |
921 function checkCompatiblePluginExists() { | 977 function checkCompatiblePluginExists() { |
922 var dummyPlugin = $('dummy-viewer') | 978 var dummyPlugin = $('dummy-viewer') |
923 return !!(dummyPlugin.onload && | 979 return !!(dummyPlugin.onload && |
924 dummyPlugin.goToPage && | 980 dummyPlugin.goToPage && |
925 dummyPlugin.removePrintButton && | 981 dummyPlugin.removePrintButton && |
926 dummyPlugin.loadPreviewPage && | 982 dummyPlugin.loadPreviewPage && |
927 dummyPlugin.printPreviewPageCount); | 983 dummyPlugin.printPreviewPageCount && |
| 984 dummyPlugin.resetPrintPreviewUrl); |
928 } | 985 } |
929 | 986 |
930 window.addEventListener('DOMContentLoaded', onLoad); | 987 window.addEventListener('DOMContentLoaded', onLoad); |
931 | 988 |
932 /** | 989 /** |
933 * Sets the default values and sends a request to regenerate preview data. | 990 * Sets the default values and sends a request to regenerate preview data. |
934 */ | 991 */ |
935 function setDefaultValuesAndRegeneratePreview() { | 992 function setDefaultValuesAndRegeneratePreview() { |
936 pageSettings.resetState(); | 993 pageSettings.resetState(); |
937 requestPrintPreview(); | 994 requestPrintPreview(); |
938 } | 995 } |
939 | 996 |
940 /// Pull in all other scripts in a single shot. | 997 /// Pull in all other scripts in a single shot. |
941 <include src="print_preview_animations.js"/> | 998 <include src="print_preview_animations.js"/> |
942 <include src="print_preview_cloud.js"/> | 999 <include src="print_preview_cloud.js"/> |
943 <include src="print_preview_utils.js"/> | 1000 <include src="print_preview_utils.js"/> |
944 <include src="print_header.js"/> | 1001 <include src="print_header.js"/> |
945 <include src="page_settings.js"/> | 1002 <include src="page_settings.js"/> |
946 <include src="copies_settings.js"/> | 1003 <include src="copies_settings.js"/> |
947 <include src="layout_settings.js"/> | 1004 <include src="layout_settings.js"/> |
948 <include src="color_settings.js"/> | 1005 <include src="color_settings.js"/> |
OLD | NEW |