Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1128)

Side by Side Diff: chrome/browser/resources/print_preview/print_preview.js

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

Powered by Google App Engine
This is Rietveld 408576698