summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/downloads/focus_row.js
blob: 5d7078ce727431e6d7a6ae29d93e6fdee5fd2c14 (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
// 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() {
  /**
   * Provides an implementation for a single column grid.
   * @constructor
   * @extends {cr.ui.FocusRow}
   */
  function FocusRow() {}

  /**
   * Decorates |focusRow| so that it can be treated as a FocusRow.
   * @param {Element} focusRow The element that has all the columns represented
   *     by |itemView|.
   * @param {!downloads.ItemView} itemView The item view this row cares about.
   * @param {Node} boundary Focus events are ignored outside of this node.
   */
  FocusRow.decorate = function(focusRow, itemView, boundary) {
    focusRow.__proto__ = FocusRow.prototype;
    focusRow.decorate(boundary);
    focusRow.addFocusableElements_();
  };

  FocusRow.prototype = {
    __proto__: cr.ui.FocusRow.prototype,

    /** @override */
    getEquivalentElement: function(element) {
      if (this.focusableElements.indexOf(element) > -1)
        return assert(element);

      // All elements default to another element with the same type.
      var columnType = element.getAttribute('focus-type');
      var equivalent = this.querySelector('[focus-type=' + columnType + ']');

      if (this.focusableElements.indexOf(equivalent) < 0) {
        equivalent = null;
        var equivalentTypes =
            ['show', 'retry', 'pause', 'resume', 'remove', 'cancel'];
        if (equivalentTypes.indexOf(columnType) != -1) {
          var allTypes = equivalentTypes.map(function(type) {
            return '[focus-type=' + type + ']:not([hidden])';
          }).join(', ');
          equivalent = this.querySelector(allTypes);
        }
      }

      // Return the first focusable element if no equivalent element is found.
      return assert(equivalent || this.focusableElements[0]);
    },

    /** @private */
    addFocusableElements_: function() {
      var possiblyFocusableElements = this.querySelectorAll('[focus-type]');
      for (var i = 0; i < possiblyFocusableElements.length; ++i) {
        var possiblyFocusableElement = possiblyFocusableElements[i];
        if (cr.ui.FocusRow.isFocusable(possiblyFocusableElement))
          this.addFocusableElement(possiblyFocusableElement);
      }
    },
  };

  return {FocusRow: FocusRow};
});