Home | History | Annotate | Download | only in new
      1 // Copyright 2013 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 /**
      6  * @fileoverview Keeps track of all the existing
      7  * PlayerProperty objects and is the entry-point for messages from the backend.
      8  */
      9 var PlayerManager = (function() {
     10   'use strict';
     11 
     12   function PlayerManager(renderManager) {
     13     this.players_ = {};
     14     this.renderman_ = renderManager;
     15     renderManager.playerManager = this;
     16 
     17     this.shouldRemovePlayer_ = function() {
     18       // This is only temporary until we get the UI hooked up.
     19       return true;
     20     };
     21   }
     22 
     23   PlayerManager.prototype = {
     24 
     25     /**
     26      * Adds a player to the list of players to manage.
     27      */
     28     addPlayer: function(id) {
     29       if (this.players_[id]) {
     30         return;
     31       }
     32       // Make the PlayerProperty and add it to the mapping
     33       this.players_[id] = new PlayerInfo(id);
     34 
     35       this.renderman_.redrawList();
     36     },
     37 
     38     /**
     39      * Attempts to remove a player from the UI.
     40      * @param id The ID of the player to remove.
     41      */
     42     removePlayer: function(id) {
     43       // Look at the check box to see if we should actually
     44       // remove it from the UI
     45       if (this.shouldRemovePlayer_()) {
     46         delete this.players_[id];
     47         this.renderman_.redrawList();
     48       } else if (this.players_[id]) {
     49         // Set a property on it to be removed at a later time
     50         this.players_[id].toRemove = true;
     51       }
     52     },
     53 
     54     /**
     55      * Selects a player and displays it on the UI.
     56      * This method is called from the UI.
     57      * @param id The ID of the player to display.
     58      */
     59     selectPlayer: function(id) {
     60       if (!this.players_[id]) {
     61         throw new Error('[selectPlayer] Id ' + id + ' does not exist.');
     62       }
     63 
     64       this.renderman_.select(id);
     65     },
     66 
     67     updatePlayerInfoNoRecord: function(id, timestamp, key, value) {
     68       if (!this.players_[id]) {
     69         console.error('[updatePlayerInfo] Id ' + id +
     70           ' does not exist');
     71         return;
     72       }
     73 
     74       this.players_[id].addPropertyNoRecord(timestamp, key, value);
     75 
     76       // If we can potentially rename the player, do so.
     77       if (key === 'name' || key === 'url') {
     78         this.renderman_.redrawList();
     79       }
     80 
     81       this.renderman_.update();
     82     },
     83 
     84     /**
     85      *
     86      * @param id The unique ID that identifies the player to be updated.
     87      * @param timestamp The timestamp of when the change occured.  This
     88      * timestamp is *not* normalized.
     89      * @param key The name of the property to be added/changed.
     90      * @param value The value of the property.
     91      */
     92     updatePlayerInfo: function(id, timestamp, key, value) {
     93       if (!this.players_[id]) {
     94         console.error('[updatePlayerInfo] Id ' + id +
     95           ' does not exist');
     96         return;
     97       }
     98 
     99       this.players_[id].addProperty(timestamp, key, value);
    100 
    101       // If we can potentially rename the player, do so.
    102       if (key === 'name' || key === 'url') {
    103         this.renderman_.redrawList();
    104       }
    105 
    106       this.renderman_.update();
    107     }
    108   };
    109 
    110   return PlayerManager;
    111 }());
    112