OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('network.status', function() { | 5 cr.define('network.status', function() { |
6 var ArrayDataModel = cr.ui.ArrayDataModel; | 6 var ArrayDataModel = cr.ui.ArrayDataModel; |
7 var List = cr.ui.List; | 7 var List = cr.ui.List; |
8 var ListItem = cr.ui.ListItem; | 8 var ListItem = cr.ui.ListItem; |
9 var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | 9 var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; |
10 | 10 |
11 /** | 11 /** |
12 * Returns the entries of |dataModel| as an array. | 12 * Returns the entries of |dataModel| as an array. |
13 * @param {ArrayDataModel} dataModel . | 13 * @param {ArrayDataModel} dataModel . |
14 * @return {Array} . | 14 * @return {Array} . |
15 */ | 15 */ |
16 function dataModelToArray(dataModel) { | 16 function dataModelToArray(dataModel) { |
17 var array = []; | 17 var array = []; |
18 for (var i = 0; i < dataModel.length; i++) { | 18 for (var i = 0; i < dataModel.length; i++) { |
19 array.push(dataModel.item(i)); | 19 array.push(dataModel.item(i)); |
20 } | 20 } |
21 return array; | 21 return array; |
22 } | 22 } |
23 | 23 |
24 /** | 24 /** |
25 * Calculates both set difference of |a| and |b| and returns them in an array: | 25 * Calculates both set difference of |a| and |b| and returns them in an array: |
26 * [ a - b, b - a ]. | 26 * [ a - b, b - a ]. |
27 * @param {Array.<T>} a . | 27 * @param {Array<T>} a . |
28 * @param {Array.<T>} b . | 28 * @param {Array<T>} b . |
29 * @param {function(T): K} toKey . | 29 * @param {function(T): K} toKey . |
30 * @return {Array.<Array.<T>>} . | 30 * @return {Array<Array<T>>} . |
31 */ | 31 */ |
32 function differenceBy(a, b, toKey) { | 32 function differenceBy(a, b, toKey) { |
33 var inA = {}; | 33 var inA = {}; |
34 a.forEach(function(elA) { | 34 a.forEach(function(elA) { |
35 inA[toKey(elA)] = elA; | 35 inA[toKey(elA)] = elA; |
36 }); | 36 }); |
37 var bMinusA = []; | 37 var bMinusA = []; |
38 b.forEach(function(elB) { | 38 b.forEach(function(elB) { |
39 var keyB = toKey(elB); | 39 var keyB = toKey(elB); |
40 if (inA[keyB]) | 40 if (inA[keyB]) |
(...skipping 24 matching lines...) Expand all Loading... |
65 toRemove.forEach(function(element) { | 65 toRemove.forEach(function(element) { |
66 dataModel.splice(dataModel.indexOf(element), 1); | 66 dataModel.splice(dataModel.indexOf(element), 1); |
67 }); | 67 }); |
68 dataModel.splice.apply(dataModel, [dataModel.length, 0].concat(toAdd)); | 68 dataModel.splice.apply(dataModel, [dataModel.length, 0].concat(toAdd)); |
69 uiList.endBatchUpdates(); | 69 uiList.endBatchUpdates(); |
70 } | 70 } |
71 | 71 |
72 /** | 72 /** |
73 * Creates a map of the entries of |array|. Each entry is associated to the | 73 * Creates a map of the entries of |array|. Each entry is associated to the |
74 * key getKey(entry). | 74 * key getKey(entry). |
75 * @param {Array.<T>} array . | 75 * @param {Array<T>} array . |
76 * @param {function(T): K} getKey . | 76 * @param {function(T): K} getKey . |
77 * @return {Object.<K, T>} . | 77 * @return {Object<K, T>} . |
78 */ | 78 */ |
79 function createMapFromList(array, getKey) { | 79 function createMapFromList(array, getKey) { |
80 var result = {}; | 80 var result = {}; |
81 array.forEach(function(entry) { | 81 array.forEach(function(entry) { |
82 result[getKey(entry)] = entry; | 82 result[getKey(entry)] = entry; |
83 }); | 83 }); |
84 return result; | 84 return result; |
85 } | 85 } |
86 | 86 |
87 /** | 87 /** |
88 * Wraps each entry in |array| into an array. The result contains rows with | 88 * Wraps each entry in |array| into an array. The result contains rows with |
89 * one entry each. | 89 * one entry each. |
90 * @param {Array.<T>} array . | 90 * @param {Array<T>} array . |
91 * @return {Array.<Array.<T>>} . | 91 * @return {Array<Array<T>>} . |
92 */ | 92 */ |
93 function arrayToTable(array) { | 93 function arrayToTable(array) { |
94 return array.map(function(e) { | 94 return array.map(function(e) { |
95 return [e]; | 95 return [e]; |
96 }); | 96 }); |
97 } | 97 } |
98 | 98 |
99 /** | 99 /** |
100 * The NetworkStatusList contains various button types according to the | 100 * The NetworkStatusList contains various button types according to the |
101 * following hierarchy. Note: this graph doesn't depict inheritance but | 101 * following hierarchy. Note: this graph doesn't depict inheritance but |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 * @override | 225 * @override |
226 */ | 226 */ |
227 decorate: function() { | 227 decorate: function() { |
228 this.dropdown_ = null; | 228 this.dropdown_ = null; |
229 this.addEventListener('click', this.toggleDropdown_.bind(this)); | 229 this.addEventListener('click', this.toggleDropdown_.bind(this)); |
230 }, | 230 }, |
231 | 231 |
232 /** | 232 /** |
233 * Returns the list of identifiers for which nested buttons will be created. | 233 * Returns the list of identifiers for which nested buttons will be created. |
234 * To be overridden by subclasses. | 234 * To be overridden by subclasses. |
235 * @return {Array.<string>} . | 235 * @return {Array<string>} . |
236 */ | 236 */ |
237 getEntries: function() { | 237 getEntries: function() { |
238 return []; | 238 return []; |
239 }, | 239 }, |
240 | 240 |
241 /** | 241 /** |
242 * Creates a nested button for |entry| of the current |getEntries|. | 242 * Creates a nested button for |entry| of the current |getEntries|. |
243 * To be overridden by subclasses. | 243 * To be overridden by subclasses. |
244 * @param {string} entry . | 244 * @param {string} entry . |
245 * @return {ListItem} . | 245 * @return {ListItem} . |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 /** | 403 /** |
404 * A map from button key to index according to |BUTTON_ORDER|. | 404 * A map from button key to index according to |BUTTON_ORDER|. |
405 */ | 405 */ |
406 var BUTTON_POSITION = {}; | 406 var BUTTON_POSITION = {}; |
407 BUTTON_ORDER.forEach(function(entry, index) { | 407 BUTTON_ORDER.forEach(function(entry, index) { |
408 BUTTON_POSITION[entry] = index; | 408 BUTTON_POSITION[entry] = index; |
409 }); | 409 }); |
410 | 410 |
411 /** | 411 /** |
412 * Groups networks by type. | 412 * Groups networks by type. |
413 * @param {Object.<string, Object>} networkByID A map from network ID to | 413 * @param {Object<string, Object>} networkByID A map from network ID to |
414 * network properties. | 414 * network properties. |
415 * @return {Object.<string, Array.<string>>} A map from network type to the | 415 * @return {Object<string, Array<string>>} A map from network type to the |
416 * list of IDs of networks of that type. | 416 * list of IDs of networks of that type. |
417 */ | 417 */ |
418 function createNetworkIDsByType(networkByID) { | 418 function createNetworkIDsByType(networkByID) { |
419 var byType = {}; | 419 var byType = {}; |
420 for (var id in networkByID) { | 420 for (var id in networkByID) { |
421 var network = networkByID[id]; | 421 var network = networkByID[id]; |
422 var group = byType[network.Type]; | 422 var group = byType[network.Type]; |
423 if (group === undefined) { | 423 if (group === undefined) { |
424 group = []; | 424 group = []; |
425 byType[network.Type] = group; | 425 byType[network.Type] = group; |
(...skipping 21 matching lines...) Expand all Loading... |
447 List.prototype.decorate.call(this); | 447 List.prototype.decorate.call(this); |
448 | 448 |
449 /** | 449 /** |
450 * The currently open unfolding button. | 450 * The currently open unfolding button. |
451 * @type {UnfoldingButton} | 451 * @type {UnfoldingButton} |
452 */ | 452 */ |
453 this.openDropdown = null; | 453 this.openDropdown = null; |
454 | 454 |
455 /** | 455 /** |
456 * The set of technologies shown to the user. | 456 * The set of technologies shown to the user. |
457 * @type {Object.<string, boolean>} | 457 * @type {Object<string, boolean>} |
458 */ | 458 */ |
459 this.technologies_ = {}; | 459 this.technologies_ = {}; |
460 | 460 |
461 /** | 461 /** |
462 * A map from network type to the array of IDs of network of that type. | 462 * A map from network type to the array of IDs of network of that type. |
463 * @type {Object.<string, Array.<string>>} | 463 * @type {Object<string, Array<string>>} |
464 */ | 464 */ |
465 this.networkIDsByType_ = {}; | 465 this.networkIDsByType_ = {}; |
466 | 466 |
467 /** | 467 /** |
468 * A map from network ID to the network's properties. | 468 * A map from network ID to the network's properties. |
469 * @type {Object.<string, Object>} | 469 * @type {Object<string, Object>} |
470 */ | 470 */ |
471 this.networkByID_ = {}; | 471 this.networkByID_ = {}; |
472 | 472 |
473 /** | 473 /** |
474 * A map from network ID to the network's position in the last received | 474 * A map from network ID to the network's position in the last received |
475 * network list. | 475 * network list. |
476 * @type {Object.<string, number>} | 476 * @type {Object<string, number>} |
477 */ | 477 */ |
478 this.networkIndexByID_ = {}; | 478 this.networkIndexByID_ = {}; |
479 | 479 |
480 /** | 480 /** |
481 * A function that handles the various user actions. | 481 * A function that handles the various user actions. |
482 * See |setUserActionHandler|. | 482 * See |setUserActionHandler|. |
483 * @type {function({command: string, networkID: string})} | 483 * @type {function({command: string, networkID: string})} |
484 */ | 484 */ |
485 this.userActionHandler_ = function() {}; | 485 this.userActionHandler_ = function() {}; |
486 | 486 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 * |command| will be one of | 526 * |command| will be one of |
527 * - openConfiguration | 527 * - openConfiguration |
528 * @param {function({command: string, networkID: string})} handler . | 528 * @param {function({command: string, networkID: string})} handler . |
529 */ | 529 */ |
530 setUserActionHandler: function(handler) { | 530 setUserActionHandler: function(handler) { |
531 this.userActionHandler_ = handler; | 531 this.userActionHandler_ = handler; |
532 }, | 532 }, |
533 | 533 |
534 /** | 534 /** |
535 * @param {string} technology . | 535 * @param {string} technology . |
536 * @return {Array.<string>} Array of network IDs. | 536 * @return {Array<string>} Array of network IDs. |
537 */ | 537 */ |
538 getNetworkIDsOfType: function(technology) { | 538 getNetworkIDsOfType: function(technology) { |
539 var networkIDs = this.networkIDsByType_[technology]; | 539 var networkIDs = this.networkIDsByType_[technology]; |
540 if (!networkIDs) | 540 if (!networkIDs) |
541 return []; | 541 return []; |
542 return networkIDs; | 542 return networkIDs; |
543 }, | 543 }, |
544 | 544 |
545 /** | 545 /** |
546 * @param {string} networkID . | 546 * @param {string} networkID . |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 var keys = Object.keys(this.technologies_); | 592 var keys = Object.keys(this.technologies_); |
593 // Add keys of always visible toplevel buttons. | 593 // Add keys of always visible toplevel buttons. |
594 keys.push('addConnection'); | 594 keys.push('addConnection'); |
595 updateDataModel(this, arrayToTable(keys)); | 595 updateDataModel(this, arrayToTable(keys)); |
596 this.items.forEach(function(button) { | 596 this.items.forEach(function(button) { |
597 button.update(); | 597 button.update(); |
598 }); | 598 }); |
599 }, | 599 }, |
600 | 600 |
601 /** | 601 /** |
602 * @param {Array.<string>} networkIDs . | 602 * @param {Array<string>} networkIDs . |
603 */ | 603 */ |
604 updateIndexes_: function(networkIDs) { | 604 updateIndexes_: function(networkIDs) { |
605 var newNetworkIndexByID = {}; | 605 var newNetworkIndexByID = {}; |
606 networkIDs.forEach(function(id, index) { | 606 networkIDs.forEach(function(id, index) { |
607 newNetworkIndexByID[id] = index; | 607 newNetworkIndexByID[id] = index; |
608 }); | 608 }); |
609 this.networkIndexByID_ = newNetworkIndexByID; | 609 this.networkIndexByID_ = newNetworkIndexByID; |
610 }, | 610 }, |
611 | 611 |
612 /** | 612 /** |
613 * @param {Array.<string>} networkIDs . | 613 * @param {Array<string>} networkIDs . |
614 */ | 614 */ |
615 onNetworkListChanged_: function(networkIDs) { | 615 onNetworkListChanged_: function(networkIDs) { |
616 var diff = differenceBy(Object.keys(this.networkByID_), | 616 var diff = differenceBy(Object.keys(this.networkByID_), |
617 networkIDs, | 617 networkIDs, |
618 function(e) { return e; }); | 618 function(e) { return e; }); |
619 var toRemove = diff[0]; | 619 var toRemove = diff[0]; |
620 var toAdd = diff[1]; | 620 var toAdd = diff[1]; |
621 | 621 |
622 var addCallback = this.addNetworkCallback_.bind(this); | 622 var addCallback = this.addNetworkCallback_.bind(this); |
623 toAdd.forEach(function(id) { | 623 toAdd.forEach(function(id) { |
624 console.log('NetworkStatus: Network ' + id + ' added.'); | 624 console.log('NetworkStatus: Network ' + id + ' added.'); |
625 chrome.networkingPrivate.getProperties(id, addCallback); | 625 chrome.networkingPrivate.getProperties(id, addCallback); |
626 }); | 626 }); |
627 | 627 |
628 toRemove.forEach(function(id) { | 628 toRemove.forEach(function(id) { |
629 console.log('NetworkStatus: Network ' + id + ' removed.'); | 629 console.log('NetworkStatus: Network ' + id + ' removed.'); |
630 delete this.networkByID_[id]; | 630 delete this.networkByID_[id]; |
631 }, this); | 631 }, this); |
632 | 632 |
633 this.updateIndexes_(networkIDs); | 633 this.updateIndexes_(networkIDs); |
634 this.updateDataStructuresAndButtons_(); | 634 this.updateDataStructuresAndButtons_(); |
635 }, | 635 }, |
636 | 636 |
637 /** | 637 /** |
638 * @param {Array.<string>} networkIDs . | 638 * @param {Array<string>} networkIDs . |
639 */ | 639 */ |
640 onNetworksChanged_: function(networkIDs) { | 640 onNetworksChanged_: function(networkIDs) { |
641 var updateCallback = this.updateNetworkCallback_.bind(this); | 641 var updateCallback = this.updateNetworkCallback_.bind(this); |
642 networkIDs.forEach(function(id) { | 642 networkIDs.forEach(function(id) { |
643 console.log('NetworkStatus: Network ' + id + ' changed.'); | 643 console.log('NetworkStatus: Network ' + id + ' changed.'); |
644 chrome.networkingPrivate.getProperties(id, updateCallback); | 644 chrome.networkingPrivate.getProperties(id, updateCallback); |
645 }); | 645 }); |
646 }, | 646 }, |
647 | 647 |
648 /** | 648 /** |
649 * @param {Object} network . | 649 * @param {Object} network . |
650 */ | 650 */ |
651 updateNetworkCallback_: function(network) { | 651 updateNetworkCallback_: function(network) { |
652 this.networkByID_[network.GUID] = network; | 652 this.networkByID_[network.GUID] = network; |
653 this.getTechnologyButtonForType_(network.Type).update(); | 653 this.getTechnologyButtonForType_(network.Type).update(); |
654 }, | 654 }, |
655 | 655 |
656 /** | 656 /** |
657 * @param {Object} network . | 657 * @param {Object} network . |
658 */ | 658 */ |
659 addNetworkCallback_: function(network) { | 659 addNetworkCallback_: function(network) { |
660 this.networkByID_[network.GUID] = network; | 660 this.networkByID_[network.GUID] = network; |
661 this.updateDataStructuresAndButtons_(); | 661 this.updateDataStructuresAndButtons_(); |
662 }, | 662 }, |
663 | 663 |
664 /** | 664 /** |
665 * @param {Array.<Object>} networks . | 665 * @param {Array<Object>} networks . |
666 */ | 666 */ |
667 setVisibleNetworks: function(networks) { | 667 setVisibleNetworks: function(networks) { |
668 this.networkByID_ = createMapFromList( | 668 this.networkByID_ = createMapFromList( |
669 networks, | 669 networks, |
670 function(network) { | 670 function(network) { |
671 return network.GUID; | 671 return network.GUID; |
672 }); | 672 }); |
673 this.updateIndexes_(networks.map(function(network) { | 673 this.updateIndexes_(networks.map(function(network) { |
674 return network.GUID; | 674 return network.GUID; |
675 })); | 675 })); |
(...skipping 12 matching lines...) Expand all Loading... |
688 chrome.networkingPrivate.getNetworks( | 688 chrome.networkingPrivate.getNetworks( |
689 { 'networkType': 'All', 'visible': true }, | 689 { 'networkType': 'All', 'visible': true }, |
690 this.setVisibleNetworks.bind(this)); | 690 this.setVisibleNetworks.bind(this)); |
691 } | 691 } |
692 }; | 692 }; |
693 | 693 |
694 return { | 694 return { |
695 NetworkStatusList: NetworkStatusList | 695 NetworkStatusList: NetworkStatusList |
696 }; | 696 }; |
697 }); | 697 }); |
OLD | NEW |