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.
5 cr.define('options', function() {
6 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
7 /** @const */ var List = cr.ui.List;
8 /** @const */ var ListItem = cr.ui.ListItem;
9 /** @const */ var HandlerOptions = options.HandlerOptions;
10 /** @const */ var DeletableItem = options.DeletableItem;
11 /** @const */ var DeletableItemList = options.DeletableItemList;
14 * Creates a new ignored protocol / content handler list item.
16 * Accepts values in the form
17 * ['mailto', 'http://www.thesite.com/%s', 'The title of the protocol'],
18 * @param {Object} entry A dictionary describing the handlers for a given
21 * @extends {cr.ui.DeletableItemList}
23 function IgnoredHandlersListItem(entry) {
24 var el = cr.doc.createElement('div');
26 el.__proto__ = IgnoredHandlersListItem.prototype;
31 IgnoredHandlersListItem.prototype = {
32 __proto__: DeletableItem.prototype,
35 decorate: function() {
36 DeletableItem.prototype.decorate.call(this);
39 var protocolElement = document.createElement('div');
40 protocolElement.textContent = this.dataItem[0];
41 protocolElement.className = 'handlers-type-column';
42 this.contentElement_.appendChild(protocolElement);
45 var titleElement = document.createElement('div');
46 titleElement.textContent = this.dataItem[2];
47 titleElement.className = 'handlers-site-column';
48 titleElement.title = this.dataItem[1];
49 this.contentElement_.appendChild(titleElement);
54 var IgnoredHandlersList = cr.ui.define('list');
56 IgnoredHandlersList.prototype = {
57 __proto__: DeletableItemList.prototype,
59 createItem: function(entry) {
60 return new IgnoredHandlersListItem(entry);
63 deleteItemAtIndex: function(index) {
64 chrome.send('removeIgnoredHandler', [this.dataModel.item(index)]);
68 * The length of the list.
71 return this.dataModel.length;
75 * Set the protocol handlers displayed by this list. See
76 * IgnoredHandlersListItem for an example of the format the list should
79 * @param {Object} list A list of ignored protocol handlers.
81 setHandlers: function(list) {
82 this.dataModel = new ArrayDataModel(list);
89 * Creates a new protocol / content handler list item.
91 * Accepts values in the form
92 * { protocol: 'mailto',
94 * ['mailto', 'http://www.thesite.com/%s', 'The title of the protocol'],
98 * @param {Object} entry A dictionary describing the handlers for a given
101 * @extends {cr.ui.ListItem}
103 function HandlerListItem(entry) {
104 var el = cr.doc.createElement('div');
106 el.__proto__ = HandlerListItem.prototype;
111 HandlerListItem.prototype = {
112 __proto__: ListItem.prototype,
114 buildWidget_: function(data, delegate) {
116 var protocolElement = document.createElement('div');
117 protocolElement.textContent = data.protocol;
118 protocolElement.className = 'handlers-type-column';
119 this.appendChild(protocolElement);
121 // Handler selection.
122 var handlerElement = document.createElement('div');
123 var selectElement = document.createElement('select');
124 var defaultOptionElement = document.createElement('option');
125 defaultOptionElement.selected = data.default_handler == -1;
126 defaultOptionElement.textContent =
127 loadTimeData.getString('handlers_none_handler');
128 defaultOptionElement.value = -1;
129 selectElement.appendChild(defaultOptionElement);
131 for (var i = 0; i < data.handlers.length; ++i) {
132 var optionElement = document.createElement('option');
133 optionElement.selected = i == data.default_handler;
134 optionElement.textContent = data.handlers[i][2];
135 optionElement.value = i;
136 selectElement.appendChild(optionElement);
139 selectElement.addEventListener('change', function(e) {
140 var index = e.target.value;
142 this.classList.add('none');
143 delegate.clearDefault(data.protocol);
145 handlerElement.classList.remove('none');
146 delegate.setDefault(data.handlers[index]);
149 handlerElement.appendChild(selectElement);
150 handlerElement.className = 'handlers-site-column';
151 if (data.default_handler == -1)
152 this.classList.add('none');
153 this.appendChild(handlerElement);
156 var removeElement = document.createElement('div');
157 removeElement.textContent =
158 loadTimeData.getString('handlers_remove_link');
159 removeElement.addEventListener('click', function(e) {
160 var value = selectElement ? selectElement.value : 0;
161 delegate.removeHandler(value, data.handlers[value]);
163 removeElement.className = 'handlers-remove-column handlers-remove-link';
164 this.appendChild(removeElement);
168 decorate: function() {
169 ListItem.prototype.decorate.call(this);
173 removeHandler: function(index, handler) {
174 chrome.send('removeHandler', [handler]);
176 setDefault: function(handler) {
177 chrome.send('setDefault', [handler]);
179 clearDefault: function(protocol) {
180 chrome.send('clearDefault', [protocol]);
184 this.buildWidget_(this.dataItem, delegate);
189 * Create a new passwords list.
191 * @extends {cr.ui.List}
193 var HandlersList = cr.ui.define('list');
195 HandlersList.prototype = {
196 __proto__: List.prototype,
199 createItem: function(entry) {
200 return new HandlerListItem(entry);
204 * The length of the list.
207 return this.dataModel.length;
211 * Set the protocol handlers displayed by this list.
212 * See HandlerListItem for an example of the format the list should take.
214 * @param {Object} list A list of protocols with their registered handlers.
216 setHandlers: function(list) {
217 this.dataModel = new ArrayDataModel(list);
222 IgnoredHandlersListItem: IgnoredHandlersListItem,
223 IgnoredHandlersList: IgnoredHandlersList,
224 HandlerListItem: HandlerListItem,
225 HandlersList: HandlersList,