Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 23 // Used to disable some printing options when the preview is not modifiable. | 23 // Used to disable some printing options when the preview is not modifiable. |
| 24 var previewModifiable = false; | 24 var previewModifiable = false; |
| 25 | 25 |
| 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 // True when a pending print preview request exists. | |
| 34 var hasPendingPreviewRequest = false; | |
| 35 | |
| 36 // True when a pending print file request exists. | |
| 37 var hasPendingPrintFileRequest = false; | |
| 38 | |
| 39 // True when a compatible plugin exists. | |
| 40 var hasCompatiblePlugin = true; | |
| 41 | |
| 42 // True when initiator tab is closed. | |
| 43 var isInitiatorTabClosed = false; | |
| 44 | |
| 33 /** | 45 /** |
| 34 * 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 |
| 35 * the printer list. | 47 * the printer list. |
| 36 */ | 48 */ |
| 37 function onLoad() { | 49 function onLoad() { |
| 38 $('system-dialog-link').addEventListener('click', showSystemDialog); | 50 $('system-dialog-link').addEventListener('click', showSystemDialog); |
| 39 $('cancel-button').addEventListener('click', handleCancelButtonClick); | 51 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
| 40 | 52 |
| 41 if (!checkCompatiblePluginExists()) { | 53 if (!checkCompatiblePluginExists()) { |
| 54 hasCompatiblePlugin = false; | |
| 42 displayErrorMessage(localStrings.getString('noPlugin'), false); | 55 displayErrorMessage(localStrings.getString('noPlugin'), false); |
| 43 $('mainview').parentElement.removeChild($('dummy-viewer')); | 56 $('mainview').parentElement.removeChild($('dummy-viewer')); |
| 44 return; | 57 return; |
| 45 } | 58 } |
| 46 $('mainview').parentElement.removeChild($('dummy-viewer')); | 59 $('mainview').parentElement.removeChild($('dummy-viewer')); |
| 47 | 60 |
| 48 $('printer-list').disabled = true; | 61 $('printer-list').disabled = true; |
| 49 $('print-button').disabled = true; | 62 $('print-button').onclick = printFile; |
| 63 | |
| 64 setDefaultHandlersForCopiesControls(); | |
| 65 setDefaultHandlersForPagesControls(); | |
| 50 showLoadingAnimation(); | 66 showLoadingAnimation(); |
| 51 chrome.send('getDefaultPrinter'); | 67 chrome.send('getDefaultPrinter'); |
| 52 } | 68 } |
| 53 | 69 |
| 54 /** | 70 /** |
| 71 * Handles all pages checkbox click event. | |
| 72 */ | |
| 73 function handleAllPagesCheckbox() { | |
| 74 updatePrintButtonState(); | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Validates the individual pages text format. | |
| 79 */ | |
| 80 function validateIndividualPagesText() { | |
| 81 $('print-pages').checked = true; | |
| 82 validatePageRangesField(); | |
| 83 updatePrintButtonState(); | |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * Handles the individual pages input event. | |
| 88 */ | |
| 89 function handleIndividualPagesInputEvent() { | |
| 90 $('print-pages').checked = true; | |
| 91 resetPageRangeFieldTimer(); | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Sets the default event handlers for pages controls. | |
| 96 */ | |
| 97 function setDefaultHandlersForPagesControls() { | |
| 98 var allPages = $('all-pages'); | |
| 99 var printPages = $('print-pages'); | |
| 100 var individualPages = $('individual-pages'); | |
| 101 | |
| 102 allPages.onclick = null; | |
| 103 printPages.onclick = null; | |
| 104 individualPages.oninput = null; | |
| 105 individualPages.onfocus = null; | |
| 106 individualPages.onblur = null; | |
| 107 | |
| 108 if (hasCompatiblePlugin && !isInitiatorTabClosed) { | |
| 109 allPages.onclick = handleAllPagesCheckbox; | |
| 110 printPages.onclick = handleIndividualPagesCheckbox; | |
| 111 individualPages.onblur = validateIndividualPagesText; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Sets the default event handlers for copies controls. | |
| 117 */ | |
| 118 function setDefaultHandlersForCopiesControls() { | |
| 119 $('copies').oninput = copiesFieldChanged; | |
| 120 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; | |
| 121 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; | |
| 122 } | |
| 123 | |
| 124 /** | |
| 55 * Adds event listeners to the settings controls. | 125 * Adds event listeners to the settings controls. |
| 56 */ | 126 */ |
| 57 function addEventListeners() { | 127 function addEventListeners() { |
| 58 $('print-button').onclick = printFile; | |
| 59 | |
| 60 // Controls that require preview rendering. | 128 // Controls that require preview rendering. |
| 61 $('all-pages').onclick = onPageSelectionMayHaveChanged; | 129 $('all-pages').onclick = onPageSelectionMayHaveChanged; |
| 62 $('print-pages').onclick = handleIndividualPagesCheckbox; | 130 $('print-pages').onclick = handleIndividualPagesCheckbox; |
| 63 var individualPages = $('individual-pages'); | 131 var individualPages = $('individual-pages'); |
| 64 individualPages.onblur = function() { | 132 individualPages.onblur = function() { |
| 65 clearTimeout(timerId); | 133 clearTimeout(timerId); |
| 66 onPageSelectionMayHaveChanged(); | 134 onPageSelectionMayHaveChanged(); |
| 67 }; | 135 }; |
| 68 individualPages.onfocus = addTimerToPageRangeField; | 136 individualPages.onfocus = addTimerToPageRangeField; |
| 69 individualPages.oninput = resetPageRangeFieldTimer; | 137 individualPages.oninput = handleIndividualPagesInputEvent; |
| 70 $('landscape').onclick = onLayoutModeToggle; | 138 $('landscape').onclick = onLayoutModeToggle; |
| 71 $('portrait').onclick = onLayoutModeToggle; | 139 $('portrait').onclick = onLayoutModeToggle; |
| 72 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; | 140 $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; |
| 73 | 141 |
| 74 // Controls that dont require preview rendering. | 142 // Controls that dont require preview rendering. |
| 75 $('copies').oninput = function() { | 143 $('copies').oninput = function() { |
| 76 copiesFieldChanged(); | 144 copiesFieldChanged(); |
| 77 updatePrintButtonState(); | 145 updatePrintButtonState(); |
| 78 updatePrintSummary(); | 146 updatePrintSummary(); |
| 79 }; | 147 }; |
| 80 $('two-sided').onclick = handleTwoSidedClick; | 148 $('two-sided').onclick = handleTwoSidedClick; |
| 81 $('color').onclick = function() { setColor(true); }; | 149 $('color').onclick = function() { setColor(true); }; |
| 82 $('bw').onclick = function() { setColor(false); }; | 150 $('bw').onclick = function() { setColor(false); }; |
| 83 $('increment').onclick = function() { | 151 $('increment').onclick = function() { |
| 84 onCopiesButtonsClicked(1); | 152 onCopiesButtonsClicked(1); |
| 85 updatePrintButtonState(); | 153 updatePrintButtonState(); |
| 86 updatePrintSummary(); | 154 updatePrintSummary(); |
| 87 }; | 155 }; |
| 88 $('decrement').onclick = function() { | 156 $('decrement').onclick = function() { |
| 89 onCopiesButtonsClicked(-1); | 157 onCopiesButtonsClicked(-1); |
| 90 updatePrintButtonState(); | 158 updatePrintButtonState(); |
| 91 updatePrintSummary(); | 159 updatePrintSummary(); |
| 92 }; | 160 }; |
| 93 } | 161 } |
| 94 | 162 |
| 95 /** | 163 /** |
| 96 * Removes event listeners from the settings controls. | 164 * Removes event listeners from the settings controls. |
| 97 */ | 165 */ |
| 98 function removeEventListeners() { | 166 function removeEventListeners() { |
| 99 // Controls that require preview rendering. | |
| 100 $('print-button').disabled = true; | |
| 101 $('all-pages').onclick = null; | |
| 102 $('print-pages').onclick = null; | |
| 103 var individualPages = $('individual-pages'); | |
| 104 individualPages.onblur = null; | |
| 105 individualPages.onfocus = null; | |
| 106 individualPages.oninput = null; | |
| 107 clearTimeout(timerId); | 167 clearTimeout(timerId); |
| 168 | |
| 169 // Controls that require preview rendering | |
| 170 setDefaultHandlersForPagesControls(); | |
| 108 $('landscape').onclick = null; | 171 $('landscape').onclick = null; |
| 109 $('portrait').onclick = null; | 172 $('portrait').onclick = null; |
| 110 $('printer-list').onchange = null; | 173 $('printer-list').onchange = null; |
| 111 | 174 |
| 112 // Controls that dont require preview rendering. | 175 // Controls that dont require preview rendering. |
| 113 $('copies').oninput = copiesFieldChanged; | |
| 114 $('two-sided').onclick = null; | 176 $('two-sided').onclick = null; |
| 115 $('color').onclick = null; | 177 $('color').onclick = null; |
| 116 $('bw').onclick = null; | 178 $('bw').onclick = null; |
| 117 $('increment').onclick = function() { onCopiesButtonsClicked(1); }; | 179 setDefaultHandlersForCopiesControls(); |
| 118 $('decrement').onclick = function() { onCopiesButtonsClicked(-1); }; | |
| 119 } | 180 } |
| 120 | 181 |
| 121 /** | 182 /** |
| 122 * Asks the browser to close the preview tab. | 183 * Asks the browser to close the preview tab. |
| 123 */ | 184 */ |
| 124 function handleCancelButtonClick() { | 185 function handleCancelButtonClick() { |
| 125 chrome.send('closePrintPreviewTab'); | 186 chrome.send('closePrintPreviewTab'); |
| 126 } | 187 } |
| 127 | 188 |
| 128 /** | 189 /** |
| 129 * Asks the browser to show the native print dialog for printing. | 190 * Asks the browser to show the native print dialog for printing. |
| 130 */ | 191 */ |
| 131 function showSystemDialog() { | 192 function showSystemDialog() { |
| 132 chrome.send('showSystemDialog'); | 193 chrome.send('showSystemDialog'); |
| 133 } | 194 } |
| 134 | 195 |
| 135 /** | 196 /** |
| 136 * Disables the controls which need the initiator tab to generate preview | 197 * Disables the controls which need the initiator tab to generate preview |
| 137 * data. This function is called when the initiator tab is closed. | 198 * data. This function is called when the initiator tab is closed. |
| 138 * @param {string} initiatorTabURL The URL of the initiator tab. | 199 * @param {string} initiatorTabURL The URL of the initiator tab. |
| 139 */ | 200 */ |
| 140 function onInitiatorTabClosed(initiatorTabURL) { | 201 function onInitiatorTabClosed(initiatorTabURL) { |
| 202 isInitiatorTabClosed = true; | |
| 141 $('reopen-page').addEventListener('click', function() { | 203 $('reopen-page').addEventListener('click', function() { |
| 142 window.location = initiatorTabURL; | 204 window.location = initiatorTabURL; |
| 143 }); | 205 }); |
| 144 displayErrorMessage(localStrings.getString('initiatorTabClosed'), true); | 206 displayErrorMessage(localStrings.getString('initiatorTabClosed'), true); |
| 145 } | 207 } |
| 146 | 208 |
| 147 /** | 209 /** |
| 148 * Gets the selected printer capabilities and updates the controls accordingly. | 210 * Gets the selected printer capabilities and updates the controls accordingly. |
| 149 */ | 211 */ |
| 150 function updateControlsWithSelectedPrinterCapabilities() { | 212 function updateControlsWithSelectedPrinterCapabilities() { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 var deviceName = ''; | 381 var deviceName = ''; |
| 320 if (selectedPrinter >= 0) | 382 if (selectedPrinter >= 0) |
| 321 deviceName = printerList.options[selectedPrinter].value; | 383 deviceName = printerList.options[selectedPrinter].value; |
| 322 return deviceName; | 384 return deviceName; |
| 323 } | 385 } |
| 324 | 386 |
| 325 /** | 387 /** |
| 326 * Asks the browser to print the preview PDF based on current print settings. | 388 * Asks the browser to print the preview PDF based on current print settings. |
| 327 */ | 389 */ |
| 328 function printFile() { | 390 function printFile() { |
| 391 hasPendingPrintFileRequest = hasPendingPreviewRequest ? true : false; | |
| 392 | |
| 393 if (hasPendingPrintFileRequest) { | |
| 394 if (getSelectedPrinterName() != PRINT_TO_PDF) | |
| 395 chrome.send('hidePreview'); | |
| 396 return; | |
| 397 } | |
| 398 | |
| 329 if (getSelectedPrinterName() != PRINT_TO_PDF) { | 399 if (getSelectedPrinterName() != PRINT_TO_PDF) { |
| 330 $('print-button').classList.add('loading'); | 400 $('print-button').classList.add('loading'); |
| 331 $('cancel-button').classList.add('loading'); | 401 $('cancel-button').classList.add('loading'); |
| 332 $('print-summary').innerHTML = localStrings.getString('printing'); | 402 $('print-summary').innerHTML = localStrings.getString('printing'); |
| 333 removeEventListeners(); | 403 removeEventListeners(); |
| 334 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, | 404 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, |
| 335 1000); | 405 1000); |
| 336 } else | 406 } else { |
| 337 chrome.send('print', [getSettingsJSON()]); | 407 chrome.send('print', [getSettingsJSON()]); |
| 408 } | |
| 338 } | 409 } |
| 339 | 410 |
| 340 /** | 411 /** |
| 341 * Asks the browser to generate a preview PDF based on current print settings. | 412 * Asks the browser to generate a preview PDF based on current print settings. |
| 342 */ | 413 */ |
| 343 function requestPrintPreview() { | 414 function requestPrintPreview() { |
| 415 hasPendingPreviewRequest = true; | |
| 344 removeEventListeners(); | 416 removeEventListeners(); |
| 345 printSettings.save(); | 417 printSettings.save(); |
| 346 showLoadingAnimation(); | 418 showLoadingAnimation(); |
| 347 chrome.send('getPreview', [getSettingsJSON()]); | 419 chrome.send('getPreview', [getSettingsJSON()]); |
| 348 } | 420 } |
| 349 | 421 |
| 350 /** | 422 /** |
| 351 * Set the default printer. If there is one, generate a print preview. | 423 * Set the default printer. If there is one, generate a print preview. |
| 352 * @param {string} printer Name of the default printer. Empty if none. | 424 * @param {string} printer Name of the default printer. Empty if none. |
| 353 */ | 425 */ |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 var pdfViewer = $('pdf-viewer'); | 503 var pdfViewer = $('pdf-viewer'); |
| 432 if (!pdfViewer) { | 504 if (!pdfViewer) { |
| 433 return; | 505 return; |
| 434 } | 506 } |
| 435 pdfViewer.grayscale(!color); | 507 pdfViewer.grayscale(!color); |
| 436 } | 508 } |
| 437 | 509 |
| 438 /** | 510 /** |
| 439 * Display an error message in the center of the preview area. | 511 * Display an error message in the center of the preview area. |
| 440 * @param {string} errorMessage The error message to be displayed. | 512 * @param {string} errorMessage The error message to be displayed. |
| 441 * @param {boolean} showButton Indivates whether the "Reopen the page" button | 513 * @param {boolean} showButton Indivates whether the "Reopen the page" button |
|
dpapad
2011/06/08 01:27:51
nit: s/Indivates/Indicates
kmadhusu
2011/06/08 17:09:50
Done.
| |
| 442 * should be displayed. | 514 * should be displayed. |
| 443 */ | 515 */ |
| 444 function displayErrorMessage(errorMessage, showButton) { | 516 function displayErrorMessage(errorMessage, showButton) { |
| 517 $('print-button').disabled = true; | |
| 445 $('overlay-layer').classList.remove('invisible'); | 518 $('overlay-layer').classList.remove('invisible'); |
| 446 $('dancing-dots-text').classList.add('hidden'); | 519 $('dancing-dots-text').classList.add('hidden'); |
| 447 $('error-text').innerHTML = errorMessage; | 520 $('error-text').innerHTML = errorMessage; |
| 448 $('error-text').classList.remove('hidden'); | 521 $('error-text').classList.remove('hidden'); |
| 449 if (showButton) | 522 if (showButton) |
| 450 $('reopen-page').classList.remove('hidden'); | 523 $('reopen-page').classList.remove('hidden'); |
| 451 else | 524 else |
| 452 $('reopen-page').classList.add('hidden'); | 525 $('reopen-page').classList.add('hidden'); |
| 453 | 526 |
| 454 removeEventListeners(); | 527 removeEventListeners(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 * @param {string} jobTitle The print job title. | 563 * @param {string} jobTitle The print job title. |
| 491 * @param {boolean} modifiable If the preview is modifiable. | 564 * @param {boolean} modifiable If the preview is modifiable. |
| 492 * @param {string} previewUid Preview unique identifier. | 565 * @param {string} previewUid Preview unique identifier. |
| 493 */ | 566 */ |
| 494 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { | 567 function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { |
| 495 var tempPrintSettings = new PrintSettings(); | 568 var tempPrintSettings = new PrintSettings(); |
| 496 tempPrintSettings.save(); | 569 tempPrintSettings.save(); |
| 497 | 570 |
| 498 previewModifiable = modifiable; | 571 previewModifiable = modifiable; |
| 499 | 572 |
| 573 hasPendingPreviewRequest = false; | |
| 574 | |
| 500 if (totalPageCount == -1) | 575 if (totalPageCount == -1) |
| 501 totalPageCount = pageCount; | 576 totalPageCount = pageCount; |
| 502 | 577 |
| 503 if (previouslySelectedPages.length == 0) | 578 if (previouslySelectedPages.length == 0) |
| 504 for (var i = 0; i < totalPageCount; i++) | 579 for (var i = 0; i < totalPageCount; i++) |
| 505 previouslySelectedPages.push(i+1); | 580 previouslySelectedPages.push(i+1); |
| 506 | 581 |
| 507 if (printSettings.deviceName != tempPrintSettings.deviceName) { | 582 if (printSettings.deviceName != tempPrintSettings.deviceName) { |
| 508 updateControlsWithSelectedPrinterCapabilities(); | 583 updateControlsWithSelectedPrinterCapabilities(); |
| 509 return; | 584 return; |
| 510 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { | 585 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { |
| 511 setDefaultValuesAndRegeneratePreview(); | 586 setDefaultValuesAndRegeneratePreview(); |
| 512 return; | 587 return; |
| 513 } else if (getSelectedPagesValidityLevel() == 1) { | 588 } else if (getSelectedPagesValidityLevel() == 1) { |
| 514 var currentlySelectedPages = getSelectedPagesSet(); | 589 var currentlySelectedPages = getSelectedPagesSet(); |
| 515 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { | 590 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { |
| 516 previouslySelectedPages = currentlySelectedPages; | 591 previouslySelectedPages = currentlySelectedPages; |
| 517 requestPrintPreview(); | 592 requestPrintPreview(); |
| 518 return; | 593 return; |
| 519 } | 594 } |
| 520 } | 595 } |
| 521 | 596 |
| 522 if (getSelectedPagesValidityLevel() != 1) | 597 if (getSelectedPagesValidityLevel() != 1) |
| 523 pageRangesFieldChanged(); | 598 pageRangesFieldChanged(); |
| 524 | 599 |
| 525 // Update the current tab title. | 600 // Update the current tab title. |
| 526 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); | 601 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
| 527 | 602 |
| 528 createPDFPlugin(previewUid); | 603 createPDFPlugin(previewUid); |
| 604 | |
| 605 if (hasPendingPrintFileRequest) { | |
| 606 printFile(); | |
| 607 return; | |
| 608 } | |
| 609 | |
| 529 updatePrintSummary(); | 610 updatePrintSummary(); |
| 530 updatePrintButtonState(); | 611 updatePrintButtonState(); |
| 531 addEventListeners(); | 612 addEventListeners(); |
| 532 } | 613 } |
| 533 | 614 |
| 534 /** | 615 /** |
| 535 * Create the PDF plugin or reload the existing one. | 616 * Create the PDF plugin or reload the existing one. |
| 536 * @param {string} previewUid Preview unique identifier. | 617 * @param {string} previewUid Preview unique identifier. |
| 537 */ | 618 */ |
| 538 function createPDFPlugin(previewUid) { | 619 function createPDFPlugin(previewUid) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 /** | 681 /** |
| 601 * Listener function that executes whenever a change occurs in the 'copies' | 682 * Listener function that executes whenever a change occurs in the 'copies' |
| 602 * field. | 683 * field. |
| 603 */ | 684 */ |
| 604 function copiesFieldChanged() { | 685 function copiesFieldChanged() { |
| 605 updateCopiesButtonsState(); | 686 updateCopiesButtonsState(); |
| 606 $('collate-option').hidden = getCopies() <= 1; | 687 $('collate-option').hidden = getCopies() <= 1; |
| 607 } | 688 } |
| 608 | 689 |
| 609 /** | 690 /** |
| 610 * Executes whenever a blur event occurs on the 'individual-pages' | 691 * Validates the page ranges text and updates the hint accordingly. |
| 611 * field or when the timer expires. It takes care of | |
| 612 * 1) showing/hiding warnings/suggestions | |
| 613 * 2) updating print button/summary | |
| 614 */ | 692 */ |
| 615 function pageRangesFieldChanged() { | 693 function validatePageRangesField() { |
| 616 var currentlySelectedPages = getSelectedPagesSet(); | |
| 617 var individualPagesField = $('individual-pages'); | 694 var individualPagesField = $('individual-pages'); |
| 618 var individualPagesHint = $('individual-pages-hint'); | 695 var individualPagesHint = $('individual-pages-hint'); |
| 619 var validityLevel = getSelectedPagesValidityLevel(); | 696 var validityLevel = getSelectedPagesValidityLevel(); |
| 620 | 697 |
| 621 if (validityLevel == 1) { | 698 if (validityLevel == 1) { |
| 622 individualPagesField.classList.remove('invalid'); | 699 individualPagesField.classList.remove('invalid'); |
| 623 fadeOutElement(individualPagesHint); | 700 fadeOutElement(individualPagesHint); |
| 624 } else { | 701 } else { |
| 625 individualPagesField.classList.add('invalid'); | 702 individualPagesField.classList.add('invalid'); |
| 626 individualPagesHint.classList.remove('suggestion'); | 703 individualPagesHint.classList.remove('suggestion'); |
| 627 individualPagesHint.innerHTML = | 704 individualPagesHint.innerHTML = |
| 628 localStrings.getStringF('pageRangeInstruction', | 705 localStrings.getStringF('pageRangeInstruction', |
| 629 localStrings.getString( | 706 localStrings.getString( |
| 630 'examplePageRangeText')); | 707 'examplePageRangeText')); |
| 631 fadeInElement(individualPagesHint); | 708 fadeInElement(individualPagesHint); |
| 632 } | 709 } |
| 710 } | |
| 711 | |
| 712 /** | |
| 713 * Executes whenever a blur event occurs on the 'individual-pages' | |
| 714 * field or when the timer expires. It takes care of | |
| 715 * 1) showing/hiding warnings/suggestions | |
| 716 * 2) updating print button/summary | |
| 717 */ | |
| 718 function pageRangesFieldChanged() { | |
| 719 validatePageRangesField(); | |
| 633 | 720 |
| 634 resetPageRangeFieldTimer(); | 721 resetPageRangeFieldTimer(); |
| 635 updatePrintButtonState(); | 722 updatePrintButtonState(); |
| 636 updatePrintSummary(); | 723 updatePrintSummary(); |
| 637 } | 724 } |
| 638 | 725 |
| 639 /** | 726 /** |
| 640 * Updates the state of the increment/decrement buttons based on the current | 727 * Updates the state of the increment/decrement buttons based on the current |
| 641 * 'copies' value. | 728 * 'copies' value. |
| 642 */ | 729 */ |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 797 var pageText = $('individual-pages').value; | 884 var pageText = $('individual-pages').value; |
| 798 | 885 |
| 799 if ($('all-pages').checked || pageText.length == 0) | 886 if ($('all-pages').checked || pageText.length == 0) |
| 800 return 1; | 887 return 1; |
| 801 | 888 |
| 802 var successfullyParsed = 0; | 889 var successfullyParsed = 0; |
| 803 var failedToParse = 0; | 890 var failedToParse = 0; |
| 804 | 891 |
| 805 var parts = pageText.split(/,/); | 892 var parts = pageText.split(/,/); |
| 806 | 893 |
| 807 for (var i = 0; i < parts.length; ++i) { | 894 for (var i = 0; i < parts.length; ++i) { |
|
dpapad
2011/06/08 01:27:51
This for loop is getting hard to read. Nothing to
kmadhusu
2011/06/08 17:09:50
we should do that.
| |
| 808 var part = parts[i].replace(/\s*/g, ''); | 895 var part = parts[i].replace(/\s*/g, ''); |
| 809 if (part.length == 0) | 896 if (part.length == 0) |
| 810 continue; | 897 continue; |
| 811 | 898 |
| 812 var match = part.match(/^([0-9]+)-([0-9]*)$/); | 899 var match = part.match(/^([0-9]+)-([0-9]*)$/); |
| 813 if (match && match[1]) { | 900 if (totalPageCount != -1) { |
| 814 var from = parseInt(match[1], 10); | 901 if (match && match[1]) { |
| 815 var to = match[2] ? parseInt(match[2], 10) : totalPageCount; | 902 var from = parseInt(match[1], 10); |
| 903 var to = match[2] ? parseInt(match[2], 10) : totalPageCount; | |
| 816 | 904 |
| 817 if (from && to && from <= to) | 905 if (from && to && from <= to) |
| 906 successfullyParsed += 1; | |
| 907 else | |
| 908 failedToParse += 1; | |
| 909 } else if (isValidNonZeroPositiveInteger(part) && | |
| 910 parseInt(part, 10) <= totalPageCount) { | |
| 818 successfullyParsed += 1; | 911 successfullyParsed += 1; |
| 819 else | 912 } else { |
| 820 failedToParse += 1; | 913 failedToParse += 1; |
| 914 } | |
| 915 } else { | |
| 916 // totalPageCount is -1. Just validate the page range format. | |
| 917 if (match && isValidNonZeroPositiveInteger(match[1])) { | |
| 918 if (match[2]) { | |
| 919 if (isValidNonZeroPositiveInteger(match[2])) { | |
| 920 if (parseInt(match[1], 10) <= parseInt(match[2], 10)) | |
| 921 successfullyParsed += 1; | |
| 922 else | |
| 923 failedToParse += 1; | |
| 924 } else { | |
| 925 failedToParse += 1; | |
| 926 } | |
| 927 } else { | |
| 928 successfullyParsed += 1; | |
| 929 } | |
| 930 } else if (isValidNonZeroPositiveInteger(part)) { | |
| 931 successfullyParsed += 1; | |
| 932 } else { | |
| 933 failedToParse += 1; | |
| 934 } | |
| 935 } | |
| 936 } | |
| 821 | 937 |
| 822 } else if (isInteger(part) && parseInt(part, 10) <= totalPageCount && | |
| 823 parseInt(part, 10) > 0) | |
| 824 successfullyParsed += 1; | |
| 825 else | |
| 826 failedToParse += 1; | |
| 827 } | |
| 828 if (successfullyParsed > 0 && failedToParse == 0) | 938 if (successfullyParsed > 0 && failedToParse == 0) |
| 829 return 1; | 939 return 1; |
| 830 else if (successfullyParsed > 0 && failedToParse > 0) | 940 else if (successfullyParsed > 0 && failedToParse > 0) |
| 831 return 0; | 941 return 0; |
| 832 else | 942 else |
| 833 return -1; | 943 return -1; |
| 834 } | 944 } |
| 835 | 945 |
| 946 /** | |
| 947 * Returns true if |value| is a valid non zero positive integer. | |
| 948 * @param {string} value The string to be tested. | |
| 949 */ | |
| 950 function isValidNonZeroPositiveInteger(value) { | |
| 951 return isInteger(value) && parseInt(value, 10) > 0; | |
| 952 } | |
| 953 | |
| 836 function isSelectedPagesFieldValid() { | 954 function isSelectedPagesFieldValid() { |
| 837 return (getSelectedPages().length != 0) | 955 return (getSelectedPages().length != 0) |
| 838 } | 956 } |
| 839 | 957 |
| 840 /** | 958 /** |
| 841 * Parses the selected page ranges, processes them and returns the results. | 959 * Parses the selected page ranges, processes them and returns the results. |
| 842 * It squashes whenever possible. Example '1-2,3,5-7' becomes 1-3,5-7 | 960 * It squashes whenever possible. Example '1-2,3,5-7' becomes 1-3,5-7 |
| 843 * | 961 * |
| 844 * @return {Array} an array of page range objects. A page range object has | 962 * @return {Array} an array of page range objects. A page range object has |
| 845 * fields 'from' and 'to'. | 963 * fields 'from' and 'to'. |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 965 this.isLandscape = ''; | 1083 this.isLandscape = ''; |
| 966 } | 1084 } |
| 967 | 1085 |
| 968 /** | 1086 /** |
| 969 * Takes a snapshot of the print settings. | 1087 * Takes a snapshot of the print settings. |
| 970 */ | 1088 */ |
| 971 PrintSettings.prototype.save = function() { | 1089 PrintSettings.prototype.save = function() { |
| 972 this.deviceName = getSelectedPrinterName(); | 1090 this.deviceName = getSelectedPrinterName(); |
| 973 this.isLandscape = isLandscape(); | 1091 this.isLandscape = isLandscape(); |
| 974 } | 1092 } |
| OLD | NEW |