summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/printing_page/cups_printers_entry_list.js
blob: 3fe5f69bd124b98a947f1e98e13e8340fa6084dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview 'settings-cups-printers-entry-list' is a component for a list
 * of PrinterListEntry's.
 */
Polymer({
  is: 'settings-cups-printers-entry-list',

  behaviors: [
    ListPropertyUpdateBehavior,
  ],

  properties: {
    /**
     * List of printers.
     * @type {!Array<!PrinterListEntry>}
     */
    printers: {
      type: Array,
      value: () => [],
    },

    /**
     * List of printers filtered through a search term.
     * @type {!Array<!PrinterListEntry>}
     * @private
     */
    filteredPrinters_: {
      type: Array,
      value: () => [],
    },

    /**
     * Search term for filtering |printers|.
     * @type {string}
     */
    searchTerm: {
      type: String,
      value: '',
    },

    /**
     * Whether to show the no search results message.
     * @type {boolean}
     * @private
     */
     showNoSearchResultsMessage_: {
      type: Boolean,
      value: false,
    },
  },

  observers: [
    'onSearchChanged_(printers.*, searchTerm)'
  ],

  /**
   * Redoes the search whenever |searchTerm| or |printers| changes.
   * @private
   */
  onSearchChanged_: function() {
    if (!this.printers) {
      return;
    }
    // Filter printers through |searchTerm|. If |searchTerm| is empty,
    // |filteredPrinters_| is just |printers|.
    const updatedPrinters = this.searchTerm ?
        this.printers.filter(
            item =>this.matchesSearchTerm_(item.printerInfo,this.searchTerm)) :
        this.printers.slice();

    updatedPrinters.sort(this.sortPrinters_);

    this.updateList('filteredPrinters_', printer => printer.printerInfo,
        updatedPrinters);

    this.showNoSearchResultsMessage_ =
        !!this.searchTerm && !this.filteredPrinters_.length;
  },


  /**
   * @param {!PrinterListEntry} first
   * @param {!PrinterListEntry} second
   * @return {number}
   * @private
   */
  sortPrinters_: function(first, second) {
    if (first.printerType == second.printerType) {
      return settings.printing.alphabeticalSort(
          first.printerInfo, second.printerInfo);
    }

    // PrinterType sort order maintained in cups_printer_types.js
    return first.printerType - second.printerType;
  },

  /**
   * @param {!CupsPrinterInfo} printer
   * @param {string} searchTerm
   * @return {boolean} True if the printer has |searchTerm| in its name.
   * @private
   */
  matchesSearchTerm_: function(printer, searchTerm) {
    return printer.printerName.toLowerCase().includes(searchTerm.toLowerCase());
  }
});