summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/md_downloads/manager.js
blob: 76017acc3017bd694801114478f8346b07baf784 (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
// 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() {
  var Manager = Polymer({
    is: 'downloads-manager',

    properties: {
      hasDownloads_: {
        type: Boolean,
        value: false,
      },

      items_: {
        type: Array,
      },
    },

    hostAttributes: {
      loading: true,
    },

    /**
     * @param {Event} e
     * @private
     */
    onCanExecute_: function(e) {
      e = /** @type {cr.ui.CanExecuteEvent} */(e);
      switch (e.command.id) {
        case 'undo-command':
          e.canExecute = this.$.toolbar.canUndo();
          break;
        case 'clear-all-command':
          e.canExecute = this.$.toolbar.canClearAll();
          break;
      }
    },

    /**
     * @param {Event} e
     * @private
     */
    onCommand_: function(e) {
      if (e.command.id == 'clear-all-command')
        downloads.ActionService.getInstance().clearAll();
      else if (e.command.id == 'undo-command')
        downloads.ActionService.getInstance().undo();
    },

    /** @private */
    onLoad_: function() {
      cr.ui.decorate('command', cr.ui.Command);
      document.addEventListener('canExecute', this.onCanExecute_.bind(this));
      document.addEventListener('command', this.onCommand_.bind(this));

      // Shows all downloads.
      downloads.ActionService.getInstance().search('');
    },

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

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

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

        this.idToIndex_[data.id] = data.index = i;

        var prev = list[i - 1];
        data.hideDate = !!prev && prev.date_string == data.date_string;
      }

      // TODO(dbeam): this resets the scroll position, which is a huge bummer.
      // Removing something from the bottom of the list should not scroll you
      // back to the top. The grand plan is to restructure how the C++ sends the
      // JS data so that it only gets updates (rather than the most recent set
      // of items). TL;DR - we can't ship with this bug.
      this.items_ = list;

      var hasDownloads = this.size_() > 0;
      if (!hasDownloads) {
        var isSearching = downloads.ActionService.getInstance().isSearching();
        var messageToShow = isSearching ? 'noSearchResults' : 'noDownloads';
        this.$['no-downloads'].querySelector('span').textContent =
            loadTimeData.getString(messageToShow);
      }
      this.hasDownloads_ = hasDownloads;

      if (loadTimeData.getBoolean('allowDeletingHistory'))
        this.$.toolbar.downloadsShowing = this.hasDownloads_;

      this.removeAttribute('loading');
    },

    /**
     * @param {!downloads.Data} data
     * @private
     */
    updateItem_: function(data) {
      var index = this.idToIndex_[data.id];
      this.set('items_.' + index, data);
      this.$['downloads-list'].updateSizeForItem(index);
    },
  });

  Manager.size = function() {
    return document.querySelector('downloads-manager').size_();
  };

  Manager.updateAll = function(list) {
    document.querySelector('downloads-manager').updateAll_(list);
  };

  Manager.updateItem = function(item) {
    document.querySelector('downloads-manager').updateItem_(item);
  };

  Manager.onLoad = function() {
    document.querySelector('downloads-manager').onLoad_();
  };

  return {Manager: Manager};
});