all files / app/teams/ teams.controller.js

100% Statements 69/69
90.91% Branches 20/22
100% Functions 17/17
100% Lines 67/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                          26×   26×                     26×     26×                                                                                                 11× 11× 10×                 11×                              
/**
 * @ngdoc controller
 * @name app.teams.controller:TeamsController
 * @description
 * Controller for displaying and editing teams
 *
 */
(function () {
  'use strict';
 
  angular
    .module('app.teams')
    .controller('TeamsController', Controller);
 
  /** @ngInject */
  function Controller($q, $filter, $log, $scope, crudHelper, teamsPath, playersSelectOptions, response) {
    var vm = this;
 
    activate();
 
    /**
     * @ngdoc function
     * @name activate
     * @methodOf app.teams.controller:TeamsController
     * @description
     * Initialize the controller:
     * * Add playerOptionsList object
     * * Call the crudHelper service with team-specific options.
     */
    function activate() {
      vm.playerOptionsList = {list: null};
 
      // authHelper(vm, $scope);
      crudHelper(vm,
        {
          response: response,
          resourceName: teamsPath,
          prepareToCreateEntity: prepareToCreateEntity,
          prepareToUpdateEntity: prepareToUpdateEntity,
          beforeShowNewEntity: beforeShowNewEntity,
          beforeShowEditEntity: beforeShowEditEntity,
          getEntityDisplayName: getEntityDisplayName,
          makeEntityBody: makeEntityBody,
          entityKind: 'Team',
          scope: $scope,
          errorsMap: {
            names: [
              'name',
              'first_player',
              'second_player'
            ]
          }
        }
      );
    }
 
    function prepareToCreateEntity(entity) {
      var result = {
        name: entity.name
      };
      prepareToApplyEntity(entity, result);
      return result;
    }
 
    function prepareToUpdateEntity(entity) {
      var result = {
        id: entity.id,
        name: entity.name
      };
      prepareToApplyEntity(entity, result);
      return result;
    }
 
    function beforeShowNewEntity() {
      var players = prepareToShowPlayerOptions();
      var promise = players.then(function() {
        var playerCount = vm.playerOptionsList.list.length;
        if (playerCount < 2) {
          vm.showToast('There must be at least two players.  ' +
            'Add players and try again.', 'Unable to Add Team', 'warning');
          return $q.reject();
        }
        else {
          if (playerCount == 2) {
            vm.newEntity.entity.select_first_player = vm.playerOptionsList.list[0];
            vm.newEntity.entity.select_second_player = vm.playerOptionsList.list[1];
          }
          return $q.resolve();
        }
      });
      return promise;
    }
 
    function beforeShowEditEntity() {
      var players = prepareToShowPlayerOptions();
      var promise = players.then(function() {
        prepareToEditEntity();
      });
      return promise;
    }
 
    function getEntityDisplayName(entity) {
      return entity.name || '(unnamed)'
    }
 
    function makeEntityBody(entity) {
      return {team: entity};
    }
 
    // internal methods
 
    function prepareToShowPlayerOptions() {
      var promise;
      if (vm.playerOptionsList.list == null) {
        promise = playersSelectOptions().then(
          function (list) {
            vm.playerOptionsList.list = list;
          },
          function () {
            vm.playerOptionsList.list = [];
            $log.error('playerOptionsList');
            return $q.resolve();
          }
        );
      }
      else {
        promise = $q.resolve();
      }
      return promise;
    }
 
    function prepareToEditEntity() {
      var first_player = null;
      var second_player = null;
      var first_id = null;
      var second_id = null;
      if (vm.editEntity.entity.first_player)
        first_id = vm.editEntity.entity.first_player.id;
      if (vm.editEntity.entity.second_player)
        second_id = vm.editEntity.entity.second_player.id;
      $filter('filter')(vm.playerOptionsList.list,
        function (o) {
          if (o.id == first_id)  first_player = o;
          if (o.id == second_id) second_player = o;
          return first_player && second_player;
        });
      vm.editEntity.entity.select_first_player = first_player;
      vm.editEntity.entity.select_second_player = second_player;
    }
 
    function prepareToApplyEntity(entity, result) {
      Eif (entity.select_first_player)
        result.first_player_id = entity.select_first_player.id;
      Eif (entity.select_second_player)
        result.second_player_id = entity.select_second_player.id;
    }
 
  }
})();