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

Unified Diff: chrome/browser/resources/print_preview.js

Issue 7051040: Print Preview: Making the UI not block when preview is generated (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing comment. Created 9 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/print_preview.js
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js
index 9bc3731f709260e2d504da5ab2f1cc35a6328143..95adc60c329c631f2eadde2954f270a393e2c61b 100644
--- a/chrome/browser/resources/print_preview.js
+++ b/chrome/browser/resources/print_preview.js
@@ -13,11 +13,6 @@ var totalPageCount = -1;
// requested more often than necessary.
var previouslySelectedPages = [];
-// The previously selected layout mode. It is used in order to prevent the
-// preview from updating when the user clicks on the already selected layout
-// mode.
-var previouslySelectedLayout = null;
-
// Timer id of the page range textfield. It is used to reset the timer whenever
// needed.
var timerId;
@@ -25,12 +20,6 @@ var timerId;
// Store the last selected printer index.
var lastSelectedPrinterIndex = 0;
-// Indicates whether a preview has been requested but not received yet.
-var isPreviewStillLoading = true;
-
-// Currently selected printer capabilities.
-var printerCapabilities;
-
// Used to disable some printing options when the preview is not modifiable.
var previewModifiable = false;
@@ -38,6 +27,9 @@ var previewModifiable = false;
const PRINT_TO_PDF = 'Print To PDF';
const MANAGE_PRINTERS = 'Manage Printers';
+// State of the print preview settings.
+var printSettings = new PrintSettings();
+
/**
* Window onload handler, sets up the page and starts print preview by getting
* the printer list.
@@ -55,33 +47,68 @@ function onLoad() {
$('printer-list').disabled = true;
$('print-button').disabled = true;
- $('print-button').addEventListener('click', printFile);
- $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged);
- $('copies').addEventListener('input', copiesFieldChanged);
- $('print-pages').addEventListener('click', handleIndividualPagesCheckbox);
- $('individual-pages').addEventListener('blur', function() {
- clearTimeout(timerId);
- onPageSelectionMayHaveChanged();
- });
- $('individual-pages').addEventListener('focus', addTimerToPageRangeField);
- $('individual-pages').addEventListener('input', resetPageRangeFieldTimer);
- $('two-sided').addEventListener('click', handleTwoSidedClick)
- $('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);
- $('increment').addEventListener('click',
- function() { onCopiesButtonsClicked(1); });
- $('decrement').addEventListener('click',
- function() { onCopiesButtonsClicked(-1); });
$('controls').onsubmit = function() { return false; };
$('dancing-dots').classList.remove('invisible');
chrome.send('getPrinters');
}
/**
+ * Adds event listeners to the settings controls.
+ */
+function addEventListeners() {
+ var individualPages = $('individual-pages');
James Hawkins 2011/05/23 21:24:06 Move this var to where it's first used.
dpapad 2011/05/23 23:04:07 Done.
+ $('print-button').onclick = printFile;
+
+ // Controls that require preview rendering.
+ $('all-pages').onclick = onPageSelectionMayHaveChanged;
+ $('print-pages').onclick = handleIndividualPagesCheckbox;
+ individualPages.onblur = function() {
+ clearTimeout(timerId);
+ onPageSelectionMayHaveChanged();
+ };
+ individualPages.onfocus = addTimerToPageRangeField;
+ individualPages.oninput = resetPageRangeFieldTimer;
+ $('landscape').onclick = onLayoutModeToggle;
+ $('portrait').onclick = onLayoutModeToggle;
+ $('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities;
+
+ // Controls that dont require preview rendering.
+ $('copies').oninput = copiesFieldChanged;
+ $('two-sided').onclick = handleTwoSidedClick;
+ $('color').onclick = function() { setColor(true); };
+ $('bw').onclick = function() { setColor(false); };
+ $('increment').onclick = function() { onCopiesButtonsClicked(1); };
+ $('decrement').onclick = function() { onCopiesButtonsClicked(-1); };
+}
+
+/**
+ * Removes event listeners from the settings controls.
+ */
+function removeEventListeners() {
+ var individualPages = $('individual-pages');
James Hawkins 2011/05/23 21:24:06 Same here.
dpapad 2011/05/23 23:04:07 Done.
+
+ // Controls that require preview rendering.
+ $('print-button').disabled = true;
+ $('all-pages').onclick = null;
+ $('print-pages').onclick = null;
+ individualPages.onblur = null;
+ individualPages.onfocus = null;
+ individualPages.oninput = null;
+ clearTimeout(timerId);
+ $('landscape').onclick = null;
+ $('portrait').onclick = null;
+ $('printer-list').onchange = null;
+
+ // Controls that dont require preview rendering.
+ $('copies').oninput = null;
+ $('two-sided').onclick = null;
+ $('color').onclick = null;
+ $('bw').onclick = null;
+ $('increment').onclick = null;
+ $('decrement').onclick = null;
+}
+
+/**
* Asks the browser to close the preview tab.
*/
function handleCancelButtonClick() {
@@ -141,11 +168,6 @@ function updateControlsWithSelectedPrinterCapabilities() {
* @param {Object} settingInfo printer setting information.
*/
function updateWithPrinterCapabilities(settingInfo) {
- printerCapabilities = settingInfo;
-
- if (isPreviewStillLoading)
- return;
-
var disableColorOption = settingInfo.disableColorOption;
var setColorAsDefault = settingInfo.setColorAsDefault;
var colorOption = $('color');
@@ -320,8 +342,8 @@ function printFile() {
* Asks the browser to generate a preview PDF based on current print settings.
*/
function requestPrintPreview() {
- isPreviewStillLoading = true;
- setControlsDisabled(true);
+ removeEventListeners();
+ printSettings.save();
$('dancing-dots').classList.remove('invisible');
chrome.send('getPreview', [getSettingsJSON()]);
}
@@ -390,7 +412,6 @@ function setColor(color) {
* should be displayed.
*/
function displayErrorMessage(errorMessage, showButton) {
- isPreviewStillLoading = false;
$('dancing-dots').classList.remove('invisible');
$('dancing-dots-text').classList.add('hidden');
$('error-text').innerHTML = errorMessage;
@@ -425,7 +446,6 @@ function onPDFLoad() {
$('pdf-viewer').fitToHeight();
$('dancing-dots').classList.add('invisible');
- setControlsDisabled(false);
if (!previewModifiable) {
$('landscape').disabled = true;
@@ -433,7 +453,6 @@ function onPDFLoad() {
}
updateCopiesButtonsState();
- updateWithPrinterCapabilities(printerCapabilities);
}
/**
@@ -446,6 +465,9 @@ function onPDFLoad() {
*
*/
function updatePrintPreview(pageCount, jobTitle, modifiable) {
+ var tempPrintSettings = new PrintSettings();
+ tempPrintSettings.save();
+
previewModifiable = modifiable;
if (totalPageCount == -1)
@@ -455,15 +477,30 @@ function updatePrintPreview(pageCount, jobTitle, modifiable) {
for (var i = 0; i < totalPageCount; i++)
previouslySelectedPages.push(i+1);
- if (previouslySelectedLayout == null)
- previouslySelectedLayout = $('portrait');
+ if (printSettings.deviceName != tempPrintSettings.deviceName) {
+ updateControlsWithSelectedPrinterCapabilities();
+ return;
+ } else if (printSettings.isLandscape != tempPrintSettings.isLandscape) {
+ requestPrintPreview();
+ return;
+ } else if (getSelectedPagesValidityLevel() == 1) {
+ var currentlySelectedPages = getSelectedPagesSet();
+ if (!areArraysEqual(previouslySelectedPages, currentlySelectedPages)) {
+ previouslySelectedPages = currentlySelectedPages;
+ requestPrintPreview();
+ return;
+ }
+ }
+
+ if (getSelectedPagesValidityLevel() != 1)
+ pageRangesFieldChanged();
dpapad 2011/05/23 21:17:06 I think it is more readable now.
// Update the current tab title.
document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle);
createPDFPlugin();
- isPreviewStillLoading = false;
updatePrintSummary();
+ addEventListeners();
}
/**
@@ -673,14 +710,10 @@ function handleIndividualPagesCheckbox() {
* Even if they are still valid the content of these pages will be different.
*/
function onLayoutModeToggle() {
- var currentlySelectedLayout = $('portrait').checked ? $('portrait') :
- $('landscape');
-
// If the chosen layout is same as before, nothing needs to be done.
- if (previouslySelectedLayout == currentlySelectedLayout)
+ if (printSettings.isLandscape == isLandscape())
return;
- previouslySelectedLayout = currentlySelectedLayout;
$('individual-pages').classList.remove('invalid');
setDefaultValuesAndRegeneratePreview();
}
@@ -908,3 +941,23 @@ function onCopiesButtonsClicked(sign) {
copiesFieldChanged();
}
+/**
+ * Class that represents the state of the print settings.
+ */
+function PrintSettings() {
+ this.deviceName = '';
+ this.isLandscape = '';
+}
+
+/**
+ * Takes a snapshot of the print settings.
+ */
+PrintSettings.prototype.save = function() {
+ var printerList = $('printer-list')
+ var selectedPrinter = printerList.selectedIndex;
+ if (selectedPrinter >= 0)
+ this.deviceName = printerList.options[selectedPrinter].value;
+ else
+ this.deviceName = '';
+ this.isLandscape = isLandscape();
+}
« 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