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