summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/downloads/toolbar.js
blob: 9d6413739b71440ad88eb3bd57b0f415d425299c (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
// 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.

import 'chrome://resources/cr_elements/cr_action_menu/cr_action_menu.m.js';
import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.m.js';
import 'chrome://resources/cr_elements/cr_toolbar/cr_toolbar.m.js';
import 'chrome://resources/cr_elements/hidden_style_css.m.js';
import 'chrome://resources/cr_elements/icons.m.js';
import 'chrome://resources/cr_elements/shared_vars_css.m.js';
import 'chrome://resources/js/util.m.js';
import 'chrome://resources/polymer/v3_0/paper-styles/color.js';
import './strings.m.js';

import {getToastManager} from 'chrome://resources/cr_elements/cr_toast/cr_toast_manager.m.js';
import {assert} from 'chrome://resources/js/assert.m.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import {BrowserProxy} from './browser_proxy.js';
import {Data} from './data.js';
import {PageHandlerInterface} from './downloads.mojom-webui.js';
import {SearchService} from './search_service.js';

Polymer({
  is: 'downloads-toolbar',

  _template: html`{__html_template__}`,

  properties: {
    hasClearableDownloads: {
      type: Boolean,
      value: false,
      observer: 'updateClearAll_',
    },

    /** @type {!Array<!Data>} */
    items: {
      type: Array,
      value: Array,
    },

    spinnerActive: {
      type: Boolean,
      notify: true,
    },
  },

  /** @private {?PageHandlerInterface} */
  mojoHandler_: null,

  /** @override */
  ready() {
    this.mojoHandler_ = BrowserProxy.getInstance().handler;
  },

  /** @return {boolean} Whether removal can be undone. */
  canUndo() {
    return !this.isSearchFocused();
  },

  /** @return {boolean} Whether "Clear all" should be allowed. */
  canClearAll() {
    return this.getSearchText().length === 0 && this.hasClearableDownloads;
  },

  /** @return {string} The full text being searched. */
  getSearchText() {
    return /** @type {!CrToolbarElement} */ (this.$.toolbar)
        .getSearchField()
        .getValue();
  },

  focusOnSearchInput() {
    return /** @type {!CrToolbarElement} */ (this.$.toolbar)
        .getSearchField()
        .showAndFocus();
  },

  isSearchFocused() {
    return /** @type {!CrToolbarElement} */ (this.$.toolbar)
        .getSearchField()
        .isSearchFocused();
  },

  /** @private */
  onClearAllTap_() {
    assert(this.canClearAll());
    this.mojoHandler_.clearAll();
    this.$.moreActionsMenu.close();
    const canUndo =
        this.items.some(data => !data.isDangerous && !data.isMixedContent);
    getToastManager().show(loadTimeData.getString('toastClearedAll'),
        /* hideSlotted= */ !canUndo);
  },

  /** @private */
  onMoreActionsTap_() {
    this.$.moreActionsMenu.showAt(this.$.moreActions);
  },

  /**
   * @param {!CustomEvent<string>} event
   * @private
   */
  onSearchChanged_(event) {
    const searchService = SearchService.getInstance();
    if (searchService.search(event.detail)) {
      this.spinnerActive = searchService.isSearching();
    }
    this.updateClearAll_();
  },

  /** @private */
  onOpenDownloadsFolderTap_() {
    this.mojoHandler_.openDownloadsFolderRequiringGesture();
    this.$.moreActionsMenu.close();
  },

  /** @private */
  updateClearAll_() {
    this.$$('.clear-all').hidden = !this.canClearAll();
  },
});