source: main/trunk/model-sites-dev/eurovision-lod/collect/eurovision/js/eurovision.js@ 34913

Last change on this file since 34913 was 34913, checked in by davidb, 3 years ago

When viewing the votes a country received, the entries in the table are now hyperlinked to the 'From-Country' doc in the DL

File size: 5.6 KB
Line 
1// The following needs to be kept in sync with the .voting-countries & .voting-points widths in css/eurovision.css
2var glob_voting_country_and_points_width = (180+35);
3
4function 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
19function 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 var library_name = gs.xsltParams["library_name"];
43 //var site_name = gs.xsltParams["site_name"];
44 var coll_name = gs.cgiParams["c"];
45
46 //var collect_http_path = gs.collectionMetadata["httpPath"];
47 var collect_doc_prefix_url = library_name + "/collection/" + coll_name + "/document/";
48
49 for (var i=0; i<num_cells; i++) {
50 var trans_i = transpose_i(i,num_cols, num_rows);
51
52 // By default, they country and points are set to be empty divs
53 var $to_country_name = $('<div>').attr("class","voting voting-country").append("");
54 var $to_country_points = $('<div>').attr("class","voting voting-points").append("");
55
56 // check to see if trans_i position is within array, otherwise default to filler
57 if (trans_i < num_countries) {
58 var from_country = VotesJSON[trans_i];
59 var from_country_points = gs.documentMetadata[from_country];
60 var year = gs.documentMetadata["Year"];
61
62 if (typeof from_country_points !== 'undefined') {
63 var from_country_base = from_country.replace(/-(T|J)$/,"");
64 var pp_from_country = from_country_base; // in future look to spacing back in for countries like United Kingdom
65
66 var hyphen_vote_type = from_country.match(/-(?:T|J)$/);
67 var from_country_id = "FromCountry-"+from_country_base + year + hyphen_vote_type;
68 var href_from_country = collect_doc_prefix_url + from_country_id;
69
70 var $a_from_country = $('<a>').attr("href",href_from_country).append(pp_from_country);
71
72 $from_country_name = $('<div>').attr("class","voting voting-country").append($a_from_country).append(":")
73 //$from_country_name = $('<div>').attr("class","voting voting-country").append(pp_from_country + ":");
74 $from_country_points = $('<div>').attr("class","voting voting-points").append(from_country_points);
75 }
76 }
77
78 $('#to-country-votes')
79 .append($from_country_name)
80 .append($from_country_points);
81
82 }
83
84 $('#to-country-votes').append($vote_points_div);
85
86 }
87}
88
89
90function compare_countries(a,b)
91{
92 if (a.Country < b.Country) {
93 return -1;
94 }
95 if (a.Country > b.Country) {
96 return 1;
97 }
98
99 // names must be equal
100 return 0;
101}
102
103function compare_points(a,b)
104{
105 return a.Points - b.Points;
106}
107
108function append_from_country_votes(from_country_year_id,vote_type)
109{
110 if (vote_type != "") {
111 var all_votes_div_width = $('#from-country-votes').width();
112
113 var votes_country_json_type = "VotesCountryJSON-" + vote_type;
114 var votes_points_json_type = "VotesPointsJSON-" + vote_type;
115 var vote_label = (vote_type == "J") ? "Jury votes" : "Televotes";
116
117 var $vote_div = $('<div>').attr("class","voting-label").append(vote_label);
118 $('#from-country-votes').append($vote_div);
119
120 var VotesCountryJSON = eval(gs.documentMetadata[votes_country_json_type]); // map JSON string into data-structure
121 var VotesPointsJSON = eval(gs.documentMetadata[votes_points_json_type]); // map JSON string into data-structure
122
123 var num_countries = VotesCountryJSON.length;
124
125 var num_cols = Math.floor(all_votes_div_width / glob_voting_country_and_points_width);
126 var num_rows = Math.ceil(num_countries / num_cols);
127 var num_cells = num_cols * num_rows;
128
129 var from_country_votes = [];
130
131 for (var i=0; i<num_countries; i++) {
132 from_country_votes.push({ "Country" : VotesCountryJSON[i], "Points" : VotesPointsJSON[i]})
133 }
134
135 from_country_votes.sort(compare_countries);
136
137 for (var i=0; i<num_cells; i++) {
138
139 var trans_i = transpose_i(i,num_cols, num_rows);
140
141 var $to_country_name = $('<div>').attr("class","voting voting-country").append("");
142 var $to_country_points = $('<div>').attr("class","voting voting-points").append("");
143
144 // check to see if trans_i position is within array, otherwise default to filler
145 if (trans_i < num_countries) {
146 var to_country = from_country_votes[trans_i].Country;
147 var to_country_points = from_country_votes[trans_i].Points;
148
149 if (typeof to_country_points !== 'undefined') {
150
151 $to_country_name = $('<div>').attr("class","voting voting-country").append(to_country + ":");
152 $to_country_points = $('<div>').attr("class","voting voting-points").append(to_country_points);
153 }
154 }
155
156 $('#from-country-votes')
157 .append($to_country_name)
158 .append($to_country_points);
159
160 }
161 }
162}
163
Note: See TracBrowser for help on using the repository browser.