forked from itinora/worldcup2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldcup.js
173 lines (145 loc) · 5.76 KB
/
worldcup.js
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
var worldcupApp = angular.module('worldcup2015', [])
.factory('_', function() {
return window._; // assumes underscore has already been loaded on the page
})
.factory('moment', function() {
return window.moment;
});
worldcupApp.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
});
worldcupApp
.controller('ScheduleCtrl', [ '$scope', 'MatchesSvc', '_', '$log', 'moment', function ($scope, MatchesSvc, _, $log, moment) {
'use strict';
$scope.timeFlag = false;
$scope.toggleTimeFlag = function() {
$scope.timeFlag = !$scope.timeFlag;
}
$scope.teamsInPool = function(pool){
var result = [];
for(var i= 0, team; team = $scope.teams[i]; i++) {
if(team.pool === pool) {
result.push(team);
}
}
var sortedResult = _(result).chain().sortBy('nrr').sortBy('pts').reverse().value();
return sortedResult;
};
MatchesSvc.getAllMatches()
.success(function(response){
MatchesSvc.setMatches(response.matches);
$scope.teams = response.teams;
$scope.poolATeams = $scope.teamsInPool('A');
$scope.poolBTeams = $scope.teamsInPool('B');
$scope.dates = MatchesSvc.getAllMatchDates();
$scope.venues = MatchesSvc.getAllVenues();
$scope.showTodaysMatches = function() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = yyyy + '-' + mm + '-' + dd;
$scope.onDateSelect(today);
highlightDatesForSelectedMatches();
};
$scope.showTodaysMatches();
});
$scope.month = function(date) {
return date.substr(6,1) === '2' ? 'Feb' : 'Mar';
};
$scope.dateOfMonth = function(date) {
return date.substr(8,2);
};
$scope.getTime = function(match) {
if ($scope.timeFlag) {
return $scope.month(match.match_date).toUpperCase()
+ " " + $scope.dateOfMonth(match.match_date).toUpperCase()
+ " " + match.start_time.toUpperCase();
} else {
return moment(match.match_datetime).format("MMM DD hh:mm A").toUpperCase();
}
};
$scope.selectedMatches = {};
$scope.onDateSelect = function(date, staySelected) {
staySelected = staySelected || false;
if(!$scope.staySelected && staySelected) {
$scope.staySelected = true;
}
$scope.deselectAll();
$scope.selectedMatches = MatchesSvc.getMatchesForDate(date);
highlightTeamsForSelectedMatches();
highlightVenuesForSelectedMatches();
};
var highlightDatesForSelectedMatches = function() {
$scope.dates = _.map($scope.dates, function(date) {
_.each($scope.selectedMatches, function(match) {
if(match.match_date === date.date) {
date.selected = true;
}
});
return date;
});
};
var highlightTeamsForSelectedMatches = function() {
$scope.teams = _.map($scope.teams, function(team) {
_.each($scope.selectedMatches, function(match) {
if(match.team_one_long === team.name || match.team_two_long === team.name) {
team.selected = true;
}
});
return team;
});
};
var highlightVenuesForSelectedMatches = function() {
$scope.venues = _.map($scope.venues, function(venue) {
_.each($scope.selectedMatches, function(match) {
if(match.city === venue.city) {
venue.selected = true;
}
});
return venue;
});
};
$scope.onTeamSelect = function(team, staySelected) {
staySelected = staySelected || false;
if(!$scope.staySelected && staySelected) {
$scope.staySelected = true;
}
$scope.deselectAll();
$scope.selectedMatches = MatchesSvc.getMatchesForTeam(team);
highlightDatesForSelectedMatches();
highlightVenuesForSelectedMatches();
};
$scope.onVenueSelect = function(venue, staySelected) {
staySelected = staySelected || false;
if(!$scope.staySelected && staySelected) {
$scope.staySelected = true;
}
$scope.deselectAll();
$scope.selectedMatches = MatchesSvc.getMatchesForVenue(venue);
highlightDatesForSelectedMatches();
highlightTeamsForSelectedMatches();
};
$scope.deselectAll = function() {
if(!$scope.staySelected) {
$scope.selectedMatches = {};
}
for(var i= 0, date; date = $scope.dates[i];i++) {
date.selected = false;
}
for(var i= 0, team; team = $scope.teams[i];i++) {
team.selected = false;
}
for(var i= 0, venue; venue = $scope.venues[i];i++) {
venue.selected = false;
}
};
}]
);