| 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 // State of the print preview settings. |
| 28 var printSettings = new PrintSettings(); |
| 29 |
| 30 // Print ready data index. |
| 31 const PRINT_READY_DATA_INDEX = -1; |
| 32 |
| 27 // The name of the default or last used printer. | 33 // The name of the default or last used printer. |
| 28 var defaultOrLastUsedPrinterName = ''; | 34 var defaultOrLastUsedPrinterName = ''; |
| 29 | 35 |
| 30 // True when a pending print preview request exists. | 36 // True when a pending print preview request exists. |
| 31 var hasPendingPreviewRequest = false; | 37 var hasPendingPreviewRequest = false; |
| 32 | 38 |
| 33 // The ID of the last preview request. | 39 // The ID of the last preview request. |
| 34 var lastPreviewRequestID = -1; | 40 var lastPreviewRequestID = -1; |
| 35 | 41 |
| 36 // The ID of the initial preview request. | 42 // The ID of the initial preview request. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 61 var colorSettings; | 67 var colorSettings; |
| 62 | 68 |
| 63 // True if the user has click 'Advanced...' in order to open the system print | 69 // True if the user has click 'Advanced...' in order to open the system print |
| 64 // dialog. | 70 // dialog. |
| 65 var showingSystemDialog = false; | 71 var showingSystemDialog = false; |
| 66 | 72 |
| 67 // The range of options in the printer dropdown controlled by cloud print. | 73 // The range of options in the printer dropdown controlled by cloud print. |
| 68 var firstCloudPrintOptionPos = 0; | 74 var firstCloudPrintOptionPos = 0; |
| 69 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; | 75 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; |
| 70 | 76 |
| 77 // Store the current previewUid. |
| 78 var currentPreviewUid = ''; |
| 79 |
| 80 // True if we need to generate draft preview data. |
| 81 var generateDraftData = true; |
| 71 | 82 |
| 72 // TODO(abodenha@chromium.org) A lot of cloud print specific logic has | 83 // 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 | 84 // made its way into this file. Refactor to create a cleaner boundary |
| 74 // between print preview and GCP code. Reference bug 88098 when fixing. | 85 // between print preview and GCP code. Reference bug 88098 when fixing. |
| 75 | 86 |
| 76 // A dictionary of cloud printers that have been added to the printer | 87 // A dictionary of cloud printers that have been added to the printer |
| 77 // dropdown. | 88 // dropdown. |
| 78 var addedCloudPrinters = {}; | 89 var addedCloudPrinters = {}; |
| 79 | 90 |
| 80 // The maximum number of cloud printers to allow in the dropdown. | 91 // The maximum number of cloud printers to allow in the dropdown. |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 'duplex': copiesSettings.duplexMode, | 340 'duplex': copiesSettings.duplexMode, |
| 330 'copies': copiesSettings.numberOfCopies, | 341 'copies': copiesSettings.numberOfCopies, |
| 331 'collate': copiesSettings.isCollated(), | 342 'collate': copiesSettings.isCollated(), |
| 332 'landscape': layoutSettings.isLandscape(), | 343 'landscape': layoutSettings.isLandscape(), |
| 333 'color': colorSettings.isColor(), | 344 'color': colorSettings.isColor(), |
| 334 'printToPDF': printToPDF, | 345 'printToPDF': printToPDF, |
| 335 'isFirstRequest' : false, | 346 'isFirstRequest' : false, |
| 336 'headerFooterEnabled': headerFooterSettings.hasHeaderFooter(), | 347 'headerFooterEnabled': headerFooterSettings.hasHeaderFooter(), |
| 337 'defaultMarginsSelected': marginSettings.isDefaultMarginsSelected(), | 348 'defaultMarginsSelected': marginSettings.isDefaultMarginsSelected(), |
| 338 'margins': marginSettings.customMargins, | 349 'margins': marginSettings.customMargins, |
| 339 'requestID': -1}; | 350 'requestID': -1, |
| 351 'generateDraftData': generateDraftData}; |
| 340 | 352 |
| 341 var printerList = $('printer-list'); | 353 var printerList = $('printer-list'); |
| 342 var selectedPrinter = printerList.selectedIndex; | 354 var selectedPrinter = printerList.selectedIndex; |
| 343 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { | 355 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { |
| 344 settings['cloudPrintID'] = | 356 settings['cloudPrintID'] = |
| 345 printerList.options[selectedPrinter].value; | 357 printerList.options[selectedPrinter].value; |
| 346 } | 358 } |
| 347 return settings; | 359 return settings; |
| 348 } | 360 } |
| 349 | 361 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 return ''; | 418 return ''; |
| 407 return printerList.options[selectedPrinter].value; | 419 return printerList.options[selectedPrinter].value; |
| 408 } | 420 } |
| 409 | 421 |
| 410 /** | 422 /** |
| 411 * Asks the browser to print the preview PDF based on current print | 423 * Asks the browser to print the preview PDF based on current print |
| 412 * settings. If the preview is still loading, printPendingFile() will get | 424 * settings. If the preview is still loading, printPendingFile() will get |
| 413 * called once the preview loads. | 425 * called once the preview loads. |
| 414 */ | 426 */ |
| 415 function requestToPrintDocument() { | 427 function requestToPrintDocument() { |
| 416 hasPendingPrintDocumentRequest = hasPendingPreviewRequest; | 428 hasPendingPrintDocumentRequest = hasPendingPrintReadyDocumentRequest; |
| 417 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; | 429 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; |
| 418 | 430 |
| 419 if (hasPendingPrintDocumentRequest) { | 431 if (hasPendingPrintDocumentRequest) { |
| 420 if (printToPDF) { | 432 if (printToPDF) { |
| 421 // TODO(thestig) disable controls here. | 433 // TODO(thestig) disable controls here. |
| 422 } else { | 434 } else { |
| 423 isTabHidden = true; | 435 isTabHidden = true; |
| 424 chrome.send('hidePreview'); | 436 chrome.send('hidePreview'); |
| 425 } | 437 } |
| 426 return; | 438 return; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 function sendPrintDocumentRequest() { | 473 function sendPrintDocumentRequest() { |
| 462 var printerList = $('printer-list'); | 474 var printerList = $('printer-list'); |
| 463 var printer = printerList[printerList.selectedIndex]; | 475 var printer = printerList[printerList.selectedIndex]; |
| 464 chrome.send('saveLastPrinter', [printer.textContent, | 476 chrome.send('saveLastPrinter', [printer.textContent, |
| 465 cloudprint.getData(printer)]); | 477 cloudprint.getData(printer)]); |
| 466 chrome.send('print', [JSON.stringify(getSettings()), | 478 chrome.send('print', [JSON.stringify(getSettings()), |
| 467 cloudprint.getPrintTicketJSON(printer)]); | 479 cloudprint.getPrintTicketJSON(printer)]); |
| 468 } | 480 } |
| 469 | 481 |
| 470 /** | 482 /** |
| 483 * Loads the selected preview pages. |
| 484 */ |
| 485 function loadSelectedPages() { |
| 486 hasPendingPreviewRequest = false; |
| 487 pageSettings.updatePageSelection(); |
| 488 var pageSet = pageSettings.previouslySelectedPages; |
| 489 var pageCount = pageSet.length; |
| 490 if (pageCount == 0 || currentPreviewUid == '') |
| 491 return; |
| 492 |
| 493 for (var i = 0; i < pageCount; i++) |
| 494 onDidPreviewPage(pageSet[i] - 1, currentPreviewUid, lastPreviewRequestID); |
| 495 } |
| 496 |
| 497 /** |
| 471 * Asks the browser to generate a preview PDF based on current print settings. | 498 * Asks the browser to generate a preview PDF based on current print settings. |
| 472 */ | 499 */ |
| 473 function requestPrintPreview() { | 500 function requestPrintPreview() { |
| 474 hasPendingPreviewRequest = true; | |
| 475 layoutSettings.updateState(); | |
| 476 if (!isTabHidden) | 501 if (!isTabHidden) |
| 477 showLoadingAnimation(); | 502 showLoadingAnimation(); |
| 478 | 503 |
| 479 chrome.send('getPreview', [JSON.stringify(getSettingsWithRequestID())]); | 504 if (!hasPendingPreviewRequest && previewModifiable && |
| 505 hasOnlyPageSettingsChanged()) { |
| 506 loadSelectedPages(); |
| 507 generateDraftData = false; |
| 508 } else { |
| 509 hasPendingPreviewRequest = true; |
| 510 generateDraftData = true; |
| 511 pageSettings.updatePageSelection(); |
| 512 } |
| 513 |
| 514 printSettings.save(); |
| 515 layoutSettings.updateState(); |
| 516 hasPendingPrintReadyDocumentRequest = true; |
| 517 |
| 518 var totalPageCount = pageSettings.totalPageCount; |
| 519 if (!previewModifiable && totalPageCount > 0) |
| 520 generateDraftData = false; |
| 521 |
| 522 var pageCount = totalPageCount != undefined ? totalPageCount : -1; |
| 523 chrome.send('getPreview', [JSON.stringify(getSettingsWithRequestID()), |
| 524 String(pageCount), |
| 525 previewModifiable]); |
| 480 } | 526 } |
| 481 | 527 |
| 482 /** | 528 /** |
| 483 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print | 529 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print |
| 484 * preview tab regarding the file selection cancel event. | 530 * preview tab regarding the file selection cancel event. |
| 485 */ | 531 */ |
| 486 function fileSelectionCancelled() { | 532 function fileSelectionCancelled() { |
| 487 // TODO(thestig) re-enable controls here. | 533 // TODO(thestig) re-enable controls here. |
| 488 } | 534 } |
| 489 | 535 |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 883 * this response. | 929 * this response. |
| 884 */ | 930 */ |
| 885 function onDidPreviewPage(pageNumber, previewUid, previewResponseId) { | 931 function onDidPreviewPage(pageNumber, previewUid, previewResponseId) { |
| 886 if (!isExpectedPreviewResponse(previewResponseId)) | 932 if (!isExpectedPreviewResponse(previewResponseId)) |
| 887 return; | 933 return; |
| 888 | 934 |
| 889 // Refactor | 935 // Refactor |
| 890 if (!previewModifiable) | 936 if (!previewModifiable) |
| 891 return; | 937 return; |
| 892 | 938 |
| 893 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); | |
| 894 | |
| 895 if (pageSettings.requestPrintPreviewIfNeeded()) | 939 if (pageSettings.requestPrintPreviewIfNeeded()) |
| 896 return; | 940 return; |
| 897 if (pageIndex == 0) | 941 |
| 898 createPDFPlugin(previewUid); | 942 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); |
| 943 if (pageIndex == -1) |
| 944 return; |
| 945 |
| 946 currentPreviewUid = previewUid; |
| 947 if (pageIndex == 0) { |
| 948 createPDFPlugin(pageNumber); |
| 949 hasPendingPreviewRequest = false; |
| 950 } |
| 899 | 951 |
| 900 $('pdf-viewer').loadPreviewPage( | 952 $('pdf-viewer').loadPreviewPage( |
| 901 getPageSrcURL(previewUid, pageNumber), pageIndex); | 953 getPageSrcURL(previewUid, pageNumber), pageIndex); |
| 902 } | 954 } |
| 903 | 955 |
| 904 /** | 956 /** |
| 905 * Update the print preview when new preview data is available. | 957 * Update the print preview when new preview data is available. |
| 906 * Create the PDF plugin as needed. | 958 * Create the PDF plugin as needed. |
| 907 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 959 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
| 908 * @param {boolean} modifiable If the preview is modifiable. | 960 * @param {boolean} modifiable If the preview is modifiable. |
| 909 * @param {string} previewUid Preview unique identifier. | 961 * @param {string} previewUid Preview unique identifier. |
| 910 * @param {number} previewResponseId The preview request id that resulted in | 962 * @param {number} previewResponseId The preview request id that resulted in |
| 911 * this response. | 963 * this response. |
| 912 */ | 964 */ |
| 913 function updatePrintPreview(previewUid, previewResponseId) { | 965 function updatePrintPreview(previewUid, previewResponseId) { |
| 914 if (!isExpectedPreviewResponse(previewResponseId)) | 966 if (!isExpectedPreviewResponse(previewResponseId)) |
| 915 return; | 967 return; |
| 916 hasPendingPreviewRequest = false; | 968 hasPendingPreviewRequest = false; |
| 969 hasPendingPrintReadyDocumentRequest = false; |
| 917 | 970 |
| 918 if (!previewModifiable) { | 971 if (!previewModifiable) { |
| 919 // If the preview is not modifiable the plugin has not been created yet. | 972 // If the preview is not modifiable the plugin has not been created yet. |
| 920 createPDFPlugin(previewUid); | 973 currentPreviewUid = previewUid; |
| 974 createPDFPlugin(PRINT_READY_DATA_INDEX); |
| 921 } | 975 } |
| 922 | 976 |
| 923 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 977 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
| 924 | 978 |
| 925 if (hasPendingPrintDocumentRequest) | 979 if (hasPendingPrintDocumentRequest) |
| 926 requestToPrintPendingDocument(); | 980 requestToPrintPendingDocument(); |
| 927 } | 981 } |
| 928 | 982 |
| 929 /** | 983 /** |
| 984 * Check if only page selection has been changed since the last preview request |
| 985 * and is valid. |
| 986 * @return {boolean} true if the new page selection is valid. |
| 987 */ |
| 988 function hasOnlyPageSettingsChanged() { |
| 989 var tempPrintSettings = new PrintSettings(); |
| 990 tempPrintSettings.save(); |
| 991 |
| 992 return !!(printSettings.deviceName == tempPrintSettings.deviceName && |
| 993 printSettings.isLandscape == tempPrintSettings.isLandscape && |
| 994 printSettings.hasHeaderFooter == |
| 995 tempPrintSettings.hasHeaderFooter && |
| 996 pageSettings.hasPageSelectionChangedAndIsValid()); |
| 997 } |
| 998 |
| 999 /** |
| 930 * Create the PDF plugin or reload the existing one. | 1000 * Create the PDF plugin or reload the existing one. |
| 931 * @param {string} previewUid Preview unique identifier. | 1001 * @param {number} srcDataIndex Preview data source index. |
| 932 */ | 1002 */ |
| 933 function createPDFPlugin(previewUid) { | 1003 function createPDFPlugin(srcDataIndex) { |
| 934 var pdfViewer = $('pdf-viewer'); | 1004 var pdfViewer = $('pdf-viewer'); |
| 1005 var srcURL = getPageSrcURL(currentPreviewUid, srcDataIndex); |
| 935 if (pdfViewer) { | 1006 if (pdfViewer) { |
| 936 // Need to call this before the reload(), where the plugin resets its | 1007 // Need to call this before the reload(), where the plugin resets its |
| 937 // internal page count. | 1008 // internal page count. |
| 938 pdfViewer.goToPage('0'); | 1009 pdfViewer.goToPage('0'); |
| 1010 pdfViewer.resetPrintPreviewUrl(srcURL); |
| 939 pdfViewer.reload(); | 1011 pdfViewer.reload(); |
| 940 pdfViewer.grayscale(!colorSettings.isColor()); | 1012 pdfViewer.grayscale(!colorSettings.isColor()); |
| 941 return; | 1013 return; |
| 942 } | 1014 } |
| 943 | 1015 |
| 944 // Get the complete preview document. | |
| 945 var dataIndex = previewModifiable ? '0' : '-1'; | |
| 946 | |
| 947 pdfViewer = document.createElement('embed'); | 1016 pdfViewer = document.createElement('embed'); |
| 948 pdfViewer.setAttribute('id', 'pdf-viewer'); | 1017 pdfViewer.setAttribute('id', 'pdf-viewer'); |
| 949 pdfViewer.setAttribute('type', | 1018 pdfViewer.setAttribute('type', |
| 950 'application/x-google-chrome-print-preview-pdf'); | 1019 'application/x-google-chrome-print-preview-pdf'); |
| 951 pdfViewer.setAttribute('src', getPageSrcURL(previewUid, dataIndex)); | 1020 pdfViewer.setAttribute('src', srcURL); |
| 952 pdfViewer.setAttribute('aria-live', 'polite'); | 1021 pdfViewer.setAttribute('aria-live', 'polite'); |
| 953 pdfViewer.setAttribute('aria-atomic', 'true'); | 1022 pdfViewer.setAttribute('aria-atomic', 'true'); |
| 954 $('mainview').appendChild(pdfViewer); | 1023 $('mainview').appendChild(pdfViewer); |
| 955 pdfViewer.onload('onPDFLoad()'); | 1024 pdfViewer.onload('onPDFLoad()'); |
| 956 pdfViewer.removePrintButton(); | 1025 pdfViewer.removePrintButton(); |
| 957 pdfViewer.grayscale(true); | 1026 pdfViewer.grayscale(true); |
| 958 } | 1027 } |
| 959 | 1028 |
| 960 /** | 1029 /** |
| 961 * @return {boolean} true if a compatible pdf plugin exists. | 1030 * @return {boolean} true if a compatible pdf plugin exists. |
| 962 */ | 1031 */ |
| 963 function checkCompatiblePluginExists() { | 1032 function checkCompatiblePluginExists() { |
| 964 var dummyPlugin = $('dummy-viewer') | 1033 var dummyPlugin = $('dummy-viewer') |
| 965 return !!(dummyPlugin.onload && | 1034 return !!(dummyPlugin.onload && |
| 966 dummyPlugin.goToPage && | 1035 dummyPlugin.goToPage && |
| 967 dummyPlugin.removePrintButton && | 1036 dummyPlugin.removePrintButton && |
| 968 dummyPlugin.loadPreviewPage && | 1037 dummyPlugin.loadPreviewPage && |
| 969 dummyPlugin.printPreviewPageCount); | 1038 dummyPlugin.printPreviewPageCount && |
| 1039 dummyPlugin.resetPrintPreviewUrl); |
| 970 } | 1040 } |
| 971 | 1041 |
| 972 window.addEventListener('DOMContentLoaded', onLoad); | 1042 window.addEventListener('DOMContentLoaded', onLoad); |
| 973 | 1043 |
| 974 /** | 1044 /** |
| 975 * Sets the default values and sends a request to regenerate preview data. | 1045 * Sets the default values and sends a request to regenerate preview data. |
| 976 * Resets the margin options only if |resetMargins| is true. | 1046 * Resets the margin options only if |resetMargins| is true. |
| 977 */ | 1047 */ |
| 978 function setDefaultValuesAndRegeneratePreview(resetMargins) { | 1048 function setDefaultValuesAndRegeneratePreview(resetMargins) { |
| 979 if (resetMargins) | 1049 if (resetMargins) |
| 980 marginSettings.resetMarginsIfNeeded(); | 1050 marginSettings.resetMarginsIfNeeded(); |
| 981 pageSettings.resetState(); | 1051 pageSettings.resetState(); |
| 982 requestPrintPreview(); | 1052 requestPrintPreview(); |
| 983 } | 1053 } |
| 984 | 1054 |
| 1055 /** |
| 1056 * Class that represents the state of the print settings. |
| 1057 */ |
| 1058 function PrintSettings() { |
| 1059 this.deviceName = ''; |
| 1060 this.isLandscape = ''; |
| 1061 this.hasHeaderFooter = ''; |
| 1062 } |
| 1063 |
| 1064 /** |
| 1065 * Takes a snapshot of the print settings. |
| 1066 */ |
| 1067 PrintSettings.prototype.save = function() { |
| 1068 this.deviceName = getSelectedPrinterName(); |
| 1069 this.isLandscape = layoutSettings.isLandscape(); |
| 1070 this.hasHeaderFooter = headerFooterSettings.hasHeaderFooter(); |
| 1071 } |
| 1072 |
| 985 /// Pull in all other scripts in a single shot. | 1073 /// Pull in all other scripts in a single shot. |
| 986 <include src="print_preview_animations.js"/> | 1074 <include src="print_preview_animations.js"/> |
| 987 <include src="print_preview_cloud.js"/> | 1075 <include src="print_preview_cloud.js"/> |
| 988 <include src="print_preview_utils.js"/> | 1076 <include src="print_preview_utils.js"/> |
| 989 <include src="print_header.js"/> | 1077 <include src="print_header.js"/> |
| 990 <include src="page_settings.js"/> | 1078 <include src="page_settings.js"/> |
| 991 <include src="copies_settings.js"/> | 1079 <include src="copies_settings.js"/> |
| 992 <include src="header_footer_settings.js"/> | 1080 <include src="header_footer_settings.js"/> |
| 993 <include src="layout_settings.js"/> | 1081 <include src="layout_settings.js"/> |
| 994 <include src="color_settings.js"/> | 1082 <include src="color_settings.js"/> |
| 995 <include src="margin_settings.js"/> | 1083 <include src="margin_settings.js"/> |
| OLD | NEW |