OLD | NEW |
(Empty) | |
| 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 |
| 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 |
| 11 const localStrings = new LocalStrings(); |
| 12 |
| 13 /** |
| 14 * Creates a new protocol / content handler list item. |
| 15 * |
| 16 * Accepts values in the form |
| 17 * { protocol: 'mailto', |
| 18 * handlers: [ |
| 19 * ['http://www.thesite.com/%s', 'The title of the protocol'], |
| 20 * ..., |
| 21 * ], |
| 22 * } |
| 23 * @param {Object} entry A dictionary describing the handlers for a given |
| 24 * protocol. |
| 25 * @constructor |
| 26 * @extends {cr.ui.ListItem} |
| 27 */ |
| 28 function HandlerListItem(entry) { |
| 29 var el = cr.doc.createElement('div'); |
| 30 el.dataItem = entry; |
| 31 el.__proto__ = HandlerListItem.prototype; |
| 32 el.decorate(); |
| 33 |
| 34 return el; |
| 35 } |
| 36 |
| 37 HandlerListItem.prototype = { |
| 38 __proto__: ListItem.prototype, |
| 39 |
| 40 buildWidget_: function(data, delegate) { |
| 41 // Protocol. |
| 42 var protocolElement = document.createElement('div'); |
| 43 protocolElement.innerText = data.protocol; |
| 44 protocolElement.className = 'handlers-type-column'; |
| 45 this.appendChild(protocolElement); |
| 46 |
| 47 // Handler selection. |
| 48 var handlerElement = document.createElement('div'); |
| 49 var selectElement; |
| 50 if (data.handlers.length > 1) { |
| 51 selectElement = document.createElement('select'); |
| 52 for (var i = 0; i < data.handlers.length; ++i) { |
| 53 var optionElement = document.createElement('option'); |
| 54 optionElement.selected = i == data.default_handler; |
| 55 optionElement.innerText = data.handlers[i][1]; |
| 56 optionElement.value = i; |
| 57 selectElement.appendChild(optionElement); |
| 58 } |
| 59 |
| 60 selectElement.addEventListener('change', function (e) { |
| 61 var index = e.target.value; |
| 62 delegate.setDefault([data.protocol].concat(data.handlers[index])); |
| 63 }); |
| 64 handlerElement.appendChild(selectElement); |
| 65 } else { |
| 66 handlerElement.innerText = data.handlers[0][1]; |
| 67 } |
| 68 handlerElement.className = 'handlers-site-column'; |
| 69 this.appendChild(handlerElement); |
| 70 |
| 71 // Remove link. |
| 72 var removeElement = document.createElement('div'); |
| 73 removeElement.innerText = |
| 74 localStrings.getString('handlers_remove_link'); |
| 75 removeElement.addEventListener('click', function (e) { |
| 76 var value = selectElement ? selectElement.value : 0; |
| 77 delegate.removeHandler(value, |
| 78 [data.protocol].concat(data.handlers[value])); |
| 79 }); |
| 80 removeElement.className = 'handlers-remove-column handlers-remove-link'; |
| 81 this.appendChild(removeElement); |
| 82 }, |
| 83 |
| 84 /** @inheritDoc */ |
| 85 decorate: function() { |
| 86 ListItem.prototype.decorate.call(this); |
| 87 |
| 88 var self = this; |
| 89 var delegate = { |
| 90 removeHandler: function(index, handler) { |
| 91 chrome.send('removeHandler', [handler]); |
| 92 }, |
| 93 setDefault: function(handler) { |
| 94 chrome.send('setDefault', [handler]); |
| 95 }, |
| 96 }; |
| 97 |
| 98 this.buildWidget_(this.dataItem, delegate); |
| 99 }, |
| 100 }; |
| 101 |
| 102 /** |
| 103 * Create a new passwords list. |
| 104 * @constructor |
| 105 * @extends {cr.ui.List} |
| 106 */ |
| 107 var HandlersList = cr.ui.define('list'); |
| 108 |
| 109 HandlersList.prototype = { |
| 110 __proto__: List.prototype, |
| 111 |
| 112 /** @inheritDoc */ |
| 113 createItem: function(entry) { |
| 114 return new HandlerListItem(entry); |
| 115 }, |
| 116 |
| 117 /** |
| 118 * The length of the list. |
| 119 */ |
| 120 get length() { |
| 121 return this.dataModel.length; |
| 122 }, |
| 123 |
| 124 /** |
| 125 * Set the protocol handlers displayed by this list. |
| 126 * See HandlerListItem for an example of the format the list should take. |
| 127 * |
| 128 * @param {Object} list A list of protocols with their registered handlers. |
| 129 */ |
| 130 setHandlers: function(list) { |
| 131 this.dataModel = new ArrayDataModel(list); |
| 132 }, |
| 133 }; |
| 134 |
| 135 return { |
| 136 HandlerListItem: HandlerListItem, |
| 137 HandlersList: HandlersList, |
| 138 }; |
| 139 }); |
OLD | NEW |