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
);
44 * @param {Object} exception
46 createItem: function(exception
) {
47 return new ProxyExceptionsItem(exception
);
51 * Adds given exception to model and update backend.
52 * @param {Object} exception A exception to be added to exception list.
54 addException: function(exception
) {
55 this.dataModel
.push(exception
);
56 this.updateBackend_();
60 * Removes given exception from model and update backend.
62 removeException: function(exception
) {
63 var dataModel
= this.dataModel
;
65 var index
= dataModel
.indexOf(exception
);
67 dataModel
.splice(index
, 1);
68 this.updateBackend_();
73 * Handles the clicks on the list and triggers exception removal if the
74 * click is on the remove exception button.
76 * @param {!Event} e The click event object.
78 handleClick_: function(e
) {
79 // Handle left button click
82 if (el
.className
== 'remove-exception-button') {
83 this.removeException(el
.parentNode
.exception
);
89 * Loads given exception list.
90 * @param {!Array} exceptions An array of exception object.
92 load_: function(exceptions
) {
93 this.dataModel
= new ArrayDataModel(exceptions
);
99 updateBackend_: function() {
100 Preferences
.setListPref(this.pref
, this.dataModel
.slice(), true);
105 * Creates a new exception list item.
106 * @param {Object} exception The exception account this represents.
108 * @extends {cr.ui.ListItem}
110 function ProxyExceptionsItem(exception
) {
111 var el
= cr
.doc
.createElement('div');
112 el
.exception
= exception
;
113 ProxyExceptionsItem
.decorate(el
);
118 * Decorates an element as a exception account item.
119 * @param {!HTMLElement} el The element to decorate.
121 ProxyExceptionsItem
.decorate = function(el
) {
122 el
.__proto__
= ProxyExceptionsItem
.prototype;
126 ProxyExceptionsItem
.prototype = {
127 __proto__
: ListItem
.prototype,
130 decorate: function() {
131 ListItem
.prototype.decorate
.call(this);
132 this.className
= 'exception-list-item';
134 var labelException
= this.ownerDocument
.createElement('span');
135 labelException
.className
= '';
136 labelException
.textContent
= this.exception
;
137 this.appendChild(labelException
);
142 ProxyExceptions
: ProxyExceptions