| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 ArrayDataModel = cr.ui.ArrayDataModel; | |
| 7 const List = cr.ui.List; | |
| 8 const ListItem = cr.ui.ListItem; | |
| 9 const HandlerOptions = options.HandlerOptions; | |
| 10 const DeletableItem = options.DeletableItem; | |
| 11 const DeletableItemList = options.DeletableItemList; | |
| 12 | |
| 13 const localStrings = new LocalStrings(); | |
| 14 | |
| 15 /** | |
| 16 * Creates a new ignored protocol / content handler list item. | |
| 17 * | |
| 18 * Accepts values in the form | |
| 19 * ['mailto', 'http://www.thesite.com/%s', 'The title of the protocol'], | |
| 20 * @param {Object} entry A dictionary describing the handlers for a given | |
| 21 * protocol. | |
| 22 * @constructor | |
| 23 * @extends {cr.ui.DeletableItemList} | |
| 24 */ | |
| 25 function IgnoredHandlersListItem(entry) { | |
| 26 var el = cr.doc.createElement('div'); | |
| 27 el.dataItem = entry; | |
| 28 el.__proto__ = IgnoredHandlersListItem.prototype; | |
| 29 el.decorate(); | |
| 30 return el; | |
| 31 } | |
| 32 | |
| 33 IgnoredHandlersListItem.prototype = { | |
| 34 __proto__: DeletableItem.prototype, | |
| 35 | |
| 36 /** @inheritDoc */ | |
| 37 decorate: function() { | |
| 38 DeletableItem.prototype.decorate.call(this); | |
| 39 | |
| 40 // Protocol. | |
| 41 var protocolElement = document.createElement('div'); | |
| 42 protocolElement.textContent = this.dataItem[0]; | |
| 43 protocolElement.className = 'handlers-type-column'; | |
| 44 this.contentElement_.appendChild(protocolElement); | |
| 45 | |
| 46 // Site title. | |
| 47 var titleElement = document.createElement('div'); | |
| 48 titleElement.textContent = this.dataItem[2]; | |
| 49 titleElement.className = 'handlers-site-column'; | |
| 50 titleElement.title = this.dataItem[1]; | |
| 51 this.contentElement_.appendChild(titleElement); | |
| 52 }, | |
| 53 }; | |
| 54 | |
| 55 | |
| 56 var IgnoredHandlersList = cr.ui.define('list'); | |
| 57 | |
| 58 IgnoredHandlersList.prototype = { | |
| 59 __proto__: DeletableItemList.prototype, | |
| 60 | |
| 61 createItem: function(entry) { | |
| 62 return new IgnoredHandlersListItem(entry); | |
| 63 }, | |
| 64 | |
| 65 deleteItemAtIndex: function(index) { | |
| 66 chrome.send('removeIgnoredHandler', [this.dataModel.item(index)]); | |
| 67 }, | |
| 68 | |
| 69 /** | |
| 70 * The length of the list. | |
| 71 */ | |
| 72 get length() { | |
| 73 return this.dataModel.length; | |
| 74 }, | |
| 75 | |
| 76 /** | |
| 77 * Set the protocol handlers displayed by this list. See | |
| 78 * IgnoredHandlersListItem for an example of the format the list should | |
| 79 * take. | |
| 80 * | |
| 81 * @param {Object} list A list of ignored protocol handlers. | |
| 82 */ | |
| 83 setHandlers: function(list) { | |
| 84 this.dataModel = new ArrayDataModel(list); | |
| 85 }, | |
| 86 }; | |
| 87 | |
| 88 | |
| 89 | |
| 90 /** | |
| 91 * Creates a new protocol / content handler list item. | |
| 92 * | |
| 93 * Accepts values in the form | |
| 94 * { protocol: 'mailto', | |
| 95 * handlers: [ | |
| 96 * ['mailto', 'http://www.thesite.com/%s', 'The title of the protocol'], | |
| 97 * ..., | |
| 98 * ], | |
| 99 * } | |
| 100 * @param {Object} entry A dictionary describing the handlers for a given | |
| 101 * protocol. | |
| 102 * @constructor | |
| 103 * @extends {cr.ui.ListItem} | |
| 104 */ | |
| 105 function HandlerListItem(entry) { | |
| 106 var el = cr.doc.createElement('div'); | |
| 107 el.dataItem = entry; | |
| 108 el.__proto__ = HandlerListItem.prototype; | |
| 109 el.decorate(); | |
| 110 return el; | |
| 111 } | |
| 112 | |
| 113 HandlerListItem.prototype = { | |
| 114 __proto__: ListItem.prototype, | |
| 115 | |
| 116 buildWidget_: function(data, delegate) { | |
| 117 // Protocol. | |
| 118 var protocolElement = document.createElement('div'); | |
| 119 protocolElement.textContent = data.protocol; | |
| 120 protocolElement.className = 'handlers-type-column'; | |
| 121 this.appendChild(protocolElement); | |
| 122 | |
| 123 // Handler selection. | |
| 124 var handlerElement = document.createElement('div'); | |
| 125 var selectElement = document.createElement('select'); | |
| 126 var defaultOptionElement = document.createElement('option'); | |
| 127 defaultOptionElement.selected = data.default_handler == -1; | |
| 128 defaultOptionElement.textContent = | |
| 129 localStrings.getString('handlers_none_handler'); | |
| 130 defaultOptionElement.value = -1; | |
| 131 selectElement.appendChild(defaultOptionElement); | |
| 132 | |
| 133 for (var i = 0; i < data.handlers.length; ++i) { | |
| 134 var optionElement = document.createElement('option'); | |
| 135 optionElement.selected = i == data.default_handler; | |
| 136 optionElement.textContent = data.handlers[i][2]; | |
| 137 optionElement.value = i; | |
| 138 selectElement.appendChild(optionElement); | |
| 139 } | |
| 140 | |
| 141 selectElement.addEventListener('change', function (e) { | |
| 142 var index = e.target.value; | |
| 143 if (index == -1) { | |
| 144 this.classList.add('none'); | |
| 145 delegate.clearDefault(data.protocol); | |
| 146 } else { | |
| 147 handlerElement.classList.remove('none'); | |
| 148 delegate.setDefault(data.handlers[index]); | |
| 149 } | |
| 150 }); | |
| 151 handlerElement.appendChild(selectElement); | |
| 152 handlerElement.className = 'handlers-site-column'; | |
| 153 if (data.default_handler == -1) | |
| 154 this.classList.add('none'); | |
| 155 this.appendChild(handlerElement); | |
| 156 | |
| 157 // Remove link. | |
| 158 var removeElement = document.createElement('div'); | |
| 159 removeElement.textContent = | |
| 160 localStrings.getString('handlers_remove_link'); | |
| 161 removeElement.addEventListener('click', function (e) { | |
| 162 var value = selectElement ? selectElement.value : 0; | |
| 163 delegate.removeHandler(value, data.handlers[value]); | |
| 164 }); | |
| 165 removeElement.className = 'handlers-remove-column handlers-remove-link'; | |
| 166 this.appendChild(removeElement); | |
| 167 }, | |
| 168 | |
| 169 /** @inheritDoc */ | |
| 170 decorate: function() { | |
| 171 ListItem.prototype.decorate.call(this); | |
| 172 | |
| 173 var self = this; | |
| 174 var delegate = { | |
| 175 removeHandler: function(index, handler) { | |
| 176 chrome.send('removeHandler', [handler]); | |
| 177 }, | |
| 178 setDefault: function(handler) { | |
| 179 chrome.send('setDefault', [handler]); | |
| 180 }, | |
| 181 clearDefault: function(protocol) { | |
| 182 chrome.send('clearDefault', [protocol]); | |
| 183 }, | |
| 184 }; | |
| 185 | |
| 186 this.buildWidget_(this.dataItem, delegate); | |
| 187 }, | |
| 188 }; | |
| 189 | |
| 190 /** | |
| 191 * Create a new passwords list. | |
| 192 * @constructor | |
| 193 * @extends {cr.ui.List} | |
| 194 */ | |
| 195 var HandlersList = cr.ui.define('list'); | |
| 196 | |
| 197 HandlersList.prototype = { | |
| 198 __proto__: List.prototype, | |
| 199 | |
| 200 /** @inheritDoc */ | |
| 201 createItem: function(entry) { | |
| 202 return new HandlerListItem(entry); | |
| 203 }, | |
| 204 | |
| 205 /** | |
| 206 * The length of the list. | |
| 207 */ | |
| 208 get length() { | |
| 209 return this.dataModel.length; | |
| 210 }, | |
| 211 | |
| 212 /** | |
| 213 * Set the protocol handlers displayed by this list. | |
| 214 * See HandlerListItem for an example of the format the list should take. | |
| 215 * | |
| 216 * @param {Object} list A list of protocols with their registered handlers. | |
| 217 */ | |
| 218 setHandlers: function(list) { | |
| 219 this.dataModel = new ArrayDataModel(list); | |
| 220 }, | |
| 221 }; | |
| 222 | |
| 223 return { | |
| 224 IgnoredHandlersListItem: IgnoredHandlersListItem, | |
| 225 IgnoredHandlersList: IgnoredHandlersList, | |
| 226 HandlerListItem: HandlerListItem, | |
| 227 HandlersList: HandlersList, | |
| 228 }; | |
| 229 }); | |
| OLD | NEW |