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