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

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: Fix whitespace Created 9 years, 2 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 unified diff | Download patch
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...) Expand 10 before | Expand all | Expand 10 after
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);
123 } else { 123 return;
124 callback.call(this, null);
125 } 124 }
126 } else {
127 callback.call(this, null);
128 } 125 }
126 addCloudPrinters(null, callback);
129 } 127 }
130 128
131 /** 129 /**
132 * Retrieve the list of printers available via cloud print. 130 * Retrieve the list of printers available via cloud print.
133 * @param {function} callback Function to be called to process response. 131 * @param {function} callback Function to be called to process response.
134 */ 132 */
135 function fetchPrinters(callback, all) { 133 function fetchPrinters(callback, all) {
136 var query = 'q=^recent'; 134 var query = 'q=^recent';
137 if (all) { 135 if (all) {
138 query = ''; 136 query = '';
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 * @param {Object} cloud_print_data Data to be stored in cloudPrintOptions. 297 * @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 298 * @param {function} add_callback The callback to be called to add the new
301 * printer to the print preview UI. 299 * printer to the print preview UI.
302 * @param {function} update_caps_callback The callback to be called to update 300 * @param {function} update_caps_callback The callback to be called to update
303 * capabilities on the new printer. 301 * capabilities on the new printer.
304 */ 302 */
305 function setDefaultPrinter(printer_name, 303 function setDefaultPrinter(printer_name,
306 cloud_print_data, 304 cloud_print_data,
307 add_callback, 305 add_callback,
308 update_caps_callback) { 306 update_caps_callback) {
309 var printer = add_callback([JSON.parse(cloud_print_data)]); 307 var printer = addCloudPrinters([JSON.parse(cloud_print_data)],
308 add_callback);
310 if (printer) 309 if (printer)
311 update_caps_callback(printer); 310 update_caps_callback(printer);
312 } 311 }
313 312
314 /** 313 /**
315 * Returns the data necessary to serialize a cloud print printer. 314 * Returns the data necessary to serialize a cloud print printer.
316 * @param {Object} printer The printer object to get data for. 315 * @param {Object} printer The printer object to get data for.
317 * @return {string} A JSON string that can be used to recreate the 316 * @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 317 * cloud print portion of the printer object, or and empty string if
319 * there is no data to save. 318 * there is no data to save.
(...skipping 23 matching lines...) Expand all
343 * identify it. 342 * identify it.
344 */ 343 */
345 function setCloudPrint(printer, name, id) { 344 function setCloudPrint(printer, name, id) {
346 if (!printer.cloudPrintOptions) { 345 if (!printer.cloudPrintOptions) {
347 printer.cloudPrintOptions = new Object; 346 printer.cloudPrintOptions = new Object;
348 } 347 }
349 printer.cloudPrintOptions.name = name; 348 printer.cloudPrintOptions.name = name;
350 printer.cloudPrintOptions.id = id; 349 printer.cloudPrintOptions.id = id;
351 } 350 }
352 351
352 /**
353 * Test if a particular cloud printer has already been added to the
354 * printer dropdown.
355 * @param {string} id A unique value to track this printer.
356 * @return {boolean} True if |id| has previously been passed to
357 * trackCloudPrinterAdded.
358 */
359 function cloudPrinterAlreadyAdded(id) {
360 return addedCloudPrinters[id];
361 }
362
363 /**
364 * Test if a particular printer has already been added to the printers
365 * dropdown. Records it if not.
366 * @param {string} id A unique value to track this printer.
367 * @return {boolean} False if adding this printer would exceed
368 * |maxCloudPrinters|.
369 */
370 function trackCloudPrinterAdded(id) {
371 if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) {
372 addedCloudPrinters[id] = true;
373 return true;
374 } else {
375 return false;
376 }
377 }
378
379 /**
380 * Add cloud printers to the list drop down.
381 * Called from the cloudprint object on receipt of printer information from
382 * the cloud print server.
383 * @param {Array} printers Array of printer info objects.
384 * @return {Object} The currently selected printer.
385 */
386 function addCloudPrinters(printers, addDestinationListOptionAtPosition) {
387 var isFirstPass = false;
388 var printerList = $('printer-list');
389
390 if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) {
391 isFirstPass = true;
392 // Remove empty entry added by setDefaultPrinter.
393 if (printerList[0] && printerList[0].textContent == '')
394 printerList.remove(0);
395 }
396 if (printers != null) {
397 for (var i = 0; i < printers.length; i++) {
398 if (!cloudPrinterAlreadyAdded(printers[i]['id'])) {
399 if (!trackCloudPrinterAdded(printers[i]['id'])) {
400 break;
401 }
402 var option = addDestinationListOptionAtPosition(
403 lastCloudPrintOptionPos++,
404 printers[i]['name'],
405 printers[i]['id'],
406 printers[i]['name'] == defaultOrLastUsedPrinterName,
407 false,
408 false);
409 cloudprint.setCloudPrint(option,
410 printers[i]['name'],
411 printers[i]['id']);
412 }
413 }
414 } else {
415 if (!cloudPrinterAlreadyAdded(SIGN_IN)) {
416 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
417 localStrings.getString('signIn'),
418 SIGN_IN,
419 false,
420 false,
421 false);
422 trackCloudPrinterAdded(SIGN_IN);
423 }
424 }
425 var selectedPrinter = printerList.selectedIndex;
426 if (selectedPrinter < 0)
427 return null;
428 return printerList.options[selectedPrinter];
429 }
430
353 return { 431 return {
432 addCloudPrinters: addCloudPrinters,
354 colorIsDefault: colorIsDefault, 433 colorIsDefault: colorIsDefault,
355 fetchPrinters: fetchPrinters, 434 fetchPrinters: fetchPrinters,
356 getBaseURL: getBaseURL, 435 getBaseURL: getBaseURL,
357 getData: getData, 436 getData: getData,
358 getPrintTicketJSON: getPrintTicketJSON, 437 getPrintTicketJSON: getPrintTicketJSON,
359 isCloudPrint: isCloudPrint, 438 isCloudPrint: isCloudPrint,
360 printToCloud: printToCloud, 439 printToCloud: printToCloud,
361 setBaseURL: setBaseURL, 440 setBaseURL: setBaseURL,
362 setCloudPrint: setCloudPrint, 441 setCloudPrint: setCloudPrint,
363 setColor: setColor, 442 setColor: setColor,
364 setDefaultPrinter: setDefaultPrinter, 443 setDefaultPrinter: setDefaultPrinter,
365 supportsColor: supportsColor, 444 supportsColor: supportsColor,
366 updatePrinterCaps: updatePrinterCaps 445 updatePrinterCaps: updatePrinterCaps
367 }; 446 };
368 }); 447 });
OLDNEW
« 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