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 // The name of the default or last used printer. | 27 // The name of the default or last used printer. |
31 var defaultOrLastUsedPrinterName = ''; | 28 var defaultOrLastUsedPrinterName = ''; |
32 | 29 |
33 // True when a pending print preview request exists. | 30 // True when a pending print preview request exists. |
34 var hasPendingPreviewRequest = false; | 31 var hasPendingPreviewRequest = false; |
35 | 32 |
36 // The ID of the last preview request. | 33 // The ID of the last preview request. |
37 var lastPreviewRequestID = -1; | 34 var lastPreviewRequestID = -1; |
38 | 35 |
36 // The ID of the initial preview request. | |
37 var initialPreviewRequestID = -1; | |
38 | |
39 // True when a pending print file request exists. | 39 // True when a pending print file request exists. |
40 var hasPendingPrintDocumentRequest = false; | 40 var hasPendingPrintDocumentRequest = false; |
41 | 41 |
42 // True when preview tab is hidden. | 42 // True when preview tab is hidden. |
43 var isTabHidden = false; | 43 var isTabHidden = false; |
44 | 44 |
45 // Object holding all the pages related settings. | 45 // Object holding all the pages related settings. |
46 var pageSettings; | 46 var pageSettings; |
47 | 47 |
48 // Object holding all the copies related settings. | 48 // Object holding all the copies related settings. |
(...skipping 18 matching lines...) Expand all Loading... | |
67 // 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 |
68 // between print preview and GCP code. Reference bug 88098 when fixing. | 68 // between print preview and GCP code. Reference bug 88098 when fixing. |
69 | 69 |
70 // 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 |
71 // dropdown. | 71 // dropdown. |
72 var addedCloudPrinters = {}; | 72 var addedCloudPrinters = {}; |
73 | 73 |
74 // The maximum number of cloud printers to allow in the dropdown. | 74 // The maximum number of cloud printers to allow in the dropdown. |
75 const maxCloudPrinters = 10; | 75 const maxCloudPrinters = 10; |
76 | 76 |
77 const MIN_REQUEST_ID = 0; | |
78 const MAX_REQUEST_ID = 32000; | |
79 | |
77 /** | 80 /** |
78 * Window onload handler, sets up the page and starts print preview by getting | 81 * Window onload handler, sets up the page and starts print preview by getting |
79 * the printer list. | 82 * the printer list. |
80 */ | 83 */ |
81 function onLoad() { | 84 function onLoad() { |
82 cr.enablePlatformSpecificCSSRules(); | 85 cr.enablePlatformSpecificCSSRules(); |
86 initialPreviewRequestID = randomInteger(MIN_REQUEST_ID, MAX_REQUEST_ID); | |
87 lastPreviewRequestID = initialPreviewRequestID; | |
83 | 88 |
84 if (!checkCompatiblePluginExists()) { | 89 if (!checkCompatiblePluginExists()) { |
85 disableInputElementsInSidebar(); | 90 disableInputElementsInSidebar(); |
86 displayErrorMessageWithButton(localStrings.getString('noPlugin'), | 91 displayErrorMessageWithButton(localStrings.getString('noPlugin'), |
87 localStrings.getString('launchNativeDialog'), | 92 localStrings.getString('launchNativeDialog'), |
88 launchNativePrintDialog); | 93 launchNativePrintDialog); |
89 $('mainview').parentElement.removeChild($('dummy-viewer')); | 94 $('mainview').parentElement.removeChild($('dummy-viewer')); |
90 return; | 95 return; |
91 } | 96 } |
92 | 97 |
93 $('system-dialog-link').addEventListener('click', onSystemDialogLinkClicked); | 98 $('system-dialog-link').addEventListener('click', onSystemDialogLinkClicked); |
94 $('mainview').parentElement.removeChild($('dummy-viewer')); | 99 $('mainview').parentElement.removeChild($('dummy-viewer')); |
95 | 100 |
96 $('printer-list').disabled = true; | 101 $('printer-list').disabled = true; |
97 | 102 |
98 printHeader = print_preview.PrintHeader.getInstance(); | 103 printHeader = print_preview.PrintHeader.getInstance(); |
99 pageSettings = print_preview.PageSettings.getInstance(); | 104 pageSettings = print_preview.PageSettings.getInstance(); |
100 copiesSettings = print_preview.CopiesSettings.getInstance(); | 105 copiesSettings = print_preview.CopiesSettings.getInstance(); |
101 layoutSettings = print_preview.LayoutSettings.getInstance(); | 106 layoutSettings = print_preview.LayoutSettings.getInstance(); |
102 colorSettings = print_preview.ColorSettings.getInstance(); | 107 colorSettings = print_preview.ColorSettings.getInstance(); |
103 printHeader.addEventListeners(); | 108 printHeader.addEventListeners(); |
104 pageSettings.addEventListeners(); | 109 pageSettings.addEventListeners(); |
105 copiesSettings.addEventListeners(); | 110 copiesSettings.addEventListeners(); |
106 layoutSettings.addEventListeners(); | 111 layoutSettings.addEventListeners(); |
107 colorSettings.addEventListeners(); | 112 colorSettings.addEventListeners(); |
113 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | |
108 | 114 |
109 showLoadingAnimation(); | 115 showLoadingAnimation(); |
110 chrome.send('getDefaultPrinter'); | 116 chrome.send('getDefaultPrinter'); |
111 } | 117 } |
112 | 118 |
113 /** | 119 /** |
114 * Adds event listeners to the settings controls. | |
115 */ | |
116 function addEventListeners() { | |
117 // Controls that require preview rendering. | |
118 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | |
119 } | |
120 | |
121 /** | |
122 * Removes event listeners from the settings controls. | |
123 */ | |
124 function removeEventListeners() { | |
125 if (pageSettings) | |
126 clearTimeout(pageSettings.timerId_); | |
127 | |
128 // Controls that require preview rendering | |
129 $('printer-list').onchange = null; | |
130 } | |
131 | |
132 /** | |
133 * Disables the input elements in the sidebar. | 120 * Disables the input elements in the sidebar. |
134 */ | 121 */ |
135 function disableInputElementsInSidebar() { | 122 function disableInputElementsInSidebar() { |
136 var els = $('sidebar').querySelectorAll('input, button, select'); | 123 var els = $('sidebar').querySelectorAll('input, button, select'); |
137 for (var i = 0; i < els.length; i++) | 124 for (var i = 0; i < els.length; i++) |
138 els[i].disabled = true; | 125 els[i].disabled = true; |
139 } | 126 } |
140 | 127 |
141 /** | 128 /** |
142 * Disables the controls in the sidebar, shows the throbber and instructs the | 129 * Disables the controls in the sidebar, shows the throbber and instructs the |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 * | 284 * |
298 * @return {Object} Object containing print job settings. | 285 * @return {Object} Object containing print job settings. |
299 */ | 286 */ |
300 function getSettings() { | 287 function getSettings() { |
301 var deviceName = getSelectedPrinterName(); | 288 var deviceName = getSelectedPrinterName(); |
302 var printToPDF = (deviceName == PRINT_TO_PDF); | 289 var printToPDF = (deviceName == PRINT_TO_PDF); |
303 | 290 |
304 var settings = | 291 var settings = |
305 {'deviceName': deviceName, | 292 {'deviceName': deviceName, |
306 'pageRange': pageSettings.selectedPageRanges, | 293 'pageRange': pageSettings.selectedPageRanges, |
307 'printAll': pageSettings.allPagesRadioButton.checked, | |
308 'duplex': copiesSettings.duplexMode, | 294 'duplex': copiesSettings.duplexMode, |
309 'copies': copiesSettings.numberOfCopies, | 295 'copies': copiesSettings.numberOfCopies, |
310 'collate': copiesSettings.isCollated(), | 296 'collate': copiesSettings.isCollated(), |
311 'landscape': layoutSettings.isLandscape(), | 297 'landscape': layoutSettings.isLandscape(), |
312 'color': colorSettings.isColor(), | 298 'color': colorSettings.isColor(), |
313 'printToPDF': printToPDF, | 299 'printToPDF': printToPDF, |
314 'requestID': 0}; | 300 'isFirstRequest' : false, |
301 'requestID': -1}; | |
315 | 302 |
316 var printerList = $('printer-list'); | 303 var printerList = $('printer-list'); |
317 var selectedPrinter = printerList.selectedIndex; | 304 var selectedPrinter = printerList.selectedIndex; |
318 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { | 305 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) { |
319 settings['cloudPrintID'] = | 306 settings['cloudPrintID'] = |
320 printerList.options[selectedPrinter].value; | 307 printerList.options[selectedPrinter].value; |
321 } | 308 } |
322 return settings; | 309 return settings; |
323 } | 310 } |
324 | 311 |
325 /** | 312 /** |
313 * Creates an object based on the values in the printer settings. | |
314 * Note: |lastPreviewRequestID| is being modified every time this function is | |
315 * called. Only call this function when a preview request is actually sent, | |
316 * otherwise (for example when debugging) call getSettings(). | |
317 * | |
318 * @return {Object} Object containing print job settings. | |
319 */ | |
320 function getSettingsWithRequestID() { | |
321 var settings = getSettings(); | |
322 settings.requestID = generatePreviewRequestID(); | |
323 settings.isFirstRequest = isFirstPreviewRequest(); | |
324 return settings; | |
325 } | |
326 | |
327 /** | |
326 * @return {number} The next unused preview request id. | 328 * @return {number} The next unused preview request id. |
327 */ | 329 */ |
328 function generatePreviewRequestID() { | 330 function generatePreviewRequestID() { |
329 return ++lastPreviewRequestID; | 331 return ++lastPreviewRequestID; |
330 } | 332 } |
331 | 333 |
332 /** | 334 /** |
333 * @return {boolean} True iff a preview has been requested. | 335 * @return {boolean} True iff a preview has been requested. |
334 */ | 336 */ |
335 function hasRequestedPreview() { | 337 function hasRequestedPreview() { |
336 return lastPreviewRequestID > -1; | 338 return lastPreviewRequestID != initialPreviewRequestID; |
337 } | 339 } |
338 | 340 |
339 /** | 341 /** |
342 * @return {boolean} True if |lastPreviewRequestID| corresponds to the initial | |
343 * preview request. | |
344 */ | |
345 function isFirstPreviewRequest() { | |
346 return lastPreviewRequestID == initialPreviewRequestID + 1; | |
347 } | |
348 | |
349 /** | |
340 * Checks if |previewResponseId| matches |lastPreviewRequestId|. Used to ignore | 350 * Checks if |previewResponseId| matches |lastPreviewRequestId|. Used to ignore |
341 * obsolete preview data responses. | 351 * obsolete preview data responses. |
342 * @param {number} previewResponseId The id to check. | 352 * @param {number} previewResponseId The id to check. |
343 * @return {boolean} True if previewResponseId reffers to the expected response. | 353 * @return {boolean} True if previewResponseId reffers to the expected response. |
344 */ | 354 */ |
345 function isExpectedPreviewResponse(previewResponseId) { | 355 function isExpectedPreviewResponse(previewResponseId) { |
346 return lastPreviewRequestID == previewResponseId; | 356 return lastPreviewRequestID == previewResponseId; |
347 } | 357 } |
348 | 358 |
349 /** | 359 /** |
(...skipping 24 matching lines...) Expand all Loading... | |
374 } else { | 384 } else { |
375 isTabHidden = true; | 385 isTabHidden = true; |
376 chrome.send('hidePreview'); | 386 chrome.send('hidePreview'); |
377 } | 387 } |
378 return; | 388 return; |
379 } | 389 } |
380 | 390 |
381 if (printToPDF) { | 391 if (printToPDF) { |
382 sendPrintDocumentRequest(); | 392 sendPrintDocumentRequest(); |
383 } else { | 393 } else { |
384 removeEventListeners(); | |
385 window.setTimeout(function() { sendPrintDocumentRequest(); }, 1000); | 394 window.setTimeout(function() { sendPrintDocumentRequest(); }, 1000); |
386 } | 395 } |
387 } | 396 } |
388 | 397 |
389 /** | 398 /** |
390 * Asks the browser to print the pending preview PDF that just finished | 399 * Asks the browser to print the pending preview PDF that just finished |
391 * loading. | 400 * loading. |
392 */ | 401 */ |
393 function requestToPrintPendingDocument() { | 402 function requestToPrintPendingDocument() { |
394 hasPendingPrintDocumentRequest = false; | 403 hasPendingPrintDocumentRequest = false; |
(...skipping 23 matching lines...) Expand all Loading... | |
418 cloudprint.getData(printer)]); | 427 cloudprint.getData(printer)]); |
419 chrome.send('print', [JSON.stringify(getSettings()), | 428 chrome.send('print', [JSON.stringify(getSettings()), |
420 cloudprint.getPrintTicketJSON(printer)]); | 429 cloudprint.getPrintTicketJSON(printer)]); |
421 } | 430 } |
422 | 431 |
423 /** | 432 /** |
424 * Asks the browser to generate a preview PDF based on current print settings. | 433 * Asks the browser to generate a preview PDF based on current print settings. |
425 */ | 434 */ |
426 function requestPrintPreview() { | 435 function requestPrintPreview() { |
427 hasPendingPreviewRequest = true; | 436 hasPendingPreviewRequest = true; |
428 removeEventListeners(); | 437 layoutSettings.updateState(); |
dpapad
2011/08/11 21:21:37
I could send an event here to update the state so
| |
429 printSettings.save(); | |
430 if (!isTabHidden) | 438 if (!isTabHidden) |
431 showLoadingAnimation(); | 439 showLoadingAnimation(); |
432 | 440 |
433 var settings = getSettings(); | 441 chrome.send('getPreview', [JSON.stringify(getSettingsWithRequestID())]); |
434 settings.requestID = generatePreviewRequestID(); | |
435 chrome.send('getPreview', [JSON.stringify(settings)]); | |
436 } | 442 } |
437 | 443 |
438 /** | 444 /** |
439 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print | 445 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print |
440 * preview tab regarding the file selection cancel event. | 446 * preview tab regarding the file selection cancel event. |
441 */ | 447 */ |
442 function fileSelectionCancelled() { | 448 function fileSelectionCancelled() { |
443 // TODO(thestig) re-enable controls here. | 449 // TODO(thestig) re-enable controls here. |
444 } | 450 } |
445 | 451 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 /** | 730 /** |
725 * Display an error message in the center of the preview area. | 731 * Display an error message in the center of the preview area. |
726 * @param {string} errorMessage The error message to be displayed. | 732 * @param {string} errorMessage The error message to be displayed. |
727 */ | 733 */ |
728 function displayErrorMessage(errorMessage) { | 734 function displayErrorMessage(errorMessage) { |
729 $('print-button').disabled = true; | 735 $('print-button').disabled = true; |
730 $('overlay-layer').classList.remove('invisible'); | 736 $('overlay-layer').classList.remove('invisible'); |
731 $('dancing-dots-text').classList.add('hidden'); | 737 $('dancing-dots-text').classList.add('hidden'); |
732 $('error-text').innerHTML = errorMessage; | 738 $('error-text').innerHTML = errorMessage; |
733 $('error-text').classList.remove('hidden'); | 739 $('error-text').classList.remove('hidden'); |
734 removeEventListeners(); | |
735 var pdfViewer = $('pdf-viewer'); | 740 var pdfViewer = $('pdf-viewer'); |
736 if (pdfViewer) | 741 if (pdfViewer) |
737 $('mainview').removeChild(pdfViewer); | 742 $('mainview').removeChild(pdfViewer); |
738 | 743 |
739 if (isTabHidden) | 744 if (isTabHidden) |
740 cancelPendingPrintRequest(); | 745 cancelPendingPrintRequest(); |
741 } | 746 } |
742 | 747 |
743 /** | 748 /** |
744 * Display an error message in the center of the preview area followed by a | 749 * Display an error message in the center of the preview area followed by a |
(...skipping 22 matching lines...) Expand all Loading... | |
767 function printPreviewFailed() { | 772 function printPreviewFailed() { |
768 displayErrorMessage(localStrings.getString('previewFailed')); | 773 displayErrorMessage(localStrings.getString('previewFailed')); |
769 } | 774 } |
770 | 775 |
771 /** | 776 /** |
772 * Called when the PDF plugin loads its document. | 777 * Called when the PDF plugin loads its document. |
773 */ | 778 */ |
774 function onPDFLoad() { | 779 function onPDFLoad() { |
775 if (previewModifiable) { | 780 if (previewModifiable) { |
776 setPluginPreviewPageCount(); | 781 setPluginPreviewPageCount(); |
777 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
778 } | 782 } |
779 $('pdf-viewer').fitToHeight(); | 783 $('pdf-viewer').fitToHeight(); |
780 cr.dispatchSimpleEvent(document, 'PDFLoaded'); | 784 cr.dispatchSimpleEvent(document, 'PDFLoaded'); |
781 hideLoadingAnimation(); | 785 hideLoadingAnimation(); |
782 } | 786 } |
783 | 787 |
784 function setPluginPreviewPageCount() { | 788 function setPluginPreviewPageCount() { |
785 $('pdf-viewer').printPreviewPageCount( | 789 $('pdf-viewer').printPreviewPageCount( |
786 pageSettings.previouslySelectedPages.length); | 790 pageSettings.previouslySelectedPages.length); |
787 } | 791 } |
788 | 792 |
789 /** | 793 /** |
790 * Update the page count and check the page range. | 794 * Update the page count and check the page range. |
791 * Called from PrintPreviewUI::OnDidGetPreviewPageCount(). | 795 * Called from PrintPreviewUI::OnDidGetPreviewPageCount(). |
792 * @param {number} pageCount The number of pages. | 796 * @param {number} pageCount The number of pages. |
793 * @param {boolean} isModifiable Indicates whether the previewed document can be | 797 * @param {boolean} isModifiable Indicates whether the previewed document can be |
794 * modified. | 798 * modified. |
799 * @param {number} previewResponseId The preview request id that resulted in | |
800 * this response. | |
795 */ | 801 */ |
796 function onDidGetPreviewPageCount(pageCount, isModifiable) { | 802 function onDidGetPreviewPageCount(pageCount, isModifiable, previewResponseId) { |
803 if (!isExpectedPreviewResponse(previewResponseId)) | |
804 return; | |
797 pageSettings.updateState(pageCount); | 805 pageSettings.updateState(pageCount); |
798 previewModifiable = isModifiable; | 806 previewModifiable = isModifiable; |
807 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
799 } | 808 } |
800 | 809 |
801 /** | 810 /** |
802 * Called when no pipelining previewed pages. | 811 * Called when no pipelining previewed pages. |
812 * @param {string} previewUid Preview unique identifier. | |
813 * @param {number} previewResponseId The preview request id that resulted in | |
814 * this response. | |
803 */ | 815 */ |
804 function reloadPreviewPages(previewUid, previewResponseId) { | 816 function reloadPreviewPages(previewUid, previewResponseId) { |
805 if (!isExpectedPreviewResponse(previewResponseId)) | 817 if (!isExpectedPreviewResponse(previewResponseId)) |
806 return; | 818 return; |
807 hasPendingPreviewRequest = false; | 819 hasPendingPreviewRequest = false; |
808 | 820 |
809 if (checkIfSettingsChangedAndRegeneratePreview()) | |
810 return; | |
811 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
812 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 821 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
813 addEventListeners(); | |
814 hideLoadingAnimation(); | 822 hideLoadingAnimation(); |
815 var pageSet = pageSettings.previouslySelectedPages; | 823 var pageSet = pageSettings.previouslySelectedPages; |
816 for (var i = 0; i < pageSet.length; i++) | 824 for (var i = 0; i < pageSet.length; i++) |
817 $('pdf-viewer').loadPreviewPage(getPageSrcURL(previewUid, pageSet[i]-1), i); | 825 $('pdf-viewer').loadPreviewPage(getPageSrcURL(previewUid, pageSet[i]-1), i); |
818 // TODO(dpapad): handle pending print file requests. | 826 // TODO(dpapad): handle pending print file requests. |
819 } | 827 } |
820 | 828 |
821 /** | 829 /** |
822 * Notification that a print preview page has been rendered. | 830 * Notification that a print preview page has been rendered. |
823 * Check if the settings have changed and request a regeneration if needed. | 831 * Check if the settings have changed and request a regeneration if needed. |
824 * Called from PrintPreviewUI::OnDidPreviewPage(). | 832 * Called from PrintPreviewUI::OnDidPreviewPage(). |
825 * @param {number} pageNumber The page number, 0-based. | 833 * @param {number} pageNumber The page number, 0-based. |
834 * @param {string} previewUid Preview unique identifier. | |
835 * @param {number} previewResponseId The preview request id that resulted in | |
836 * this response. | |
826 */ | 837 */ |
827 function onDidPreviewPage(pageNumber, previewUid) { | 838 function onDidPreviewPage(pageNumber, previewUid, previewResponseId) { |
839 if (!isExpectedPreviewResponse(previewResponseId)) | |
840 return; | |
841 | |
828 // Refactor | 842 // Refactor |
829 if (!previewModifiable) | 843 if (!previewModifiable) |
830 return; | 844 return; |
831 | 845 |
832 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); | 846 var pageIndex = pageSettings.previouslySelectedPages.indexOf(pageNumber + 1); |
833 | 847 |
834 if (checkIfSettingsChangedAndRegeneratePreview()) | 848 if (pageSettings.requestPrintPreviewIfNeeded()) |
835 return; | 849 return; |
836 if (pageIndex == 0) | 850 if (pageIndex == 0) |
837 createPDFPlugin(previewUid); | 851 createPDFPlugin(previewUid); |
838 | 852 |
839 $('pdf-viewer').loadPreviewPage( | 853 $('pdf-viewer').loadPreviewPage( |
840 getPageSrcURL(previewUid, pageNumber), pageIndex); | 854 getPageSrcURL(previewUid, pageNumber), pageIndex); |
841 } | 855 } |
842 | 856 |
843 /** | 857 /** |
844 * Update the print preview when new preview data is available. | 858 * Update the print preview when new preview data is available. |
845 * Create the PDF plugin as needed. | 859 * Create the PDF plugin as needed. |
846 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 860 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
847 * @param {string} jobTitle The print job title. | 861 * @param {string} jobTitle The print job title. |
848 * @param {boolean} modifiable If the preview is modifiable. | 862 * @param {boolean} modifiable If the preview is modifiable. |
849 * @param {string} previewUid Preview unique identifier. | 863 * @param {string} previewUid Preview unique identifier. |
850 * @param {number} previewResponseId The preview request id that resulted in | 864 * @param {number} previewResponseId The preview request id that resulted in |
851 * this response. | 865 * this response. |
852 */ | 866 */ |
853 function updatePrintPreview(jobTitle, | 867 function updatePrintPreview(jobTitle, |
854 previewUid, | 868 previewUid, |
855 previewResponseId) { | 869 previewResponseId) { |
856 if (!isExpectedPreviewResponse(previewResponseId)) | 870 if (!isExpectedPreviewResponse(previewResponseId)) |
857 return; | 871 return; |
858 hasPendingPreviewRequest = false; | 872 hasPendingPreviewRequest = false; |
859 | 873 |
860 if (checkIfSettingsChangedAndRegeneratePreview()) | |
861 return; | |
862 | |
863 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 874 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
864 | 875 |
865 if (!previewModifiable) { | 876 if (!previewModifiable) { |
866 // If the preview is not modifiable the plugin has not been created yet. | 877 // If the preview is not modifiable the plugin has not been created yet. |
867 createPDFPlugin(previewUid); | 878 createPDFPlugin(previewUid); |
868 } | 879 } |
869 | 880 |
870 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
871 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 881 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
872 addEventListeners(); | |
873 | 882 |
874 if (hasPendingPrintDocumentRequest) | 883 if (hasPendingPrintDocumentRequest) |
875 requestToPrintPendingDocument(); | 884 requestToPrintPendingDocument(); |
876 } | 885 } |
877 | 886 |
878 /** | 887 /** |
879 * Check if any print settings changed and regenerate the preview if needed. | |
880 * @return {boolean} true if a new preview is required. | |
881 */ | |
882 function checkIfSettingsChangedAndRegeneratePreview() { | |
883 var tempPrintSettings = new PrintSettings(); | |
884 tempPrintSettings.save(); | |
885 | |
886 if (printSettings.deviceName != tempPrintSettings.deviceName) { | |
887 updateControlsWithSelectedPrinterCapabilities(); | |
888 return true; | |
889 } | |
890 if (printSettings.isLandscape != tempPrintSettings.isLandscape) { | |
891 setDefaultValuesAndRegeneratePreview(); | |
892 return true; | |
893 } | |
894 if (pageSettings.requestPrintPreviewIfNeeded()) | |
895 return true; | |
896 | |
897 return false; | |
898 } | |
899 | |
900 /** | |
901 * Create the PDF plugin or reload the existing one. | 888 * Create the PDF plugin or reload the existing one. |
902 * @param {string} previewUid Preview unique identifier. | 889 * @param {string} previewUid Preview unique identifier. |
903 */ | 890 */ |
904 function createPDFPlugin(previewUid) { | 891 function createPDFPlugin(previewUid) { |
905 var pdfViewer = $('pdf-viewer'); | 892 var pdfViewer = $('pdf-viewer'); |
906 if (pdfViewer) { | 893 if (pdfViewer) { |
907 // Need to call this before the reload(), where the plugin resets its | 894 // Need to call this before the reload(), where the plugin resets its |
908 // internal page count. | 895 // internal page count. |
909 pdfViewer.goToPage('0'); | 896 pdfViewer.goToPage('0'); |
910 pdfViewer.reload(); | 897 pdfViewer.reload(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
943 window.addEventListener('DOMContentLoaded', onLoad); | 930 window.addEventListener('DOMContentLoaded', onLoad); |
944 | 931 |
945 /** | 932 /** |
946 * Sets the default values and sends a request to regenerate preview data. | 933 * Sets the default values and sends a request to regenerate preview data. |
947 */ | 934 */ |
948 function setDefaultValuesAndRegeneratePreview() { | 935 function setDefaultValuesAndRegeneratePreview() { |
949 pageSettings.resetState(); | 936 pageSettings.resetState(); |
950 requestPrintPreview(); | 937 requestPrintPreview(); |
951 } | 938 } |
952 | 939 |
953 /** | |
954 * Class that represents the state of the print settings. | |
955 */ | |
956 function PrintSettings() { | |
957 this.deviceName = ''; | |
958 this.isLandscape = ''; | |
959 } | |
960 | |
961 /** | |
962 * Takes a snapshot of the print settings. | |
963 */ | |
964 PrintSettings.prototype.save = function() { | |
965 this.deviceName = getSelectedPrinterName(); | |
966 this.isLandscape = layoutSettings.isLandscape(); | |
967 } | |
968 | |
969 /// Pull in all other scripts in a single shot. | 940 /// Pull in all other scripts in a single shot. |
970 <include src="print_preview_animations.js"/> | 941 <include src="print_preview_animations.js"/> |
971 <include src="print_preview_cloud.js"/> | 942 <include src="print_preview_cloud.js"/> |
972 <include src="print_preview_utils.js"/> | 943 <include src="print_preview_utils.js"/> |
973 <include src="print_header.js"/> | 944 <include src="print_header.js"/> |
974 <include src="page_settings.js"/> | 945 <include src="page_settings.js"/> |
975 <include src="copies_settings.js"/> | 946 <include src="copies_settings.js"/> |
976 <include src="layout_settings.js"/> | 947 <include src="layout_settings.js"/> |
977 <include src="color_settings.js"/> | 948 <include src="color_settings.js"/> |
OLD | NEW |