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..567862e1af608207b7a1172f85d172abd22e67d1 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; |
+ |
// 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(); |
@@ -157,14 +166,20 @@ function updateControlsWithSelectedPrinterCapabilities() { |
return; |
var selectedValue = printerList.options[selectedIndex].value; |
- if (selectedValue == PRINT_TO_PDF) { |
- updateWithPrinterCapabilities({'disableColorOption': true, |
- 'setColorAsDefault': true, |
- 'disableCopiesOption': true}); |
- } else if (selectedValue == MANAGE_PRINTERS) { |
+ if (printerList.options[selectedIndex].isCloudPrint) { |
+ updateWithCloudPrinterCapabilities(); |
+ return; |
sanjeevr
2011/06/10 18:13:48
Nit: Is there a way to restructure this so we don'
Albert Bodenhamer
2011/06/10 23:13:45
Done.
|
+ } else if (selectedValue == SIGN_IN || |
+ selectedValue == MANAGE_CLOUD_PRINTERS || |
+ selectedValue == MANAGE_LOCAL_PRINTERS) { |
printerList.selectedIndex = lastSelectedPrinterIndex; |
- chrome.send('managePrinters'); |
+ chrome.send(selectedValue); |
return; |
+ } else if (selectedValue == PRINT_TO_PDF) { |
+ updateWithPrinterCapabilities({'disableColorOption': true, |
+ 'setColorAsDefault': true, |
+ 'disableCopiesOption': true, |
+ 'disableLandscapeOption': false}); |
} else { |
// This message will call back to 'updateWithPrinterCapabilities' |
// function. |
@@ -177,6 +192,33 @@ function updateControlsWithSelectedPrinterCapabilities() { |
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. |
+ setDefaultValuesAndRegeneratePreview(); |
+} |
+ |
/** |
* Updates the controls with printer capabilities information. |
* @param {Object} settingInfo printer setting information. |
@@ -185,6 +227,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'); |
@@ -196,6 +239,12 @@ function updateWithPrinterCapabilities(settingInfo) { |
$('hr-before-copies').classList.add('invisible'); |
} |
+ if (disableLandscapeOption) { |
+ fadeOutElement($('landscape-option')); |
+ } else { |
+ fadeInElement($('landscape-option')); |
dpapad
2011/06/10 20:29:07
nit: Could you use the more compressed form?
disa
Albert Bodenhamer
2011/06/10 23:13:45
I have a strong dislike of the ternary operator fo
|
+ } |
+ |
disableColorOption ? fadeOutElement($('color-options')) : |
fadeInElement($('color-options')); |
@@ -207,6 +256,44 @@ function updateWithPrinterCapabilities(settingInfo) { |
} |
/** |
+ * Turn on the integration of Cloud Print. |
+ * @param {enable} true if cloud print should be used. |
+ */ |
+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. |
+ */ |
+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(); |
+} |
+ |
+/** |
+ * Disables or enables all controls in the options pane except for the cancel |
+ * button. |
+ */ |
+function setControlsDisabled(disabled) { |
dpapad
2011/06/10 20:29:07
This function was removed in earlier revisions, do
Albert Bodenhamer
2011/06/10 23:13:45
Done.
|
+ var elementList = $('controls').elements; |
+ for (var i = 0; i < elementList.length; ++i) { |
+ if (elementList[i] == $('cancel-button')) |
+ continue; |
+ elementList[i].disabled = disabled; |
+ } |
+} |
+ |
+/** |
* 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. |
@@ -297,19 +384,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); |
} |
/** |
@@ -334,10 +429,12 @@ function printFile() { |
$('cancel-button').classList.add('loading'); |
$('print-summary').innerHTML = localStrings.getString('printing'); |
removeEventListeners(); |
- window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, |
- 1000); |
+ var printerList = $('printer-list') |
+ var printer = printerList[printerList.selectedIndex]; |
+ window.setTimeout(function() { chrome.send('print', |
+ [getSettingsJSON(), cloudprint.getPrintTicketJSON(printer)]); }, 1000); |
sanjeevr
2011/06/10 18:13:48
You probably need to check whether this is a cloud
Albert Bodenhamer
2011/06/10 23:13:45
getPrintTicketJSON now returns null for non-cloud
|
} else |
- chrome.send('print', [getSettingsJSON()]); |
+ chrome.send('print', [getSettingsJSON(), null]); |
} |
/** |
@@ -372,6 +469,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 == ''); |
@@ -394,9 +501,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; |
@@ -419,6 +532,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; |
+ if (l > maxCloudPrinters) { |
+ l = maxCloudPrinters; |
+ } |
+ for (var i = 0; i < l; i++) { |
+ var option = addDestinationListOption(printers[i]['name'], |
+ printers[i]['id'], |
+ i == 0, |
dpapad
2011/06/10 20:29:07
I believe this should be
printers[i]['name'] == d
Albert Bodenhamer
2011/06/10 23:13:45
Done.
|
+ false); |
+ 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); |
+ } |
} |
/** |
@@ -432,6 +583,8 @@ function setColor(color) { |
return; |
} |
pdfViewer.grayscale(!color); |
+ var printerList = $('printer-list') |
+ cloudprint.setColor(printerList[printerList.selectedIndex], color); |
} |
/** |
@@ -962,3 +1115,4 @@ PrintSettings.prototype.save = function() { |
this.deviceName = getSelectedPrinterName(); |
this.isLandscape = isLandscape(); |
} |
+ |