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