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 var localStrings = new LocalStrings(); | 5 var localStrings = new LocalStrings(); |
6 | 6 |
7 // The total page count of the previewed document regardless of which pages the | 7 // The total page count of the previewed document regardless of which pages the |
8 // user has selected. | 8 // user has selected. |
9 var totalPageCount = -1; | 9 var totalPageCount = -1; |
10 | 10 |
11 // The previously selected pages by the user. It is used in | 11 // The previously selected pages by the user. It is used in |
12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not | 12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not |
13 // requested more often than necessary. | 13 // requested more often than necessary. |
14 var previouslySelectedPages = []; | 14 var previouslySelectedPages = []; |
15 | 15 |
16 // The previously selected layout mode. It is used in order to prevent the | |
17 // preview from updating when the user clicks on the already selected layout | |
18 // mode. | |
19 var previouslySelectedLayout = null; | |
20 | |
21 // Timer id of the page range textfield. It is used to reset the timer whenever | 16 // Timer id of the page range textfield. It is used to reset the timer whenever |
22 // needed. | 17 // needed. |
23 var timerId; | 18 var timerId; |
24 | 19 |
25 // Store the last selected printer index. | 20 // Store the last selected printer index. |
26 var lastSelectedPrinterIndex = 0; | 21 var lastSelectedPrinterIndex = 0; |
27 | 22 |
28 // Indicates whether a preview has been requested but not received yet. | 23 // Indicates whether a preview has been requested but not received yet. |
29 var isPreviewStillLoading = true; | 24 var isPreviewStillLoading = true; |
30 | 25 |
31 // Currently selected printer capabilities. | 26 // Currently selected printer capabilities. |
32 var printerCapabilities; | 27 var printerCapabilities; |
33 | 28 |
34 // Used to disable some printing options when the preview is not modifiable. | 29 // Used to disable some printing options when the preview is not modifiable. |
35 var previewModifiable = false; | 30 var previewModifiable = false; |
36 | 31 |
37 // Destination list special value constants. | 32 // Destination list special value constants. |
38 const PRINT_TO_PDF = 'Print To PDF'; | 33 const PRINT_TO_PDF = 'Print To PDF'; |
39 const MANAGE_PRINTERS = 'Manage Printers'; | 34 const MANAGE_PRINTERS = 'Manage Printers'; |
40 | 35 |
36 // State of the print preview settings. | |
37 var settingsState = new SettingsState(); | |
38 | |
41 /** | 39 /** |
42 * Window onload handler, sets up the page and starts print preview by getting | 40 * Window onload handler, sets up the page and starts print preview by getting |
43 * the printer list. | 41 * the printer list. |
44 */ | 42 */ |
45 function onLoad() { | 43 function onLoad() { |
46 $('system-dialog-link').addEventListener('click', showSystemDialog); | 44 $('system-dialog-link').addEventListener('click', showSystemDialog); |
47 $('cancel-button').addEventListener('click', handleCancelButtonClick); | 45 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
48 | 46 |
49 if (!checkCompatiblePluginExists()) { | 47 if (!checkCompatiblePluginExists()) { |
50 displayErrorMessage(localStrings.getString('noPlugin')); | 48 displayErrorMessage(localStrings.getString('noPlugin')); |
51 $('mainview').parentElement.removeChild($('dummy-viewer')); | 49 $('mainview').parentElement.removeChild($('dummy-viewer')); |
52 return; | 50 return; |
53 } | 51 } |
54 $('mainview').parentElement.removeChild($('dummy-viewer')); | 52 $('mainview').parentElement.removeChild($('dummy-viewer')); |
55 | 53 |
56 $('printer-list').disabled = true; | 54 $('printer-list').disabled = true; |
57 $('print-button').disabled = true; | 55 $('print-button').disabled = true; |
58 $('print-button').addEventListener('click', printFile); | |
59 $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged); | |
60 $('copies').addEventListener('input', copiesFieldChanged); | |
61 $('print-pages').addEventListener('click', handleIndividualPagesCheckbox); | |
62 $('individual-pages').addEventListener('blur', function() { | |
63 clearTimeout(timerId); | |
64 onPageSelectionMayHaveChanged(); | |
65 }); | |
66 $('individual-pages').addEventListener('focus', addTimerToPageRangeField); | |
67 $('individual-pages').addEventListener('input', resetPageRangeFieldTimer); | |
68 $('two-sided').addEventListener('click', handleTwoSidedClick) | |
69 $('landscape').addEventListener('click', onLayoutModeToggle); | |
70 $('portrait').addEventListener('click', onLayoutModeToggle); | |
71 $('color').addEventListener('click', function() { setColor(true); }); | |
72 $('bw').addEventListener('click', function() { setColor(false); }); | |
73 $('printer-list').addEventListener( | |
74 'change', updateControlsWithSelectedPrinterCapabilities); | |
75 $('increment').addEventListener('click', | |
76 function() { onCopiesButtonsClicked(1); }); | |
77 $('decrement').addEventListener('click', | |
78 function() { onCopiesButtonsClicked(-1); }); | |
79 $('controls').onsubmit = function() { return false; }; | 56 $('controls').onsubmit = function() { return false; }; |
80 $('dancing-dots').classList.remove('invisible'); | 57 $('dancing-dots').classList.remove('invisible'); |
81 chrome.send('getPrinters'); | 58 chrome.send('getPrinters'); |
82 } | 59 } |
83 | 60 |
84 /** | 61 /** |
62 * Adds event listeners to the settings controls. | |
63 */ | |
64 function addEventListeners() { | |
65 var individualPages = $('individual-pages'); | |
66 $('print-button').onclick = printFile; | |
67 $('all-pages').onclick = onPageSelectionMayHaveChanged; | |
68 | |
69 // Controls that require preview rendering. | |
70 $('print-pages').onclick = handleIndividualPagesCheckbox; | |
71 individualPages.onblur = function() { | |
72 clearTimeout(timerId); | |
73 onPageSelectionMayHaveChanged(); | |
74 }; | |
75 individualPages.onfocus = addTimerToPageRangeField; | |
76 individualPages.oninput = resetPageRangeFieldTimer; | |
77 $('landscape').onclick = onLayoutModeToggle; | |
78 $('portrait').onclick = onLayoutModeToggle; | |
79 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | |
80 | |
81 // Controls that dont require preview rendering. | |
82 $('copies').oninput = copiesFieldChanged; | |
83 $('two-sided').onclick = handleTwoSidedClick; | |
84 $('color').onclick = function() { setColor(true); }; | |
85 $('bw').onclick = function() { setColor(false); }; | |
86 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; | |
87 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; | |
88 } | |
89 | |
90 /** | |
91 * Removes event listeners from the settings controls. | |
92 */ | |
93 function removeEventListeners() { | |
94 var individualPages = $('individual-pages'); | |
95 | |
96 // Controls that require preview rendering. | |
97 $('print-pages').onclick = null; | |
98 individualPages.onblur = null; | |
99 individualPages.onfocus = null; | |
100 individualPages.oninput = null; | |
101 clearTimeout(timerId); | |
102 $('landscape').onclick = null; | |
103 $('portrait').onclick = null; | |
104 $('printer-list').onchange = null; | |
105 | |
106 // Controls that dont require preview rendering. | |
107 $('copies').oninput = null; | |
108 $('two-sided').onclick = null; | |
109 $('color').onclick = null; | |
110 $('bw').onclick = null; | |
111 $('increment').onclick = null; | |
112 $('decrement').onclick = null; | |
113 } | |
114 | |
115 /** | |
85 * Asks the browser to close the preview tab. | 116 * Asks the browser to close the preview tab. |
86 */ | 117 */ |
87 function handleCancelButtonClick() { | 118 function handleCancelButtonClick() { |
88 chrome.send('closePrintPreviewTab'); | 119 chrome.send('closePrintPreviewTab'); |
89 } | 120 } |
90 | 121 |
91 /** | 122 /** |
92 * Asks the browser to show the native print dialog for printing. | 123 * Asks the browser to show the native print dialog for printing. |
93 */ | 124 */ |
94 function showSystemDialog() { | 125 function showSystemDialog() { |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 * Asks the browser to print the preview PDF based on current print settings. | 342 * Asks the browser to print the preview PDF based on current print settings. |
312 */ | 343 */ |
313 function printFile() { | 344 function printFile() { |
314 chrome.send('print', [getSettingsJSON()]); | 345 chrome.send('print', [getSettingsJSON()]); |
315 } | 346 } |
316 | 347 |
317 /** | 348 /** |
318 * Asks the browser to generate a preview PDF based on current print settings. | 349 * Asks the browser to generate a preview PDF based on current print settings. |
319 */ | 350 */ |
320 function requestPrintPreview() { | 351 function requestPrintPreview() { |
352 removeEventListeners(); | |
321 isPreviewStillLoading = true; | 353 isPreviewStillLoading = true; |
322 setControlsDisabled(true); | 354 settingsState.save(); |
323 $('dancing-dots').classList.remove('invisible'); | 355 $('dancing-dots').classList.remove('invisible'); |
324 chrome.send('getPreview', [getSettingsJSON()]); | 356 chrome.send('getPreview', [getSettingsJSON()]); |
325 } | 357 } |
326 | 358 |
327 /** | 359 /** |
328 * Fill the printer list drop down. | 360 * Fill the printer list drop down. |
329 * Called from PrintPreviewHandler::SendPrinterList(). | 361 * Called from PrintPreviewHandler::SendPrinterList(). |
330 * @param {Array} printers Array of printer info objects. | 362 * @param {Array} printers Array of printer info objects. |
331 * @param {number} defaultPrinterIndex The index of the default printer. | 363 * @param {number} defaultPrinterIndex The index of the default printer. |
332 */ | 364 */ |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 /** | 462 /** |
431 * Update the print preview when new preview data is available. | 463 * Update the print preview when new preview data is available. |
432 * Create the PDF plugin as needed. | 464 * Create the PDF plugin as needed. |
433 * Called from PrintPreviewUI::PreviewDataIsAvailable(). | 465 * Called from PrintPreviewUI::PreviewDataIsAvailable(). |
434 * @param {number} pageCount The expected total pages count. | 466 * @param {number} pageCount The expected total pages count. |
435 * @param {string} jobTitle The print job title. | 467 * @param {string} jobTitle The print job title. |
436 * @param {boolean} modifiable If the preview is modifiable. | 468 * @param {boolean} modifiable If the preview is modifiable. |
437 * | 469 * |
438 */ | 470 */ |
439 function updatePrintPreview(pageCount, jobTitle, modifiable) { | 471 function updatePrintPreview(pageCount, jobTitle, modifiable) { |
472 var tempSettingsState = new SettingsState(); | |
473 tempSettingsState.save(); | |
474 | |
440 previewModifiable = modifiable; | 475 previewModifiable = modifiable; |
441 | 476 |
442 if (totalPageCount == -1) | 477 if (totalPageCount == -1) |
443 totalPageCount = pageCount; | 478 totalPageCount = pageCount; |
444 | 479 |
445 if (previouslySelectedPages.length == 0) | 480 if (previouslySelectedPages.length == 0) |
446 for (var i = 0; i < totalPageCount; i++) | 481 for (var i = 0; i < totalPageCount; i++) |
447 previouslySelectedPages.push(i+1); | 482 previouslySelectedPages.push(i+1); |
448 | 483 |
449 if (previouslySelectedLayout == null) | 484 if (settingsState.deviceName != tempSettingsState.deviceName) { |
450 previouslySelectedLayout = $('portrait'); | 485 updateControlsWithSelectedPrinterCapabilities(); |
486 return; | |
487 } else if (settingsState.isLandscape != tempSettingsState.isLandscape) { | |
488 requestPrintPreview(); | |
489 return; | |
490 } else if (getSelectedPagesValidityLevel() != 1) { | |
491 pageRangesFieldChanged(); | |
492 } else if (getSelectedPagesValidityLevel() == 1) { | |
493 var currentlySelectedPages = getSelectedPagesSet(); | |
494 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { | |
495 previouslySelectedPages = currentlySelectedPages; | |
496 requestPrintPreview(); | |
497 return; | |
498 } | |
499 } | |
451 | 500 |
452 // Update the current tab title. | 501 // Update the current tab title. |
453 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 502 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
454 | 503 |
455 createPDFPlugin(); | 504 createPDFPlugin(); |
456 isPreviewStillLoading = false; | 505 isPreviewStillLoading = false; |
457 updatePrintSummary(); | 506 updatePrintSummary(); |
507 addEventListeners(); | |
458 } | 508 } |
459 | 509 |
460 /** | 510 /** |
461 * Create the PDF plugin or reload the existing one. | 511 * Create the PDF plugin or reload the existing one. |
462 */ | 512 */ |
463 function createPDFPlugin() { | 513 function createPDFPlugin() { |
464 // Enable the print button. | 514 // Enable the print button. |
465 if (!$('printer-list').disabled) { | 515 if (!$('printer-list').disabled) { |
466 $('print-button').disabled = false; | 516 $('print-button').disabled = false; |
467 } | 517 } |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
657 $('individual-pages').focus(); | 707 $('individual-pages').focus(); |
658 } | 708 } |
659 | 709 |
660 /** | 710 /** |
661 * When the user switches printing orientation mode the page field selection is | 711 * When the user switches printing orientation mode the page field selection is |
662 * reset to "all pages selected". After the change the number of pages will be | 712 * reset to "all pages selected". After the change the number of pages will be |
663 * different and currently selected page numbers might no longer be valid. | 713 * different and currently selected page numbers might no longer be valid. |
664 * Even if they are still valid the content of these pages will be different. | 714 * Even if they are still valid the content of these pages will be different. |
665 */ | 715 */ |
666 function onLayoutModeToggle() { | 716 function onLayoutModeToggle() { |
667 var currentlySelectedLayout = $('portrait').checked ? $('portrait') : | |
668 $('landscape'); | |
669 | |
670 // If the chosen layout is same as before, nothing needs to be done. | 717 // If the chosen layout is same as before, nothing needs to be done. |
671 if (previouslySelectedLayout == currentlySelectedLayout) | 718 if (settingsState.isLandscape == isLandscape()) |
672 return; | 719 return; |
673 | 720 |
674 previouslySelectedLayout = currentlySelectedLayout; | |
675 $('individual-pages').classList.remove('invalid'); | 721 $('individual-pages').classList.remove('invalid'); |
676 setDefaultValuesAndRegeneratePreview(); | 722 setDefaultValuesAndRegeneratePreview(); |
677 } | 723 } |
678 | 724 |
679 /** | 725 /** |
680 * Sets the default values and sends a request to regenerate preview data. | 726 * Sets the default values and sends a request to regenerate preview data. |
681 */ | 727 */ |
682 function setDefaultValuesAndRegeneratePreview() { | 728 function setDefaultValuesAndRegeneratePreview() { |
683 $('individual-pages').value = ''; | 729 $('individual-pages').value = ''; |
684 hideInvalidHint($('individual-pages-hint')); | 730 hideInvalidHint($('individual-pages-hint')); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
892 copiesField.value = 1; | 938 copiesField.value = 1; |
893 else { | 939 else { |
894 var newValue = getCopies() + sign * 1; | 940 var newValue = getCopies() + sign * 1; |
895 if (newValue < copiesField.min || newValue > copiesField.max) | 941 if (newValue < copiesField.min || newValue > copiesField.max) |
896 return; | 942 return; |
897 copiesField.value = newValue; | 943 copiesField.value = newValue; |
898 } | 944 } |
899 copiesFieldChanged(); | 945 copiesFieldChanged(); |
900 } | 946 } |
901 | 947 |
948 /** | |
949 * Class that represents the state of the print settings. | |
950 */ | |
951 function SettingsState() { | |
952 this.deviceName = ''; | |
953 this.isLandscape = ''; | |
dpapad
2011/05/23 18:00:58
I think that more state variables (defined at the
| |
954 } | |
955 | |
956 /** | |
957 * Takes a snapshot of the print settings. | |
958 */ | |
959 SettingsState.prototype.save = function() { | |
960 var printerList = $('printer-list') | |
961 var selectedPrinter = printerList.selectedIndex; | |
962 if (selectedPrinter >= 0) | |
963 this.deviceName = printerList.options[selectedPrinter].value; | |
964 else | |
965 this.deviceName = ''; | |
966 this.isLandscape = isLandscape(); | |
967 } | |
OLD | NEW |