1 | // The following needs to be kept in sync with the .voting-countries & .voting-points widths in css/eurovision.css
|
---|
2 | var glob_voting_country_and_points_width = (180+35);
|
---|
3 |
|
---|
4 | function transpose_i(i,num_cols, num_rows)
|
---|
5 | {
|
---|
6 | // Map 'i' to (x,y) position in grid layout
|
---|
7 | var x = (i % num_cols);
|
---|
8 | var y = Math.floor(i / num_cols); // affect int division
|
---|
9 |
|
---|
10 | // Transpose, and then reverse engineer what value of 'i' the transposed position needs to be
|
---|
11 | var trans_x = y;
|
---|
12 | var trans_y = x;
|
---|
13 |
|
---|
14 | var trans_i = trans_y * num_rows + trans_x;
|
---|
15 |
|
---|
16 | return trans_i;
|
---|
17 | }
|
---|
18 |
|
---|
19 | function append_to_country_votes(vote_label, votes_json_type, votes_type_total)
|
---|
20 | {
|
---|
21 | // TODO
|
---|
22 | // Figure out why "United Kingdom-J" and not "UnitedKingdom-J"
|
---|
23 |
|
---|
24 | if (gs.documentMetadata[votes_json_type]) {
|
---|
25 | var all_votes_div_width = $('#to-country-votes').width();
|
---|
26 |
|
---|
27 | var $vote_label_div = $('<div>').attr("class","voting-label").append(vote_label);
|
---|
28 | $vote_label_div.append(" (Total: " + gs.documentMetadata[votes_type_total] + ")");
|
---|
29 | $('#to-country-votes').append($vote_label_div);
|
---|
30 |
|
---|
31 | var $vote_points_div = $('<div>').attr("class","voting-points-div");
|
---|
32 |
|
---|
33 | var VotesJSON = eval(gs.documentMetadata[votes_json_type]);
|
---|
34 | var num_countries = VotesJSON.length;
|
---|
35 |
|
---|
36 | var num_cols = Math.floor(all_votes_div_width / glob_voting_country_and_points_width);
|
---|
37 | var num_rows = Math.ceil(num_countries / num_cols);
|
---|
38 | var num_cells = num_cols * num_rows;
|
---|
39 |
|
---|
40 | VotesJSON.sort();
|
---|
41 |
|
---|
42 | for (var i=0; i<num_cells; i++) {
|
---|
43 | var trans_i = transpose_i(i,num_cols, num_rows);
|
---|
44 |
|
---|
45 | // By default, they country and points are set to be empty divs
|
---|
46 | var $to_country_name = $('<div>').attr("class","voting voting-country").append("");
|
---|
47 | var $to_country_points = $('<div>').attr("class","voting voting-points").append("");
|
---|
48 |
|
---|
49 | // check to see if trans_i position is within array, otherwise default to filler
|
---|
50 | if (trans_i < num_countries) {
|
---|
51 | var from_country = VotesJSON[trans_i];
|
---|
52 | var from_country_points = gs.documentMetadata[from_country];
|
---|
53 |
|
---|
54 | if (typeof from_country_points !== 'undefined') {
|
---|
55 | var pp_from_country = from_country.replace(/-(T|J)$/,"");
|
---|
56 |
|
---|
57 | $from_country_name = $('<div>').attr("class","voting voting-country").append(pp_from_country + ":");
|
---|
58 | $from_country_points = $('<div>').attr("class","voting voting-points").append(from_country_points);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | $('#to-country-votes')
|
---|
63 | .append($from_country_name)
|
---|
64 | .append($from_country_points);
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | $('#to-country-votes').append($vote_points_div);
|
---|
69 |
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | function compare_countries(a,b)
|
---|
75 | {
|
---|
76 | if (a.Country < b.Country) {
|
---|
77 | return -1;
|
---|
78 | }
|
---|
79 | if (a.Country > b.Country) {
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // names must be equal
|
---|
84 | return 0;
|
---|
85 | }
|
---|
86 |
|
---|
87 | function compare_points(a,b)
|
---|
88 | {
|
---|
89 | return a.Points - b.Points;
|
---|
90 | }
|
---|
91 |
|
---|
92 | function append_from_country_votes(from_country_year_id,vote_type)
|
---|
93 | {
|
---|
94 | if (vote_type != "") {
|
---|
95 | var all_votes_div_width = $('#from-country-votes').width();
|
---|
96 |
|
---|
97 | var votes_country_json_type = "VotesCountryJSON-" + vote_type;
|
---|
98 | var votes_points_json_type = "VotesPointsJSON-" + vote_type;
|
---|
99 | var vote_label = (vote_type == "J") ? "Jury votes" : "Televotes";
|
---|
100 |
|
---|
101 | var $vote_div = $('<div>').attr("class","voting-label").append(vote_label);
|
---|
102 | $('#from-country-votes').append($vote_div);
|
---|
103 |
|
---|
104 | var VotesCountryJSON = eval(gs.documentMetadata[votes_country_json_type]); // map JSON string into data-structure
|
---|
105 | var VotesPointsJSON = eval(gs.documentMetadata[votes_points_json_type]); // map JSON string into data-structure
|
---|
106 |
|
---|
107 | var num_countries = VotesCountryJSON.length;
|
---|
108 |
|
---|
109 | var num_cols = Math.floor(all_votes_div_width / glob_voting_country_and_points_width);
|
---|
110 | var num_rows = Math.ceil(num_countries / num_cols);
|
---|
111 | var num_cells = num_cols * num_rows;
|
---|
112 |
|
---|
113 | var from_country_votes = [];
|
---|
114 |
|
---|
115 | for (var i=0; i<num_countries; i++) {
|
---|
116 | from_country_votes.push({ "Country" : VotesCountryJSON[i], "Points" : VotesPointsJSON[i]})
|
---|
117 | }
|
---|
118 |
|
---|
119 | from_country_votes.sort(compare_countries);
|
---|
120 |
|
---|
121 | for (var i=0; i<num_cells; i++) {
|
---|
122 |
|
---|
123 | var trans_i = transpose_i(i,num_cols, num_rows);
|
---|
124 |
|
---|
125 | var $to_country_name = $('<div>').attr("class","voting voting-country").append("");
|
---|
126 | var $to_country_points = $('<div>').attr("class","voting voting-points").append("");
|
---|
127 |
|
---|
128 | // check to see if trans_i position is within array, otherwise default to filler
|
---|
129 | if (trans_i < num_countries) {
|
---|
130 | var to_country = from_country_votes[trans_i].Country;
|
---|
131 | var to_country_points = from_country_votes[trans_i].Points;
|
---|
132 |
|
---|
133 | if (typeof to_country_points !== 'undefined') {
|
---|
134 |
|
---|
135 | $to_country_name = $('<div>').attr("class","voting voting-country").append(to_country + ":");
|
---|
136 | $to_country_points = $('<div>').attr("class","voting voting-points").append(to_country_points);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | $('#from-country-votes')
|
---|
141 | .append($to_country_name)
|
---|
142 | .append($to_country_points);
|
---|
143 |
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|