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 |
(...skipping 15 matching lines...) Expand all Loading... |
26 // Destination list special value constants. | 26 // Destination list special value constants. |
27 const PRINT_TO_PDF = 'Print To PDF'; | 27 const PRINT_TO_PDF = 'Print To PDF'; |
28 const MANAGE_PRINTERS = 'Manage Printers'; | 28 const MANAGE_PRINTERS = 'Manage Printers'; |
29 | 29 |
30 // State of the print preview settings. | 30 // State of the print preview settings. |
31 var printSettings = new PrintSettings(); | 31 var printSettings = new PrintSettings(); |
32 | 32 |
33 // The name of the default or last used printer. | 33 // The name of the default or last used printer. |
34 var defaultOrLastUsedPrinterName = ''; | 34 var defaultOrLastUsedPrinterName = ''; |
35 | 35 |
| 36 // True when a pending print preview request exists. |
| 37 var hasPendingPreviewRequest = false; |
| 38 |
| 39 // True when a pending print file request exists. |
| 40 var hasPendingPrintFileRequest = false; |
| 41 |
| 42 // True when preview tab has some error. |
| 43 var hasError = false; |
| 44 |
36 /** | 45 /** |
37 * Window onload handler, sets up the page and starts print preview by getting | 46 * Window onload handler, sets up the page and starts print preview by getting |
38 * the printer list. | 47 * the printer list. |
39 */ | 48 */ |
40 function onLoad() { | 49 function onLoad() { |
41 $('system-dialog-link').addEventListener('click', showSystemDialog); | 50 $('system-dialog-link').addEventListener('click', showSystemDialog); |
42 $('cancel-button').addEventListener('click', handleCancelButtonClick); | 51 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
43 | 52 |
44 if (!checkCompatiblePluginExists()) { | 53 if (!checkCompatiblePluginExists()) { |
45 displayErrorMessageWithButton(localStrings.getString('noPlugin'), | 54 displayErrorMessageWithButton(localStrings.getString('noPlugin'), |
46 localStrings.getString('launchNativeDialog'), | 55 localStrings.getString('launchNativeDialog'), |
47 showSystemDialog); | 56 showSystemDialog); |
48 $('mainview').parentElement.removeChild($('dummy-viewer')); | 57 $('mainview').parentElement.removeChild($('dummy-viewer')); |
49 return; | 58 return; |
50 } | 59 } |
51 $('mainview').parentElement.removeChild($('dummy-viewer')); | 60 $('mainview').parentElement.removeChild($('dummy-viewer')); |
52 | 61 |
53 $('printer-list').disabled = true; | 62 $('printer-list').disabled = true; |
54 $('print-button').disabled = true; | 63 $('print-button').onclick = printFile; |
| 64 |
| 65 setDefaultHandlersForPagesAndCopiesControls(); |
55 showLoadingAnimation(); | 66 showLoadingAnimation(); |
56 chrome.send('getDefaultPrinter'); | 67 chrome.send('getDefaultPrinter'); |
57 } | 68 } |
58 | 69 |
59 /** | 70 /** |
| 71 * Handles the individual pages input event. |
| 72 */ |
| 73 function handleIndividualPagesInputEvent() { |
| 74 $('print-pages').checked = true; |
| 75 resetPageRangeFieldTimer(); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Handles the individual pages blur event. |
| 80 */ |
| 81 function onPageRangesFieldBlur() { |
| 82 $('print-pages').checked = true; |
| 83 validatePageRangesField(); |
| 84 updatePrintButtonState(); |
| 85 } |
| 86 |
| 87 /** |
| 88 * Sets the default event handlers for pages and copies controls. |
| 89 */ |
| 90 function setDefaultHandlersForPagesAndCopiesControls() { |
| 91 var allPages = $('all-pages'); |
| 92 var printPages = $('print-pages'); |
| 93 var individualPages = $('individual-pages'); |
| 94 |
| 95 allPages.onclick = null; |
| 96 printPages.onclick = null; |
| 97 individualPages.oninput = null; |
| 98 individualPages.onfocus = null; |
| 99 individualPages.onblur = null; |
| 100 |
| 101 if (!hasError) { |
| 102 allPages.onclick = updatePrintButtonState; |
| 103 printPages.onclick = handleIndividualPagesCheckbox; |
| 104 individualPages.onblur = onPageRangesFieldBlur; |
| 105 } |
| 106 |
| 107 $('copies').oninput = copiesFieldChanged; |
| 108 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; |
| 109 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; |
| 110 } |
| 111 |
| 112 /** |
60 * Adds event listeners to the settings controls. | 113 * Adds event listeners to the settings controls. |
61 */ | 114 */ |
62 function addEventListeners() { | 115 function addEventListeners() { |
63 $('print-button').onclick = printFile; | |
64 | |
65 // Controls that require preview rendering. | 116 // Controls that require preview rendering. |
66 $('all-pages').onclick = onPageSelectionMayHaveChanged; | 117 $('all-pages').onclick = onPageSelectionMayHaveChanged; |
67 $('print-pages').onclick = handleIndividualPagesCheckbox; | 118 $('print-pages').onclick = handleIndividualPagesCheckbox; |
68 var individualPages = $('individual-pages'); | 119 var individualPages = $('individual-pages'); |
69 individualPages.onblur = function() { | 120 individualPages.onblur = function() { |
70 clearTimeout(timerId); | 121 clearTimeout(timerId); |
71 onPageSelectionMayHaveChanged(); | 122 onPageSelectionMayHaveChanged(); |
72 }; | 123 }; |
73 individualPages.onfocus = addTimerToPageRangeField; | 124 individualPages.onfocus = addTimerToPageRangeField; |
74 individualPages.oninput = resetPageRangeFieldTimer; | 125 individualPages.oninput = handleIndividualPagesInputEvent; |
75 $('landscape').onclick = onLayoutModeToggle; | 126 $('landscape').onclick = onLayoutModeToggle; |
76 $('portrait').onclick = onLayoutModeToggle; | 127 $('portrait').onclick = onLayoutModeToggle; |
77 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | 128 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; |
78 | 129 |
79 // Controls that dont require preview rendering. | 130 // Controls that dont require preview rendering. |
80 $('copies').oninput = function() { | 131 $('copies').oninput = function() { |
81 copiesFieldChanged(); | 132 copiesFieldChanged(); |
82 updatePrintButtonState(); | 133 updatePrintButtonState(); |
83 updatePrintSummary(); | 134 updatePrintSummary(); |
84 }; | 135 }; |
85 $('two-sided').onclick = handleTwoSidedClick; | 136 $('two-sided').onclick = handleTwoSidedClick; |
86 $('color').onclick = function() { setColor(true); }; | 137 $('color').onclick = function() { setColor(true); }; |
87 $('bw').onclick = function() { setColor(false); }; | 138 $('bw').onclick = function() { setColor(false); }; |
88 $('increment').onclick = function() { | 139 $('increment').onclick = function() { |
89 onCopiesButtonsClicked(1); | 140 onCopiesButtonsClicked(1); |
90 updatePrintButtonState(); | 141 updatePrintButtonState(); |
91 updatePrintSummary(); | 142 updatePrintSummary(); |
92 }; | 143 }; |
93 $('decrement').onclick = function() { | 144 $('decrement').onclick = function() { |
94 onCopiesButtonsClicked(-1); | 145 onCopiesButtonsClicked(-1); |
95 updatePrintButtonState(); | 146 updatePrintButtonState(); |
96 updatePrintSummary(); | 147 updatePrintSummary(); |
97 }; | 148 }; |
98 } | 149 } |
99 | 150 |
100 /** | 151 /** |
101 * Removes event listeners from the settings controls. | 152 * Removes event listeners from the settings controls. |
102 */ | 153 */ |
103 function removeEventListeners() { | 154 function removeEventListeners() { |
104 // Controls that require preview rendering. | |
105 $('print-button').disabled = true; | |
106 $('all-pages').onclick = null; | |
107 $('print-pages').onclick = null; | |
108 var individualPages = $('individual-pages'); | |
109 individualPages.onblur = null; | |
110 individualPages.onfocus = null; | |
111 individualPages.oninput = null; | |
112 clearTimeout(timerId); | 155 clearTimeout(timerId); |
| 156 setDefaultHandlersForPagesAndCopiesControls(); |
| 157 |
| 158 // Controls that require preview rendering |
113 $('landscape').onclick = null; | 159 $('landscape').onclick = null; |
114 $('portrait').onclick = null; | 160 $('portrait').onclick = null; |
115 $('printer-list').onchange = null; | 161 $('printer-list').onchange = null; |
116 | 162 |
117 // Controls that dont require preview rendering. | 163 // Controls that dont require preview rendering. |
118 $('copies').oninput = copiesFieldChanged; | |
119 $('two-sided').onclick = null; | 164 $('two-sided').onclick = null; |
120 $('color').onclick = null; | 165 $('color').onclick = null; |
121 $('bw').onclick = null; | 166 $('bw').onclick = null; |
122 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; | |
123 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; | |
124 } | 167 } |
125 | 168 |
126 /** | 169 /** |
127 * Asks the browser to close the preview tab. | 170 * Asks the browser to close the preview tab. |
128 */ | 171 */ |
129 function handleCancelButtonClick() { | 172 function handleCancelButtonClick() { |
130 chrome.send('closePrintPreviewTab'); | 173 chrome.send('closePrintPreviewTab'); |
131 } | 174 } |
132 | 175 |
133 /** | 176 /** |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 var deviceName = ''; | 367 var deviceName = ''; |
325 if (selectedPrinter >= 0) | 368 if (selectedPrinter >= 0) |
326 deviceName = printerList.options[selectedPrinter].value; | 369 deviceName = printerList.options[selectedPrinter].value; |
327 return deviceName; | 370 return deviceName; |
328 } | 371 } |
329 | 372 |
330 /** | 373 /** |
331 * Asks the browser to print the preview PDF based on current print settings. | 374 * Asks the browser to print the preview PDF based on current print settings. |
332 */ | 375 */ |
333 function printFile() { | 376 function printFile() { |
| 377 hasPendingPrintFileRequest = hasPendingPreviewRequest; |
| 378 |
| 379 if (hasPendingPrintFileRequest) { |
| 380 if (getSelectedPrinterName() != PRINT_TO_PDF) |
| 381 chrome.send('hidePreview'); |
| 382 return; |
| 383 } |
| 384 |
334 if (getSelectedPrinterName() != PRINT_TO_PDF) { | 385 if (getSelectedPrinterName() != PRINT_TO_PDF) { |
335 $('print-button').classList.add('loading'); | 386 $('print-button').classList.add('loading'); |
336 $('cancel-button').classList.add('loading'); | 387 $('cancel-button').classList.add('loading'); |
337 $('print-summary').innerHTML = localStrings.getString('printing'); | 388 $('print-summary').innerHTML = localStrings.getString('printing'); |
338 removeEventListeners(); | 389 removeEventListeners(); |
339 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, | 390 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, |
340 1000); | 391 1000); |
341 } else | 392 } else { |
342 chrome.send('print', [getSettingsJSON()]); | 393 chrome.send('print', [getSettingsJSON()]); |
| 394 } |
343 } | 395 } |
344 | 396 |
345 /** | 397 /** |
346 * Asks the browser to generate a preview PDF based on current print settings. | 398 * Asks the browser to generate a preview PDF based on current print settings. |
347 */ | 399 */ |
348 function requestPrintPreview() { | 400 function requestPrintPreview() { |
| 401 hasPendingPreviewRequest = true; |
349 removeEventListeners(); | 402 removeEventListeners(); |
350 printSettings.save(); | 403 printSettings.save(); |
351 showLoadingAnimation(); | 404 showLoadingAnimation(); |
352 chrome.send('getPreview', [getSettingsJSON()]); | 405 chrome.send('getPreview', [getSettingsJSON()]); |
353 } | 406 } |
354 | 407 |
355 /** | 408 /** |
356 * Set the default printer. If there is one, generate a print preview. | 409 * Set the default printer. If there is one, generate a print preview. |
357 * @param {string} printer Name of the default printer. Empty if none. | 410 * @param {string} printer Name of the default printer. Empty if none. |
358 */ | 411 */ |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 return; | 487 return; |
435 } | 488 } |
436 pdfViewer.grayscale(!color); | 489 pdfViewer.grayscale(!color); |
437 } | 490 } |
438 | 491 |
439 /** | 492 /** |
440 * Display an error message in the center of the preview area. | 493 * Display an error message in the center of the preview area. |
441 * @param {string} errorMessage The error message to be displayed. | 494 * @param {string} errorMessage The error message to be displayed. |
442 */ | 495 */ |
443 function displayErrorMessage(errorMessage) { | 496 function displayErrorMessage(errorMessage) { |
| 497 hasError = true; |
| 498 $('print-button').disabled = true; |
444 $('overlay-layer').classList.remove('invisible'); | 499 $('overlay-layer').classList.remove('invisible'); |
445 $('dancing-dots-text').classList.add('hidden'); | 500 $('dancing-dots-text').classList.add('hidden'); |
446 $('error-text').innerHTML = errorMessage; | 501 $('error-text').innerHTML = errorMessage; |
447 $('error-text').classList.remove('hidden'); | 502 $('error-text').classList.remove('hidden'); |
448 removeEventListeners(); | 503 removeEventListeners(); |
449 var pdfViewer = $('pdf-viewer'); | 504 var pdfViewer = $('pdf-viewer'); |
450 if (pdfViewer) | 505 if (pdfViewer) |
451 $('mainview').removeChild(pdfViewer); | 506 $('mainview').removeChild(pdfViewer); |
452 } | 507 } |
453 | 508 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 * @param {string} jobTitle The print job title. | 556 * @param {string} jobTitle The print job title. |
502 * @param {boolean} modifiable If the preview is modifiable. | 557 * @param {boolean} modifiable If the preview is modifiable. |
503 * @param {string} previewUid Preview unique identifier. | 558 * @param {string} previewUid Preview unique identifier. |
504 */ | 559 */ |
505 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { | 560 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { |
506 var tempPrintSettings = new PrintSettings(); | 561 var tempPrintSettings = new PrintSettings(); |
507 tempPrintSettings.save(); | 562 tempPrintSettings.save(); |
508 | 563 |
509 previewModifiable = modifiable; | 564 previewModifiable = modifiable; |
510 | 565 |
| 566 hasPendingPreviewRequest = false; |
| 567 |
511 if (totalPageCount == -1) | 568 if (totalPageCount == -1) |
512 totalPageCount = pageCount; | 569 totalPageCount = pageCount; |
513 | 570 |
514 if (previouslySelectedPages.length == 0) | 571 if (previouslySelectedPages.length == 0) |
515 for (var i = 0; i < totalPageCount; i++) | 572 for (var i = 0; i < totalPageCount; i++) |
516 previouslySelectedPages.push(i+1); | 573 previouslySelectedPages.push(i+1); |
517 | 574 |
518 if (printSettings.deviceName != tempPrintSettings.deviceName) { | 575 if (printSettings.deviceName != tempPrintSettings.deviceName) { |
519 updateControlsWithSelectedPrinterCapabilities(); | 576 updateControlsWithSelectedPrinterCapabilities(); |
520 return; | 577 return; |
(...skipping 12 matching lines...) Expand all Loading... |
533 if (!isSelectedPagesValid()) | 590 if (!isSelectedPagesValid()) |
534 pageRangesFieldChanged(); | 591 pageRangesFieldChanged(); |
535 | 592 |
536 // Update the current tab title. | 593 // Update the current tab title. |
537 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 594 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
538 | 595 |
539 createPDFPlugin(previewUid); | 596 createPDFPlugin(previewUid); |
540 updatePrintSummary(); | 597 updatePrintSummary(); |
541 updatePrintButtonState(); | 598 updatePrintButtonState(); |
542 addEventListeners(); | 599 addEventListeners(); |
| 600 |
| 601 if (hasPendingPrintFileRequest) |
| 602 printFile(); |
543 } | 603 } |
544 | 604 |
545 /** | 605 /** |
546 * Create the PDF plugin or reload the existing one. | 606 * Create the PDF plugin or reload the existing one. |
547 * @param {string} previewUid Preview unique identifier. | 607 * @param {string} previewUid Preview unique identifier. |
548 */ | 608 */ |
549 function createPDFPlugin(previewUid) { | 609 function createPDFPlugin(previewUid) { |
550 // Enable the print button. | 610 // Enable the print button. |
551 if (!$('printer-list').disabled) { | 611 if (!$('printer-list').disabled) { |
552 $('print-button').disabled = false; | 612 $('print-button').disabled = false; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 /** | 671 /** |
612 * Listener function that executes whenever a change occurs in the 'copies' | 672 * Listener function that executes whenever a change occurs in the 'copies' |
613 * field. | 673 * field. |
614 */ | 674 */ |
615 function copiesFieldChanged() { | 675 function copiesFieldChanged() { |
616 updateCopiesButtonsState(); | 676 updateCopiesButtonsState(); |
617 $('collate-option').hidden = getCopies() <= 1; | 677 $('collate-option').hidden = getCopies() <= 1; |
618 } | 678 } |
619 | 679 |
620 /** | 680 /** |
621 * Executes whenever a blur event occurs on the 'individual-pages' | 681 * Validates the page ranges text and updates the hint accordingly. |
622 * field or when the timer expires. It takes care of | |
623 * 1) showing/hiding warnings/suggestions | |
624 * 2) updating print button/summary | |
625 */ | 682 */ |
626 function pageRangesFieldChanged() { | 683 function validatePageRangesField() { |
627 var currentlySelectedPages = getSelectedPagesSet(); | |
628 var individualPagesField = $('individual-pages'); | 684 var individualPagesField = $('individual-pages'); |
629 var individualPagesHint = $('individual-pages-hint'); | 685 var individualPagesHint = $('individual-pages-hint'); |
630 | 686 |
631 if (isSelectedPagesValid()) { | 687 if (isSelectedPagesValid()) { |
632 individualPagesField.classList.remove('invalid'); | 688 individualPagesField.classList.remove('invalid'); |
633 fadeOutElement(individualPagesHint); | 689 fadeOutElement(individualPagesHint); |
634 } else { | 690 } else { |
635 individualPagesField.classList.add('invalid'); | 691 individualPagesField.classList.add('invalid'); |
636 individualPagesHint.classList.remove('suggestion'); | 692 individualPagesHint.classList.remove('suggestion'); |
637 individualPagesHint.innerHTML = | 693 individualPagesHint.innerHTML = |
638 localStrings.getStringF('pageRangeInstruction', | 694 localStrings.getStringF('pageRangeInstruction', |
639 localStrings.getString( | 695 localStrings.getString( |
640 'examplePageRangeText')); | 696 'examplePageRangeText')); |
641 fadeInElement(individualPagesHint); | 697 fadeInElement(individualPagesHint); |
642 } | 698 } |
| 699 } |
| 700 |
| 701 /** |
| 702 * Executes whenever a blur event occurs on the 'individual-pages' |
| 703 * field or when the timer expires. It takes care of |
| 704 * 1) showing/hiding warnings/suggestions |
| 705 * 2) updating print button/summary |
| 706 */ |
| 707 function pageRangesFieldChanged() { |
| 708 validatePageRangesField(); |
643 | 709 |
644 resetPageRangeFieldTimer(); | 710 resetPageRangeFieldTimer(); |
645 updatePrintButtonState(); | 711 updatePrintButtonState(); |
646 updatePrintSummary(); | 712 updatePrintSummary(); |
647 } | 713 } |
648 | 714 |
649 /** | 715 /** |
650 * Updates the state of the increment/decrement buttons based on the current | 716 * Updates the state of the increment/decrement buttons based on the current |
651 * 'copies' value. | 717 * 'copies' value. |
652 */ | 718 */ |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
811 var successfullyParsed = 0; | 877 var successfullyParsed = 0; |
812 var parts = pageText.split(/,/); | 878 var parts = pageText.split(/,/); |
813 | 879 |
814 for (var i = 0; i < parts.length; ++i) { | 880 for (var i = 0; i < parts.length; ++i) { |
815 var part = parts[i].replace(/\s*/g, ''); | 881 var part = parts[i].replace(/\s*/g, ''); |
816 if (part.length == 0) | 882 if (part.length == 0) |
817 continue; | 883 continue; |
818 | 884 |
819 var match = part.match(/^([0-9]+)-([0-9]*)$/); | 885 var match = part.match(/^([0-9]+)-([0-9]*)$/); |
820 if (match && isValidNonZeroPositiveInteger(match[1])) { | 886 if (match && isValidNonZeroPositiveInteger(match[1])) { |
| 887 if (!match[2] && totalPageCount == -1) { |
| 888 successfullyParsed += 1; |
| 889 continue; |
| 890 } |
821 var from = parseInt(match[1], 10); | 891 var from = parseInt(match[1], 10); |
822 var to = match[2] ? parseInt(match[2], 10) : totalPageCount; | 892 var to = match[2] ? parseInt(match[2], 10) : totalPageCount; |
823 | 893 |
824 if (!to || from > to) | 894 if (!to || from > to) |
825 return false; | 895 return false; |
826 } else if (!isValidNonZeroPositiveInteger(part) || | 896 } else if (!isValidNonZeroPositiveInteger(part) || (totalPageCount != -1 && |
827 !(parseInt(part, 10) <= totalPageCount)) { | 897 !(parseInt(part, 10) <= totalPageCount))) { |
828 return false; | 898 return false; |
829 } | 899 } |
830 successfullyParsed += 1; | 900 successfullyParsed += 1; |
831 } | 901 } |
832 return successfullyParsed > 0 | 902 return successfullyParsed > 0 |
833 } | 903 } |
834 | 904 |
835 /** | 905 /** |
836 * Returns true if |value| is a valid non zero positive integer. | 906 * Returns true if |value| is a valid non zero positive integer. |
837 * @param {string} value The string to be tested. | 907 * @param {string} value The string to be tested. |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
967 this.isLandscape = ''; | 1037 this.isLandscape = ''; |
968 } | 1038 } |
969 | 1039 |
970 /** | 1040 /** |
971 * Takes a snapshot of the print settings. | 1041 * Takes a snapshot of the print settings. |
972 */ | 1042 */ |
973 PrintSettings.prototype.save = function() { | 1043 PrintSettings.prototype.save = function() { |
974 this.deviceName = getSelectedPrinterName(); | 1044 this.deviceName = getSelectedPrinterName(); |
975 this.isLandscape = isLandscape(); | 1045 this.isLandscape = isLandscape(); |
976 } | 1046 } |
OLD | NEW |