summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/md_downloads/manager.js
blob: ebd003f47fc0e37e53ba7f5370eda0efaa46291a (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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('downloads', function() {
  /**
   * Class to own and manage download items.
   * @constructor
   */
  function Manager() {}

  cr.addSingletonGetter(Manager);

  Manager.prototype = {
    /** @private {string} */
    searchText_: '',

    /**
     * Sets the search text, updates related UIs, and tells the browser.
     * @param {string} searchText Text we're searching for.
     * @private
     */
    setSearchText_: function(searchText) {
      this.searchText_ = searchText;

      $('downloads-summary-text').textContent = this.searchText_ ?
          loadTimeData.getStringF('searchResultsFor', this.searchText_) : '';

      // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']).
      function trim(s) { return s.trim(); }
      chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim));
    },

    /**
     * @return {number} A guess at how many items could be visible at once.
     * @private
     */
    guesstimateNumberOfVisibleItems_: function() {
      var toolbarHeight = $('downloads-toolbar').offsetHeight;
      var summaryHeight = $('downloads-summary').offsetHeight;
      var nonItemSpace = toolbarHeight + summaryHeight;
      return Math.floor((window.innerHeight - nonItemSpace) / 46) + 1;
    },

    /**
     * Called when all items need to be updated.
     * @param {!Array<!downloads.Data>} list A list of new download data.
     * @private
     */
    updateAll_: function(list) {
      var oldIdMap = this.idMap_ || {};

      /** @private {!Object<!downloads.ItemView>} */
      this.idMap_ = {};

      /** @private {!Array<!downloads.ItemView>} */
      this.items_ = [];

      if (!this.iconLoader_) {
        var guesstimate = Math.max(this.guesstimateNumberOfVisibleItems_(), 1);
        /** @private {downloads.ThrottledIconLoader} */
        this.iconLoader_ = new downloads.ThrottledIconLoader(guesstimate);
      }

      for (var i = 0; i < list.length; ++i) {
        var data = list[i];
        var id = data.id;

        // Re-use old items when possible (saves work, preserves focus).
        var item = oldIdMap[id] || new downloads.ItemView(this.iconLoader_);

        this.idMap_[id] = item;  // Associated by ID for fast lookup.
        this.items_.push(item);  // Add to sorted list for order.

        // Render |item| but don't actually add to the DOM yet. |this.items_|
        // must be fully created to be able to find the right spot to insert.
        item.update(data);

        // Collapse redundant dates.
        var prev = list[i - 1];
        item.hideDate = !!prev && prev.date_string == data.date_string;

        delete oldIdMap[id];
      }

      // Remove stale, previously rendered items from the DOM.
      for (var id in oldIdMap) {
        if (oldIdMap[id].parentNode)
          oldIdMap[id].parentNode.removeChild(oldIdMap[id]);
        delete oldIdMap[id];
      }

      for (var i = 0; i < this.items_.length; ++i) {
        var item = this.items_[i];
        if (item.parentNode)  // Already in the DOM; skip.
          continue;

        var before = null;
        // Find the next rendered item after this one, and insert before it.
        for (var j = i + 1; !before && j < this.items_.length; ++j) {
          if (this.items_[j].parentNode)
            before = this.items_[j];
        }
        // If |before| is null, |item| will just get added at the end.
        this.node_.insertBefore(item, before);
      }

      var noDownloadsOrResults = $('no-downloads-or-results');
      noDownloadsOrResults.textContent = loadTimeData.getString(
          this.searchText_ ? 'noSearchResults' : 'noDownloads');

      var hasDownloads = this.size_() > 0;
      this.node_.hidden = !hasDownloads;
      noDownloadsOrResults.hidden = hasDownloads;

      if (loadTimeData.getBoolean('allowDeletingHistory'))
        $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0;
    },

    /**
     * @param {!downloads.Data} data
     * @private
     */
    updateItem_: function(data) {
      this.idMap_[data.id].update(data);
    },

    /**
     * @return {number} The number of downloads shown on the page.
     * @private
     */
    size_: function() {
      return this.items_.length;
    },

    /** @private */
    clearAll_: function() {
      if (loadTimeData.getBoolean('allowDeletingHistory')) {
        chrome.send('clearAll');
        this.setSearchText_('');
      }
    },

    /** @private */
    onLoad_: function() {
      this.node_ = $('downloads-display');

      $('clear-all').onclick = function() {
        this.clearAll_();
      }.bind(this);

      $('open-downloads-folder').onclick = function() {
        chrome.send('openDownloadsFolder');
      };

      $('search-button').onclick = function() {
        if (!$('search-term').hidden)
          return;
        $('clear-search').hidden = false;
        $('search-term').hidden = false;
      };

      $('clear-search').onclick = function() {
        $('clear-search').hidden = true;
        $('search-term').hidden = true;
        $('search-term').value = '';
        this.setSearchText_('');
      }.bind(this);

      // TODO(dbeam): this previously used onsearch, which batches keystrokes
      // together. This should probably be re-instated eventually.
      $('search-term').oninput = function(e) {
        this.setSearchText_($('search-term').value);
      }.bind(this);

      cr.ui.decorate('command', cr.ui.Command);
      document.addEventListener('canExecute', this.onCanExecute_.bind(this));
      document.addEventListener('command', this.onCommand_.bind(this));

      this.setSearchText_('');
    },

    /**
     * @param {Event} e
     * @private
     */
    onCanExecute_: function(e) {
      e = /** @type {cr.ui.CanExecuteEvent} */(e);
      switch (e.command.id) {
        case 'undo-command':
          e.canExecute = !$('search-term').contains(document.activeElement);
          break;
        case 'clear-all-command':
          e.canExecute = true;
          break;
      }
    },

    /**
     * @param {Event} e
     * @private
     */
    onCommand_: function(e) {
      if (e.command.id == 'undo-command')
        chrome.send('undo');
      else if (e.command.id == 'clear-all-command')
        this.clearAll_();
    },
  };

  Manager.updateAll = function(list) {
    Manager.getInstance().updateAll_(list);
  };

  Manager.updateItem = function(item) {
    Manager.getInstance().updateItem_(item);
  };

  Manager.setSearchText = function(searchText) {
    Manager.getInstance().setSearchText_(searchText);
  };

  Manager.onLoad = function() {
    Manager.getInstance().onLoad_();
  };

  Manager.size = function() {
    return Manager.getInstance().size_();
  };

  return {Manager: Manager};
});

window.addEventListener('load', downloads.Manager.onLoad);