diff options
author | dpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-14 01:14:57 +0000 |
---|---|---|
committer | dpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-14 01:14:57 +0000 |
commit | 3f8bd9a957402a8d977a929b8c48bc22fac5d216 (patch) | |
tree | 3f5eeaf063015afc5ffc32ecb441e60be4d0a948 /chrome | |
parent | 8b71c08456fe0dc7a420aa05c609ab433d08181d (diff) | |
download | chromium_src-3f8bd9a957402a8d977a929b8c48bc22fac5d216.zip chromium_src-3f8bd9a957402a8d977a929b8c48bc22fac5d216.tar.gz chromium_src-3f8bd9a957402a8d977a929b8c48bc22fac5d216.tar.bz2 |
Print Preview: Refactoring/Cleaning up print_preview.js.
Moving several functions to print_preview_utils.js. Also modifying signatures
of these functions so that they dont depend on print_preview.js. This helps in
testing those functions and it is much cleaner.
BUG=NONE
TEST=Behavior of print preview should be unchanged.
Review URL: http://codereview.chromium.org/7003153
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88938 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/resources/print_preview.html | 1 | ||||
-rw-r--r-- | chrome/browser/resources/print_preview.js | 208 | ||||
-rw-r--r-- | chrome/browser/resources/print_preview_utils.js | 179 |
3 files changed, 217 insertions, 171 deletions
diff --git a/chrome/browser/resources/print_preview.html b/chrome/browser/resources/print_preview.html index 289ad0b..7efa464 100644 --- a/chrome/browser/resources/print_preview.html +++ b/chrome/browser/resources/print_preview.html @@ -10,6 +10,7 @@ <script src="chrome://resources/js/util.js"></script> <script src="print_preview.js"></script> <script src="print_preview_animations.js"></script> +<script src="print_preview_utils.js"></script> </head> <body> <div class="sidebar"> diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js index 8dedff6..1146381 100644 --- a/chrome/browser/resources/print_preview.js +++ b/chrome/browser/resources/print_preview.js @@ -260,21 +260,8 @@ function updateWithPrinterCapabilities(settingInfo) { * @return {boolean} true if the number of copies is valid else returns false. */ function isNumberOfCopiesValid() { - var copiesFieldText = $('copies').value.replace(/\s/g, ''); - if (copiesFieldText == '') - return true; - - return (isInteger(copiesFieldText) && Number(copiesFieldText) > 0); -} - -/** - * Returns true if |toTest| contains only digits. Leading and trailing - * whitespace is allowed. - * @param {string} toTest The string to be tested. - */ -function isInteger(toTest) { - var numericExp = /^\s*[0-9]+\s*$/; - return numericExp.test(toTest); + var copiesFieldText = $('copies').value; + return copiesFieldText == '' ? true : isPositiveInteger(copiesFieldText); } /** @@ -348,15 +335,16 @@ function getSettingsJSON() { var deviceName = getSelectedPrinterName(); var printToPDF = (deviceName == PRINT_TO_PDF); - return JSON.stringify({'deviceName': deviceName, - 'pageRange': getSelectedPageRanges(), - 'printAll': printAll, - 'duplex': getDuplexMode(), - 'copies': getCopies(), - 'collate': isCollated(), - 'landscape': isLandscape(), - 'color': isColor(), - 'printToPDF': printToPDF}); + return JSON.stringify( + {'deviceName': deviceName, + 'pageRange': pageSetToPageRanges(getSelectedPagesSet()), + 'printAll': printAll, + 'duplex': getDuplexMode(), + 'copies': getCopies(), + 'collate': isCollated(), + 'landscape': isLandscape(), + 'color': isColor(), + 'printToPDF': printToPDF}); } /** @@ -635,9 +623,8 @@ function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { */ function createPDFPlugin(previewUid) { // Enable the print button. - if (!$('printer-list').disabled) { + if (!$('printer-list').disabled) $('print-button').disabled = false; - } var pdfViewer = $('pdf-viewer'); if (pdfViewer) { @@ -650,15 +637,14 @@ function createPDFPlugin(previewUid) { return; } - var pdfPlugin = document.createElement('embed'); - pdfPlugin.setAttribute('id', 'pdf-viewer'); - pdfPlugin.setAttribute('type', 'application/pdf'); - pdfPlugin.setAttribute('src', 'chrome://print/' + previewUid + '/print.pdf'); - var mainView = $('mainview'); - mainView.appendChild(pdfPlugin); - pdfPlugin.onload('onPDFLoad()'); - pdfPlugin.removePrintButton(); - pdfPlugin.grayscale(true); + pdfViewer = document.createElement('embed'); + pdfViewer.setAttribute('id', 'pdf-viewer'); + pdfViewer.setAttribute('type', 'application/pdf'); + pdfViewer.setAttribute('src', 'chrome://print/' + previewUid + '/print.pdf'); + $('mainview').appendChild(pdfViewer); + pdfViewer.onload('onPDFLoad()'); + pdfViewer.removePrintButton(); + pdfViewer.grayscale(true); } /** @@ -771,8 +757,8 @@ function updatePrintSummary() { return; } - var pageList = getSelectedPagesSet(); - var numOfSheets = pageList.length; + var pageSet = getSelectedPagesSet(); + var numOfSheets = pageSet.length; var sheetsLabel = localStrings.getString('printPreviewSheetsLabelSingular'); var numOfPagesText = ''; var pagesLabel = ''; @@ -785,8 +771,8 @@ function updatePrintSummary() { sheetsLabel = localStrings.getString('printPreviewSheetsLabelPlural'); var html = ''; - if (pageList.length * copies != numOfSheets) { - numOfPagesText = pageList.length * copies; + if (pageSet.length * copies != numOfSheets) { + numOfPagesText = pageSet.length * copies; pagesLabel = localStrings.getString('printPreviewPageLabelPlural'); html = localStrings.getStringF('printPreviewSummaryFormatLong', '<b>' + numOfSheets + '</b>', @@ -843,44 +829,18 @@ function setDefaultValuesAndRegeneratePreview() { } /** - * Returns a list of all pages in the specified ranges. The pages are listed in - * the order they appear in the 'individual-pages' textbox and duplicates are - * not eliminated. If the page ranges can't be parsed an empty list is - * returned. + * Returns the selected pages in ascending order without any duplicates. * * @return {Array} */ -function getSelectedPages() { - var pageText = $('individual-pages').value; - - if ($('all-pages').checked || pageText.length == 0) - pageText = '1-' + totalPageCount; - - var pageList = []; - var parts = pageText.split(/,/); - - for (var i = 0; i < parts.length; ++i) { - var part = parts[i]; - var match = part.match(/^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/); - - if (match && match[1]) { - var from = parseInt(match[1], 10); - var to = match[2] ? parseInt(match[2], 10) : totalPageCount; - - if (from && to) { - for (var j = from; j <= to; ++j) - if (j <= totalPageCount) - pageList.push(j); - } - } else { - var singlePageNumber = parseInt(part, 10); - if (singlePageNumber && singlePageNumber > 0 && - singlePageNumber <= totalPageCount) { - pageList.push(parseInt(part, 10)); - } - } - } - return pageList; +function getSelectedPagesSet() { + var pageRangeText = $('individual-pages').value; + + if ($('all-pages').checked || pageRangeText.length == 0) + pageRangeText = '1-' + totalPageCount; + + var pageList = pageRangeTextToPageList(pageRangeText, totalPageCount); + return pageListToPageSet(pageList); } /** @@ -889,94 +849,12 @@ function getSelectedPages() { * @return {boolean} true if the text is valid. */ function isSelectedPagesValid() { - var pageText = $('individual-pages').value; + var pageRangeText = $('individual-pages').value; - if ($('all-pages').checked || pageText.length == 0) + if ($('all-pages').checked || pageRangeText.length == 0) return true; - var successfullyParsed = 0; - var parts = pageText.split(/,/); - - for (var i = 0; i < parts.length; ++i) { - var part = parts[i].replace(/\s*/g, ''); - if (part.length == 0) - continue; - - var match = part.match(/^([0-9]+)-([0-9]*)$/); - if (match && isValidNonZeroPositiveInteger(match[1])) { - if (!match[2] && totalPageCount == -1) { - successfullyParsed += 1; - continue; - } - var from = parseInt(match[1], 10); - var to = match[2] ? parseInt(match[2], 10) : totalPageCount; - - if (!to || from > to) - return false; - } else if (!isValidNonZeroPositiveInteger(part) || (totalPageCount != -1 && - !(parseInt(part, 10) <= totalPageCount))) { - return false; - } - successfullyParsed += 1; - } - return successfullyParsed > 0 -} - -/** - * Returns true if |value| is a valid non zero positive integer. - * @param {string} value The string to be tested. - */ -function isValidNonZeroPositiveInteger(value) { - return isInteger(value) && parseInt(value, 10) > 0; -} - -/** - * Parses the selected page ranges, processes them and returns the results. - * It squashes whenever possible. Example '1-2,3,5-7' becomes 1-3,5-7 - * - * @return {Array} an array of page range objects. A page range object has - * fields 'from' and 'to'. - */ -function getSelectedPageRanges() { - var pageList = getSelectedPagesSet(); - var pageRanges = []; - for (var i = 0; i < pageList.length; ++i) { - tempFrom = pageList[i]; - while (i + 1 < pageList.length && pageList[i + 1] == pageList[i] + 1) - ++i; - tempTo = pageList[i]; - pageRanges.push({'from': tempFrom, 'to': tempTo}); - } - return pageRanges; -} - -/** - * Returns the selected pages in ascending order without any duplicates. - */ -function getSelectedPagesSet() { - var pageList = getSelectedPages(); - pageList.sort(function(a,b) { return a - b; }); - pageList = removeDuplicates(pageList); - return pageList; -} - -/** - * Removes duplicate elements from |inArray| and returns a new array. - * |inArray| is not affected. It assumes that the array is already sorted. - * - * @param {Array} inArray The array to be processed. - */ -function removeDuplicates(inArray) { - var out = []; - - if(inArray.length == 0) - return out; - - out.push(inArray[0]); - for (var i = 1; i < inArray.length; ++i) - if(inArray[i] != inArray[i - 1]) - out.push(inArray[i]); - return out; + return isPageRangeTextValid(pageRangeText, totalPageCount); } /** @@ -1022,18 +900,6 @@ function onPageSelectionMayHaveChanged() { } /** - * Returns true if the contents of the two arrays are equal. - */ -function areArraysEqual(array1, array2) { - if (array1.length != array2.length) - return false; - for (var i = 0; i < array1.length; i++) - if(array1[i] != array2[i]) - return false; - return true; -} - -/** * Executed when the 'increment' or 'decrement' button is clicked. */ function onCopiesButtonsClicked(sign) { diff --git a/chrome/browser/resources/print_preview_utils.js b/chrome/browser/resources/print_preview_utils.js new file mode 100644 index 0000000..9400424 --- /dev/null +++ b/chrome/browser/resources/print_preview_utils.js @@ -0,0 +1,179 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * Returns true if |toTest| contains only digits. Leading and trailing + * whitespace is allowed. + * @param {string} toTest The string to be tested. + */ +function isInteger(toTest) { + var numericExp = /^\s*[0-9]+\s*$/; + return numericExp.test(toTest); +} + +/** + * Returns true if |value| is a valid non zero positive integer. + * @param {string} value The string to be tested. + * + * @return {boolean} true if the |value| is valid non zero positive integer. + */ +function isPositiveInteger(value) { + return isInteger(value) && parseInt(value, 10) > 0; +} + +/** + * Returns true if the contents of the two arrays are equal. + * @param {Array} array1 The first array. + * @param {Array} array1 The second array. + * + * @return {boolean} true if the arrays are equal. + */ +function areArraysEqual(array1, array2) { + if (array1.length != array2.length) + return false; + for (var i = 0; i < array1.length; i++) + if(array1[i] != array2[i]) + return false; + return true; +} + +/** + * Removes duplicate elements from |inArray| and returns a new array. + * |inArray| is not affected. It assumes that |inArray| is already sorted. + * + * @param {Array} inArray The array to be processed. + */ +function removeDuplicates(inArray) { + var out = []; + + if(inArray.length == 0) + return out; + + out.push(inArray[0]); + for (var i = 1; i < inArray.length; ++i) + if(inArray[i] != inArray[i - 1]) + out.push(inArray[i]); + return out; +} + +/** + * Checks if |pageRangeText| represents a valid page selection. + * A valid selection has a parsable format and every page identifier is + * <= |totalPageCount| unless wildcards are used (see examples). + * Example: "1-4, 9, 3-6, 10, 11" is valid, assuming |totalPageCount| >= 11. + * Example: "1-4, 6-6" is valid, assuming |totalPageCount| >= 6. + * Example: "2-" is valid, assuming |totalPageCount| >= 2, means from 2 to the + * end. + * Example: "1-10000" is valid, regardless of |totalPageCount|, means from 1 to + * the end if |totalPageCount| < 10000. + * Example: "1-4dsf, 11" is invalid regardless of |totalPageCount|. + * Example: "4-2, 11, -6" is invalid regardless of |totalPageCount|. + * + * Note: If |totalPageCount| is undefined the validation does not take + * |totalPageCount| into account. + * Example: "34853253" is valid. + * Example: "1-4, 9, 3-6, 10, 11" is valid. + * + * @return {boolean} true if the |pageRangeText| is valid. + */ +function isPageRangeTextValid(pageRangeText, totalPageCount) { + var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; + var successfullyParsed = 0; + + // Splitting around commas. + var parts = pageRangeText.split(/,/); + + for (var i = 0; i < parts.length; ++i) { + var match = parts[i].match(regex); + if (parts[i].length == 0) + continue; + + if (match && match[1] && isPositiveInteger(match[1])) { + var from = parseInt(match[1], 10); + var to = isPositiveInteger(match[2]) ? parseInt(match[2], 10) : + totalPageCount; + if (from > to) + return false; + } else if (!isPositiveInteger(parts[i]) || (totalPageCount != -1 && + parseInt(parts[i], 10) > totalPageCount)) { + return false; + } + successfullyParsed++; + } + return successfullyParsed > 0; +} + +/** + * Returns a list of all pages specified in |pagesRangeText|. The pages are + * listed in the order they appear in |pageRangeText| and duplicates are not + * eliminated. If |pageRangeText| is not valid according to + * isPageRangeTextValid(), or |totalPageCount| is undefined an empty list is + * returned. + * + * @return {Array} + */ +function pageRangeTextToPageList(pageRangeText, totalPageCount) { + var pageList = []; + if ((totalPageCount && !isPageRangeTextValid(pageRangeText, totalPageCount)) + || !totalPageCount) { + return pageList; + } + + var regex = /^\s*([0-9]+)\s*-\s*([0-9]*)\s*$/; + var parts = pageRangeText.split(/,/); + + for (var i = 0; i < parts.length; ++i) { + var match = parts[i].match(regex); + + if (match && match[1]) { + var from = parseInt(match[1], 10); + var to = match[2] ? parseInt(match[2], 10) : totalPageCount; + + for (var j = from; j <= Math.min(to, totalPageCount); ++j) + pageList.push(j); + } else { + var singlePageNumber = parseInt(parts[i], 10); + if (isPositiveInteger(singlePageNumber) && + singlePageNumber <= totalPageCount) { + pageList.push(singlePageNumber); + } + } + } + return pageList; +} + +/** + * Returns the contents of |pageList| in ascending order and without any + * duplicates. |pageList| is not affected. + * + * @return {Array} + */ +function pageListToPageSet(pageList) { + var pageSet = []; + if (pageList.length == 0) + return pageSet; + pageSet = pageList.slice(0); + pageSet.sort(function(a,b) { return a - b; }); + pageSet = removeDuplicates(pageSet); + return pageSet; +} + +/** + * Converts |pageSet| to page ranges. It squashes whenever possible. + * Example: '1-2,3,5-7' becomes 1-3,5-7. + * + * @return {Array} an array of page range objects. A page range object has + * fields 'from' and 'to'. + */ +function pageSetToPageRanges(pageSet) { + var pageRanges = []; + for (var i = 0; i < pageSet.length; ++i) { + tempFrom = pageSet[i]; + while (i + 1 < pageSet.length && pageSet[i + 1] == pageSet[i] + 1) + ++i; + tempTo = pageSet[i]; + pageRanges.push({'from': tempFrom, 'to': tempTo}); + } + return pageRanges; +} |