diff options
Diffstat (limited to 'chromium/chrome/browser/resources/settings/printing_page/cups_nearby_printers.js')
-rw-r--r-- | chromium/chrome/browser/resources/settings/printing_page/cups_nearby_printers.js | 137 |
1 files changed, 90 insertions, 47 deletions
diff --git a/chromium/chrome/browser/resources/settings/printing_page/cups_nearby_printers.js b/chromium/chrome/browser/resources/settings/printing_page/cups_nearby_printers.js index aa4912c4010..f4020c2e390 100644 --- a/chromium/chrome/browser/resources/settings/printing_page/cups_nearby_printers.js +++ b/chromium/chrome/browser/resources/settings/printing_page/cups_nearby_printers.js @@ -9,20 +9,16 @@ Polymer({ is: 'settings-cups-nearby-printers', - behaviors: [WebUIListenerBehavior], + // ListPropertyUpdateBehavior is used in CupsPrintersEntryListBehavior. + behaviors: [ + CupsPrintersEntryListBehavior, + ListPropertyUpdateBehavior, + WebUIListenerBehavior, + ], properties: { /** - * @type {!Array<!PrinterListEntry>} - * @private - */ - nearbyPrinters_: { - type: Array, - value: () => [], - }, - - /** - * Search term for filtering |nearbyPrinters_|. + * Search term for filtering |nearbyPrinters|. * @type {string} */ searchTerm: { @@ -30,6 +26,12 @@ Polymer({ value: '', }, + /** @type {?CupsPrinterInfo} */ + activePrinter: { + type: Object, + notify: true, + }, + /** * @type {number} * @private @@ -39,48 +41,47 @@ Polymer({ value: -1, }, - /** @type {?CupsPrinterInfo} */ - activePrinter: { - type: Object, - notify: true, + /** + * List of printers filtered through a search term. + * @type {!Array<!PrinterListEntry>} + * @private + */ + filteredPrinters_: { + type: Array, + value: () => [], }, }, listeners: { 'add-automatic-printer': 'onAddAutomaticPrinter_', + 'query-discovered-printer': 'onQueryDiscoveredPrinter_', }, - /** @override */ - attached: function() { - settings.CupsPrintersBrowserProxyImpl.getInstance() - .startDiscoveringPrinters(); - this.addWebUIListener( - 'on-nearby-printers-changed', this.onNearbyPrintersChanged_.bind(this)); - }, + observers: [ + 'onSearchOrPrintersChanged_(nearbyPrinters.*, searchTerm)' + ], /** - * @param {!Array<!CupsPrinterInfo>} automaticPrinters - * @param {!Array<!CupsPrinterInfo>} discoveredPrinters + * Redoes the search whenever |searchTerm| or |nearbyPrinters| changes. * @private */ - onNearbyPrintersChanged_: function(automaticPrinters, discoveredPrinters) { - if (!automaticPrinters && !discoveredPrinters) { + onSearchOrPrintersChanged_: function() { + if (!this.nearbyPrinters) { return; } - - const printers = /** @type{!Array<!PrinterListEntry>} */ ([]); - - for (const printer of automaticPrinters) { - printers.push({printerInfo: printer, - printerType: PrinterType.AUTOMATIC}); - } - - for (const printer of discoveredPrinters) { - printers.push({printerInfo: printer, - printerType: PrinterType.DISCOVERED}); - } - - this.nearbyPrinters_ = printers; + // Filter printers through |searchTerm|. If |searchTerm| is empty, + // |filteredPrinters_| is just |nearbyPrinters|. + const updatedPrinters = this.searchTerm ? + this.nearbyPrinters.filter( + item => settings.printing.matchesSearchTerm( + item.printerInfo,this.searchTerm)) : + this.nearbyPrinters.slice(); + + updatedPrinters.sort(settings.printing.sortPrinters); + + this.updateList( + 'filteredPrinters_', printer => printer.printerInfo.printerId, + updatedPrinters); }, /** @@ -100,18 +101,38 @@ Polymer({ }, /** + * @param {!CustomEvent<{item: !PrinterListEntry}>} e + * @private + */ + onQueryDiscoveredPrinter_: function(e) { + const item = e.detail.item; + this.setActivePrinter_(item); + + // This is a workaround to ensure type safety on the params of the casted + // function. We do this because the closure compiler does not work well with + // rejected js promises. + const queryDiscoveredPrinterFailed = /** @type {!Function}) */( + this.onQueryDiscoveredPrinterFailed_.bind(this)); + settings.CupsPrintersBrowserProxyImpl.getInstance() + .addDiscoveredPrinter(item.printerInfo.printerId) + .then( + this.onQueryDiscoveredPrinterSucceeded_.bind(this, + item.printerInfo.printerName), + queryDiscoveredPrinterFailed); + }, + + /** * Retrieves the index of |item| in |nearbyPrinters_| and sets that printer as * the active printer. * @param {!PrinterListEntry} item * @private */ setActivePrinter_: function(item) { - this.activePrinterListEntryIndex_ = - this.nearbyPrinters_.findIndex( - printer => printer.printerInfo == item.printerInfo); + this.activePrinterListEntryIndex_ = this.nearbyPrinters.findIndex( + printer => printer.printerInfo.printerId == item.printerInfo.printerId); this.activePrinter = - this.get(['nearbyPrinters_', this.activePrinterListEntryIndex_]) + this.get(['nearbyPrinters', this.activePrinterListEntryIndex_]) .printerInfo; }, @@ -140,10 +161,32 @@ Polymer({ }, /** - * @return {boolean} Returns true if noPrinterMessage should be visible. + * Handler for queryDiscoveredPrinter success. + * @param {string} printerName + * @param {!PrinterSetupResult} result + * @private + */ + onQueryDiscoveredPrinterSucceeded_: function(printerName, result) { + this.fire( + 'show-cups-printer-toast', + {resultCode: result, printerName: printerName}); + }, + + /** + * Handler for queryDiscoveredPrinter failure. + * @param {!CupsPrinterInfo} printer + * @private + */ + onQueryDiscoveredPrinterFailed_: function(printer) { + this.fire('open-manufacturer-model-dialog-for-specified-printer', + {item: /** @type {CupsPrinterInfo} */(printer)}); + }, + + /** + * @return {boolean} Returns true if the no search message should be visible. * @private */ - shouldShowNoNearbyPrinterMessage_: function() { - return !this.searchTerm && !this.nearbyPrinters_.length; + showNoSearchResultsMessage_: function() { + return !!this.searchTerm && !this.filteredPrinters_.length; } });
\ No newline at end of file |