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

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

Issue 7976017: Simplified print preview printer selection to be more consistent with Chrome (Closed)
Patch Set: Fix whitespace Created 9 years, 3 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/print_preview_cloud.js
diff --git a/chrome/browser/resources/print_preview/print_preview_cloud.js b/chrome/browser/resources/print_preview/print_preview_cloud.js
index 943f667c60ee2221ea7d2d6c8722e50480836ba3..dc2aa1a28b51c654f03a5812e95c649e9100fa15 100644
--- a/chrome/browser/resources/print_preview/print_preview_cloud.js
+++ b/chrome/browser/resources/print_preview/print_preview_cloud.js
@@ -119,13 +119,11 @@ cr.define('cloudprint', function() {
var searchResult = JSON.parse(xhr.responseText);
if (searchResult['success']) {
var printerList = searchResult['printers'];
- callback.call(this, printerList);
- } else {
- callback.call(this, null);
+ addCloudPrinters(printerList, callback);
+ return;
}
- } else {
- callback.call(this, null);
}
+ addCloudPrinters(null, callback);
}
/**
@@ -306,7 +304,8 @@ cr.define('cloudprint', function() {
cloud_print_data,
add_callback,
update_caps_callback) {
- var printer = add_callback([JSON.parse(cloud_print_data)]);
+ var printer = addCloudPrinters([JSON.parse(cloud_print_data)],
+ add_callback);
if (printer)
update_caps_callback(printer);
}
@@ -350,7 +349,87 @@ cr.define('cloudprint', function() {
printer.cloudPrintOptions.id = id;
}
+ /**
+ * Test if a particular cloud printer has already been added to the
+ * printer dropdown.
+ * @param {string} id A unique value to track this printer.
+ * @return {boolean} True if |id| has previously been passed to
+ * trackCloudPrinterAdded.
+ */
+ function cloudPrinterAlreadyAdded(id) {
+ return addedCloudPrinters[id];
+ }
+
+ /**
+ * Test if a particular printer has already been added to the printers
+ * dropdown. Records it if not.
+ * @param {string} id A unique value to track this printer.
+ * @return {boolean} False if adding this printer would exceed
+ * |maxCloudPrinters|.
+ */
+ function trackCloudPrinterAdded(id) {
+ if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) {
+ addedCloudPrinters[id] = true;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Add cloud printers to the list drop down.
+ * Called from the cloudprint object on receipt of printer information from
+ * the cloud print server.
+ * @param {Array} printers Array of printer info objects.
+ * @return {Object} The currently selected printer.
+ */
+ function addCloudPrinters(printers, addDestinationListOptionAtPosition) {
+ var isFirstPass = false;
+ var printerList = $('printer-list');
+
+ if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) {
+ isFirstPass = true;
+ // Remove empty entry added by setDefaultPrinter.
+ if (printerList[0] && printerList[0].textContent == '')
+ printerList.remove(0);
+ }
+ if (printers != null) {
+ for (var i = 0; i < printers.length; i++) {
+ if (!cloudPrinterAlreadyAdded(printers[i]['id'])) {
+ if (!trackCloudPrinterAdded(printers[i]['id'])) {
+ break;
+ }
+ var option = addDestinationListOptionAtPosition(
+ lastCloudPrintOptionPos++,
+ printers[i]['name'],
+ printers[i]['id'],
+ printers[i]['name'] == defaultOrLastUsedPrinterName,
+ false,
+ false);
+ cloudprint.setCloudPrint(option,
+ printers[i]['name'],
+ printers[i]['id']);
+ }
+ }
+ } else {
+ if (!cloudPrinterAlreadyAdded(SIGN_IN)) {
+ addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
+ localStrings.getString('signIn'),
+ SIGN_IN,
+ false,
+ false,
+ false);
+ trackCloudPrinterAdded(SIGN_IN);
+ }
+ }
+ var selectedPrinter = printerList.selectedIndex;
+ if (selectedPrinter < 0)
+ return null;
+ return printerList.options[selectedPrinter];
+ }
+
return {
+ addCloudPrinters: addCloudPrinters,
colorIsDefault: colorIsDefault,
fetchPrinters: fetchPrinters,
getBaseURL: getBaseURL,
« no previous file with comments | « chrome/browser/resources/print_preview/print_preview.js ('k') | chrome/browser/ui/webui/print_preview_data_source.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698