Index: chrome/browser/resources/print_preview.js |
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js |
index 8dedff6a2210a1c37482c35308b916400dabb8ab..19644b12067a263f215e51fb6784059382396ed3 100644 |
--- a/chrome/browser/resources/print_preview.js |
+++ b/chrome/browser/resources/print_preview.js |
@@ -2,8 +2,13 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+// require: cr/ui/print_preview_cloud.js |
+ |
var localStrings = new LocalStrings(); |
+var useCloudPrint = false; |
+var maxCloudPrinters = 10; |
dpapad
2011/06/14 01:14:46
Document these variables.
Albert Bodenhamer
2011/06/14 19:43:54
Done.
|
+ |
// The total page count of the previewed document regardless of which pages the |
// user has selected. |
var totalPageCount = -1; |
@@ -24,8 +29,12 @@ var lastSelectedPrinterIndex = 0; |
var previewModifiable = false; |
// Destination list special value constants. |
+const ADD_PRINTER = 'addPrinter'; |
+const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; |
+const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; |
+const MORE_PRINTERS = 'morePrinters'; |
+const SIGN_IN = 'signIn'; |
const PRINT_TO_PDF = 'Print To PDF'; |
-const MANAGE_PRINTERS = 'Manage Printers'; |
// State of the print preview settings. |
var printSettings = new PrintSettings(); |
@@ -203,22 +212,56 @@ function updateControlsWithSelectedPrinterCapabilities() { |
var selectedIndex = printerList.selectedIndex; |
if (selectedIndex < 0) |
return; |
- |
+ var skip_refresh = false; |
var selectedValue = printerList.options[selectedIndex].value; |
- if (selectedValue == PRINT_TO_PDF) { |
+ if (printerList.options[selectedIndex].isCloudPrint) { |
+ updateWithCloudPrinterCapabilities(); |
+ skip_refresh = true; |
+ } else if (selectedValue == SIGN_IN || |
+ selectedValue == MANAGE_CLOUD_PRINTERS || |
+ selectedValue == MANAGE_LOCAL_PRINTERS) { |
+ printerList.selectedIndex = lastSelectedPrinterIndex; |
+ chrome.send(selectedValue); |
+ skip_refresh = true; |
+ } else if (selectedValue == PRINT_TO_PDF) { |
updateWithPrinterCapabilities({'disableColorOption': true, |
'setColorAsDefault': true, |
- 'disableCopiesOption': true}); |
- } else if (selectedValue == MANAGE_PRINTERS) { |
- printerList.selectedIndex = lastSelectedPrinterIndex; |
- chrome.send('managePrinters'); |
- return; |
+ 'disableCopiesOption': true, |
+ 'disableLandscapeOption': false}); |
} else { |
// This message will call back to 'updateWithPrinterCapabilities' |
// function. |
chrome.send('getPrinterCapabilities', [selectedValue]); |
} |
+ if (!skip_refresh) { |
+ lastSelectedPrinterIndex = selectedIndex; |
+ |
+ // Regenerate the preview data based on selected printer settings. |
+ setDefaultValuesAndRegeneratePreview(); |
+ } |
+} |
+ |
+function updateWithCloudPrinterCapabilities() { |
+ var printerList = $('printer-list'); |
+ var selectedIndex = printerList.selectedIndex; |
+ if (printerList.options[selectedIndex].capabilities) { |
+ doUpdateCloudPrinterCapabilities( |
+ printerList.options[selectedIndex], |
+ printerList.options[selectedIndex].capabilities); |
+ } else { |
+ cloudprint.updatePrinterCaps(printerList.options[selectedIndex], |
+ doUpdateCloudPrinterCapabilities); |
+ } |
+} |
+function doUpdateCloudPrinterCapabilities(printer) { |
+ var settings = {'disableColorOption': !cloudprint.supportsColor(printer), |
+ 'setColorAsDefault': cloudprint.colorIsDefault(printer), |
+ 'disableCopiesOption': true, |
+ 'disableLandscapeOption': true}; |
+ updateWithPrinterCapabilities(settings); |
+ var printerList = $('printer-list'); |
+ var selectedIndex = printerList.selectedIndex; |
lastSelectedPrinterIndex = selectedIndex; |
// Regenerate the preview data based on selected printer settings. |
@@ -233,6 +276,7 @@ function updateWithPrinterCapabilities(settingInfo) { |
var disableColorOption = settingInfo.disableColorOption; |
var disableCopiesOption = settingInfo.disableCopiesOption; |
var setColorAsDefault = settingInfo.setColorAsDefault; |
+ var disableLandscapeOption = settingInfo.disableLandscapeOption; |
var colorOption = $('color'); |
var bwOption = $('bw'); |
@@ -244,6 +288,12 @@ function updateWithPrinterCapabilities(settingInfo) { |
$('hr-before-copies').classList.add('invisible'); |
} |
+ if (disableLandscapeOption) { |
+ fadeOutElement($('landscape-option')); |
+ } else { |
+ fadeInElement($('landscape-option')); |
+ } |
+ |
disableColorOption ? fadeOutElement($('color-options')) : |
fadeInElement($('color-options')); |
@@ -254,6 +304,31 @@ function updateWithPrinterCapabilities(settingInfo) { |
} |
/** |
+ * Turn on the integration of Cloud Print. |
+ * @param {enable} true if cloud print should be used. |
dpapad
2011/06/14 01:14:46
@param {boolean} enable True if could print should
|
+ */ |
+function setUseCloudPrint(enable, cloudPrintURL) { |
+ useCloudPrint = enable; |
+ cloudprint.setBaseURL(cloudPrintURL); |
+} |
+ |
+/** |
+ * Take the PDF data handed to us and submit it to the cloud, closing the print |
+ * preview tab once the upload is successful. |
+ * @param {pdfData} data to send as the print job. |
dpapad
2011/06/14 01:14:46
Here and elsewhere
@param {string} pdfData ....
|
+ */ |
+function printToCloud(data) { |
+ cloudprint.printToCloud(data, finishedCloudPrinting); |
+} |
+ |
+/** |
+ * Cloud print upload of the PDF file is finished, time to close the dialog. |
+ */ |
+function finishedCloudPrinting() { |
+ window.location = cloudprint.getBaseURL(); |
+} |
+ |
+/** |
* Validates the copies text field value. |
* NOTE: An empty copies field text is considered valid because the blur event |
* listener of this field will set it back to a default value. |
@@ -344,19 +419,27 @@ function getDuplexMode() { |
* @return {string} JSON string with print job settings. |
*/ |
function getSettingsJSON() { |
+ var printerList = $('printer-list'); |
+ var selectedPrinter = printerList.selectedIndex; |
var printAll = $('all-pages').checked; |
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}); |
+ var settings = {'deviceName': deviceName, |
+ 'pageRange': getSelectedPageRanges(), |
+ 'printAll': printAll, |
+ 'duplex': getDuplexMode(), |
+ 'copies': getCopies(), |
+ 'collate': isCollated(), |
+ 'landscape': isLandscape(), |
+ 'color': isColor(), |
+ 'printToPDF': printToPDF}; |
+ if (printerList.options[selectedPrinter].isCloudPrint) { |
+ settings['cloudPrintID'] = |
+ printerList.options[selectedPrinter].value; |
+ } |
+ |
+ return JSON.stringify(settings); |
} |
/** |
@@ -413,7 +496,10 @@ function cancelPendingPrintRequest() { |
* Sends a message to initiate print workflow. |
*/ |
function sendPrintFileRequest() { |
- chrome.send('print', [getSettingsJSON()]); |
+ var printerList = $('printer-list') |
dpapad
2011/06/14 01:14:46
Add ; at the end.
Albert Bodenhamer
2011/06/14 19:43:54
Done.
|
+ var printer = printerList[printerList.selectedIndex]; |
+ chrome.send('print', [getSettingsJSON(), |
+ cloudprint.getPrintTicketJSON(printer)]); |
} |
/** |
@@ -449,6 +535,16 @@ function setDefaultPrinter(printer) { |
*/ |
function setPrinters(printers) { |
var printerList = $('printer-list'); |
+ if (useCloudPrint) { |
+ cloudprint.fetchPrinters(addCloudPrinters); |
+ if (printers.length > 0) { |
+ option = addDestinationListOption('', '', false, true); |
+ option = addDestinationListOption(localStrings.getString('localPrinters'), |
+ '', |
+ false, |
+ true); |
+ } |
+ } |
// If there exists a dummy printer value, then setDefaultPrinter() already |
// requested a preview, so no need to do it again. |
var needPreview = (printerList[0].value == ''); |
@@ -471,9 +567,15 @@ function setPrinters(printers) { |
false); |
addDestinationListOption('', '', false, true); |
- // Add an option to manage printers. |
- addDestinationListOption(localStrings.getString('managePrinters'), |
- MANAGE_PRINTERS, false, false); |
+ // Add options to manage printers. |
+ if (!cr.isChromeOS) { |
+ addDestinationListOption(localStrings.getString('manageLocalPrinters'), |
+ MANAGE_LOCAL_PRINTERS, false, false); |
+ } |
+ if (useCloudPrint) { |
+ addDestinationListOption(localStrings.getString('manageCloudPrinters'), |
+ MANAGE_CLOUD_PRINTERS, false, false); |
+ } |
printerList.disabled = false; |
@@ -487,6 +589,7 @@ function setPrinters(printers) { |
* @param {String} optionValue specifies the option value. |
* @param {boolean} isDefault is true if the option needs to be selected. |
* @param {boolean} isDisabled is true if the option needs to be disabled. |
+ * returns the created option. |
dpapad
2011/06/14 01:14:46
For consistency could you use the following format
Albert Bodenhamer
2011/06/14 19:43:54
Done.
|
*/ |
function addDestinationListOption(optionText, optionValue, isDefault, |
isDisabled) { |
@@ -496,6 +599,44 @@ function addDestinationListOption(optionText, optionValue, isDefault, |
$('printer-list').add(option); |
option.selected = isDefault; |
option.disabled = isDisabled; |
+ return option; |
+} |
+ |
+/** |
+ * Add cloud printers to the list drop down. |
+ * Called from the cloudprint object on receipt of printer information from the |
+ * cloud print server. |
+ */ |
+function addCloudPrinters(printers) { |
+ addDestinationListOption(localStrings.getString('cloudPrinters'), |
+ '', |
+ false, |
+ true); |
+ if (printers != null) { |
+ var l = printers.length; |
dpapad
2011/06/14 01:14:46
Nit: This makes it shorter to write but harder to
|
+ if (l > maxCloudPrinters) { |
+ l = maxCloudPrinters; |
+ } |
+ for (var i = 0; i < l; i++) { |
+ var option = addDestinationListOption(printers[i]['name'], |
+ printers[i]['id'], |
+ i == 0, |
+ printers[i]['name'] == defaultOrLastUsedPrinterName); |
+ option.isCloudPrint = true; |
+ } |
+ if (printers.length == 0) { |
+ addDestinationListOption(localStrings.getString('addCloudPrinter'), |
+ ADD_PRINTER, false, false); |
+ } |
+ if (l < printers.length) { |
+ var option = addDestinationListOption('', '', false, true); |
+ addDestinationListOption(localStrings.getString('morePrinters'), |
+ MORE_PRINTERS, false, false); |
+ } |
+ } else { |
+ addDestinationListOption(localStrings.getString('signIn'), |
+ SIGN_IN, false, false); |
+ } |
} |
/** |
@@ -509,6 +650,8 @@ function setColor(color) { |
return; |
} |
pdfViewer.grayscale(!color); |
+ var printerList = $('printer-list') |
+ cloudprint.setColor(printerList[printerList.selectedIndex], color); |
} |
/** |
@@ -1064,3 +1207,4 @@ PrintSettings.prototype.save = function() { |
this.deviceName = getSelectedPrinterName(); |
this.isLandscape = isLandscape(); |
} |
+ |