OLD | NEW |
(Empty) | |
| 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.network', function() { |
| 6 |
| 7 var ArrayDataModel = cr.ui.ArrayDataModel; |
| 8 var List = cr.ui.List; |
| 9 var ListItem = cr.ui.ListItem; |
| 10 |
| 11 /** |
| 12 * Network settings constants. These enums usually match their C++ |
| 13 * counterparts. |
| 14 */ |
| 15 function Constants() {} |
| 16 |
| 17 // Network types: |
| 18 Constants.TYPE_UNKNOWN = 0; |
| 19 Constants.TYPE_ETHERNET = 1; |
| 20 Constants.TYPE_WIFI = 2; |
| 21 Constants.TYPE_WIMAX = 3; |
| 22 Constants.TYPE_BLUETOOTH = 4; |
| 23 Constants.TYPE_CELLULAR = 5; |
| 24 Constants.TYPE_VPN = 6; |
| 25 |
| 26 /** |
| 27 * Create an element in the network list for controlling network |
| 28 * connectivity. |
| 29 * @constructor |
| 30 */ |
| 31 function NetworkListItem(data) { |
| 32 var el = cr.doc.createElement('li'); |
| 33 el.data_ = {}; |
| 34 for (var key in data) |
| 35 el.data_[key] = data[key]; |
| 36 NetworkListItem.decorate(el); |
| 37 return el; |
| 38 } |
| 39 |
| 40 /** |
| 41 * Decorates an element as a NetworkList. |
| 42 * @param{!Element} el The element to decorate. |
| 43 */ |
| 44 NetworkListItem.decorate = function(el) { |
| 45 el.__proto__ = NetworkListItem.prototype; |
| 46 el.decorate(); |
| 47 }; |
| 48 |
| 49 NetworkListItem.prototype = { |
| 50 __proto__: ListItem.prototype, |
| 51 |
| 52 /** |
| 53 * Description of the network. |
| 54 * @type {{key: string, |
| 55 * networkList: Array}} |
| 56 * @private |
| 57 */ |
| 58 data_: null, |
| 59 |
| 60 /* @inheritDoc */ |
| 61 decorate: function() { |
| 62 ListItem.prototype.decorate.call(this); |
| 63 this.className = 'network-group'; |
| 64 var networkIcon = this.ownerDocument.createElement('div'); |
| 65 networkIcon.className = 'network-icon'; |
| 66 this.appendChild(networkIcon); |
| 67 var textContent = this.ownerDocument.createElement('div'); |
| 68 this.appendChild(textContent); |
| 69 var categoryLabel = this.ownerDocument.createElement('div'); |
| 70 var title = this.data_.key + 'Title'; |
| 71 categoryLabel.className = 'network-title'; |
| 72 categoryLabel.textContent = templateData[title]; |
| 73 textContent.appendChild(categoryLabel); |
| 74 var selector = this.ownerDocument.createElement('span'); |
| 75 selector.className = 'network-selector'; |
| 76 textContent.appendChild(selector); |
| 77 if (this.data_.networkList) { |
| 78 // TODO(kevers): Generalize method of setting default label. |
| 79 var defaultMessage = this.data_.key == 'wifi' ? |
| 80 'networkOffline' : 'joinNetwork'; |
| 81 selector.textContent = templateData[defaultMessage]; |
| 82 var list = this.data_.networkList; |
| 83 for (var i = 0; i < list.length; i++) { |
| 84 var networkDetails = list[i]; |
| 85 if (networkDetails.connected || networkDetails.connecting) { |
| 86 selector.textContent = networkDetails.networkName; |
| 87 networkIcon.style.backgroundImage = url(networkDetails.iconURL); |
| 88 break; |
| 89 } |
| 90 } |
| 91 } |
| 92 // TODO(kevers): Create popup menu for network lists. |
| 93 // TODO(kevers): Add default icon when no network is connected or |
| 94 // connecting. |
| 95 // TODO(kevers): Add support for other types of list items including a |
| 96 // toggle control for airplane mode, and a control for a new conenction |
| 97 // dialog. |
| 98 }, |
| 99 |
| 100 }; |
| 101 |
| 102 /** |
| 103 * A list of controls for manipulating network connectivity. |
| 104 * @constructor |
| 105 */ |
| 106 var NetworkList = cr.ui.define('list'); |
| 107 |
| 108 NetworkList.prototype = { |
| 109 __proto__: List.prototype, |
| 110 |
| 111 /** @inheritDoc */ |
| 112 decorate: function() { |
| 113 List.prototype.decorate.call(this); |
| 114 this.addEventListener('blur', this.onBlur_); |
| 115 this.dataModel = new ArrayDataModel([]); |
| 116 }, |
| 117 |
| 118 /** |
| 119 * When the list loses focus, unselect all items in the list. |
| 120 * @private |
| 121 */ |
| 122 onBlur_: function() { |
| 123 this.selectionModel.unselectAll(); |
| 124 }, |
| 125 |
| 126 /** |
| 127 * Finds the index of a network item within the data model based on |
| 128 * category. |
| 129 * @param {string} key Unique key for the item in the list. |
| 130 * @return {number|undefined} |
| 131 */ |
| 132 indexOf: function(key) { |
| 133 var size = this.dataModel.length; |
| 134 for (var i = 0; i < size; i++) { |
| 135 var entry = this.dataModel.item(i); |
| 136 if (entry.key == key) |
| 137 return i; |
| 138 } |
| 139 }, |
| 140 |
| 141 /** |
| 142 * Updates a network control. |
| 143 * @param{Object.<string,string>} data Description of the entry. |
| 144 */ |
| 145 update: function(data) { |
| 146 var index = this.indexOf(data.key); |
| 147 if (index == undefined) { |
| 148 this.dataModel.push(data); |
| 149 this.redraw(); |
| 150 } else { |
| 151 this.dataModel.splice(index, 1, data); |
| 152 this.redrawItem(index); |
| 153 } |
| 154 }, |
| 155 |
| 156 /** @inheritDoc */ |
| 157 createItem: function(entry) { |
| 158 return new NetworkListItem(entry); |
| 159 }, |
| 160 }; |
| 161 |
| 162 /** |
| 163 * Chrome callback for updating network controls. |
| 164 * @param{Object} data Description of available network devices and their |
| 165 * corresponding state. |
| 166 */ |
| 167 NetworkList.refreshNetworkData = function(data) { |
| 168 loadData_('wifi', |
| 169 data.wirelessList, |
| 170 function(data) { |
| 171 return data.networkType == Constants.TYPE_WIFI; |
| 172 }); |
| 173 loadData_('cellular', |
| 174 data.wirelessList, |
| 175 function(data) { |
| 176 return data.networkType == Constants.TYPE_CELLULAR; |
| 177 }); |
| 178 loadData_('vpn', data.vpnList); |
| 179 }; |
| 180 |
| 181 /** |
| 182 * Updates the list of available networks and their status filtered by |
| 183 * network type. |
| 184 * @param{string} category The type of network. |
| 185 * @param{Array} list The list of networks and their status. |
| 186 * @param{function(Object)=} opt_filter Optional filter for pruning the list |
| 187 * of networks. |
| 188 */ |
| 189 function loadData_(category, list, opt_filter) { |
| 190 var data = {key: category, networkList: list}; |
| 191 if (opt_filter) { |
| 192 var filteredList = []; |
| 193 for (var i = 0; i < list.length; i++) { |
| 194 if (opt_filter(list[i])) |
| 195 filteredList.push(list[i]); |
| 196 } |
| 197 data.networkList = filteredList; |
| 198 } |
| 199 $('network-list').update(data); |
| 200 } |
| 201 |
| 202 // Export |
| 203 return { |
| 204 NetworkList: NetworkList |
| 205 }; |
| 206 |
| 207 }); |
OLD | NEW |