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
5cr.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    /**
43     * @override
44     * @param {Object} exception
45     */
46    createItem: function(exception) {
47      return new ProxyExceptionsItem(exception);
48    },
49
50    /**
51     * Adds given exception to model and update backend.
52     * @param {Object} exception A exception to be added to exception list.
53     */
54    addException: function(exception) {
55      this.dataModel.push(exception);
56      this.updateBackend_();
57    },
58
59    /**
60     * Removes given exception from model and update backend.
61     */
62    removeException: function(exception) {
63      var dataModel = this.dataModel;
64
65      var index = dataModel.indexOf(exception);
66      if (index >= 0) {
67        dataModel.splice(index, 1);
68        this.updateBackend_();
69      }
70    },
71
72    /**
73     * Handles the clicks on the list and triggers exception removal if the
74     * click is on the remove exception button.
75     * @private
76     * @param {!Event} e The click event object.
77     */
78    handleClick_: function(e) {
79      // Handle left button click
80      if (e.button == 0) {
81        var el = e.target;
82        if (el.className == 'remove-exception-button') {
83          this.removeException(el.parentNode.exception);
84        }
85      }
86    },
87
88    /**
89     * Loads given exception list.
90     * @param {!Array} exceptions An array of exception object.
91     */
92    load_: function(exceptions) {
93      this.dataModel = new ArrayDataModel(exceptions);
94    },
95
96    /**
97     * Updates backend.
98     */
99    updateBackend_: function() {
100      Preferences.setListPref(this.pref, this.dataModel.slice(), true);
101    }
102  };
103
104  /**
105   * Creates a new exception list item.
106   * @param {Object} exception The exception account this represents.
107   * @constructor
108   * @extends {cr.ui.ListItem}
109   */
110  function ProxyExceptionsItem(exception) {
111    var el = cr.doc.createElement('div');
112    el.exception = exception;
113    ProxyExceptionsItem.decorate(el);
114    return el;
115  }
116
117  /**
118   * Decorates an element as a exception account item.
119   * @param {!HTMLElement} el The element to decorate.
120   */
121  ProxyExceptionsItem.decorate = function(el) {
122    el.__proto__ = ProxyExceptionsItem.prototype;
123    el.decorate();
124  };
125
126  ProxyExceptionsItem.prototype = {
127    __proto__: ListItem.prototype,
128
129    /** @override */
130    decorate: function() {
131      ListItem.prototype.decorate.call(this);
132      this.className = 'exception-list-item';
133
134      var labelException = this.ownerDocument.createElement('span');
135      labelException.className = '';
136      labelException.textContent = this.exception;
137      this.appendChild(labelException);
138    }
139  };
140
141  return {
142    ProxyExceptions: ProxyExceptions
143  };
144});
145