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

Side by Side Diff: chrome/browser/resources/print_preview/print_preview.js

Issue 7408004: Printer dropdown cleanup (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review related changes Created 9 years, 5 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 | Annotate | Revision Log
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 // require: cr/ui/print_preview_cloud.js 5 // require: cr/ui/print_preview_cloud.js
6 6
7 var localStrings = new LocalStrings(); 7 var localStrings = new LocalStrings();
8 8
9 // If useCloudPrint is true we attempt to connect to cloud print 9 // If useCloudPrint is true we attempt to connect to cloud print
10 // and populate the list of printers with cloud print printers. 10 // and populate the list of printers with cloud print printers.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 var copiesSettings; 49 var copiesSettings;
50 50
51 // Object holding all the layout related settings. 51 // Object holding all the layout related settings.
52 var layoutSettings; 52 var layoutSettings;
53 53
54 // True if the user has click 'Advanced...' in order to open the system print 54 // True if the user has click 'Advanced...' in order to open the system print
55 // dialog. 55 // dialog.
56 var showingSystemDialog = false; 56 var showingSystemDialog = false;
57 57
58 // The range of options in the printer dropdown controlled by cloud print. 58 // The range of options in the printer dropdown controlled by cloud print.
59 var firstCloudPrintOptionPos = 0 59 var firstCloudPrintOptionPos = 0;
60 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; 60 var lastCloudPrintOptionPos = firstCloudPrintOptionPos;
61 61
62
63 //TODO(abodenha@chromium.org) A lot of cloud print specific logic has
dpapad 2011/07/19 22:22:58 Nit: space after //.
Albert Bodenhamer 2011/07/19 22:43:48 Done.
64 // made its way into this file. Refactor to create a cleaner boundary
65 // between print preview and GCP code.
66
67 // A dictionary of cloud printers that have been added to the printer
68 // dropdown.
69 var addedCloudPrinters = {};
70
71 // The maximum number of cloud printers to allow in the dropdown.
72 const maxCloudPrinters = 10;
73
74
62 /** 75 /**
63 * Window onload handler, sets up the page and starts print preview by getting 76 * Window onload handler, sets up the page and starts print preview by getting
64 * the printer list. 77 * the printer list.
65 */ 78 */
66 function onLoad() { 79 function onLoad() {
67 cr.enablePlatformSpecificCSSRules(); 80 cr.enablePlatformSpecificCSSRules();
68 81
69 $('cancel-button').addEventListener('click', handleCancelButtonClick); 82 $('cancel-button').addEventListener('click', handleCancelButtonClick);
70 83
71 if (!checkCompatiblePluginExists()) { 84 if (!checkCompatiblePluginExists()) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 if (cloudprint.isCloudPrint(printerList.options[selectedIndex])) { 199 if (cloudprint.isCloudPrint(printerList.options[selectedIndex])) {
187 updateWithCloudPrinterCapabilities(); 200 updateWithCloudPrinterCapabilities();
188 skip_refresh = true; 201 skip_refresh = true;
189 } else if (selectedValue == SIGN_IN || 202 } else if (selectedValue == SIGN_IN ||
190 selectedValue == MANAGE_CLOUD_PRINTERS || 203 selectedValue == MANAGE_CLOUD_PRINTERS ||
191 selectedValue == MANAGE_LOCAL_PRINTERS || 204 selectedValue == MANAGE_LOCAL_PRINTERS ||
192 selectedValue == ADD_CLOUD_PRINTER) { 205 selectedValue == ADD_CLOUD_PRINTER) {
193 printerList.selectedIndex = lastSelectedPrinterIndex; 206 printerList.selectedIndex = lastSelectedPrinterIndex;
194 chrome.send(selectedValue); 207 chrome.send(selectedValue);
195 skip_refresh = true; 208 skip_refresh = true;
209 } else if (selectedValue == MORE_PRINTERS) {
210 onSystemDialogLinkClicked();
211 skip_refresh = true;
196 } else if (selectedValue == PRINT_TO_PDF) { 212 } else if (selectedValue == PRINT_TO_PDF) {
197 updateWithPrinterCapabilities({ 213 updateWithPrinterCapabilities({
198 'disableColorOption': true, 214 'disableColorOption': true,
199 'setColorAsDefault': true, 215 'setColorAsDefault': true,
200 'setDuplexAsDefault': false, 216 'setDuplexAsDefault': false,
201 'disableCopiesOption': true}); 217 'disableCopiesOption': true});
202 } else { 218 } else {
203 // This message will call back to 'updateWithPrinterCapabilities' 219 // This message will call back to 'updateWithPrinterCapabilities'
204 // function. 220 // function.
205 chrome.send('getPrinterCapabilities', [selectedValue]); 221 chrome.send('getPrinterCapabilities', [selectedValue]);
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 } 349 }
334 350
335 /** 351 /**
336 * @return {number} The next unused preview request id. 352 * @return {number} The next unused preview request id.
337 */ 353 */
338 function generatePreviewRequestID() { 354 function generatePreviewRequestID() {
339 return ++lastPreviewRequestID; 355 return ++lastPreviewRequestID;
340 } 356 }
341 357
342 /** 358 /**
359 * @return {boolean} True iff a preview has been requested.
360 */
361 function hasRequestedPreview() {
362 return lastPreviewRequestID > -1;
363 }
364
365
dpapad 2011/07/19 22:22:58 Nit: 1 blank line between functions.
Albert Bodenhamer 2011/07/19 22:43:48 Done.
366 /**
343 * Returns the name of the selected printer or the empty string if no 367 * Returns the name of the selected printer or the empty string if no
344 * printer is selected. 368 * printer is selected.
345 * @return {string} The name of the currently selected printer. 369 * @return {string} The name of the currently selected printer.
346 */ 370 */
347 function getSelectedPrinterName() { 371 function getSelectedPrinterName() {
348 var printerList = $('printer-list') 372 var printerList = $('printer-list')
349 var selectedPrinter = printerList.selectedIndex; 373 var selectedPrinter = printerList.selectedIndex;
350 if (selectedPrinter < 0) 374 if (selectedPrinter < 0)
351 return ''; 375 return '';
352 return printerList.options[selectedPrinter].value; 376 return printerList.options[selectedPrinter].value;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 // TODO(thestig) re-enable controls here. 463 // TODO(thestig) re-enable controls here.
440 } 464 }
441 465
442 /** 466 /**
443 * Set the default printer. If there is one, generate a print preview. 467 * Set the default printer. If there is one, generate a print preview.
444 * @param {string} printer Name of the default printer. Empty if none. 468 * @param {string} printer Name of the default printer. Empty if none.
445 * @param {string} cloudPrintData Cloud print related data to restore if 469 * @param {string} cloudPrintData Cloud print related data to restore if
446 * the default printer is a cloud printer. 470 * the default printer is a cloud printer.
447 */ 471 */
448 function setDefaultPrinter(printer_name, cloudPrintData) { 472 function setDefaultPrinter(printer_name, cloudPrintData) {
449 // Add a placeholder value so the printer list looks valid.
450 addDestinationListOption('', '', true, true, true);
451 if (printer_name) { 473 if (printer_name) {
452 defaultOrLastUsedPrinterName = printer_name; 474 defaultOrLastUsedPrinterName = printer_name;
453 if (cloudPrintData) { 475 if (cloudPrintData) {
454 cloudprint.setDefaultPrinter(printer_name, 476 cloudprint.setDefaultPrinter(printer_name,
455 cloudPrintData, 477 cloudPrintData,
456 addCloudPrinters, 478 addCloudPrinters,
457 doUpdateCloudPrinterCapabilities); 479 doUpdateCloudPrinterCapabilities);
458 } else { 480 } else {
459 $('printer-list')[0].value = defaultOrLastUsedPrinterName; 481 addDestinationListOption('',
482 defaultOrLastUsedPrinterName,
483 true,
484 true,
485 true);
460 updateControlsWithSelectedPrinterCapabilities(); 486 updateControlsWithSelectedPrinterCapabilities();
461 } 487 }
462 } 488 }
463 chrome.send('getPrinters'); 489 chrome.send('getPrinters');
464 } 490 }
465 491
466 /** 492 /**
467 * Fill the printer list drop down. 493 * Fill the printer list drop down.
468 * Called from PrintPreviewHandler::SendPrinterList(). 494 * Called from PrintPreviewHandler::SendPrinterList().
469 * @param {Array} printers Array of printer info objects. 495 * @param {Array} printers Array of printer info objects.
470 */ 496 */
471 function setPrinters(printers) { 497 function setPrinters(printers) {
472 var printerList = $('printer-list');
473 // If there exists a dummy printer value, then setDefaultPrinter() already
474 // requested a preview, so no need to do it again.
475 var needPreview = (printerList[0].value == '');
476 for (var i = 0; i < printers.length; ++i) { 498 for (var i = 0; i < printers.length; ++i) {
477 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName); 499 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName);
478 addDestinationListOption(printers[i].printerName, printers[i].deviceName, 500 addDestinationListOption(printers[i].printerName, printers[i].deviceName,
479 isDefault, false, false); 501 isDefault, false, false);
480 } 502 }
481 503
482 if (!cloudprint.isCloudPrint(printerList[0]))
483 // Remove the dummy printer added in setDefaultPrinter().
484 printerList.remove(0);
485
486 if (printers.length != 0) 504 if (printers.length != 0)
487 addDestinationListOption('', '', false, true, true); 505 addDestinationListOption('', '', false, true, true);
488 506
489 // Adding option for saving PDF to disk. 507 // Adding option for saving PDF to disk.
490 addDestinationListOption(localStrings.getString('printToPDF'), 508 addDestinationListOption(localStrings.getString('printToPDF'),
491 PRINT_TO_PDF, 509 PRINT_TO_PDF,
492 defaultOrLastUsedPrinterName == PRINT_TO_PDF, 510 defaultOrLastUsedPrinterName == PRINT_TO_PDF,
493 false, 511 false,
494 false); 512 false);
495 addDestinationListOption('', '', false, true, true); 513 addDestinationListOption('', '', false, true, true);
496 514
497 // Add options to manage printers. 515 // Add options to manage printers.
498 if (!cr.isChromeOS) { 516 if (!cr.isChromeOS) {
499 addDestinationListOption(localStrings.getString('manageLocalPrinters'), 517 addDestinationListOption(localStrings.getString('manageLocalPrinters'),
500 MANAGE_LOCAL_PRINTERS, false, false, false); 518 MANAGE_LOCAL_PRINTERS, false, false, false);
501 } 519 }
502 if (useCloudPrint) { 520 if (useCloudPrint) {
521 // Fetch recent printers.
503 cloudprint.fetchPrinters(addCloudPrinters, false); 522 cloudprint.fetchPrinters(addCloudPrinters, false);
523 // Fetch the full printer list.
524 cloudprint.fetchPrinters(addCloudPrinters, true);
504 addDestinationListOption(localStrings.getString('manageCloudPrinters'), 525 addDestinationListOption(localStrings.getString('manageCloudPrinters'),
505 MANAGE_CLOUD_PRINTERS, false, false, false); 526 MANAGE_CLOUD_PRINTERS, false, false, false);
506 } 527 }
507 528
508 printerList.disabled = false; 529 $('printer-list').disabled = false;
509 530
510 if (needPreview) 531 if (!hasRequestedPreview())
511 updateControlsWithSelectedPrinterCapabilities(); 532 updateControlsWithSelectedPrinterCapabilities();
512 } 533 }
513 534
514 /** 535 /**
515 * Creates an option that can be added to the printer destination list. 536 * Creates an option that can be added to the printer destination list.
516 * @param {String} optionText specifies the option text content. 537 * @param {String} optionText specifies the option text content.
517 * @param {String} optionValue specifies the option value. 538 * @param {String} optionValue specifies the option value.
518 * @param {boolean} isDefault is true if the option needs to be selected. 539 * @param {boolean} isDefault is true if the option needs to be selected.
519 * @param {boolean} isDisabled is true if the option needs to be disabled. 540 * @param {boolean} isDisabled is true if the option needs to be disabled.
520 * @param {boolean} isSeparator is true if the option is a visual separator and 541 * @param {boolean} isSeparator is true if the option is a visual separator and
521 * needs to be disabled. 542 * needs to be disabled.
522 * @return {Object} The created option. 543 * @return {Object} The created option.
523 */ 544 */
524 function createDestinationListOption(optionText, optionValue, isDefault, 545 function createDestinationListOption(optionText, optionValue, isDefault,
525 isDisabled, isSeparator) { 546 isDisabled, isSeparator) {
526 var option = document.createElement('option'); 547 var option = document.createElement('option');
527 option.textContent = optionText; 548 option.textContent = optionText;
528 option.value = optionValue; 549 option.value = optionValue;
529 option.selected = isDefault; 550 option.selected = isDefault;
530 option.disabled = isSeparator || isDisabled; 551 option.disabled = isSeparator || isDisabled;
531 // Adding attribute for improved accessibility. 552 // Adding attribute for improved accessibility.
(...skipping 16 matching lines...) Expand all
548 optionValue, 569 optionValue,
549 isDefault, 570 isDefault,
550 isDisabled, 571 isDisabled,
551 isSeparator); 572 isSeparator);
552 $('printer-list').add(option); 573 $('printer-list').add(option);
553 return option; 574 return option;
554 } 575 }
555 576
556 /** 577 /**
557 * Adds an option to the printer destination list at a specified position. 578 * Adds an option to the printer destination list at a specified position.
558 * @param {Integer} position The index in the printer-list wher the option 579 * @param {Integer} position The index in the printer-list wher the option
dpapad 2011/07/19 22:22:58 Nit: @param {number}, there is no type Integer in
Albert Bodenhamer 2011/07/19 22:43:48 Done.
559 should be created. 580 should be created.
560 * @param {String} optionText specifies the option text content. 581 * @param {String} optionText specifies the option text content.
dpapad 2011/07/19 22:22:58 Nit optional: Just for consistency (within this fi
Albert Bodenhamer 2011/07/19 22:43:48 Done.
561 * @param {String} optionValue specifies the option value. 582 * @param {String} optionValue specifies the option value.
562 * @param {boolean} isDefault is true if the option needs to be selected. 583 * @param {boolean} isDefault is true if the option needs to be selected.
563 * @param {boolean} isDisabled is true if the option needs to be disabled. 584 * @param {boolean} isDisabled is true if the option needs to be disabled.
564 * @return {Object} The created option. 585 * @return {Object} The created option.
565 */ 586 */
566 function addDestinationListOptionAtPosition(position, 587 function addDestinationListOptionAtPosition(position,
567 optionText, 588 optionText,
568 optionValue, 589 optionValue,
569 isDefault, 590 isDefault,
570 isDisabled, 591 isDisabled,
571 isSeparator) { 592 isSeparator) {
572 var option = createDestinationListOption(optionText, 593 var option = createDestinationListOption(optionText,
573 optionValue, 594 optionValue,
574 isDefault, 595 isDefault,
575 isDisabled, 596 isDisabled,
576 isSeparator); 597 isSeparator);
577 var printerList = $('printer-list'); 598 var printerList = $('printer-list');
578 var before = printerList[position]; 599 var before = printerList[position];
579 printerList.add(option, before); 600 printerList.add(option, before);
580 return option; 601 return option;
581 } 602 }
582 603
583 /** 604 /**
584 * Removes the list of cloud printers from the printer pull down. 605 * Test if a particular cloud printer has already been added to the
606 * printer dropdown.
607 * @param {String} id A unique value to track this printer.
608 * @return {boolean} Returns true if this id has previously
609 * been passed to trackCloudPrinterAdded.
585 */ 610 */
586 function clearCloudPrinters() { 611 function cloudPrinterAlreadyAdded(id) {
587 var printerList = $('printer-list'); 612 return (addedCloudPrinters[id]);
588 while (firstCloudPrintOptionPos < lastCloudPrintOptionPos) { 613 }
589 lastCloudPrintOptionPos--; 614
590 printerList.remove(lastCloudPrintOptionPos); 615 /**
616 * Record that a cloud printer will added to the printer dropdown.
617 * @param {String} id A unique value to track this printer.
618 * @return {boolean} Returns false if there adding this printer
dpapad 2011/07/19 22:22:58 Change to "Returns false if adding this printer".
Albert Bodenhamer 2011/07/19 22:43:48 Done.
619 * would exceed maxCloudPrinters.
dpapad 2011/07/19 22:22:58 Nit: s/maxCloudPrinters/|maxCloudPrinters|
Albert Bodenhamer 2011/07/19 22:43:48 Done.
620 */
621 function trackCloudPrinterAdded(id) {
622 if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) {
623 addedCloudPrinters[id] = true;
624 return true;
625 } else {
626 return false;
591 } 627 }
592 } 628 }
593 629
630
594 /** 631 /**
595 * Add cloud printers to the list drop down. 632 * Add cloud printers to the list drop down.
596 * Called from the cloudprint object on receipt of printer information from the 633 * Called from the cloudprint object on receipt of printer information from the
597 * cloud print server. 634 * cloud print server.
598 * @param {Array} printers Array of printer info objects. 635 * @param {Array} printers Array of printer info objects.
599 * @return {Object} The currently selected printer. 636 * @return {Object} The currently selected printer.
600 */ 637 */
601 function addCloudPrinters(printers, showMorePrintersOption) { 638 function addCloudPrinters(printers) {
602 // TODO(abodenha@chromium.org) Avoid removing printers from the list. 639 var isFirstPass = false;
603 // Instead search for duplicates and don't add printers that already exist 640 var showMorePrintersOption = false;
604 // in the list. 641
605 clearCloudPrinters(); 642 if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) {
606 addDestinationListOptionAtPosition( 643 isFirstPass = true;
607 lastCloudPrintOptionPos++, 644 var option = addDestinationListOptionAtPosition(
608 localStrings.getString('cloudPrinters'), 645 lastCloudPrintOptionPos++,
609 '', 646 localStrings.getString('cloudPrinters'),
610 false, 647 'Label',
611 true, 648 false,
612 false); 649 true,
650 false);
651 cloudprint.setCloudPrint(option, null, null);
652 }
613 if (printers != null) { 653 if (printers != null) {
614 if (printers.length == 0) { 654 if (printers.length == 0) {
615 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 655 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
616 localStrings.getString('addCloudPrinter'), 656 localStrings.getString('addCloudPrinter'),
617 ADD_CLOUD_PRINTER, 657 ADD_CLOUD_PRINTER,
618 false, 658 false,
619 false, 659 false,
620 false); 660 false);
621 } else { 661 } else {
622 for (printer in printers) { 662 for (var printer = 0; printer < printers.length; printer++) {
dpapad 2011/07/19 22:22:58 Nit optional: Using a counter like "i" seems a bit
Albert Bodenhamer 2011/07/19 22:43:48 Done.
623 var option = addDestinationListOptionAtPosition( 663 if (!cloudPrinterAlreadyAdded(printers[printer]['id'])) {
624 lastCloudPrintOptionPos++, 664 var option = addDestinationListOptionAtPosition(
625 printers[printer]['name'], 665 lastCloudPrintOptionPos++,
626 printers[printer]['id'], 666 printers[printer]['name'],
627 printers[printer]['name'] == defaultOrLastUsedPrinterName, 667 printers[printer]['id'],
628 false, 668 printers[printer]['name'] == defaultOrLastUsedPrinterName,
629 false); 669 false,
630 cloudprint.setCloudPrint(option, 670 false);
631 printers[printer]['name'], 671 cloudprint.setCloudPrint(option,
632 printers[printer]['id']); 672 printers[printer]['name'],
673 printers[printer]['id']);
674 if (!trackCloudPrinterAdded(printers[printer]['id'])) {
675 showMorePrintersOption = true;
676 break;
677 }
678 }
633 } 679 }
634 if (showMorePrintersOption) { 680 if (showMorePrintersOption) {
635 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 681 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
636 '', 682 '',
637 '', 683 '',
638 false, 684 false,
639 true, 685 true,
640 true); 686 true);
641 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 687 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
642 localStrings.getString('morePrinters'), 688 localStrings.getString('morePrinters'),
643 MORE_PRINTERS, false, false, false); 689 MORE_PRINTERS, false, false, false);
644 } 690 }
645 } 691 }
646 } else { 692 } else {
647 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 693 if (!cloudPrinterAlreadyAdded(SIGN_IN)) {
648 localStrings.getString('signIn'), 694 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
649 SIGN_IN, 695 localStrings.getString('signIn'),
696 SIGN_IN,
697 false,
698 false,
699 false);
700 trackCloudPrinterAdded(SIGN_IN);
701 }
702 }
703 if (isFirstPass) {
704 addDestinationListOptionAtPosition(lastCloudPrintOptionPos,
705 '',
706 '',
650 false, 707 false,
708 true,
709 true);
710 addDestinationListOptionAtPosition(lastCloudPrintOptionPos + 1,
711 localStrings.getString('localPrinters'),
712 '',
651 false, 713 false,
714 true,
652 false); 715 false);
653 } 716 }
654 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
655 '',
656 '',
657 false,
658 true,
659 true);
660 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
661 localStrings.getString('localPrinters'),
662 '',
663 false,
664 true,
665 false);
666 var printerList = $('printer-list') 717 var printerList = $('printer-list')
667 var selectedPrinter = printerList.selectedIndex; 718 var selectedPrinter = printerList.selectedIndex;
668 if (selectedPrinter < 0) 719 if (selectedPrinter < 0)
669 return null; 720 return null;
670 return printerList.options[selectedPrinter]; 721 return printerList.options[selectedPrinter];
671 } 722 }
672 723
673 /** 724 /**
674 * Sets the color mode for the PDF plugin. 725 * Sets the color mode for the PDF plugin.
675 * Called from PrintPreviewHandler::ProcessColorSetting(). 726 * Called from PrintPreviewHandler::ProcessColorSetting().
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 } 999 }
949 1000
950 /** 1001 /**
951 * Takes a snapshot of the print settings. 1002 * Takes a snapshot of the print settings.
952 */ 1003 */
953 PrintSettings.prototype.save = function() { 1004 PrintSettings.prototype.save = function() {
954 this.deviceName = getSelectedPrinterName(); 1005 this.deviceName = getSelectedPrinterName();
955 this.isLandscape = layoutSettings.isLandscape(); 1006 this.isLandscape = layoutSettings.isLandscape();
956 } 1007 }
957 1008
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698