Index: chrome/browser/resources/print_preview.js |
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js |
index 7118615fcf1abd994fc2ab2ca60befd4356448ce..10a62640e618d636b02df56ece6a8b0cd69496a3 100644 |
--- a/chrome/browser/resources/print_preview.js |
+++ b/chrome/browser/resources/print_preview.js |
@@ -30,6 +30,18 @@ const MANAGE_PRINTERS = 'Manage Printers'; |
// State of the print preview settings. |
var printSettings = new PrintSettings(); |
+// 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; |
+ |
+// 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. |
@@ -39,6 +51,7 @@ function onLoad() { |
$('cancel-button').addEventListener('click', handleCancelButtonClick); |
if (!checkCompatiblePluginExists()) { |
+ hasCompatiblePlugin = false; |
displayErrorMessage(localStrings.getString('noPlugin'), false); |
$('mainview').parentElement.removeChild($('dummy-viewer')); |
return; |
@@ -46,17 +59,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() { |
+ updatePrintButtonState(); |
+} |
+ |
+/** |
+ * Validates the individual pages text format. |
+ */ |
+function validateIndividualPagesText() { |
+ $('print-pages').checked = true; |
+ validatePageRangesField(); |
+ updatePrintButtonState(); |
+} |
+ |
+/** |
+ * Handles the individual pages input event. |
+ */ |
+function handleIndividualPagesInputEvent() { |
+ $('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; |
@@ -66,7 +134,7 @@ function addEventListeners() { |
onPageSelectionMayHaveChanged(); |
}; |
individualPages.onfocus = addTimerToPageRangeField; |
- individualPages.oninput = resetPageRangeFieldTimer; |
+ individualPages.oninput = handleIndividualPagesInputEvent; |
$('landscape').onclick = onLayoutModeToggle; |
$('portrait').onclick = onLayoutModeToggle; |
$('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities; |
@@ -96,26 +164,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(); |
} |
/** |
@@ -138,6 +199,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; |
}); |
@@ -326,6 +388,14 @@ function getSelectedPrinterName() { |
* Asks the browser to print the preview PDF based on current print settings. |
*/ |
function printFile() { |
+ hasPendingPrintFileRequest = hasPendingPreviewRequest ? true : false; |
+ |
+ 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'); |
@@ -333,14 +403,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(); |
@@ -442,6 +514,7 @@ function setColor(color) { |
* 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; |
@@ -497,6 +570,8 @@ function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { |
previewModifiable = modifiable; |
+ hasPendingPreviewRequest = false; |
+ |
if (totalPageCount == -1) |
totalPageCount = pageCount; |
@@ -526,6 +601,12 @@ function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { |
document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); |
createPDFPlugin(previewUid); |
+ |
+ if (hasPendingPrintFileRequest) { |
+ printFile(); |
+ return; |
+ } |
+ |
updatePrintSummary(); |
updatePrintButtonState(); |
addEventListeners(); |
@@ -607,13 +688,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'); |
var validityLevel = getSelectedPagesValidityLevel(); |
@@ -630,6 +707,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(); |
@@ -810,21 +897,44 @@ function getSelectedPagesValidityLevel() { |
continue; |
var match = part.match(/^([0-9]+)-([0-9]*)$/); |
- if (match && match[1]) { |
- var from = parseInt(match[1], 10); |
- var to = match[2] ? parseInt(match[2], 10) : totalPageCount; |
- |
- if (from && to && from <= to) |
+ if (totalPageCount != -1) { |
+ if (match && match[1]) { |
+ var from = parseInt(match[1], 10); |
+ var to = match[2] ? parseInt(match[2], 10) : totalPageCount; |
+ |
+ if (from && to && from <= to) |
+ successfullyParsed += 1; |
+ else |
+ failedToParse += 1; |
+ } else if (isValidNonZeroPositiveInteger(part) && |
+ parseInt(part, 10) <= totalPageCount) { |
successfullyParsed += 1; |
- else |
+ } else { |
failedToParse += 1; |
- |
- } else if (isInteger(part) && parseInt(part, 10) <= totalPageCount && |
- parseInt(part, 10) > 0) |
- successfullyParsed += 1; |
- else |
- failedToParse += 1; |
+ } |
+ } else { |
+ // totalPageCount is -1. Just validate the page range format. |
+ if (match && isValidNonZeroPositiveInteger(match[1])) { |
+ if (match[2]) { |
+ if (isValidNonZeroPositiveInteger(match[2])) { |
+ if (parseInt(match[1], 10) <= parseInt(match[2], 10)) |
+ successfullyParsed += 1; |
+ else |
+ failedToParse += 1; |
+ } else { |
+ failedToParse += 1; |
+ } |
+ } else { |
+ successfullyParsed += 1; |
+ } |
+ } else if (isValidNonZeroPositiveInteger(part)) { |
+ successfullyParsed += 1; |
+ } else { |
+ failedToParse += 1; |
+ } |
+ } |
} |
+ |
if (successfullyParsed > 0 && failedToParse == 0) |
return 1; |
else if (successfullyParsed > 0 && failedToParse > 0) |
@@ -833,6 +943,14 @@ function getSelectedPagesValidityLevel() { |
return -1; |
} |
+/** |
+ * 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; |
+} |
+ |
function isSelectedPagesFieldValid() { |
return (getSelectedPages().length != 0) |
} |