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.proxyexceptions', function() {
6 /** @const */ var List
= cr
.ui
.List
;
7 /** @const */ var ListItem
= cr
.ui
.ListItem
;
8 /** @const */ var ArrayDataModel
= cr
.ui
.ArrayDataModel
;
11 * Creates a new exception list.
12 * @param {Object=} opt_propertyBag Optional properties.
14 * @extends {cr.ui.List}
16 var ProxyExceptions
= cr
.ui
.define('list');
18 ProxyExceptions
.prototype = {
19 __proto__
: List
.prototype,
21 pref
: 'cros.session.proxy.ignorelist',
24 decorate: function() {
25 List
.prototype.decorate
.call(this);
26 this.autoExpands
= true;
28 // HACK(arv): http://crbug.com/40902
29 window
.addEventListener('resize', this.redraw
.bind(this));
31 this.addEventListener('click', this.handleClick_
);
35 // Listens to pref changes.
36 Preferences
.getInstance().addEventListener(this.pref
,
38 self
.load_(event
.value
.value
);
42 createItem: function(exception
) {
43 return new ProxyExceptionsItem(exception
);
47 * Adds given exception to model and update backend.
48 * @param {Object} exception A exception to be added to exception list.
50 addException: function(exception
) {
51 this.dataModel
.push(exception
);
52 this.updateBackend_();
56 * Removes given exception from model and update backend.
58 removeException: function(exception
) {
59 var dataModel
= this.dataModel
;
61 var index
= dataModel
.indexOf(exception
);
63 dataModel
.splice(index
, 1);
64 this.updateBackend_();
69 * Handles the clicks on the list and triggers exception removal if the
70 * click is on the remove exception button.
72 * @param {!Event} e The click event object.
74 handleClick_: function(e
) {
75 // Handle left button click
78 if (el
.className
== 'remove-exception-button') {
79 this.removeException(el
.parentNode
.exception
);
85 * Loads given exception list.
86 * @param {!Array} exceptions An array of exception object.
88 load_: function(exceptions
) {
89 this.dataModel
= new ArrayDataModel(exceptions
);
95 updateBackend_: function() {
96 Preferences
.setListPref(this.pref
, this.dataModel
.slice(), true);
101 * Creates a new exception list item.
102 * @param {Object} exception The exception account this represents.
104 * @extends {cr.ui.ListItem}
106 function ProxyExceptionsItem(exception
) {
107 var el
= cr
.doc
.createElement('div');
108 el
.exception
= exception
;
109 ProxyExceptionsItem
.decorate(el
);
114 * Decorates an element as a exception account item.
115 * @param {!HTMLElement} el The element to decorate.
117 ProxyExceptionsItem
.decorate = function(el
) {
118 el
.__proto__
= ProxyExceptionsItem
.prototype;
122 ProxyExceptionsItem
.prototype = {
123 __proto__
: ListItem
.prototype,
126 decorate: function() {
127 ListItem
.prototype.decorate
.call(this);
128 this.className
= 'exception-list-item';
130 var labelException
= this.ownerDocument
.createElement('span');
131 labelException
.className
= '';
132 labelException
.textContent
= this.exception
;
133 this.appendChild(labelException
);
138 ProxyExceptions
: ProxyExceptions