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

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

Issue 7003060: PrintPreview Cleanup: Rename and refactor getSelectedPagesValidity() function. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 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 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 if (previouslySelectedPages.length == 0) 503 if (previouslySelectedPages.length == 0)
504 for (var i = 0; i < totalPageCount; i++) 504 for (var i = 0; i < totalPageCount; i++)
505 previouslySelectedPages.push(i+1); 505 previouslySelectedPages.push(i+1);
506 506
507 if (printSettings.deviceName != tempPrintSettings.deviceName) { 507 if (printSettings.deviceName != tempPrintSettings.deviceName) {
508 updateControlsWithSelectedPrinterCapabilities(); 508 updateControlsWithSelectedPrinterCapabilities();
509 return; 509 return;
510 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) { 510 } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) {
511 setDefaultValuesAndRegeneratePreview(); 511 setDefaultValuesAndRegeneratePreview();
512 return; 512 return;
513 } else if (getSelectedPagesValidityLevel() == 1) { 513 } else if (isSelectedPagesValid()) {
514 var currentlySelectedPages = getSelectedPagesSet(); 514 var currentlySelectedPages = getSelectedPagesSet();
515 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { 515 if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) {
516 previouslySelectedPages = currentlySelectedPages; 516 previouslySelectedPages = currentlySelectedPages;
517 requestPrintPreview(); 517 requestPrintPreview();
518 return; 518 return;
519 } 519 }
520 } 520 }
521 521
522 if (getSelectedPagesValidityLevel() != 1) 522 if (!isSelectedPagesValid())
523 pageRangesFieldChanged(); 523 pageRangesFieldChanged();
524 524
525 // Update the current tab title. 525 // Update the current tab title.
526 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); 526 document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle);
527 527
528 createPDFPlugin(previewUid); 528 createPDFPlugin(previewUid);
529 updatePrintSummary(); 529 updatePrintSummary();
530 updatePrintButtonState(); 530 updatePrintButtonState();
531 addEventListeners(); 531 addEventListeners();
532 } 532 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } 581 }
582 582
583 /** 583 /**
584 * Updates the state of print button depending on the user selection. 584 * Updates the state of print button depending on the user selection.
585 * The button is enabled only when the following conditions are true. 585 * The button is enabled only when the following conditions are true.
586 * 1) The selected page ranges are valid. 586 * 1) The selected page ranges are valid.
587 * 2) The number of copies is valid (if applicable). 587 * 2) The number of copies is valid (if applicable).
588 */ 588 */
589 function updatePrintButtonState() { 589 function updatePrintButtonState() {
590 if (getSelectedPrinterName() == PRINT_TO_PDF) { 590 if (getSelectedPrinterName() == PRINT_TO_PDF) {
591 $('print-button').disabled = (getSelectedPagesValidityLevel() != 1); 591 $('print-button').disabled = !isSelectedPagesValid();
592 } else { 592 } else {
593 $('print-button').disabled = (!isNumberOfCopiesValid() || 593 $('print-button').disabled = (!isNumberOfCopiesValid() ||
594 getSelectedPagesValidityLevel() != 1); 594 !isSelectedPagesValid());
595 } 595 }
596 } 596 }
597 597
598 window.addEventListener('DOMContentLoaded', onLoad); 598 window.addEventListener('DOMContentLoaded', onLoad);
599 599
600 /** 600 /**
601 * Listener function that executes whenever a change occurs in the 'copies' 601 * Listener function that executes whenever a change occurs in the 'copies'
602 * field. 602 * field.
603 */ 603 */
604 function copiesFieldChanged() { 604 function copiesFieldChanged() {
605 updateCopiesButtonsState(); 605 updateCopiesButtonsState();
606 $('collate-option').hidden = getCopies() <= 1; 606 $('collate-option').hidden = getCopies() <= 1;
607 } 607 }
608 608
609 /** 609 /**
610 * Executes whenever a blur event occurs on the 'individual-pages' 610 * Executes whenever a blur event occurs on the 'individual-pages'
611 * field or when the timer expires. It takes care of 611 * field or when the timer expires. It takes care of
612 * 1) showing/hiding warnings/suggestions 612 * 1) showing/hiding warnings/suggestions
613 * 2) updating print button/summary 613 * 2) updating print button/summary
614 */ 614 */
615 function pageRangesFieldChanged() { 615 function pageRangesFieldChanged() {
616 var currentlySelectedPages = getSelectedPagesSet(); 616 var currentlySelectedPages = getSelectedPagesSet();
617 var individualPagesField = $('individual-pages'); 617 var individualPagesField = $('individual-pages');
618 var individualPagesHint = $('individual-pages-hint'); 618 var individualPagesHint = $('individual-pages-hint');
619 var validityLevel = getSelectedPagesValidityLevel();
620 619
621 if (validityLevel == 1) { 620 if (isSelectedPagesValid()) {
622 individualPagesField.classList.remove('invalid'); 621 individualPagesField.classList.remove('invalid');
623 fadeOutElement(individualPagesHint); 622 fadeOutElement(individualPagesHint);
624 } else { 623 } else {
625 individualPagesField.classList.add('invalid'); 624 individualPagesField.classList.add('invalid');
626 individualPagesHint.classList.remove('suggestion'); 625 individualPagesHint.classList.remove('suggestion');
627 individualPagesHint.innerHTML = 626 individualPagesHint.innerHTML =
628 localStrings.getStringF('pageRangeInstruction', 627 localStrings.getStringF('pageRangeInstruction',
629 localStrings.getString( 628 localStrings.getString(
630 'examplePageRangeText')); 629 'examplePageRangeText'));
631 fadeInElement(individualPagesHint); 630 fadeInElement(individualPagesHint);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 function updatePrintSummary() { 662 function updatePrintSummary() {
664 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF; 663 var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF;
665 var copies = printToPDF ? 1 : getCopies(); 664 var copies = printToPDF ? 1 : getCopies();
666 var printSummary = $('print-summary'); 665 var printSummary = $('print-summary');
667 666
668 if (!printToPDF && !isNumberOfCopiesValid()) { 667 if (!printToPDF && !isNumberOfCopiesValid()) {
669 printSummary.innerHTML = localStrings.getString('invalidNumberOfCopies'); 668 printSummary.innerHTML = localStrings.getString('invalidNumberOfCopies');
670 return; 669 return;
671 } 670 }
672 671
673 if (getSelectedPagesValidityLevel() != 1) { 672 if (!isSelectedPagesValid()) {
674 printSummary.innerHTML = ''; 673 printSummary.innerHTML = '';
675 return; 674 return;
676 } 675 }
677 676
678 var pageList = getSelectedPagesSet(); 677 var pageList = getSelectedPagesSet();
679 var numOfSheets = pageList.length; 678 var numOfSheets = pageList.length;
680 var sheetsLabel = localStrings.getString('printPreviewSheetsLabelSingular'); 679 var sheetsLabel = localStrings.getString('printPreviewSheetsLabelSingular');
681 var numOfPagesText = ''; 680 var numOfPagesText = '';
682 var pagesLabel = ''; 681 var pagesLabel = '';
683 682
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 if (singlePageNumber && singlePageNumber > 0 && 780 if (singlePageNumber && singlePageNumber > 0 &&
782 singlePageNumber <= totalPageCount) { 781 singlePageNumber <= totalPageCount) {
783 pageList.push(parseInt(part, 10)); 782 pageList.push(parseInt(part, 10));
784 } 783 }
785 } 784 }
786 } 785 }
787 return pageList; 786 return pageList;
788 } 787 }
789 788
790 /** 789 /**
791 * Checks the 'individual-pages' field and returns -1 if nothing is valid, 0 if 790 * Validates the 'individual-pages' text field value.
792 * it is partially valid, 1 if it is completely valid. 791 *
793 * Note: This function is stricter than getSelectedPages(), in other words this 792 * @return {boolean} true if the text is valid.
794 * could return -1 and getSelectedPages() might still extract some pages.
795 */ 793 */
796 function getSelectedPagesValidityLevel() { 794 function isSelectedPagesValid() {
797 var pageText = $('individual-pages').value; 795 var pageText = $('individual-pages').value;
798 796
799 if ($('all-pages').checked || pageText.length == 0) 797 if ($('all-pages').checked || pageText.length == 0)
800 return 1; 798 return true;
801 799
802 var successfullyParsed = 0; 800 var successfullyParsed = 0;
dpapad 2011/06/08 18:58:13 We don't need this variable either anymore. Remove
dpapad 2011/06/08 20:10:06 Summarizing our online discussion, lets leave this
803 var failedToParse = 0;
804
805 var parts = pageText.split(/,/); 801 var parts = pageText.split(/,/);
806 802
807 for (var i = 0; i < parts.length; ++i) { 803 for (var i = 0; i < parts.length; ++i) {
808 var part = parts[i].replace(/\s*/g, ''); 804 var part = parts[i].replace(/\s*/g, '');
809 if (part.length == 0) 805 if (part.length == 0)
810 continue; 806 continue;
811 807
812 var match = part.match(/^([0-9]+)-([0-9]*)$/); 808 var match = part.match(/^([0-9]+)-([0-9]*)$/);
813 if (match && match[1]) { 809 if (match && isValidNonZeroPositiveInteger(match[1])) {
814 var from = parseInt(match[1], 10); 810 var from = parseInt(match[1], 10);
815 var to = match[2] ? parseInt(match[2], 10) : totalPageCount; 811 var to = match[2] ? parseInt(match[2], 10) : totalPageCount;
816 812
817 if (from && to && from <= to) 813 if (!to || from > to)
818 successfullyParsed += 1; 814 return false;
819 else 815 } else if (!isValidNonZeroPositiveInteger(part) ||
820 failedToParse += 1; 816 !(parseInt(part, 10) <= totalPageCount)) {
821 817 return false;
822 } else if (isInteger(part) && parseInt(part, 10) <= totalPageCount && 818 }
823 parseInt(part, 10) > 0) 819 successfullyParsed += 1;
824 successfullyParsed += 1;
825 else
826 failedToParse += 1;
827 } 820 }
828 if (successfullyParsed > 0 && failedToParse == 0) 821 return successfullyParsed > 0
829 return 1;
830 else if (successfullyParsed > 0 && failedToParse > 0)
831 return 0;
832 else
833 return -1;
834 }
835
836 function isSelectedPagesFieldValid() {
837 return (getSelectedPages().length != 0)
838 } 822 }
839 823
840 /** 824 /**
825 * Returns true if |value| is a valid non zero positive integer.
826 * @param {string} value The string to be tested.
827 */
828 function isValidNonZeroPositiveInteger(value) {
829 return isInteger(value) && parseInt(value, 10) > 0;
830 }
831
832 /**
841 * Parses the selected page ranges, processes them and returns the results. 833 * 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 834 * It squashes whenever possible. Example '1-2,3,5-7' becomes 1-3,5-7
843 * 835 *
844 * @return {Array} an array of page range objects. A page range object has 836 * @return {Array} an array of page range objects. A page range object has
845 * fields 'from' and 'to'. 837 * fields 'from' and 'to'.
846 */ 838 */
847 function getSelectedPageRanges() { 839 function getSelectedPageRanges() {
848 var pageList = getSelectedPagesSet(); 840 var pageList = getSelectedPagesSet();
849 var pageRanges = []; 841 var pageRanges = [];
850 for (var i = 0; i < pageList.length; ++i) { 842 for (var i = 0; i < pageList.length; ++i) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
905 897
906 /** 898 /**
907 * When the user stops typing in the page range textfield or clicks on the 899 * When the user stops typing in the page range textfield or clicks on the
908 * 'all-pages' checkbox, a new print preview is requested, only if 900 * 'all-pages' checkbox, a new print preview is requested, only if
909 * 1) The input is compeletely valid (it can be parsed in its entirety). 901 * 1) The input is compeletely valid (it can be parsed in its entirety).
910 * 2) The newly selected pages differ from the previously selected. 902 * 2) The newly selected pages differ from the previously selected.
911 */ 903 */
912 function onPageSelectionMayHaveChanged() { 904 function onPageSelectionMayHaveChanged() {
913 if ($('print-pages').checked) 905 if ($('print-pages').checked)
914 pageRangesFieldChanged(); 906 pageRangesFieldChanged();
915 var validityLevel = getSelectedPagesValidityLevel();
916 var currentlySelectedPages = getSelectedPagesSet(); 907 var currentlySelectedPages = getSelectedPagesSet();
917 908
918 // Toggling between "all pages"/"some pages" radio buttons while having an 909 // Toggling between "all pages"/"some pages" radio buttons while having an
919 // invalid entry in the page selection textfield still requires updating the 910 // invalid entry in the page selection textfield still requires updating the
920 // print summary and print button. 911 // print summary and print button.
921 if (validityLevel < 1 || 912 if (!isSelectedPagesValid() ||
922 areArraysEqual(previouslySelectedPages, currentlySelectedPages)) { 913 areArraysEqual(previouslySelectedPages, currentlySelectedPages)) {
923 updatePrintButtonState(); 914 updatePrintButtonState();
924 updatePrintSummary(); 915 updatePrintSummary();
925 return; 916 return;
926 } 917 }
927 918
928 previouslySelectedPages = currentlySelectedPages; 919 previouslySelectedPages = currentlySelectedPages;
929 requestPrintPreview(); 920 requestPrintPreview();
930 } 921 }
931 922
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 this.isLandscape = ''; 956 this.isLandscape = '';
966 } 957 }
967 958
968 /** 959 /**
969 * Takes a snapshot of the print settings. 960 * Takes a snapshot of the print settings.
970 */ 961 */
971 PrintSettings.prototype.save = function() { 962 PrintSettings.prototype.save = function() {
972 this.deviceName = getSelectedPrinterName(); 963 this.deviceName = getSelectedPrinterName();
973 this.isLandscape = isLandscape(); 964 this.isLandscape = isLandscape();
974 } 965 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698