- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / chromeos / proxy_rules_list.js
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.
4
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;
9
10   /**
11    * Creates a new exception list.
12    * @param {Object=} opt_propertyBag Optional properties.
13    * @constructor
14    * @extends {cr.ui.List}
15    */
16   var ProxyExceptions = cr.ui.define('list');
17
18   ProxyExceptions.prototype = {
19     __proto__: List.prototype,
20
21     pref: 'cros.session.proxy.ignorelist',
22
23     /** @override */
24     decorate: function() {
25       List.prototype.decorate.call(this);
26       this.autoExpands = true;
27
28       // HACK(arv): http://crbug.com/40902
29       window.addEventListener('resize', this.redraw.bind(this));
30
31       this.addEventListener('click', this.handleClick_);
32
33       var self = this;
34
35       // Listens to pref changes.
36       Preferences.getInstance().addEventListener(this.pref,
37           function(event) {
38             self.load_(event.value.value);
39           });
40     },
41
42     createItem: function(exception) {
43       return new ProxyExceptionsItem(exception);
44     },
45
46     /**
47      * Adds given exception to model and update backend.
48      * @param {Object} exception A exception to be added to exception list.
49      */
50     addException: function(exception) {
51       this.dataModel.push(exception);
52       this.updateBackend_();
53     },
54
55     /**
56      * Removes given exception from model and update backend.
57      */
58     removeException: function(exception) {
59       var dataModel = this.dataModel;
60
61       var index = dataModel.indexOf(exception);
62       if (index >= 0) {
63         dataModel.splice(index, 1);
64         this.updateBackend_();
65       }
66     },
67
68     /**
69      * Handles the clicks on the list and triggers exception removal if the
70      * click is on the remove exception button.
71      * @private
72      * @param {!Event} e The click event object.
73      */
74     handleClick_: function(e) {
75       // Handle left button click
76       if (e.button == 0) {
77         var el = e.target;
78         if (el.className == 'remove-exception-button') {
79           this.removeException(el.parentNode.exception);
80         }
81       }
82     },
83
84     /**
85      * Loads given exception list.
86      * @param {Array} exceptions An array of exception object.
87      */
88     load_: function(exceptions) {
89       this.dataModel = new ArrayDataModel(exceptions);
90     },
91
92     /**
93      * Updates backend.
94      */
95     updateBackend_: function() {
96       Preferences.setListPref(this.pref, this.dataModel.slice(), true);
97     }
98   };
99
100   /**
101    * Creates a new exception list item.
102    * @param {Object} exception The exception account this represents.
103    * @constructor
104    * @extends {cr.ui.ListItem}
105    */
106   function ProxyExceptionsItem(exception) {
107     var el = cr.doc.createElement('div');
108     el.exception = exception;
109     ProxyExceptionsItem.decorate(el);
110     return el;
111   }
112
113   /**
114    * Decorates an element as a exception account item.
115    * @param {!HTMLElement} el The element to decorate.
116    */
117   ProxyExceptionsItem.decorate = function(el) {
118     el.__proto__ = ProxyExceptionsItem.prototype;
119     el.decorate();
120   };
121
122   ProxyExceptionsItem.prototype = {
123     __proto__: ListItem.prototype,
124
125     /** @override */
126     decorate: function() {
127       ListItem.prototype.decorate.call(this);
128       this.className = 'exception-list-item';
129
130       var labelException = this.ownerDocument.createElement('span');
131       labelException.className = '';
132       labelException.textContent = this.exception;
133       this.appendChild(labelException);
134     }
135   };
136
137   return {
138     ProxyExceptions: ProxyExceptions
139   };
140 });