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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 1× 1× 1× 46× 46× 1× 46× 46× 46× 1× 2× 2× 2× 1× 2× 2× 2× 1× 17× 17× 17× 17× 17× 17× 17× 17× 17× 17× 5× 5× 12× 1× 12× 2× 2× 12× 2× 2× 12× 17× 1× 10× 10× 10× 10× 10× 10× 10× 10× 10× 1× 2× 1× 1× 1× 4× 4× 4× 4× 2× 2× 1× 2× 1× 2× 1× 27× 27× 25× 23× 2× 2× 2× 2× 27× 1× 27× 27× 25× 23× 2× 2× 2× 2× 27× 1× 10× 6× 6× 6× 6× 6× 4× 6× 4× 6× 18× 18× 18× 6× 6× 4× 2× 2× 1× 2× 2× 2× 2× 1× 10× 4× 4× 4× 4× 4× 4× 4× 4× 4× 12× 12× 12× 4× 4× 6× 2× 2× 1× 2× 2× 2× 2× | /** * @ngdoc controller * @name app.matches.controller:MatchesController * @description * Controller for displaying and editing matches * */ (function () { 'use strict'; angular .module('app.matches') .controller('MatchesController', Controller); /** @ngInject */ function Controller($filter, $q, $log, $scope, crudHelper, matchesPath, playersSelectOptions, teamsSelectOptions, response) { var vm = this; activate(); /** * @ngdoc function * @name activate * @methodOf app.matches.controller:MatchesController * @description * Initialize the controller: * * Add playerOptionsList object * * Add teamOptionsList object * * Call the crudHelper service with match-specific options */ function activate() { vm.teamOptionsList = {list: null}; vm.playerOptionsList = {list: null}; // authHelper(vm, $scope); crudHelper(vm, { response: response, resourceName: matchesPath, prepareToCreateEntity: prepareToCreateEntity, prepareToUpdateEntity: prepareToUpdateEntity, beforeShowNewEntity: beforeShowNewEntity, beforeShowEditEntity: beforeShowEditEntity, getEntityDisplayName: getEntityDisplayName, makeEntityBody: makeEntityBody, entityKind: 'Match', scope: $scope, errorsMap: { names: [ 'title', 'first_team', 'second_team', 'first_player', 'second_player' ] } } ); } function prepareToCreateEntity(entity) { var result = {}; prepareToSubmitEntity(entity, result); return result; } function prepareToUpdateEntity(entity) { var result = { id: entity.id }; prepareToSubmitEntity(entity, result); return result; } // Return a promise function beforeShowNewEntity() { // TODO: Preserve last selection when add multiple matches vm.newEntity.entity.doubles = false; vm.newEntity.entity.scoring = 'two_six_game_ten_point'; var teams = prepareToShowTeamOptions(); var players = prepareToShowPlayerOptions(); var all = $q.all([teams, players]); var endWait = vm.beginWait(); var promise = all.then(function () { var playerCount = vm.playerOptionsList.list.length; var teamCount = vm.teamOptionsList.list.length; if (playerCount < 2 && teamCount < 2) { vm.showToast('There must be at least two teams or two players. ' + 'Add teams or players and try again.', 'Unable to Add Match'); return $q.reject(); } else { if (playerCount < 2) vm.newEntity.entity.doubles = true; if (playerCount == 2) { vm.newEntity.entity.select_first_player = vm.playerOptionsList.list[0]; vm.newEntity.entity.select_second_player = vm.playerOptionsList.list[1]; } if (teamCount == 2) { vm.newEntity.entity.select_first_team = vm.teamOptionsList.list[0]; vm.newEntity.entity.select_second_team = vm.teamOptionsList.list[1]; } return $q.resolve(); } }).finally(endWait); return promise; } // Return a promise function beforeShowEditEntity() { var teams = prepareToShowTeamOptions(); var players = prepareToShowPlayerOptions(); var all = $q.all([teams, players]); var endWait = vm.beginWait(); var promise = all.then(function () { prepareToEditTeams(); prepareToEditPlayers(); return $q.resolve() }).finally(endWait); return promise; } function getEntityDisplayName(entity) { return entity.title || '(untitled)' } function makeEntityBody(entity) { return {match: entity}; } // "Private" methods function prepareToSubmitEntity(entity, result) { result.title = entity.title; result.scoring = entity.scoring; result.doubles = entity.doubles; if (entity.doubles) { prepareToSubmitDoubles(entity, result); } else { prepareToSubmitSingles(entity, result); } } function prepareToSubmitDoubles(entity, result) { prepareToSubmitTeams(entity, result); } function prepareToSubmitSingles(entity, result) { prepareToSubmitPlayers(entity, result); } function prepareToShowTeamOptions() { var promise; if (vm.teamOptionsList.list == null) { promise = teamsSelectOptions().then( function (list) { vm.teamOptionsList.list = list; }, function () { $log.error('teamOptionsList'); vm.teamOptionsList.list = []; return $q.resolve(); } ); } else { promise = $q.resolve(); } return promise; } function prepareToShowPlayerOptions() { var promise; if (vm.playerOptionsList.list == null) { promise = playersSelectOptions().then( function (list) { vm.playerOptionsList.list = list; }, function () { $log.error('playerOptionsList'); vm.playerOptionsList.list = []; return $q.resolve(); } ); } else { promise = $q.resolve(); } return promise; } function prepareToEditPlayers() { if (!vm.editEntity.entity.doubles) { 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; } else { if (vm.playerOptionsList.list.length == 2) { // If user selects doubles then default to only two teams vm.editEntity.entity.select_first_player = vm.playerOptionsList.list[0]; vm.editEntity.entity.select_second_player = vm.playerOptionsList.list[1]; } } } function prepareToSubmitPlayers(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; } function prepareToEditTeams() { if (vm.editEntity.entity.doubles) { var first_team = null; var second_team = null; var first_id = null; var second_id = null; Eif (vm.editEntity.entity.first_team) first_id = vm.editEntity.entity.first_team.id; Eif (vm.editEntity.entity.second_team) second_id = vm.editEntity.entity.second_team.id; $filter('filter')(vm.teamOptionsList.list, function (o) { if (o.id == first_id) first_team = o; if (o.id == second_id) second_team = o; return first_team && second_team; }); vm.editEntity.entity.select_first_team = first_team; vm.editEntity.entity.select_second_team = second_team; } else { if (vm.teamOptionsList.list.length == 2) { // If user unselects doubles then default to only two teams vm.editEntity.entity.select_first_team = vm.teamOptionsList.list[0]; vm.editEntity.entity.select_second_team = vm.teamOptionsList.list[1]; } } } function prepareToSubmitTeams(entity, result) { Eif (entity.select_first_team) result.first_team_id = entity.select_first_team.id; Eif (entity.select_second_team) result.second_team_id = entity.select_second_team.id; } } })(); |