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