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.network', function() {
7 var ArrayDataModel = cr.ui.ArrayDataModel;
9 var ListItem = cr.ui.ListItem;
10 var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel;
11 var Menu = cr.ui.Menu;
12 var MenuItem = cr.ui.MenuItem;
13 var ControlledSettingIndicator = options.ControlledSettingIndicator;
16 * Network settings constants. These enums usually match their C++
19 function Constants() {}
22 Constants.TYPE_UNKNOWN = 'UNKNOWN';
23 Constants.TYPE_ETHERNET = 'ethernet';
24 Constants.TYPE_WIFI = 'wifi';
25 Constants.TYPE_WIMAX = 'wimax';
26 Constants.TYPE_BLUETOOTH = 'bluetooth';
27 Constants.TYPE_CELLULAR = 'cellular';
28 Constants.TYPE_VPN = 'vpn';
30 // Cellular activation states:
31 Constants.ACTIVATION_STATE_UNKNOWN = 0;
32 Constants.ACTIVATION_STATE_ACTIVATED = 1;
33 Constants.ACTIVATION_STATE_ACTIVATING = 2;
34 Constants.ACTIVATION_STATE_NOT_ACTIVATED = 3;
35 Constants.ACTIVATION_STATE_PARTIALLY_ACTIVATED = 4;
38 * Order in which controls are to appear in the network list sorted by key.
40 Constants.NETWORK_ORDER = ['ethernet',
48 * Mapping of network category titles to the network type.
51 'cellular': Constants.TYPE_CELLULAR,
52 'ethernet': Constants.TYPE_ETHERNET,
53 'wimax': Constants.TYPE_WIMAX,
54 'wifi': Constants.TYPE_WIFI,
55 'vpn': Constants.TYPE_VPN
59 * ID of the menu that is currently visible.
63 var activeMenu_ = null;
66 * Indicates if cellular networks are available.
70 var cellularAvailable_ = false;
73 * Indicates if cellular networks are enabled.
77 var cellularEnabled_ = false;
80 * Indicates if cellular device supports network scanning.
84 var cellularSupportsScan_ = false;
87 * Indicates if WiMAX networks are available.
91 var wimaxAvailable_ = false;
94 * Indicates if WiMAX networks are enabled.
98 var wimaxEnabled_ = false;
101 * Indicates if mobile data roaming is enabled.
105 var enableDataRoaming_ = false;
108 * Icon to use when not connected to a particular type of network.
109 * @type {!Object.<string, string>} Mapping of network type to icon data url.
112 var defaultIcons_ = {};
115 * Contains the current logged in user type, which is one of 'none',
116 * 'regular', 'owner', 'guest', 'retail-mode', 'public-account',
117 * 'locally-managed', and 'kiosk-app', or empty string if the data has not
122 var loggedInUserType_ = '';
125 * Create an element in the network list for controlling network
127 * @param {Object} data Description of the network list or command.
130 function NetworkListItem(data) {
131 var el = cr.doc.createElement('li');
133 for (var key in data)
134 el.data_[key] = data[key];
135 NetworkListItem.decorate(el);
140 * Decorate an element as a NetworkListItem.
141 * @param {!Element} el The element to decorate.
143 NetworkListItem.decorate = function(el) {
144 el.__proto__ = NetworkListItem.prototype;
148 NetworkListItem.prototype = {
149 __proto__: ListItem.prototype,
152 * Description of the network group or control.
153 * @type {Object.<string,Object>}
159 * Element for the control's subtitle.
166 * Icon for the network control.
173 * Indicates if in the process of connecting to a network.
180 * Description of the network control.
188 * Text label for the subtitle.
193 this.subtitle_.textContent = text;
194 this.subtitle_.hidden = !text;
198 * URL for the network icon.
201 set iconURL(iconURL) {
202 this.icon_.style.backgroundImage = url(iconURL);
206 * Type of network icon. Each type corresponds to a CSS rule.
210 if (defaultIcons_[type])
211 this.iconURL = defaultIcons_[type];
213 this.icon_.classList.add('network-' + type);
217 * Indicates if the network is in the process of being connected.
220 set connecting(state) {
221 this.connecting_ = state;
223 this.icon_.classList.add('network-connecting');
225 this.icon_.classList.remove('network-connecting');
229 * Indicates if the network is in the process of being connected.
233 return this.connecting_;
237 * Set the direction of the text.
238 * @param {string} direction The direction of the text, e.g. 'ltr'.
240 setSubtitleDirection: function(direction) {
241 this.subtitle_.dir = direction;
245 * Indicate that the selector arrow should be shown.
247 showSelector: function() {
248 this.subtitle_.classList.add('network-selector');
252 * Adds an indicator to show that the network is policy managed.
254 showManagedNetworkIndicator: function() {
255 this.appendChild(new ManagedNetworkIndicator());
259 decorate: function() {
260 ListItem.prototype.decorate.call(this);
261 this.className = 'network-group';
262 this.icon_ = this.ownerDocument.createElement('div');
263 this.icon_.className = 'network-icon';
264 this.appendChild(this.icon_);
265 var textContent = this.ownerDocument.createElement('div');
266 textContent.className = 'network-group-labels';
267 this.appendChild(textContent);
268 var categoryLabel = this.ownerDocument.createElement('div');
269 var title = this.data_.key + 'Title';
270 categoryLabel.className = 'network-title';
271 categoryLabel.textContent = loadTimeData.getString(title);
272 textContent.appendChild(categoryLabel);
273 this.subtitle_ = this.ownerDocument.createElement('div');
274 this.subtitle_.className = 'network-subtitle';
275 textContent.appendChild(this.subtitle_);
280 * Creates a control that displays a popup menu when clicked.
281 * @param {Object} data Description of the control.
283 function NetworkMenuItem(data) {
284 var el = new NetworkListItem(data);
285 el.__proto__ = NetworkMenuItem.prototype;
290 NetworkMenuItem.prototype = {
291 __proto__: NetworkListItem.prototype,
294 * Popup menu element.
301 decorate: function() {
302 this.subtitle = null;
303 if (this.data.iconType)
304 this.iconType = this.data.iconType;
305 this.addEventListener('click', function() {
311 * Retrieves the ID for the menu.
313 getMenuName: function() {
314 return this.data_.key + '-network-menu';
318 * Creates a popup menu for the control.
319 * @return {Element} The newly created menu.
321 createMenu: function() {
322 if (this.data.menu) {
323 var menu = this.ownerDocument.createElement('div');
324 menu.id = this.getMenuName();
325 menu.className = 'network-menu';
328 for (var i = 0; i < this.data.menu.length; i++) {
329 var entry = this.data.menu[i];
330 createCallback_(menu, null, entry.label, entry.command);
337 canUpdateMenu: function() {
342 * Displays a popup menu.
344 showMenu: function() {
346 // Force a rescan if opening the menu for WiFi networks to ensure the
347 // list is up to date. Networks are periodically rescanned, but depending
348 // on timing, there could be an excessive delay before the first rescan
350 var rescan = !activeMenu_ && this.data_.key == 'wifi';
353 var existing = $(this.getMenuName());
355 if (this.updateMenu())
359 this.menu_ = this.createMenu();
360 this.menu_.addEventListener('mousedown', function(e) {
361 // Prevent blurring of list, which would close the menu.
364 var parent = $('network-menus');
366 parent.replaceChild(this.menu_, existing);
368 parent.appendChild(this.menu_);
370 var top = this.offsetTop + this.clientHeight;
371 var menuId = this.getMenuName();
372 if (menuId != activeMenu_ || rebuild) {
374 activeMenu_ = menuId;
375 this.menu_.style.setProperty('top', top + 'px');
376 this.menu_.hidden = false;
379 chrome.send('refreshNetworks');
384 * Creates a control for selecting or configuring a network connection based
385 * on the type of connection (e.g. wifi versus vpn).
386 * @param {{key: string,
387 * networkList: Array.<Object>} data Description of the network.
390 function NetworkSelectorItem(data) {
391 var el = new NetworkMenuItem(data);
392 el.__proto__ = NetworkSelectorItem.prototype;
397 NetworkSelectorItem.prototype = {
398 __proto__: NetworkMenuItem.prototype,
401 decorate: function() {
402 // TODO(kevers): Generalize method of setting default label.
403 var policyManaged = false;
404 var defaultMessage = this.data_.key == 'wifi' ?
405 'networkOffline' : 'networkNotConnected';
406 this.subtitle = loadTimeData.getString(defaultMessage);
407 var list = this.data_.networkList;
408 var candidateURL = null;
409 for (var i = 0; i < list.length; i++) {
410 var networkDetails = list[i];
411 if (networkDetails.connecting || networkDetails.connected) {
412 this.subtitle = networkDetails.networkName;
413 this.setSubtitleDirection('ltr');
414 policyManaged = networkDetails.policyManaged;
415 candidateURL = networkDetails.iconURL;
416 // Only break when we see a connecting network as it is possible to
417 // have a connected network and a connecting network at the same
419 if (networkDetails.connecting) {
420 this.connecting = true;
427 this.iconURL = candidateURL;
429 this.iconType = this.data.key;
434 this.showManagedNetworkIndicator();
436 if (activeMenu_ == this.getMenuName()) {
437 // Menu is already showing and needs to be updated. Explicitly calling
438 // show menu will force the existing menu to be replaced. The call
439 // is deferred in order to ensure that position of this element has
440 // beem properly updated.
442 setTimeout(function() {self.showMenu();}, 0);
447 * Creates a menu for selecting, configuring or disconnecting from a
449 * @return {Element} The newly created menu.
451 createMenu: function() {
452 var menu = this.ownerDocument.createElement('div');
453 menu.id = this.getMenuName();
454 menu.className = 'network-menu';
458 if (this.data_.key == 'wifi') {
459 addendum.push({label: loadTimeData.getString('joinOtherNetwork'),
461 data: {networkType: Constants.TYPE_WIFI,
463 } else if (this.data_.key == 'cellular') {
464 if (cellularEnabled_ && cellularSupportsScan_) {
466 label: loadTimeData.getString('otherCellularNetworks'),
467 command: createAddConnectionCallback_(Constants.TYPE_CELLULAR),
468 addClass: ['other-cellulars'],
471 addendum.push(entry);
474 var label = enableDataRoaming_ ? 'disableDataRoaming' :
476 var disabled = loggedInUserType_ != 'owner';
477 var entry = {label: loadTimeData.getString(label),
480 entry.command = null;
482 loadTimeData.getString('dataRoamingDisableToggleTooltip');
485 entry.command = function() {
486 options.Preferences.setBooleanPref(
487 'cros.signed.data_roaming_enabled',
488 !enableDataRoaming_, true);
489 // Force revalidation of the menu the next time it is displayed.
493 addendum.push(entry);
495 var list = this.data.rememberedNetworks;
496 if (list && list.length > 0) {
497 var callback = function(list) {
498 $('remembered-network-list').clear();
499 var dialog = options.PreferredNetworks.getInstance();
500 OptionsPage.showPageByName('preferredNetworksPage', false);
503 addendum.push({label: loadTimeData.getString('preferredNetworks'),
508 var networkGroup = this.ownerDocument.createElement('div');
509 networkGroup.className = 'network-menu-group';
510 list = this.data.networkList;
511 var empty = !list || list.length == 0;
513 for (var i = 0; i < list.length; i++) {
515 this.createNetworkOptionsCallback_(networkGroup, data);
516 if (data.connected) {
517 if (data.networkType == Constants.TYPE_VPN) {
520 var i18nKey = 'disconnectNetwork';
521 addendum.push({label: loadTimeData.getString(i18nKey),
522 command: 'disconnect',
528 if (this.data_.key == 'wifi' || this.data_.key == 'wimax' ||
529 this.data_.key == 'cellular') {
531 if (this.data_.key == 'wifi') {
532 addendum.push({label: loadTimeData.getString('turnOffWifi'),
533 command: function() {
534 chrome.send('disableWifi');
537 } else if (this.data_.key == 'wimax') {
538 addendum.push({label: loadTimeData.getString('turnOffWimax'),
539 command: function() {
540 chrome.send('disableWimax');
543 } else if (this.data_.key == 'cellular') {
544 addendum.push({label: loadTimeData.getString('turnOffCellular'),
545 command: function() {
546 chrome.send('disableCellular');
552 menu.appendChild(networkGroup);
553 if (addendum.length > 0) {
554 var separator = false;
556 menu.appendChild(MenuItem.createSeparator());
559 for (var i = 0; i < addendum.length; i++) {
560 var value = addendum[i];
562 var item = createCallback_(menu, value.data, value.label,
565 item.title = value.tooltip;
567 item.classList.add(value.addClass);
569 } else if (!separator) {
570 menu.appendChild(MenuItem.createSeparator());
579 * Determines if a menu can be updated on the fly. Menus that cannot be
580 * updated are fully regenerated using createMenu. The advantage of
581 * updating a menu is that it can preserve ordering of networks avoiding
582 * entries from jumping around after an update.
584 canUpdateMenu: function() {
585 return this.data_.key == 'wifi' && activeMenu_ == this.getMenuName();
589 * Updates an existing menu. Updated menus preserve ordering of prior
590 * entries. During the update process, the ordering may differ from the
591 * preferred ordering as determined by the network library. If the
592 * ordering becomes potentially out of sync, then the updated menu is
593 * marked for disposal on close. Reopening the menu will force a
594 * regeneration, which will in turn fix the ordering.
595 * @return {boolean} True if successfully updated.
597 updateMenu: function() {
598 if (!this.canUpdateMenu())
600 var oldMenu = $(this.getMenuName());
601 var group = oldMenu.getElementsByClassName('network-menu-group')[0];
604 var newMenu = this.createMenu();
605 var discardOnClose = false;
606 var oldNetworkButtons = this.extractNetworkConnectButtons_(oldMenu);
607 var newNetworkButtons = this.extractNetworkConnectButtons_(newMenu);
608 for (var key in oldNetworkButtons) {
609 if (newNetworkButtons[key]) {
610 group.replaceChild(newNetworkButtons[key].button,
611 oldNetworkButtons[key].button);
612 if (newNetworkButtons[key].index != oldNetworkButtons[key].index)
613 discardOnClose = true;
614 newNetworkButtons[key] = null;
616 // Leave item in list to prevent network items from jumping due to
618 oldNetworkButtons[key].disabled = true;
619 discardOnClose = true;
622 for (var key in newNetworkButtons) {
623 var entry = newNetworkButtons[key];
625 group.appendChild(entry.button);
626 discardOnClose = true;
629 oldMenu.data = {discardOnClose: discardOnClose};
634 * Extracts a mapping of network names to menu element and position.
635 * @param {!Element} menu The menu to process.
636 * @return {Object.<string, Element>} Network mapping.
639 extractNetworkConnectButtons_: function(menu) {
640 var group = menu.getElementsByClassName('network-menu-group')[0];
641 var networkButtons = {};
643 return networkButtons;
644 var buttons = group.getElementsByClassName('network-menu-item');
645 for (var i = 0; i < buttons.length; i++) {
646 var label = buttons[i].data.label;
647 networkButtons[label] = {index: i, button: buttons[i]};
649 return networkButtons;
653 * Adds a menu item for showing network details.
654 * @param {!Element} parent The parent element.
655 * @param {Object} data Description of the network.
658 createNetworkOptionsCallback_: function(parent, data) {
659 var menuItem = createCallback_(parent,
664 if (data.policyManaged)
665 menuItem.appendChild(new ManagedNetworkIndicator());
666 if (data.connected || data.connecting) {
667 var label = menuItem.getElementsByClassName(
668 'network-menu-item-label')[0];
669 label.classList.add('active-network');
675 * Creates a button-like control for configurating internet connectivity.
676 * @param {{key: string,
678 * command: function} data Description of the network control.
681 function NetworkButtonItem(data) {
682 var el = new NetworkListItem(data);
683 el.__proto__ = NetworkButtonItem.prototype;
688 NetworkButtonItem.prototype = {
689 __proto__: NetworkListItem.prototype,
692 decorate: function() {
693 if (this.data.subtitle)
694 this.subtitle = this.data.subtitle;
696 this.subtitle = null;
697 if (this.data.command)
698 this.addEventListener('click', this.data.command);
699 if (this.data.iconURL)
700 this.iconURL = this.data.iconURL;
701 else if (this.data.iconType)
702 this.iconType = this.data.iconType;
703 if (this.data.policyManaged)
704 this.showManagedNetworkIndicator();
709 * Adds a command to a menu for modifying network settings.
710 * @param {!Element} menu Parent menu.
711 * @param {!Object} data Description of the network.
712 * @param {!string} label Display name for the menu item.
713 * @param {?(string|function)} command Callback function or name
714 * of the command for |networkCommand|.
715 * @param {?string=} opt_iconURL Optional URL to an icon for the menu item.
716 * @return {!Element} The created menu item.
719 function createCallback_(menu, data, label, command, opt_iconURL) {
720 var button = menu.ownerDocument.createElement('div');
721 button.className = 'network-menu-item';
723 var buttonIcon = menu.ownerDocument.createElement('div');
724 buttonIcon.className = 'network-menu-item-icon';
725 button.appendChild(buttonIcon);
727 buttonIcon.style.backgroundImage = url(opt_iconURL);
729 var buttonLabel = menu.ownerDocument.createElement('span');
730 buttonLabel.className = 'network-menu-item-label';
731 buttonLabel.textContent = label;
732 button.appendChild(buttonLabel);
734 if (typeof command == 'string') {
735 var type = data.networkType;
736 var path = data.servicePath;
737 callback = function() {
738 chrome.send('networkCommand',
739 [type, path, command]);
742 } else if (command != null) {
744 callback = function() {
749 callback = function() {
755 if (callback != null)
756 button.addEventListener('click', callback);
758 buttonLabel.classList.add('network-disabled-control');
760 button.data = {label: label};
761 MenuItem.decorate(button);
762 menu.appendChild(button);
767 * A list of controls for manipulating network connectivity.
770 var NetworkList = cr.ui.define('list');
772 NetworkList.prototype = {
773 __proto__: List.prototype,
776 decorate: function() {
777 List.prototype.decorate.call(this);
778 this.startBatchUpdates();
779 this.autoExpands = true;
780 this.dataModel = new ArrayDataModel([]);
781 this.selectionModel = new ListSingleSelectionModel();
782 this.addEventListener('blur', this.onBlur_.bind(this));
783 this.selectionModel.addEventListener('change',
784 this.onSelectionChange_.bind(this));
786 // Wi-Fi control is always visible.
787 this.update({key: 'wifi', networkList: []});
790 label: loadTimeData.getString('addConnectionWifi'),
791 command: createAddConnectionCallback_(Constants.TYPE_WIFI)
794 label: loadTimeData.getString('addConnectionVPN'),
795 command: createAddConnectionCallback_(Constants.TYPE_VPN)
797 this.update({key: 'addConnection',
798 iconType: 'add-connection',
799 menu: [entryAddWifi, entryAddVPN]
802 var prefs = options.Preferences.getInstance();
803 prefs.addEventListener('cros.signed.data_roaming_enabled',
805 enableDataRoaming_ = event.value.value;
807 this.endBatchUpdates();
811 * When the list loses focus, unselect all items in the list and close the
815 onBlur_: function() {
816 this.selectionModel.unselectAll();
821 * Close bubble and menu when a different list item is selected.
822 * @param {Event} event Event detailing the selection change.
825 onSelectionChange_: function(event) {
826 OptionsPage.hideBubble();
827 // A list item may temporarily become unselected while it is constructing
828 // its menu. The menu should therefore only be closed if a different item
829 // is selected, not when the menu's owner item is deselected.
831 for (var i = 0; i < event.changes.length; ++i) {
832 if (event.changes[i].selected) {
833 var item = this.dataModel.item(event.changes[i].index);
834 if (!item.getMenuName || item.getMenuName() != activeMenu_) {
844 * Finds the index of a network item within the data model based on
846 * @param {string} key Unique key for the item in the list.
847 * @return {number} The index of the network item, or |undefined| if it is
850 indexOf: function(key) {
851 var size = this.dataModel.length;
852 for (var i = 0; i < size; i++) {
853 var entry = this.dataModel.item(i);
854 if (entry.key == key)
860 * Updates a network control.
861 * @param {Object.<string,string>} data Description of the entry.
863 update: function(data) {
864 this.startBatchUpdates();
865 var index = this.indexOf(data.key);
866 if (index == undefined) {
867 // Find reference position for adding the element. We cannot hide
868 // individual list elements, thus we need to conditionally add or
869 // remove elements and cannot rely on any element having a fixed index.
870 for (var i = 0; i < Constants.NETWORK_ORDER.length; i++) {
871 if (data.key == Constants.NETWORK_ORDER[i]) {
876 var referenceIndex = -1;
877 for (var i = 0; i < this.dataModel.length; i++) {
878 var entry = this.dataModel.item(i);
879 if (entry.sortIndex < data.sortIndex)
884 if (referenceIndex == -1) {
885 // Prepend to the start of the list.
886 this.dataModel.splice(0, 0, data);
887 } else if (referenceIndex == this.dataModel.length) {
888 // Append to the end of the list.
889 this.dataModel.push(data);
891 // Insert after the reference element.
892 this.dataModel.splice(referenceIndex + 1, 0, data);
895 var entry = this.dataModel.item(index);
896 data.sortIndex = entry.sortIndex;
897 this.dataModel.splice(index, 1, data);
899 this.endBatchUpdates();
903 createItem: function(entry) {
904 if (entry.networkList)
905 return new NetworkSelectorItem(entry);
907 return new NetworkButtonItem(entry);
909 return new NetworkMenuItem(entry);
913 * Deletes an element from the list.
914 * @param {string} key Unique identifier for the element.
916 deleteItem: function(key) {
917 var index = this.indexOf(key);
918 if (index != undefined)
919 this.dataModel.splice(index, 1);
923 * Updates the state of a toggle button.
924 * @param {string} key Unique identifier for the element.
925 * @param {boolean} active Whether the control is active.
927 updateToggleControl: function(key, active) {
928 var index = this.indexOf(key);
929 if (index != undefined) {
930 var entry = this.dataModel.item(index);
931 entry.iconType = active ? 'control-active' :
939 * Sets the default icon to use for each network type if disconnected.
940 * @param {!Object.<string, string>} data Mapping of network type to icon
943 NetworkList.setDefaultNetworkIcons = function(data) {
944 defaultIcons_ = Object.create(data);
948 * Sets the current logged in user type.
949 * @param {string} userType Current logged in user type.
951 NetworkList.updateLoggedInUserType = function(userType) {
952 loggedInUserType_ = String(userType);
956 * Chrome callback for updating network controls.
957 * @param {Object} data Description of available network devices and their
958 * corresponding state.
960 NetworkList.refreshNetworkData = function(data) {
961 var networkList = $('network-list');
962 networkList.startBatchUpdates();
963 cellularAvailable_ = data.cellularAvailable;
964 cellularEnabled_ = data.cellularEnabled;
965 cellularSupportsScan_ = data.cellularSupportsScan;
966 wimaxAvailable_ = data.wimaxAvailable;
967 wimaxEnabled_ = data.wimaxEnabled;
969 // Only show Ethernet control if connected.
970 var ethernetConnection = getConnection_(data.wiredList);
971 if (ethernetConnection) {
972 var type = String(Constants.TYPE_ETHERNET);
973 var path = ethernetConnection.servicePath;
974 var ethernetOptions = function() {
975 chrome.send('networkCommand',
976 [type, path, 'options']);
978 networkList.update({key: 'ethernet',
979 subtitle: loadTimeData.getString('networkConnected'),
980 iconURL: ethernetConnection.iconURL,
981 command: ethernetOptions,
982 policyManaged: ethernetConnection.policyManaged});
984 networkList.deleteItem('ethernet');
987 if (data.wifiEnabled)
988 loadData_('wifi', data.wirelessList, data.rememberedList);
990 addEnableNetworkButton_('wifi', 'enableWifi', 'wifi');
992 // Only show cellular control if available.
993 if (data.cellularAvailable) {
994 if (data.cellularEnabled)
995 loadData_('cellular', data.wirelessList, data.rememberedList);
997 addEnableNetworkButton_('cellular', 'enableCellular', 'cellular');
999 networkList.deleteItem('cellular');
1002 // Only show cellular control if available.
1003 if (data.wimaxAvailable) {
1004 if (data.wimaxEnabled)
1005 loadData_('wimax', data.wirelessList, data.rememberedList);
1007 addEnableNetworkButton_('wimax', 'enableWimax', 'cellular');
1009 networkList.deleteItem('wimax');
1012 // Only show VPN control if there is at least one VPN configured.
1013 if (data.vpnList.length > 0)
1014 loadData_('vpn', data.vpnList, data.rememberedList);
1016 networkList.deleteItem('vpn');
1017 networkList.endBatchUpdates();
1021 * Replaces a network menu with a button for reenabling the type of network.
1022 * @param {string} name The type of network (wifi, cellular or wimax).
1023 * @param {string} command The command for reenabling the network.
1024 * @param {string} type of icon (wifi or cellular).
1027 function addEnableNetworkButton_(name, command, icon) {
1028 var subtitle = loadTimeData.getString('networkDisabled');
1029 var enableNetwork = function() {
1030 chrome.send(command);
1032 var networkList = $('network-list');
1033 networkList.update({key: name,
1036 command: enableNetwork});
1040 * Element for indicating a policy managed network.
1043 function ManagedNetworkIndicator() {
1044 var el = cr.doc.createElement('span');
1045 el.__proto__ = ManagedNetworkIndicator.prototype;
1050 ManagedNetworkIndicator.prototype = {
1051 __proto__: ControlledSettingIndicator.prototype,
1054 decorate: function() {
1055 ControlledSettingIndicator.prototype.decorate.call(this);
1056 this.controlledBy = 'policy';
1057 var policyLabel = loadTimeData.getString('managedNetwork');
1058 this.setAttribute('textPolicy', policyLabel);
1059 this.removeAttribute('tabindex');
1063 handleEvent: function(event) {
1064 // Prevent focus blurring as that would close any currently open menu.
1065 if (event.type == 'mousedown')
1067 ControlledSettingIndicator.prototype.handleEvent.call(this, event);
1071 * Handle mouse events received by the bubble, preventing focus blurring as
1072 * that would close any currently open menu and preventing propagation to
1073 * any elements located behind the bubble.
1074 * @param {Event} Mouse event.
1076 stopEvent: function(event) {
1077 event.preventDefault();
1078 event.stopPropagation();
1082 toggleBubble_: function() {
1083 if (activeMenu_ && !$(activeMenu_).contains(this))
1085 ControlledSettingIndicator.prototype.toggleBubble_.call(this);
1086 if (this.showingBubble) {
1087 var bubble = OptionsPage.getVisibleBubble();
1088 bubble.addEventListener('mousedown', this.stopEvent);
1089 bubble.addEventListener('click', this.stopEvent);
1095 * Updates the list of available networks and their status, filtered by
1097 * @param {string} category The type of network.
1098 * @param {Array} available The list of available networks and their status.
1099 * @param {Array} remembered The list of remmebered networks.
1101 function loadData_(category, available, remembered) {
1102 var data = {key: category};
1103 var type = categoryMap[category];
1104 var availableNetworks = [];
1105 for (var i = 0; i < available.length; i++) {
1106 if (available[i].networkType == type)
1107 availableNetworks.push(available[i]);
1109 data.networkList = availableNetworks;
1111 var rememberedNetworks = [];
1112 for (var i = 0; i < remembered.length; i++) {
1113 if (remembered[i].networkType == type)
1114 rememberedNetworks.push(remembered[i]);
1116 data.rememberedNetworks = rememberedNetworks;
1118 $('network-list').update(data);
1122 * Hides the currently visible menu.
1125 function closeMenu_() {
1127 var menu = $(activeMenu_);
1129 if (menu.data && menu.data.discardOnClose)
1130 menu.parentNode.removeChild(menu);
1136 * Fetches the active connection.
1137 * @param {Array.<Object>} networkList List of networks.
1138 * @return {boolean} True if connected or connecting to a network.
1141 function getConnection_(networkList) {
1144 for (var i = 0; i < networkList.length; i++) {
1145 var entry = networkList[i];
1146 if (entry.connected || entry.connecting)
1153 * Create a callback function that adds a new connection of the given type.
1154 * @param {!number} type A network type Constants.TYPE_*.
1155 * @return {function()} The created callback.
1158 function createAddConnectionCallback_(type) {
1160 chrome.send('networkCommand', [String(type), '', 'add']);
1165 * Whether the Network list is disabled. Only used for display purpose.
1168 cr.defineProperty(NetworkList, 'disabled', cr.PropertyKind.BOOL_ATTR);
1172 NetworkList: NetworkList