diff options
author | dpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-20 01:58:39 +0000 |
---|---|---|
committer | dpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-20 01:58:39 +0000 |
commit | b73bb80b3496c7e1e8de3d808fc451663885d0b7 (patch) | |
tree | da7f0508ab0de726bdc88f241569e42579a1d904 /chrome | |
parent | d2a2a409f713ce9f28b8d40b3ed2ef9140057a38 (diff) | |
download | chromium_src-b73bb80b3496c7e1e8de3d808fc451663885d0b7.zip chromium_src-b73bb80b3496c7e1e8de3d808fc451663885d0b7.tar.gz chromium_src-b73bb80b3496c7e1e8de3d808fc451663885d0b7.tar.bz2 |
Updating print preview based on the pages selected by the user.
BUG=79525
TEST=Open a multi-page document in print preview, select specific pages, the
preview should automatically update.
Review URL: http://codereview.chromium.org/6882019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@82213 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/resources/print_preview.html | 3 | ||||
-rw-r--r-- | chrome/browser/resources/print_preview.js | 129 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_handler.cc | 38 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_handler.h | 9 |
4 files changed, 107 insertions, 72 deletions
diff --git a/chrome/browser/resources/print_preview.html b/chrome/browser/resources/print_preview.html index 936a464..933258c 100644 --- a/chrome/browser/resources/print_preview.html +++ b/chrome/browser/resources/print_preview.html @@ -40,8 +40,7 @@ <label for="all-pages" i18n-content="optionAllPages"></label> </div> <div> - <input name="pages" type="radio" onclick="printSettingChanged()" - id="print-pages"> + <input name="pages" type="radio" id="print-pages"> <label for="print-pages"> <input id="individual-pages" type="text" class="medium with-hint" diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js index 60ac7d9..89b2be2 100644 --- a/chrome/browser/resources/print_preview.js +++ b/chrome/browser/resources/print_preview.js @@ -4,8 +4,19 @@ var localStrings = new LocalStrings(); var hasPDFPlugin = true; -var expectedPageCount = 0; -var pageRangesInfo = []; + +// The total page count of the previewed document regardless of which pages the +// user has selected. +var totalPageCount = -1; + +// The previously selected pages by the user. It is used in +// onPageSelectionMayHaveChanged() to make sure that a new preview is not +// requested more often than necessary. +var previouslySelectedPages = []; + +// Timer id of the page range textfield. It is used to reset the timer whenever +// needed. +var timerId; /** * Window onload handler, sets up the page and starts print preview by getting @@ -21,17 +32,22 @@ function onLoad() { window.close(); }); + $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged); $('copies').addEventListener('input', validateNumberOfCopies); $('copies').addEventListener('blur', handleCopiesFieldBlur); + $('print-pages').addEventListener('click', handleIndividualPagesCheckbox); $('individual-pages').addEventListener('blur', handlePageRangesFieldBlur); - $('landscape').onclick = onLayoutModeToggle; - $('portrait').onclick = onLayoutModeToggle; - $('color').onclick = getPreview; - $('bw').onclick = getPreview; - $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; + $('individual-pages').addEventListener('focus', addTimerToPageRangeField); + $('individual-pages').addEventListener('input', resetPageRangeFieldTimer); + $('landscape').addEventListener('click', onLayoutModeToggle); + $('portrait').addEventListener('click', onLayoutModeToggle); + $('color').addEventListener('click', function() { setColor(true); }); + $('bw').addEventListener('click', function() { setColor(false); }); + $('printer-list').addEventListener( + 'change', updateControlsWithSelectedPrinterCapabilities); chrome.send('getPrinters'); -}; +} /** * Gets the selected printer capabilities and updates the controls accordingly. @@ -71,7 +87,7 @@ function updateWithPrinterCapabilities(settingInfo) { if (colorOption.checked != setColorAsDefault) { colorOption.checked = setColorAsDefault; bwOption.checked = !setColorAsDefault; - getPreview(); + setColor(colorOption.checked); } } @@ -118,6 +134,7 @@ function handleCopiesFieldBlur() { */ function handlePageRangesFieldBlur() { checkAndSetPageRangesField(); + onPageSelectionMayHaveChanged(); } /** @@ -160,16 +177,10 @@ function checkAndSetCopiesField() { * */ function checkAndSetPageRangesField() { - var pageRanges = getPageRanges(); + var pageRanges = getSelectedPageRanges(); var parsedPageRanges = ''; var individualPagesField = $('individual-pages'); - if (pageRanges.length == 1 && pageRanges[0].from == 1 && - pageRanges[0].to == expectedPageCount) { - individualPagesField.value = parsedPageRanges; - return; - } - for (var i = 0; i < pageRanges.length; ++i) { if (pageRanges[i].from == pageRanges[i].to) parsedPageRanges += pageRanges[i].from; @@ -248,7 +259,7 @@ function getSettingsJSON() { var printToPDF = (printerName == localStrings.getString('printToPDF')); return JSON.stringify({'printerName': printerName, - 'pageRange': getPageRanges(), + 'pageRange': getSelectedPageRanges(), 'printAll': printAll, 'twoSided': isTwoSided(), 'copies': getCopies(), @@ -331,8 +342,16 @@ function onPDFLoad() { * */ function updatePrintPreview(pageCount, jobTitle) { - // Set the expected page count. - expectedPageCount = pageCount; + // Initialize the expected page count. + if (totalPageCount == -1) + totalPageCount = pageCount; + + // Initialize the selected pages (defaults to all selected). + if (previouslySelectedPages.length == 0) + for (var i = 0; i < totalPageCount; i++) + previouslySelectedPages.push(i+1); + + regeneratePreview = false; // Update the current tab title. document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); @@ -422,7 +441,7 @@ function updateSummary() { return; } - var pageList = getPageList(); + var pageList = getSelectedPages(); if (pageList.length <= 0) { printSummary.innerHTML = localStrings.getString('pageRangeInvalidTitleToolTip'); @@ -485,6 +504,15 @@ function handleTwoSidedClick(event) { } /** + * Gives focus to the individual pages textfield when 'print-pages' textbox is + * clicked. + */ +function handleIndividualPagesCheckbox() { + printSettingChanged(); + $('individual-pages').focus(); +} + +/** * When the user switches printing orientation mode the page field selection is * reset to "all pages selected". After the change the number of pages will be * different and currently selected page numbers might no longer be valid. @@ -493,6 +521,8 @@ function handleTwoSidedClick(event) { function onLayoutModeToggle() { $('individual-pages').value = ''; $('all-pages').checked = true; + totalPageCount = -1; + previouslySelectedPages.length = 0; getPreview(); } @@ -502,11 +532,11 @@ function onLayoutModeToggle() { * * @return {Array} */ -function getPageList() { +function getSelectedPages() { var pageText = $('individual-pages').value; if ($('all-pages').checked || pageText == '') - pageText = '1-' + expectedPageCount; + pageText = '1-' + totalPageCount; var pageList = []; var parts = pageText.split(/,/); @@ -521,11 +551,11 @@ function getPageList() { if (from && to) { for (var j = from; j <= to; ++j) - if (j <= expectedPageCount) + if (j <= totalPageCount) pageList.push(j); } } else if (parseInt(part, 10)) { - if (parseInt(part, 10) <= expectedPageCount) + if (parseInt(part, 10) <= totalPageCount) pageList.push(parseInt(part, 10)); } } @@ -539,8 +569,8 @@ function getPageList() { * @return {Array} an array of page range objects. A page range object has * fields 'from' and 'to'. */ -function getPageRanges() { - var pageList = getPageList(); +function getSelectedPageRanges() { + var pageList = getSelectedPages(); var pageRanges = []; for (var i = 0; i < pageList.length; ++i) { tempFrom = pageList[i]; @@ -551,3 +581,50 @@ function getPageRanges() { } return pageRanges; } + +/** + * Whenever the page range textfield gains focus we add a timer to detect when + * the user stops typing in order to update the print preview. + */ +function addTimerToPageRangeField() { + timerId = window.setTimeout(onPageSelectionMayHaveChanged, 500); +} + +/** + * As the user types in the page range textfield, we need to reset this timer, + * since the page ranges are still being edited. + */ +function resetPageRangeFieldTimer() { + clearTimeout(timerId); + addTimerToPageRangeField(); +} + +/** + * When the user stops typing in the page range textfield or clicks on the + * 'all-pages' checkbox, a new print preview is requested, only if + * 1) The input is valid (it can be parsed, even only partially). + * 2) The newly selected pages differ from the previously selected. + */ +function onPageSelectionMayHaveChanged() { + var currentlySelectedPages = getSelectedPages(); + + if (currentlySelectedPages.length == 0) + return; + if (areArraysEqual(previouslySelectedPages, currentlySelectedPages)) + return; + + previouslySelectedPages = currentlySelectedPages; + getPreview(); +} + +/** + * 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; +} diff --git a/chrome/browser/ui/webui/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview_handler.cc index aaf6081..ee62440 100644 --- a/chrome/browser/ui/webui/print_preview_handler.cc +++ b/chrome/browser/ui/webui/print_preview_handler.cc @@ -213,10 +213,7 @@ class PrintToPdfTask : public Task { FilePath* PrintPreviewHandler::last_saved_path_ = NULL; PrintPreviewHandler::PrintPreviewHandler() - : print_backend_(printing::PrintBackend::CreateInstance(NULL)), - need_to_generate_preview_(true), - color_(kColorDefaultValue), - landscape_(kLandscapeDefaultValue) { + : print_backend_(printing::PrintBackend::CreateInstance(NULL)) { } PrintPreviewHandler::~PrintPreviewHandler() { @@ -252,17 +249,8 @@ void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { if (!settings.get()) return; - // Handle settings that do not require print preview regeneration. - ProcessColorSetting(*settings); - - // Handle settings that require print preview regeneration. - ProcessLandscapeSetting(*settings); - - if (need_to_generate_preview_) { - RenderViewHost* rvh = initiator_tab->render_view_host(); - rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings)); - need_to_generate_preview_ = false; - } + RenderViewHost* rvh = initiator_tab->render_view_host(); + rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings)); } void PrintPreviewHandler::HandlePrint(const ListValue* args) { @@ -336,26 +324,6 @@ void PrintPreviewHandler::SendPrinterList( default_printer_index); } -void PrintPreviewHandler::ProcessColorSetting(const DictionaryValue& settings) { - bool color = kColorDefaultValue; - settings.GetBoolean(printing::kSettingColor, &color); - if (color != color_) { - color_ = color; - FundamentalValue color_value(color_); - web_ui_->CallJavascriptFunction("setColor", color_value); - } -} - -void PrintPreviewHandler::ProcessLandscapeSetting( - const DictionaryValue& settings) { - bool landscape = kLandscapeDefaultValue; - settings.GetBoolean(printing::kSettingLandscape, &landscape); - if (landscape != landscape_) { - landscape_ = landscape; - need_to_generate_preview_ = true; - } -} - void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { SelectFileDialog::FileTypeInfo file_type_info; file_type_info.extensions.resize(1); diff --git a/chrome/browser/ui/webui/print_preview_handler.h b/chrome/browser/ui/webui/print_preview_handler.h index 35a913b..da9d4b2 100644 --- a/chrome/browser/ui/webui/print_preview_handler.h +++ b/chrome/browser/ui/webui/print_preview_handler.h @@ -71,15 +71,6 @@ class PrintPreviewHandler : public WebUIMessageHandler, // Pointer to current print system. scoped_refptr<printing::PrintBackend> print_backend_; - // Set to true if we need to generate a new print preview. - bool need_to_generate_preview_; - - // Set to true if the preview should be in color. - bool color_; - - // Set to true if the preview should be landscape. - bool landscape_; - // The underlying dialog object. scoped_refptr<SelectFileDialog> select_file_dialog_; |