/
schedule.js
85 lines (68 loc) · 2.22 KB
/
schedule.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
function bocceSchedule(sheet) {
var key = '1HJP6Ws5T5zQ4F6QW3UGrJb_Cj8VlsAcugxt8xFIpO7s',
$ = jQuery,
$el = $('#bocce-schedule'),
url = 'https://spreadsheets.google.com/feeds/list/' + key + '/' + sheet + '/public/basic?alt=json&callback=?';
$el.html('<div class="loading">loading...</div>');
$.getJSON(url, function(data) {
var games = $.map(data.feed.entry, function(entry) {
var cells = $.map(entry.content.$t.split(','), function(cell) {
return cell.replace(/^.*: /, '');
});
return {
time: entry.title.$t,
court: cells[0],
home: cells[1],
visitor: cells[2]
};
});
var teams = [];
$.each(games, function(ndx, game) {
if ($.inArray(game.home, teams) == -1) teams.push(game.home);
if ($.inArray(game.visitor, teams) == -1) teams.push(game.visitor);
});
teams.sort();
var html = '';
html += '<div class="team-select">';
html += '<select>';
html += '<option value="">all teams</option>';
$.each(teams, function(ndx, team) {
html += '<option value="' + ndx + '">' + team + '</option>';
});
html += '</select>';
html += '<table>';
html += '</div>';
html += '<tr>';
html += '<th class="time">date / time</th>';
html += '<th class="court">court</th>';
html += '<th class="home">home</th>';
html += '<th class="visitor">visitor</th>';
html += '</tr>';
$.each(games, function(ndx, game) {
if (!game.home) return;
var homeTeamNdx = $.inArray(game.home, teams),
visitorTeamNdx = $.inArray(game.visitor, teams);
html += '<tr class="game">';
html += '<td class="time">' + game.time + '</td>';
html += '<td class="court">' + game.court + '</td>';
html += '<td class="home team team-' + homeTeamNdx + '">' + game.home + '</td>';
html += '<td class="visitor team team-' + visitorTeamNdx + '">' + game.visitor + '</td>';
html += '</tr>';
});
html += '</table>';
$el.html(html);
var teamRows = $el.find('.game'),
teamCells = $el.find('.team');
$el.find('.team-select select').change(function() {
var ndx = $(this).val();
teamRows.show();
teamCells.removeClass('chosen');
if (ndx) {
teamRows.hide();
var chosen = $el.find('.team-' + ndx);
chosen.closest('tr').show();
chosen.addClass('chosen');
}
});
});
}