1 // Copyright (c) 2012 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 5 cr.define('options', function() { 6 /** @const */ var Tree = cr.ui.Tree; 7 /** @const */ var TreeItem = cr.ui.TreeItem; 8 9 /** 10 * Creates a new tree folder for certificate data. 11 * @param {Object=} data Data used to create a certificate tree folder. 12 * @constructor 13 * @extends {TreeItem} 14 */ 15 function CertificateTreeFolder(data) { 16 data.isCert = false; 17 var treeFolder = new TreeItem({ 18 label: data.name, 19 data: data 20 }); 21 treeFolder.__proto__ = CertificateTreeFolder.prototype; 22 23 if (data.icon) 24 treeFolder.icon = data.icon; 25 26 return treeFolder; 27 } 28 29 CertificateTreeFolder.prototype = { 30 __proto__: TreeItem.prototype, 31 32 /** 33 * The tree path id/. 34 * @type {string} 35 */ 36 get pathId() { 37 return this.data.id; 38 } 39 }; 40 41 /** 42 * Creates a new tree item for certificate data. 43 * @param {Object=} data Data used to create a certificate tree item. 44 * @constructor 45 * @extends {TreeItem} 46 */ 47 function CertificateTreeItem(data) { 48 data.isCert = true; 49 // TODO(mattm): other columns 50 var treeItem = new TreeItem({ 51 label: data.name, 52 data: data 53 }); 54 treeItem.__proto__ = CertificateTreeItem.prototype; 55 56 if (data.icon) 57 treeItem.icon = data.icon; 58 59 if (data.untrusted) { 60 var badge = document.createElement('span'); 61 badge.classList.add('cert-untrusted'); 62 badge.textContent = loadTimeData.getString('badgeCertUntrusted'); 63 treeItem.labelElement.insertBefore( 64 badge, treeItem.labelElement.firstChild); 65 } 66 67 if (data.policy) { 68 var policyIndicator = new options.ControlledSettingIndicator(); 69 policyIndicator.controlledBy = 'policy'; 70 policyIndicator.setAttribute( 71 'textpolicy', loadTimeData.getString('certPolicyInstalled')); 72 policyIndicator.classList.add('cert-policy'); 73 treeItem.labelElement.appendChild(policyIndicator); 74 } 75 76 return treeItem; 77 } 78 79 CertificateTreeItem.prototype = { 80 __proto__: TreeItem.prototype, 81 82 /** 83 * The tree path id/. 84 * @type {string} 85 */ 86 get pathId() { 87 return this.parentItem.pathId + ',' + this.data.id; 88 } 89 }; 90 91 /** 92 * Creates a new cookies tree. 93 * @param {Object=} opt_propertyBag Optional properties. 94 * @constructor 95 * @extends {Tree} 96 */ 97 var CertificatesTree = cr.ui.define('tree'); 98 99 CertificatesTree.prototype = { 100 __proto__: Tree.prototype, 101 102 /** @override */ 103 decorate: function() { 104 Tree.prototype.decorate.call(this); 105 this.treeLookup_ = {}; 106 }, 107 108 /** @override */ 109 addAt: function(child, index) { 110 Tree.prototype.addAt.call(this, child, index); 111 if (child.data && child.data.id) 112 this.treeLookup_[child.data.id] = child; 113 }, 114 115 /** @override */ 116 remove: function(child) { 117 Tree.prototype.remove.call(this, child); 118 if (child.data && child.data.id) 119 delete this.treeLookup_[child.data.id]; 120 }, 121 122 /** 123 * Clears the tree. 124 */ 125 clear: function() { 126 // Remove all fields without recreating the object since other code 127 // references it. 128 for (var id in this.treeLookup_) 129 delete this.treeLookup_[id]; 130 this.textContent = ''; 131 }, 132 133 /** 134 * Populate the tree. 135 * @param {Array} nodesData Nodes data array. 136 */ 137 populate: function(nodesData) { 138 this.clear(); 139 140 for (var i = 0; i < nodesData.length; ++i) { 141 var subnodes = nodesData[i].subnodes; 142 delete nodesData[i].subnodes; 143 144 var item = new CertificateTreeFolder(nodesData[i]); 145 this.addAt(item, i); 146 147 for (var j = 0; j < subnodes.length; ++j) { 148 var subitem = new CertificateTreeItem(subnodes[j]); 149 item.addAt(subitem, j); 150 } 151 // Make tree expanded by default. 152 item.expanded = true; 153 } 154 155 cr.dispatchSimpleEvent(this, 'change'); 156 }, 157 }; 158 159 return { 160 CertificatesTree: CertificatesTree 161 }; 162 }); 163 164