1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5cr.define('options', function() {
6  const Tree = cr.ui.Tree;
7  const TreeItem = cr.ui.TreeItem;
8
9  /**
10   * Creates a new tree item for certificate data.
11   * @param {Object=} data Data used to create a certificate tree item.
12   * @constructor
13   * @extends {TreeItem}
14   */
15  function CertificateTreeItem(data) {
16    // TODO(mattm): other columns
17    var treeItem = new TreeItem({
18      label: data.name,
19      data: data
20    });
21    treeItem.__proto__ = CertificateTreeItem.prototype;
22
23    if (data.icon) {
24      treeItem.icon = data.icon;
25    }
26
27    return treeItem;
28  }
29
30  CertificateTreeItem.prototype = {
31    __proto__: TreeItem.prototype,
32
33    /**
34     * The tree path id/.
35     * @type {string}
36     */
37    get pathId() {
38      var parent = this.parentItem;
39      if (parent && parent instanceof CertificateTreeItem) {
40        return parent.pathId + ',' + this.data.id;
41      } else {
42        return this.data.id;
43      }
44    }
45  };
46
47  /**
48   * Creates a new cookies tree.
49   * @param {Object=} opt_propertyBag Optional properties.
50   * @constructor
51   * @extends {Tree}
52   */
53  var CertificatesTree = cr.ui.define('tree');
54
55  CertificatesTree.prototype = {
56    __proto__: Tree.prototype,
57
58    /** @inheritDoc */
59    decorate: function() {
60      Tree.prototype.decorate.call(this);
61      this.treeLookup_ = {};
62    },
63
64    /** @inheritDoc */
65    addAt: function(child, index) {
66      Tree.prototype.addAt.call(this, child, index);
67      if (child.data && child.data.id)
68        this.treeLookup_[child.data.id] = child;
69    },
70
71    /** @inheritDoc */
72    remove: function(child) {
73      Tree.prototype.remove.call(this, child);
74      if (child.data && child.data.id)
75        delete this.treeLookup_[child.data.id];
76    },
77
78    /**
79     * Clears the tree.
80     */
81    clear: function() {
82      // Remove all fields without recreating the object since other code
83      // references it.
84      for (var id in this.treeLookup_){
85        delete this.treeLookup_[id];
86      }
87      this.textContent = '';
88    },
89
90    /**
91     * Populate the tree.
92     * @param {Array} nodesData Nodes data array.
93     */
94    populate: function(nodesData) {
95      this.clear();
96
97      for (var i = 0; i < nodesData.length; ++i) {
98        var subnodes = nodesData[i]['subnodes'];
99        delete nodesData[i]['subnodes'];
100
101        var item = new CertificateTreeItem(nodesData[i]);
102        this.addAt(item, i);
103
104        for (var j = 0; j < subnodes.length; ++j) {
105          var subitem = new CertificateTreeItem(subnodes[j]);
106          item.addAt(subitem, j);
107        }
108        // Make tree expanded by default.
109        item.expanded = true;
110      }
111
112      cr.dispatchSimpleEvent(this, 'change');
113    },
114  };
115
116  return {
117    CertificatesTree: CertificatesTree
118  };
119});
120
121