Chromium Code Reviews

Side by Side 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: Fixed flow in chrome Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 cr.define('cloudprint', function() { 5 cr.define('cloudprint', function() {
6 6
7 // The URL to use to access the cloud print servers. 7 // The URL to use to access the cloud print servers.
8 // Set by a call to setBaseURL. 8 // Set by a call to setBaseURL.
9 var cloudPrintBaseURL = ''; 9 var cloudPrintBaseURL = '';
10 10
(...skipping 101 matching lines...)
112 /** 112 /**
113 * Parse the response from the fetchPrinters call. 113 * Parse the response from the fetchPrinters call.
114 * @param {function} callback Function to be called to process response. 114 * @param {function} callback Function to be called to process response.
115 * @param {XMLHttpRequest} xhr The object used to make the request. 115 * @param {XMLHttpRequest} xhr The object used to make the request.
116 */ 116 */
117 function fetchPrintersResponse(callback, xhr) { 117 function fetchPrintersResponse(callback, xhr) {
118 if (xhr.status == 200) { 118 if (xhr.status == 200) {
119 var searchResult = JSON.parse(xhr.responseText); 119 var searchResult = JSON.parse(xhr.responseText);
120 if (searchResult['success']) { 120 if (searchResult['success']) {
121 var printerList = searchResult['printers']; 121 var printerList = searchResult['printers'];
122 callback.call(this, printerList); 122 addCloudPrinters(printerList, callback);
dpapad 2011/09/22 18:11:57 Fix indentation.
Albert Bodenhamer 2011/09/22 23:23:39 Done.
123 } else { 123 } else {
124 callback.call(this, null); 124 addCloudPrinters(null, callback);
125 } 125 }
126 } else { 126 } else {
127 callback.call(this, null); 127 addCloudPrinters(null, callback);
dpapad 2011/09/22 18:11:57 Could you restructure the if blocks as follows? if
Albert Bodenhamer 2011/09/22 23:23:39 Done.
128 } 128 }
129 } 129 }
130 130
131 /** 131 /**
132 * Retrieve the list of printers available via cloud print. 132 * Retrieve the list of printers available via cloud print.
133 * @param {function} callback Function to be called to process response. 133 * @param {function} callback Function to be called to process response.
134 */ 134 */
135 function fetchPrinters(callback, all) { 135 function fetchPrinters(callback, all) {
136 var query = 'q=^recent'; 136 var query = 'q=^recent';
137 if (all) { 137 if (all) {
(...skipping 161 matching lines...)
299 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions. 299 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions.
300 * @param {function} add_callback The callback to be called to add the new 300 * @param {function} add_callback The callback to be called to add the new
301 * printer to the print preview UI. 301 * printer to the print preview UI.
302 * @param {function} update_caps_callback The callback to be called to update 302 * @param {function} update_caps_callback The callback to be called to update
303 * capabilities on the new printer. 303 * capabilities on the new printer.
304 */ 304 */
305 function setDefaultPrinter(printer_name, 305 function setDefaultPrinter(printer_name,
306 cloud_print_data, 306 cloud_print_data,
307 add_callback, 307 add_callback,
308 update_caps_callback) { 308 update_caps_callback) {
309 var printer = add_callback([JSON.parse(cloud_print_data)]); 309 var printer = addCloudPrinters([JSON.parse(cloud_print_data)],
310 add_callback);
310 if (printer) 311 if (printer)
311 update_caps_callback(printer); 312 update_caps_callback(printer);
312 } 313 }
313 314
314 /** 315 /**
315 * Returns the data necessary to serialize a cloud print printer. 316 * Returns the data necessary to serialize a cloud print printer.
316 * @param {Object} printer The printer object to get data for. 317 * @param {Object} printer The printer object to get data for.
317 * @return {string} A JSON string that can be used to recreate the 318 * @return {string} A JSON string that can be used to recreate the
318 * cloud print portion of the printer object, or and empty string if 319 * cloud print portion of the printer object, or and empty string if
319 * there is no data to save. 320 * there is no data to save.
(...skipping 23 matching lines...)
343 * identify it. 344 * identify it.
344 */ 345 */
345 function setCloudPrint(printer, name, id) { 346 function setCloudPrint(printer, name, id) {
346 if (!printer.cloudPrintOptions) { 347 if (!printer.cloudPrintOptions) {
347 printer.cloudPrintOptions = new Object; 348 printer.cloudPrintOptions = new Object;
348 } 349 }
349 printer.cloudPrintOptions.name = name; 350 printer.cloudPrintOptions.name = name;
350 printer.cloudPrintOptions.id = id; 351 printer.cloudPrintOptions.id = id;
351 } 352 }
352 353
354 /**
355 * Test if a particular cloud printer has already been added to the
356 * printer dropdown.
357 * @param {string} id A unique value to track this printer.
358 * @return {boolean} True if this id has previously been passed to
dpapad 2011/09/22 18:11:57 Nit: True if |id| has ...
Albert Bodenhamer 2011/09/22 23:23:39 Done.
359 * trackCloudPrinterAdded.
360 */
361 function cloudPrinterAlreadyAdded(id) {
362 return (addedCloudPrinters[id]);
dpapad 2011/09/22 18:11:57 Nit: No need for parenthesis.
Albert Bodenhamer 2011/09/22 23:23:39 Done.
363 }
364
365 /**
366 * Record that a cloud printer will added to the printer dropdown.
367 * @param {string} id A unique value to track this printer.
368 * @return {boolean} False if adding this printer would exceed
369 * |maxCloudPrinters|.
370 */
371 function trackCloudPrinterAdded(id) {
372 if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) {
373 addedCloudPrinters[id] = true;
374 return true;
375 } else {
376 return false;
377 }
378 }
379
380 /**
381 * Add cloud printers to the list drop down.
382 * Called from the cloudprint object on receipt of printer information from
383 * the cloud print server.
384 * @param {Array} printers Array of printer info objects.
385 * @return {Object} The currently selected printer.
386 */
387 function addCloudPrinters(printers, addDestinationListOptionAtPosition) {
388 var isFirstPass = false;
389 var printerList = $('printer-list');
390
391 if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) {
392 isFirstPass = true;
393 // Remove empty entry added by setDefaultPrinter.
394 if (printerList[0] && printerList[0].textContent == '')
395 printerList.remove(0);
396 }
397 if (printers != null) {
398 for (var i = 0; i < printers.length; i++) {
399 if (!cloudPrinterAlreadyAdded(printers[i]['id'])) {
400 if (!trackCloudPrinterAdded(printers[i]['id'])) {
401 break;
402 }
403 var option = addDestinationListOptionAtPosition(
404 lastCloudPrintOptionPos++,
405 printers[i]['name'],
406 printers[i]['id'],
407 printers[i]['name'] == defaultOrLastUsedPrinterName,
408 false,
409 false);
410 cloudprint.setCloudPrint(option,
411 printers[i]['name'],
412 printers[i]['id']);
413 }
414 }
415 } else {
416 if (!cloudPrinterAlreadyAdded(SIGN_IN)) {
417 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
418 localStrings.getString('signIn'),
419 SIGN_IN,
420 false,
421 false,
422 false);
423 trackCloudPrinterAdded(SIGN_IN);
424 }
425 }
426 var selectedPrinter = printerList.selectedIndex;
427 if (selectedPrinter < 0)
428 return null;
429 return printerList.options[selectedPrinter];
430 }
431
353 return { 432 return {
433 addCloudPrinters: addCloudPrinters,
354 colorIsDefault: colorIsDefault, 434 colorIsDefault: colorIsDefault,
355 fetchPrinters: fetchPrinters, 435 fetchPrinters: fetchPrinters,
356 getBaseURL: getBaseURL, 436 getBaseURL: getBaseURL,
357 getData: getData, 437 getData: getData,
358 getPrintTicketJSON: getPrintTicketJSON, 438 getPrintTicketJSON: getPrintTicketJSON,
359 isCloudPrint: isCloudPrint, 439 isCloudPrint: isCloudPrint,
360 printToCloud: printToCloud, 440 printToCloud: printToCloud,
361 setBaseURL: setBaseURL, 441 setBaseURL: setBaseURL,
362 setCloudPrint: setCloudPrint, 442 setCloudPrint: setCloudPrint,
363 setColor: setColor, 443 setColor: setColor,
364 setDefaultPrinter: setDefaultPrinter, 444 setDefaultPrinter: setDefaultPrinter,
365 supportsColor: supportsColor, 445 supportsColor: supportsColor,
366 updatePrinterCaps: updatePrinterCaps 446 updatePrinterCaps: updatePrinterCaps
367 }; 447 };
368 }); 448 });
OLDNEW

Powered by Google App Engine