summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/extensions/item_list.js
blob: 04f8df0dfd5d7cc708acbb52174879420d516cc7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015 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.

cr.define('extensions', function() {
  const ItemList = Polymer({
    is: 'extensions-item-list',

    behaviors: [CrContainerShadowBehavior, I18nBehavior],

    properties: {
      /** @type {!Array<!chrome.developerPrivate.ExtensionInfo>} */
      apps: Array,

      /** @type {!Array<!chrome.developerPrivate.ExtensionInfo>} */
      extensions: Array,

      /** @type {extensions.ItemDelegate} */
      delegate: Object,

      inDevMode: {
        type: Boolean,
        value: false,
      },

      filter: {
        type: String,
      },

      /** @private */
      computedFilter_: {
        type: String,
        computed: 'computeFilter_(filter)',
        observer: 'announceSearchResults_',
      },

      /** @private */
      shownExtensionsCount_: {
        type: Number,
        value: 0,
      },

      /** @private */
      shownAppsCount_: {
        type: Number,
        value: 0,
      },
    },

    /**
     * @param {string} id
     * @return {?Element}
     */
    getDetailsButton: function(id) {
      const item = this.$$(`#${id}`);
      return item && item.getDetailsButton();
    },

    /**
     * @param {string} id
     * @return {?Element}
     */
    getErrorsButton: function(id) {
      const item = this.$$(`#${id}`);
      return item && item.getErrorsButton();
    },

    /**
     * Computes the filter function to be used for determining which items
     * should be shown. A |null| value indicates that everything should be
     * shown.
     * return {?Function}
     * @private
     */
    computeFilter_: function() {
      const formattedFilter = this.filter.trim().toLowerCase();
      return formattedFilter ?
          i => i.name.toLowerCase().includes(formattedFilter) :
          null;
    },

    /** @private */
    shouldShowEmptyItemsMessage_: function() {
      if (!this.apps || !this.extensions) {
        return;
      }

      return this.apps.length === 0 && this.extensions.length === 0;
    },

    /** @private */
    shouldShowEmptySearchMessage_: function() {
      return !this.shouldShowEmptyItemsMessage_() &&
          this.shownAppsCount_ === 0 && this.shownExtensionsCount_ === 0;
    },

    /** @private */
    onNoExtensionsTap_: function(e) {
      if (e.target.tagName == 'A') {
        chrome.metricsPrivate.recordUserAction('Options_GetMoreExtensions');
      }
    },

    /** @private */
    announceSearchResults_: function() {
      if (this.computedFilter_) {
        Polymer.IronA11yAnnouncer.requestAvailability();
        this.async(() => {  // Async to allow list to update.
          const total = this.shownAppsCount_ + this.shownExtensionsCount_;
          this.fire('iron-announce', {
            text: this.shouldShowEmptySearchMessage_() ?
                this.i18n('noSearchResults') :
                (total == 1 ?
                     this.i18n('searchResultsSingular', this.filter) :
                     this.i18n(
                         'searchResultsPlural', total.toString(), this.filter)),
          });
        });
      }
    },
  });

  return {
    ItemList: ItemList,
  };
});