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

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: Fixed issue with signing appearing twice 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.
11 var useCloudPrint = false; 11 var useCloudPrint = false;
12 12
13 // Store the last selected printer index. 13 // Store the last selected printer index.
14 var lastSelectedPrinterIndex = 0; 14 var lastSelectedPrinterIndex = 0;
15 15
16 // Used to disable some printing options when the preview is not modifiable. 16 // Used to disable some printing options when the preview is not modifiable.
17 var previewModifiable = false; 17 var previewModifiable = false;
18 18
19 // True if a request to generate a preview has been sent to the browser in
20 // this session.
21 var previewRequested = false;
dpapad 2011/07/19 18:44:55 Instead of adding another state variable, you coul
Albert Bodenhamer 2011/07/19 21:53:19 Done.
22
19 // Destination list special value constants. 23 // Destination list special value constants.
20 const ADD_CLOUD_PRINTER = 'addCloudPrinter'; 24 const ADD_CLOUD_PRINTER = 'addCloudPrinter';
21 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; 25 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters';
22 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; 26 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters';
23 const MORE_PRINTERS = 'morePrinters'; 27 const MORE_PRINTERS = 'morePrinters';
24 const SIGN_IN = 'signIn'; 28 const SIGN_IN = 'signIn';
25 const PRINT_TO_PDF = 'Print To PDF'; 29 const PRINT_TO_PDF = 'Print To PDF';
26 30
27 // State of the print preview settings. 31 // State of the print preview settings.
28 var printSettings = new PrintSettings(); 32 var printSettings = new PrintSettings();
(...skipping 20 matching lines...) Expand all
49 var copiesSettings; 53 var copiesSettings;
50 54
51 // Object holding all the layout related settings. 55 // Object holding all the layout related settings.
52 var layoutSettings; 56 var layoutSettings;
53 57
54 // True if the user has click 'Advanced...' in order to open the system print 58 // True if the user has click 'Advanced...' in order to open the system print
55 // dialog. 59 // dialog.
56 var showingSystemDialog = false; 60 var showingSystemDialog = false;
57 61
58 // The range of options in the printer dropdown controlled by cloud print. 62 // The range of options in the printer dropdown controlled by cloud print.
59 var firstCloudPrintOptionPos = 0 63 var firstCloudPrintOptionPos = 0;
60 var lastCloudPrintOptionPos = firstCloudPrintOptionPos; 64 var lastCloudPrintOptionPos = firstCloudPrintOptionPos;
61 65
66 // A dictionary of cloud printers that have been added to the printer
67 // dropdown.
68 var addedCloudPrinters = {};
dpapad 2011/07/19 18:44:55 Could these be moved within print_preview_cloud.js
Albert Bodenhamer 2011/07/19 21:53:19 The boundary has definitely gotten muddy as things
dpapad 2011/07/19 22:22:58 The UI logic has migrated (to some degree) outside
69
70 // The maximum number of cloud printers to allow in the dropdown.
71 const maxCloudPrinters = 10;
72
73
62 /** 74 /**
63 * Window onload handler, sets up the page and starts print preview by getting 75 * Window onload handler, sets up the page and starts print preview by getting
64 * the printer list. 76 * the printer list.
65 */ 77 */
66 function onLoad() { 78 function onLoad() {
67 cr.enablePlatformSpecificCSSRules(); 79 cr.enablePlatformSpecificCSSRules();
68 80
69 $('cancel-button').addEventListener('click', handleCancelButtonClick); 81 $('cancel-button').addEventListener('click', handleCancelButtonClick);
70 82
71 if (!checkCompatiblePluginExists()) { 83 if (!checkCompatiblePluginExists()) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 if (cloudprint.isCloudPrint(printerList.options[selectedIndex])) { 198 if (cloudprint.isCloudPrint(printerList.options[selectedIndex])) {
187 updateWithCloudPrinterCapabilities(); 199 updateWithCloudPrinterCapabilities();
188 skip_refresh = true; 200 skip_refresh = true;
189 } else if (selectedValue == SIGN_IN || 201 } else if (selectedValue == SIGN_IN ||
190 selectedValue == MANAGE_CLOUD_PRINTERS || 202 selectedValue == MANAGE_CLOUD_PRINTERS ||
191 selectedValue == MANAGE_LOCAL_PRINTERS || 203 selectedValue == MANAGE_LOCAL_PRINTERS ||
192 selectedValue == ADD_CLOUD_PRINTER) { 204 selectedValue == ADD_CLOUD_PRINTER) {
193 printerList.selectedIndex = lastSelectedPrinterIndex; 205 printerList.selectedIndex = lastSelectedPrinterIndex;
194 chrome.send(selectedValue); 206 chrome.send(selectedValue);
195 skip_refresh = true; 207 skip_refresh = true;
208 } else if (selectedValue == MORE_PRINTERS) {
209 onSystemDialogLinkClicked();
210 skip_refresh = true;
196 } else if (selectedValue == PRINT_TO_PDF) { 211 } else if (selectedValue == PRINT_TO_PDF) {
197 updateWithPrinterCapabilities({ 212 updateWithPrinterCapabilities({
198 'disableColorOption': true, 213 'disableColorOption': true,
199 'setColorAsDefault': true, 214 'setColorAsDefault': true,
200 'setDuplexAsDefault': false, 215 'setDuplexAsDefault': false,
201 'disableCopiesOption': true}); 216 'disableCopiesOption': true});
202 } else { 217 } else {
203 // This message will call back to 'updateWithPrinterCapabilities' 218 // This message will call back to 'updateWithPrinterCapabilities'
204 // function. 219 // function.
205 chrome.send('getPrinterCapabilities', [selectedValue]); 220 chrome.send('getPrinterCapabilities', [selectedValue]);
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 */ 436 */
422 function requestPrintPreview() { 437 function requestPrintPreview() {
423 hasPendingPreviewRequest = true; 438 hasPendingPreviewRequest = true;
424 removeEventListeners(); 439 removeEventListeners();
425 printSettings.save(); 440 printSettings.save();
426 if (!isTabHidden) 441 if (!isTabHidden)
427 showLoadingAnimation(); 442 showLoadingAnimation();
428 443
429 var settings = getSettings(); 444 var settings = getSettings();
430 settings.requestID = generatePreviewRequestID(); 445 settings.requestID = generatePreviewRequestID();
446 previewRequested = true;
431 chrome.send('getPreview', [JSON.stringify(settings)]); 447 chrome.send('getPreview', [JSON.stringify(settings)]);
432 } 448 }
433 449
434 /** 450 /**
435 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print 451 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
436 * preview tab regarding the file selection cancel event. 452 * preview tab regarding the file selection cancel event.
437 */ 453 */
438 function fileSelectionCancelled() { 454 function fileSelectionCancelled() {
439 // TODO(thestig) re-enable controls here. 455 // TODO(thestig) re-enable controls here.
440 } 456 }
441 457
442 /** 458 /**
443 * Set the default printer. If there is one, generate a print preview. 459 * Set the default printer. If there is one, generate a print preview.
444 * @param {string} printer Name of the default printer. Empty if none. 460 * @param {string} printer Name of the default printer. Empty if none.
445 * @param {string} cloudPrintData Cloud print related data to restore if 461 * @param {string} cloudPrintData Cloud print related data to restore if
446 * the default printer is a cloud printer. 462 * the default printer is a cloud printer.
447 */ 463 */
448 function setDefaultPrinter(printer_name, cloudPrintData) { 464 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) { 465 if (printer_name) {
452 defaultOrLastUsedPrinterName = printer_name; 466 defaultOrLastUsedPrinterName = printer_name;
453 if (cloudPrintData) { 467 if (cloudPrintData) {
454 cloudprint.setDefaultPrinter(printer_name, 468 cloudprint.setDefaultPrinter(printer_name,
455 cloudPrintData, 469 cloudPrintData,
456 addCloudPrinters, 470 addCloudPrinters,
457 doUpdateCloudPrinterCapabilities); 471 doUpdateCloudPrinterCapabilities);
458 } else { 472 } else {
459 $('printer-list')[0].value = defaultOrLastUsedPrinterName; 473 addDestinationListOption('',
474 defaultOrLastUsedPrinterName,
475 true,
476 true,
477 true);
460 updateControlsWithSelectedPrinterCapabilities(); 478 updateControlsWithSelectedPrinterCapabilities();
461 } 479 }
462 } 480 }
463 chrome.send('getPrinters'); 481 chrome.send('getPrinters');
464 } 482 }
465 483
466 /** 484 /**
467 * Fill the printer list drop down. 485 * Fill the printer list drop down.
468 * Called from PrintPreviewHandler::SendPrinterList(). 486 * Called from PrintPreviewHandler::SendPrinterList().
469 * @param {Array} printers Array of printer info objects. 487 * @param {Array} printers Array of printer info objects.
470 */ 488 */
471 function setPrinters(printers) { 489 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) { 490 for (var i = 0; i < printers.length; ++i) {
477 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName); 491 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName);
478 addDestinationListOption(printers[i].printerName, printers[i].deviceName, 492 addDestinationListOption(printers[i].printerName, printers[i].deviceName,
479 isDefault, false, false); 493 isDefault, false, false);
480 } 494 }
481 495
482 if (!cloudprint.isCloudPrint(printerList[0]))
483 // Remove the dummy printer added in setDefaultPrinter().
484 printerList.remove(0);
485
486 if (printers.length != 0) 496 if (printers.length != 0)
487 addDestinationListOption('', '', false, true, true); 497 addDestinationListOption('', '', false, true, true);
488 498
489 // Adding option for saving PDF to disk. 499 // Adding option for saving PDF to disk.
490 addDestinationListOption(localStrings.getString('printToPDF'), 500 addDestinationListOption(localStrings.getString('printToPDF'),
491 PRINT_TO_PDF, 501 PRINT_TO_PDF,
492 defaultOrLastUsedPrinterName == PRINT_TO_PDF, 502 defaultOrLastUsedPrinterName == PRINT_TO_PDF,
493 false, 503 false,
494 false); 504 false);
495 addDestinationListOption('', '', false, true, true); 505 addDestinationListOption('', '', false, true, true);
496 506
497 // Add options to manage printers. 507 // Add options to manage printers.
498 if (!cr.isChromeOS) { 508 if (!cr.isChromeOS) {
499 addDestinationListOption(localStrings.getString('manageLocalPrinters'), 509 addDestinationListOption(localStrings.getString('manageLocalPrinters'),
500 MANAGE_LOCAL_PRINTERS, false, false, false); 510 MANAGE_LOCAL_PRINTERS, false, false, false);
501 } 511 }
502 if (useCloudPrint) { 512 if (useCloudPrint) {
503 cloudprint.fetchPrinters(addCloudPrinters, false); 513 cloudprint.fetchPrinters(addCloudPrinters, false);
514 cloudprint.fetchPrinters(addCloudPrinters, true);
dpapad 2011/07/19 18:44:55 Why fetching printers twice? Is this for getting a
Albert Bodenhamer 2011/07/19 21:53:19 Yep. We fetch the recents and the full list separ
504 addDestinationListOption(localStrings.getString('manageCloudPrinters'), 515 addDestinationListOption(localStrings.getString('manageCloudPrinters'),
505 MANAGE_CLOUD_PRINTERS, false, false, false); 516 MANAGE_CLOUD_PRINTERS, false, false, false);
506 } 517 }
507 518
519 var printerList = $('printer-list');
dpapad 2011/07/19 18:44:55 Since $('printer-list') is only used once here, no
Albert Bodenhamer 2011/07/19 21:53:19 Done.
508 printerList.disabled = false; 520 printerList.disabled = false;
509 521
510 if (needPreview) 522 if (!previewRequested)
511 updateControlsWithSelectedPrinterCapabilities(); 523 updateControlsWithSelectedPrinterCapabilities();
512 } 524 }
513 525
514 /** 526 /**
515 * Creates an option that can be added to the printer destination list. 527 * Creates an option that can be added to the printer destination list.
516 * @param {String} optionText specifies the option text content. 528 * @param {String} optionText specifies the option text content.
517 * @param {String} optionValue specifies the option value. 529 * @param {String} optionValue specifies the option value.
518 * @param {boolean} isDefault is true if the option needs to be selected. 530 * @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. 531 * @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 532 * @param {boolean} isSeparator is true if the option is a visual separator and
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 isDefault, 586 isDefault,
575 isDisabled, 587 isDisabled,
576 isSeparator); 588 isSeparator);
577 var printerList = $('printer-list'); 589 var printerList = $('printer-list');
578 var before = printerList[position]; 590 var before = printerList[position];
579 printerList.add(option, before); 591 printerList.add(option, before);
580 return option; 592 return option;
581 } 593 }
582 594
583 /** 595 /**
584 * Removes the list of cloud printers from the printer pull down. 596 * Test if a particular cloud printer has already been added to the
597 * printer dropdown.
598 * @param {String} id - A unique value to track this printer.
dpapad 2011/07/19 18:44:55 Nit: No need for "-", just use a space between par
Albert Bodenhamer 2011/07/19 21:53:19 Done.
599 * @return {boolean} Returns true if this id has previously
600 * been passed to trackCloudPrinterAdded.
dpapad 2011/07/19 18:44:55 Nit: Use 4 space indent as usual, here and elsewhe
Albert Bodenhamer 2011/07/19 21:53:19 Done.
585 */ 601 */
586 function clearCloudPrinters() { 602 function cloudPrinterAlreadyAdded(id) {
587 var printerList = $('printer-list'); 603 return (addedCloudPrinters[id]);
588 while (firstCloudPrintOptionPos < lastCloudPrintOptionPos) { 604 }
589 lastCloudPrintOptionPos--; 605
590 printerList.remove(lastCloudPrintOptionPos); 606 /**
607 * Record that a cloud printer will added to the printer dropdown.
608 * @param {String} id - A unique value to track this printer.
609 * @return {boolean} Returns false if there adding this printer
610 * would exceed maxCloudPrinters.
611 */
612 function trackCloudPrinterAdded(id) {
613 if (Object.keys(addedCloudPrinters).length < maxCloudPrinters) {
614 addedCloudPrinters[id] = true;
615 return true;
616 } else {
617 return false;
591 } 618 }
592 } 619 }
593 620
621
594 /** 622 /**
595 * Add cloud printers to the list drop down. 623 * Add cloud printers to the list drop down.
596 * Called from the cloudprint object on receipt of printer information from the 624 * Called from the cloudprint object on receipt of printer information from the
597 * cloud print server. 625 * cloud print server.
598 * @param {Array} printers Array of printer info objects. 626 * @param {Array} printers Array of printer info objects.
599 * @return {Object} The currently selected printer. 627 * @return {Object} The currently selected printer.
600 */ 628 */
601 function addCloudPrinters(printers, showMorePrintersOption) { 629 function addCloudPrinters(printers) {
602 // TODO(abodenha@chromium.org) Avoid removing printers from the list. 630 var isFirstPass = false;
603 // Instead search for duplicates and don't add printers that already exist 631 var showMorePrintersOption = false;
604 // in the list. 632
605 clearCloudPrinters(); 633 if (firstCloudPrintOptionPos == lastCloudPrintOptionPos) {
606 addDestinationListOptionAtPosition( 634 isFirstPass = true;
607 lastCloudPrintOptionPos++, 635 var option = addDestinationListOptionAtPosition(
608 localStrings.getString('cloudPrinters'), 636 lastCloudPrintOptionPos++,
609 '', 637 localStrings.getString('cloudPrinters'),
610 false, 638 'Label',
611 true, 639 false,
612 false); 640 true,
641 false);
642 cloudprint.setCloudPrint(option, null, null);
643 }
613 if (printers != null) { 644 if (printers != null) {
614 if (printers.length == 0) { 645 if (printers.length == 0) {
615 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 646 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
616 localStrings.getString('addCloudPrinter'), 647 localStrings.getString('addCloudPrinter'),
617 ADD_CLOUD_PRINTER, 648 ADD_CLOUD_PRINTER,
618 false, 649 false,
619 false, 650 false,
620 false); 651 false);
621 } else { 652 } else {
622 for (printer in printers) { 653 for (printer in printers) {
dpapad 2011/07/19 18:44:55 Do not use for in statement with arrays, since it
Albert Bodenhamer 2011/07/19 21:53:19 Done.
623 var option = addDestinationListOptionAtPosition( 654 if (!cloudPrinterAlreadyAdded(printers[printer]['id'])) {
624 lastCloudPrintOptionPos++, 655 var option = addDestinationListOptionAtPosition(
625 printers[printer]['name'], 656 lastCloudPrintOptionPos++,
626 printers[printer]['id'], 657 printers[printer]['name'],
627 printers[printer]['name'] == defaultOrLastUsedPrinterName, 658 printers[printer]['id'],
628 false, 659 printers[printer]['name'] == defaultOrLastUsedPrinterName,
629 false); 660 false,
630 cloudprint.setCloudPrint(option, 661 false);
631 printers[printer]['name'], 662 cloudprint.setCloudPrint(option,
632 printers[printer]['id']); 663 printers[printer]['name'],
664 printers[printer]['id']);
665 if (!trackCloudPrinterAdded(printers[printer]['id'])) {
666 showMorePrintersOption = true;
667 break;
668 }
669 }
633 } 670 }
634 if (showMorePrintersOption) { 671 if (showMorePrintersOption) {
635 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 672 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
636 '', 673 '',
637 '', 674 '',
638 false, 675 false,
639 true, 676 true,
640 true); 677 true);
641 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 678 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
642 localStrings.getString('morePrinters'), 679 localStrings.getString('morePrinters'),
643 MORE_PRINTERS, false, false, false); 680 MORE_PRINTERS, false, false, false);
644 } 681 }
645 } 682 }
646 } else { 683 } else {
647 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++, 684 if (!cloudPrinterAlreadyAdded(SIGN_IN)) {
648 localStrings.getString('signIn'), 685 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
649 SIGN_IN, 686 localStrings.getString('signIn'),
687 SIGN_IN,
688 false,
689 false,
690 false);
691 trackCloudPrinterAdded(SIGN_IN);
692 }
693 }
694 if (isFirstPass) {
695 addDestinationListOptionAtPosition(lastCloudPrintOptionPos,
696 '',
697 '',
650 false, 698 false,
699 true,
700 true);
701 addDestinationListOptionAtPosition(lastCloudPrintOptionPos + 1,
702 localStrings.getString('localPrinters'),
703 '',
651 false, 704 false,
705 true,
652 false); 706 false);
653 } 707 }
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') 708 var printerList = $('printer-list')
667 var selectedPrinter = printerList.selectedIndex; 709 var selectedPrinter = printerList.selectedIndex;
668 if (selectedPrinter < 0) 710 if (selectedPrinter < 0)
669 return null; 711 return null;
670 return printerList.options[selectedPrinter]; 712 return printerList.options[selectedPrinter];
671 } 713 }
672 714
673 /** 715 /**
674 * Sets the color mode for the PDF plugin. 716 * Sets the color mode for the PDF plugin.
675 * Called from PrintPreviewHandler::ProcessColorSetting(). 717 * Called from PrintPreviewHandler::ProcessColorSetting().
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 } 990 }
949 991
950 /** 992 /**
951 * Takes a snapshot of the print settings. 993 * Takes a snapshot of the print settings.
952 */ 994 */
953 PrintSettings.prototype.save = function() { 995 PrintSettings.prototype.save = function() {
954 this.deviceName = getSelectedPrinterName(); 996 this.deviceName = getSelectedPrinterName();
955 this.isLandscape = layoutSettings.isLandscape(); 997 this.isLandscape = layoutSettings.isLandscape();
956 } 998 }
957 999
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698