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', function() {
7 var OptionsPage = options.OptionsPage;
8 var ArrayDataModel = cr.ui.ArrayDataModel;
9 var DeletableItem = options.DeletableItem;
10 var DeletableItemList = options.DeletableItemList;
12 /////////////////////////////////////////////////////////////////////////////
13 // NetworkPreferences class:
16 * Encapsulated handling of ChromeOS network preferences page.
19 function PreferredNetworks(model) {
20 OptionsPage.call(this,
21 'preferredNetworksPage',
23 'preferredNetworksPage');
26 cr.addSingletonGetter(PreferredNetworks);
28 PreferredNetworks.prototype = {
29 __proto__: OptionsPage.prototype,
32 * Initializes the preferred networks page.
34 initializePage: function() {
35 OptionsPage.prototype.initializePage.call(this);
36 PreferredNetworkList.decorate($('remembered-network-list'));
37 $('preferred-networks-confirm').onclick =
38 OptionsPage.closeOverlay.bind(OptionsPage);
41 update: function(rememberedNetworks) {
42 var list = $('remembered-network-list');
44 for (var i = 0; i < rememberedNetworks.length; i++) {
45 list.append(rememberedNetworks[i]);
53 * Creates a list entry for a remembered network.
54 * @param{{networkName: string,
56 servicePath: string}} data
57 * Description of the network.
60 function PreferredNetworkListItem(data) {
61 var el = cr.doc.createElement('div');
62 el.__proto__ = PreferredNetworkListItem.prototype;
65 el.data[key] = data[key];
70 PreferredNetworkListItem.prototype = {
71 __proto__: DeletableItem.prototype,
74 * Description of the network.
75 * @type {{networkName: string,
76 * networkType: string,
77 * servicePath: string}}
82 decorate: function() {
83 DeletableItem.prototype.decorate.call(this);
84 var label = this.ownerDocument.createElement('div');
85 label.textContent = this.data.networkName;
86 if (this.data.policyManaged)
87 this.deletable = false;
88 this.contentElement.appendChild(label);
93 * Class for displaying a list of preferred networks.
95 * @extends {options.DeletableItemList}
97 var PreferredNetworkList = cr.ui.define('list');
99 PreferredNetworkList.prototype = {
100 __proto__: DeletableItemList.prototype,
103 decorate: function() {
104 DeletableItemList.prototype.decorate.call(this);
105 this.addEventListener('blur', this.onBlur_);
110 * When the list loses focus, unselect all items in the list.
113 onBlur_: function() {
114 this.selectionModel.unselectAll();
118 createItem: function(entry) {
119 return new PreferredNetworkListItem(entry);
123 deleteItemAtIndex: function(index) {
124 var item = this.dataModel.item(index);
126 // Inform the network library that we are forgetting this network.
127 chrome.send('networkCommand',
132 this.dataModel.splice(index, 1);
133 // Invalidate the list since it has a stale cache after a splice
134 // involving a deletion.
140 * Purges all networks from the list.
143 this.dataModel = new ArrayDataModel([]);
148 * Adds a remembered network to the list.
149 * @param {{networkName: string,
151 servicePath: string} data
152 * Description of the network.
154 append: function(data) {
155 this.dataModel.push(data);
161 PreferredNetworks: PreferredNetworks