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 DeletableItem = options.DeletableItem;
10 /** @const */ var DeletableItemList = options.DeletableItemList;
13 * Creates a new ignored protocol / content handler list item.
15 * Accepts values in the form
16 * ['mailto', 'http://www.thesite.com/%s', 'www.thesite.com'],
17 * @param {Object} entry A dictionary describing the handlers for a given
20 * @extends {options.DeletableItem}
22 function IgnoredHandlersListItem(entry) {
23 var el = cr.doc.createElement('div');
25 el.__proto__ = IgnoredHandlersListItem.prototype;
30 IgnoredHandlersListItem.prototype = {
31 __proto__: DeletableItem.prototype,
34 decorate: function() {
35 DeletableItem.prototype.decorate.call(this);
38 var protocolElement = document.createElement('div');
39 protocolElement.textContent = this.dataItem[0];
40 protocolElement.className = 'handlers-type-column';
41 this.contentElement_.appendChild(protocolElement);
44 var hostElement = document.createElement('div');
45 hostElement.textContent = this.dataItem[2];
46 hostElement.className = 'handlers-site-column';
47 hostElement.title = this.dataItem[1];
48 this.contentElement_.appendChild(hostElement);
54 * @extends {options.DeletableItemList}
56 var IgnoredHandlersList = cr.ui.define('list');
58 IgnoredHandlersList.prototype = {
59 __proto__: DeletableItemList.prototype,
63 * @param {Object} entry
65 createItem: function(entry) {
66 return new IgnoredHandlersListItem(entry);
69 deleteItemAtIndex: function(index) {
70 chrome.send('removeIgnoredHandler', [this.dataModel.item(index)]);
74 * The length of the list.
77 return this.dataModel.length;
81 * Set the protocol handlers displayed by this list. See
82 * IgnoredHandlersListItem for an example of the format the list should
85 * @param {!Array} list A list of ignored protocol handlers.
87 setHandlers: function(list) {
88 this.dataModel = new ArrayDataModel(list);
93 * Creates a new protocol / content handler list item.
95 * Accepts values in the form
96 * { protocol: 'mailto',
98 * ['mailto', 'http://www.thesite.com/%s', 'www.thesite.com'],
102 * @param {Object} entry A dictionary describing the handlers for a given
105 * @extends {cr.ui.ListItem}
107 function HandlerListItem(entry) {
108 var el = cr.doc.createElement('div');
110 el.__proto__ = HandlerListItem.prototype;
115 HandlerListItem.prototype = {
116 __proto__: ListItem.prototype,
119 * @param {Handlers} data
120 * @param {{removeHandler: Function, setDefault: Function,
121 * clearDefault: Function}} delegate
123 buildWidget_: function(data, delegate) {
125 var protocolElement = document.createElement('div');
126 protocolElement.textContent = data.protocol;
127 protocolElement.className = 'handlers-type-column';
128 this.appendChild(protocolElement);
130 // Handler selection.
131 var handlerElement = document.createElement('div');
132 var selectElement = document.createElement('select');
133 var defaultOptionElement = document.createElement('option');
134 defaultOptionElement.selected = data.default_handler == -1;
135 defaultOptionElement.textContent =
136 loadTimeData.getString('handlersNoneHandler');
137 defaultOptionElement.value = -1;
138 selectElement.appendChild(defaultOptionElement);
140 for (var i = 0; i < data.handlers.length; ++i) {
141 var optionElement = document.createElement('option');
142 optionElement.selected = i == data.default_handler;
143 optionElement.textContent = data.handlers[i][2];
144 optionElement.value = i;
145 selectElement.appendChild(optionElement);
148 selectElement.addEventListener('change', function(e) {
149 var index = e.target.value;
151 this.classList.add('none');
152 delegate.clearDefault(data.protocol);
154 handlerElement.classList.remove('none');
155 delegate.setDefault(data.handlers[index]);
158 handlerElement.appendChild(selectElement);
159 handlerElement.className = 'handlers-site-column';
160 if (data.default_handler == -1)
161 this.classList.add('none');
162 this.appendChild(handlerElement);
164 if (data.has_policy_recommendations) {
165 // Create an indicator to show that the handler has policy
167 var indicator = new options.ControlledSettingIndicator();
168 if (data.is_default_handler_set_by_user || data.default_handler == -1) {
169 // The default handler is registered by the user or set to none, which
170 // indicates that the user setting has overridden a policy
171 // recommendation. Show the appropriate bubble.
172 indicator.controlledBy = 'hasRecommendation';
173 indicator.resetHandler = function() {
174 // If there is a policy recommendation, data.handlers.length >= 1.
175 // Setting the default handler to 0 ensures that it won't be 'none',
176 // and there *is* a user registered handler created by setDefault,
177 // which is required for a change notification.
178 // The user-registered handlers are removed in a loop. Note that if
179 // a handler is installed by policy, removeHandler does nothing.
180 delegate.setDefault(data.handlers[0]);
181 for (var i = 0; i < data.handlers.length; ++i) {
182 delegate.removeHandler(i, data.handlers[i]);
186 indicator.controlledBy = 'recommended';
188 this.appendChild(indicator);
191 if (data.is_default_handler_set_by_user) {
193 var removeElement = document.createElement('div');
194 removeElement.textContent =
195 loadTimeData.getString('handlersRemoveLink');
196 removeElement.addEventListener('click', function(e) {
197 var value = selectElement ? selectElement.value : 0;
198 delegate.removeHandler(value, data.handlers[value]);
200 removeElement.className =
201 'handlers-remove-column handlers-remove-link';
202 this.appendChild(removeElement);
207 decorate: function() {
208 ListItem.prototype.decorate.call(this);
211 removeHandler: function(index, handler) {
212 chrome.send('removeHandler', [handler]);
214 setDefault: function(handler) {
215 chrome.send('setDefault', [handler]);
217 clearDefault: function(protocol) {
218 chrome.send('clearDefault', [protocol]);
222 this.buildWidget_(this.dataItem, delegate);
227 * Create a new passwords list.
229 * @extends {cr.ui.List}
231 var HandlersList = cr.ui.define('list');
233 HandlersList.prototype = {
234 __proto__: List.prototype,
238 * @param {Object} entry
240 createItem: function(entry) {
241 return new HandlerListItem(entry);
245 * The length of the list.
248 return this.dataModel.length;
252 * Set the protocol handlers displayed by this list.
253 * See HandlerListItem for an example of the format the list should take.
255 * @param {!Array} list A list of protocols with their registered handlers.
257 setHandlers: function(list) {
258 this.dataModel = new ArrayDataModel(list);
263 IgnoredHandlersListItem: IgnoredHandlersListItem,
264 IgnoredHandlersList: IgnoredHandlersList,
265 HandlerListItem: HandlerListItem,
266 HandlersList: HandlersList,