all files / app/scores/ scoresController.js

100% Statements 30/30
100% Branches 6/6
100% Functions 8/8
100% Lines 30/30
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                                                                                                             
/**
 * @ngdoc controller
 * @name app.scores.controller:ScoresController
 * @description
 * Controller for listing and selecting matches and for displaying a 
 * drop down menu next to the match list.  
 * Selecting a match updates a child view. 
 * See {@link app.scores.controller:ScoreboardController}
 *
 */
 
(function () {
  'use strict';
 
  angular
    .module('app.scores')
    .controller('ScoresController', Controller);
 
  /** @ngInject */
  function Controller($filter, $log, $scope, $state, loadingHelper,
                      authHelper, response) {
 
    var vm = this;
 
    activate();
 
    /**
     * @ngdoc function
     * @name activate
     * @methodOf  app.scores.controller:ScoresController
     * @description
     * Initialize the controller:
     * * Add auth support
     * * Add loading support
     * * Add members
     *   * matches - array of matches
     *   * selectedMatch - currently selected match
     *   * selectedMatchChange() - transitions the child view to the selected match
     */
    function activate() {
      vm.matches = [];
      vm.selectedMatch = null;
      vm.selectedMatchChange = selectedMatchChange;
 
      authHelper(vm, $scope);
      loadingHelper(vm);
 
      if (angular.isArray(response))
        getMatchesHasSucceeded(response);
      else
        getMatchesHasFailed(response);
    }
 
    function getMatchesHasSucceeded(response) {
      vm.matches = response;
      vm.loading.hasCompleted();
      selectMatch();
    }
 
    function getMatchesHasFailed(response) {
      $log.error('data error ' + response.status + " " + response.statusText);
      vm.loading.hasFailed(response);
    }
 
    function selectMatch() {
      if ($state.current.name === 'scores.board') {
        var id = $state.params.id;
        var found = $filter('filter')(vm.matches, function (o) {
          return o.id == id;
        });
        if (found.length > 0)
          vm.selectedMatch = found[0];
      }
    }
 
    function selectedMatchChange() {
      $state.transitionTo('scores.board', {id: vm.selectedMatch.id});
    }
  }
})();