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

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

Issue 7056070: PrintPreview: Preview generation should not block print button. (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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview.js
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js
index 2b5d938fcebcdcfd4a25ca8097bb8e2281cf4e3a..ef250c86696da6f061bf1f04db81e12a095eacef 100644
--- a/chrome/browser/resources/print_preview.js
+++ b/chrome/browser/resources/print_preview.js
@@ -33,6 +33,18 @@ var printSettings = new PrintSettings();
// The name of the default or last used printer.
var defaultOrLastUsedPrinterName = '';
+// True when a pending print preview request exists.
+var hasPendingPreviewRequest = false;
+
+// True when a pending print file request exists.
+var hasPendingPrintFileRequest = false;
+
+// True when a compatible plugin exists.
+var hasCompatiblePlugin = true;
Lei Zhang 2011/06/09 09:54:55 Probably safer to start with this set to false and
kmadhusu 2011/06/09 18:11:44 Done.
+
+// True when initiator tab is closed.
+var isInitiatorTabClosed = false;
+
/**
* Window onload handler, sets up the page and starts print preview by getting
* the printer list.
@@ -42,6 +54,7 @@ function onLoad() {
$('cancel-button').addEventListener('click', handleCancelButtonClick);
if (!checkCompatiblePluginExists()) {
+ hasCompatiblePlugin = false;
displayErrorMessage(localStrings.getString('noPlugin'), false);
$('mainview').parentElement.removeChild($('dummy-viewer'));
return;
@@ -49,17 +62,72 @@ function onLoad() {
$('mainview').parentElement.removeChild($('dummy-viewer'));
$('printer-list').disabled = true;
- $('print-button').disabled = true;
+ $('print-button').onclick = printFile;
+
+ setDefaultHandlersForCopiesControls();
+ setDefaultHandlersForPagesControls();
showLoadingAnimation();
chrome.send('getDefaultPrinter');
}
/**
+ * Handles all pages checkbox click event.
+ */
+function handleAllPagesCheckbox() {
dpapad 2011/06/09 16:33:35 Could you remove this function and also change lin
kmadhusu 2011/06/09 18:11:44 Done.
+ updatePrintButtonState();
+}
+
+/**
+ * Validates the individual pages text format.
+ */
+function validateIndividualPagesText() {
+ $('print-pages').checked = true;
dpapad 2011/06/09 16:33:35 Why is this necessary? If the print-pages checkbox
kmadhusu 2011/06/09 18:11:44 It is required. Test case: When you navigate to
+ validatePageRangesField();
+ updatePrintButtonState();
+}
+
+/**
+ * Handles the individual pages input event.
+ */
+function handleIndividualPagesInputEvent() {
Lei Zhang 2011/06/09 09:54:55 nit: can you put this next to handleAllPagesCheckb
kmadhusu 2011/06/09 18:11:44 Done.
+ $('print-pages').checked = true;
+ resetPageRangeFieldTimer();
+}
+
+/**
+ * Sets the default event handlers for pages controls.
+ */
+function setDefaultHandlersForPagesControls() {
+ var allPages = $('all-pages');
+ var printPages = $('print-pages');
+ var individualPages = $('individual-pages');
+
+ allPages.onclick = null;
+ printPages.onclick = null;
+ individualPages.oninput = null;
+ individualPages.onfocus = null;
+ individualPages.onblur = null;
+
+ if (hasCompatiblePlugin && !isInitiatorTabClosed) {
+ allPages.onclick = handleAllPagesCheckbox;
+ printPages.onclick = handleIndividualPagesCheckbox;
+ individualPages.onblur = validateIndividualPagesText;
+ }
+}
+
+/**
+ * Sets the default event handlers for copies controls.
+ */
+function setDefaultHandlersForCopiesControls() {
+ $('copies').oninput = copiesFieldChanged;
+ $('increment').onclick = function() { onCopiesButtonsClicked(1); };
+ $('decrement').onclick = function() { onCopiesButtonsClicked(-1); };
+}
+
+/**
* Adds event listeners to the settings controls.
*/
function addEventListeners() {
- $('print-button').onclick = printFile;
-
// Controls that require preview rendering.
$('all-pages').onclick = onPageSelectionMayHaveChanged;
$('print-pages').onclick = handleIndividualPagesCheckbox;
@@ -69,7 +137,7 @@ function addEventListeners() {
onPageSelectionMayHaveChanged();
};
individualPages.onfocus = addTimerToPageRangeField;
- individualPages.oninput = resetPageRangeFieldTimer;
+ individualPages.oninput = handleIndividualPagesInputEvent;
$('landscape').onclick = onLayoutModeToggle;
$('portrait').onclick = onLayoutModeToggle;
$('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities;
@@ -99,26 +167,19 @@ function addEventListeners() {
* Removes event listeners from the settings controls.
*/
function removeEventListeners() {
- // Controls that require preview rendering.
- $('print-button').disabled = true;
- $('all-pages').onclick = null;
- $('print-pages').onclick = null;
- var individualPages = $('individual-pages');
- individualPages.onblur = null;
- individualPages.onfocus = null;
- individualPages.oninput = null;
clearTimeout(timerId);
+
+ // Controls that require preview rendering
+ setDefaultHandlersForPagesControls();
$('landscape').onclick = null;
$('portrait').onclick = null;
$('printer-list').onchange = null;
// Controls that dont require preview rendering.
- $('copies').oninput = copiesFieldChanged;
$('two-sided').onclick = null;
$('color').onclick = null;
$('bw').onclick = null;
- $('increment').onclick = function() { onCopiesButtonsClicked(1); };
- $('decrement').onclick = function() { onCopiesButtonsClicked(-1); };
+ setDefaultHandlersForCopiesControls();
Lei Zhang 2011/06/09 09:54:55 Does this need to be here for can it be at line 17
kmadhusu 2011/06/09 18:11:44 Done.
}
/**
@@ -141,6 +202,7 @@ function showSystemDialog() {
* @param {string} initiatorTabURL The URL of the initiator tab.
*/
function onInitiatorTabClosed(initiatorTabURL) {
+ isInitiatorTabClosed = true;
$('reopen-page').addEventListener('click', function() {
window.location = initiatorTabURL;
});
@@ -329,6 +391,14 @@ function getSelectedPrinterName() {
* Asks the browser to print the preview PDF based on current print settings.
*/
function printFile() {
+ hasPendingPrintFileRequest = hasPendingPreviewRequest ? true : false;
Lei Zhang 2011/06/09 09:54:55 Uhh, isn't this the same as: hasPendingPrintFileRe
kmadhusu 2011/06/09 18:11:44 Just for better readability I used tri-state forma
Lei Zhang 2011/06/09 21:03:27 I don't understand how that enhances readability.
+
+ if (hasPendingPrintFileRequest) {
+ if (getSelectedPrinterName() != PRINT_TO_PDF)
+ chrome.send('hidePreview');
+ return;
+ }
+
if (getSelectedPrinterName() != PRINT_TO_PDF) {
$('print-button').classList.add('loading');
$('cancel-button').classList.add('loading');
@@ -336,14 +406,16 @@ function printFile() {
removeEventListeners();
window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); },
1000);
- } else
+ } else {
chrome.send('print', [getSettingsJSON()]);
+ }
}
/**
* Asks the browser to generate a preview PDF based on current print settings.
*/
function requestPrintPreview() {
+ hasPendingPreviewRequest = true;
removeEventListeners();
printSettings.save();
showLoadingAnimation();
@@ -437,10 +509,11 @@ function setColor(color) {
/**
* Display an error message in the center of the preview area.
* @param {string} errorMessage The error message to be displayed.
- * @param {boolean} showButton Indivates whether the "Reopen the page" button
+ * @param {boolean} showButton Indicates whether the "Reopen the page" button
* should be displayed.
*/
function displayErrorMessage(errorMessage, showButton) {
+ $('print-button').disabled = true;
$('overlay-layer').classList.remove('invisible');
$('dancing-dots-text').classList.add('hidden');
$('error-text').innerHTML = errorMessage;
@@ -496,6 +569,8 @@ function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) {
previewModifiable = modifiable;
+ hasPendingPreviewRequest = false;
+
if (totalPageCount == -1)
totalPageCount = pageCount;
@@ -525,9 +600,15 @@ function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) {
document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle);
createPDFPlugin(previewUid);
+
updatePrintSummary();
updatePrintButtonState();
addEventListeners();
+
+ if (hasPendingPrintFileRequest) {
+ printFile();
+ return;
dpapad 2011/06/09 16:33:35 Remove return since it is not needed here.
kmadhusu 2011/06/09 18:11:44 Done.
+ }
}
/**
@@ -606,13 +687,9 @@ function copiesFieldChanged() {
}
/**
- * Executes whenever a blur event occurs on the 'individual-pages'
- * field or when the timer expires. It takes care of
- * 1) showing/hiding warnings/suggestions
- * 2) updating print button/summary
+ * Validates the page ranges text and updates the hint accordingly.
*/
-function pageRangesFieldChanged() {
- var currentlySelectedPages = getSelectedPagesSet();
+function validatePageRangesField() {
var individualPagesField = $('individual-pages');
var individualPagesHint = $('individual-pages-hint');
@@ -628,6 +705,16 @@ function pageRangesFieldChanged() {
'examplePageRangeText'));
fadeInElement(individualPagesHint);
}
+}
+
+/**
+ * Executes whenever a blur event occurs on the 'individual-pages'
+ * field or when the timer expires. It takes care of
+ * 1) showing/hiding warnings/suggestions
+ * 2) updating print button/summary
+ */
+function pageRangesFieldChanged() {
+ validatePageRangesField();
resetPageRangeFieldTimer();
updatePrintButtonState();
@@ -806,13 +893,17 @@ function isSelectedPagesValid() {
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) ||
- !(parseInt(part, 10) <= totalPageCount)) {
+ } else if (!isValidNonZeroPositiveInteger(part) || (totalPageCount != -1 &&
+ !(parseInt(part, 10) <= totalPageCount))) {
return false;
}
successfullyParsed += 1;

Powered by Google App Engine
This is Rietveld 408576698