1 |
|
---|
2 | // Variable for the currently selected flag ratio
|
---|
3 | var currentRatio = null;
|
---|
4 |
|
---|
5 | var aspectRatios = null;
|
---|
6 | var aspectMaxRatio = 0;
|
---|
7 |
|
---|
8 | function calcMaxRatio(aspect_ratio_data)
|
---|
9 | {
|
---|
10 | // work out the maximum ratio
|
---|
11 | var max_ratio = 0;
|
---|
12 |
|
---|
13 | $.each(aspect_ratio_data, function(key, ratio_table) {
|
---|
14 | var ratio_array = key.split(":");
|
---|
15 | var ratio_x = ratio_array[0];
|
---|
16 | var ratio_y = ratio_array[1];
|
---|
17 | var ratio = ratio_y / ratio_x;
|
---|
18 |
|
---|
19 | if (ratio > max_ratio) {
|
---|
20 | max_ratio = ratio;
|
---|
21 | }
|
---|
22 | });
|
---|
23 |
|
---|
24 | return max_ratio;
|
---|
25 | }
|
---|
26 |
|
---|
27 | function calcCompleteWidth(aspect_ratio_data,scale)
|
---|
28 | {
|
---|
29 | // work out the maximum ratio
|
---|
30 | var max_width = 0;
|
---|
31 |
|
---|
32 | $.each(aspect_ratio_data, function(key, ratio_table) {
|
---|
33 | var ratio_array = key.split(":");
|
---|
34 | var ratio_x = ratio_array[0];
|
---|
35 | var ratio_y = ratio_array[1];
|
---|
36 | var ratio = ratio_y / ratio_x;
|
---|
37 |
|
---|
38 | max_width += (scale*ratio);
|
---|
39 | });
|
---|
40 |
|
---|
41 | return max_width;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | function scaledDivsFitInPage(aspect_ratio_data, scale_to_height, num_of_rows_allowed, la_x_dim)
|
---|
46 | {
|
---|
47 | var divs_fit = true;
|
---|
48 |
|
---|
49 | var row = 1;
|
---|
50 | var total_row_width = 0;
|
---|
51 | var total_height = 0;
|
---|
52 |
|
---|
53 | $.each(aspect_ratio_data, function(key, ratio_table) {
|
---|
54 | var ratio_array = key.split(":");
|
---|
55 | var ratio_x = ratio_array[0];
|
---|
56 | var ratio_y = ratio_array[1];
|
---|
57 | var ratio = ratio_y / ratio_x;
|
---|
58 |
|
---|
59 | var scaled_div_width = scale_to_height*ratio;
|
---|
60 | if (total_row_width + scaled_div_width > la_x_dim) {
|
---|
61 | // this div doesn't fit on the current row
|
---|
62 | // => start a new row
|
---|
63 | total_row_width = scaled_div_width;
|
---|
64 | row++;
|
---|
65 |
|
---|
66 | if (row>num_of_rows_allowed) {
|
---|
67 | // blow out!
|
---|
68 | divs_fit = false;
|
---|
69 | return false; // jquery.each equivalent to 'break'
|
---|
70 | }
|
---|
71 | }
|
---|
72 | else {
|
---|
73 | total_row_width += scaled_div_width;
|
---|
74 | row++;
|
---|
75 | }
|
---|
76 | });
|
---|
77 |
|
---|
78 | return divs_fit;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | function calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim)
|
---|
83 | {
|
---|
84 | // determine scale factor so divs max out (as in, filling in the page) based la_y_dim height,
|
---|
85 |
|
---|
86 | var num_of_rows_allowed = 1;
|
---|
87 | var scale_to_height = la_y_dim; // optimized version of true calcuation, la_y_dim / num_of_rows_allowed;
|
---|
88 |
|
---|
89 | //var div_width = complete_width_100 * (div_height/100)
|
---|
90 |
|
---|
91 | var num_flag_bins = aspectRatios.length;
|
---|
92 |
|
---|
93 | while (!scaledDivsFitInPage(aspectRatios,scale_to_height,num_of_rows_allowed,la_x_dim)) {
|
---|
94 |
|
---|
95 | num_of_rows_allowed++;
|
---|
96 |
|
---|
97 | if (num_of_rows_allowed>num_flag_bins) {
|
---|
98 | // needing more rows than there are flag bins is an indication that things
|
---|
99 | // just don't fit, and so will need to use a scroll bar.
|
---|
100 | // Exiting at this point will naturally achieve this
|
---|
101 | break;
|
---|
102 | }
|
---|
103 |
|
---|
104 | scale_to_height = la_y_dim/num_of_rows_allowed;
|
---|
105 |
|
---|
106 | // if get to here, then need
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | console.log("**** num_of_rows = " + num_of_rows_allowed + ", scale to height = " + scale_to_height);
|
---|
111 | return scale_to_height;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | $(function() {
|
---|
116 | $.getJSON( "../similarity-2d/flag-aspect-ratios-json.jsp", function(data) {
|
---|
117 |
|
---|
118 | aspectRatios = data;
|
---|
119 |
|
---|
120 | //var scale_to_y_dim = 130;
|
---|
121 |
|
---|
122 | // express scale_to_y_dim to use relative to height of window
|
---|
123 |
|
---|
124 | var window_height = $(window).height();
|
---|
125 | var div_top = $('#aspect-ratio-div').offset().top + 20;
|
---|
126 |
|
---|
127 | var la_y_dim = (div_top>0) ? window_height - div_top : window_height * 0.6;
|
---|
128 | var la_x_dim = $(window).width() * 0.7;
|
---|
129 |
|
---|
130 | //var scale_to_y_dim = la_y_dim/4;
|
---|
131 |
|
---|
132 | //console.log("*** scale to y dim = " + la_y_dim);
|
---|
133 |
|
---|
134 | //alert("scale_to_y_dim = " + scale_to_y_dim);
|
---|
135 |
|
---|
136 |
|
---|
137 | aspectMaxRatio = calcMaxRatio(aspectRatios);
|
---|
138 | //var complete_width_100 = calcCompleteWidth(aspectRatios,100); // as if every aspect ratio bin is scaled to 100 pixels high
|
---|
139 |
|
---|
140 |
|
---|
141 | scale_to_y_dim = calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim);
|
---|
142 |
|
---|
143 |
|
---|
144 | var items = [];
|
---|
145 |
|
---|
146 |
|
---|
147 | $.each(data, function(key, ratio_table) {
|
---|
148 | var freq = ratio_table.freq;
|
---|
149 | var ratio_array = key.split(":");
|
---|
150 | var ratio_x = ratio_array[0];
|
---|
151 | var ratio_y = ratio_array[1];
|
---|
152 | var ratio = ratio_y / ratio_x;
|
---|
153 |
|
---|
154 | var ratioScale = aspectMaxRatio/ratio;
|
---|
155 |
|
---|
156 | var x_dim = Math.round(scale_to_y_dim * ratioScale);
|
---|
157 | var y_dim = scale_to_y_dim;
|
---|
158 |
|
---|
159 | var innertext = "<div style='font-size: 180%;'>"+freq + " countries, " + key + "</div>";
|
---|
160 |
|
---|
161 | var flags = [];
|
---|
162 |
|
---|
163 | var nz_prioritized_flags = ratio_table.flags.sort();
|
---|
164 |
|
---|
165 | // Promote NZ flag to start of array, but otherwise keep sorted
|
---|
166 | var p = nz_prioritized_flags.length-1;
|
---|
167 | var found_nz = false;
|
---|
168 |
|
---|
169 | while (p>0) {
|
---|
170 | var curr;
|
---|
171 | var curr_title;
|
---|
172 |
|
---|
173 | if (!found_nz) {
|
---|
174 | curr = ratio_table.flags[p];
|
---|
175 | curr_title = curr.replace(/^.*\//,"").replace(/\.(.*?)$/,"");
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (curr_title == "nz") {
|
---|
179 | found_nz = true;
|
---|
180 |
|
---|
181 | // swap with adjacent cell
|
---|
182 | var tmp_m1 = nz_prioritized_flags[p-1];
|
---|
183 | nz_prioritized_flags[p-1] = curr;
|
---|
184 | nz_prioritized_flags[p] = tmp_m1;
|
---|
185 | }
|
---|
186 | p--;
|
---|
187 | }
|
---|
188 |
|
---|
189 | $.each(nz_prioritized_flags, function(index, flag_filename) {
|
---|
190 | var title = flag_filename.replace(/^.*\//,"").replace(/\.(.*?)$/,"");
|
---|
191 |
|
---|
192 | flags.push("<img src='../similarity-2d/"+flag_filename+"' id='aspect-ratio-"+ title +"' style='height: 70px; padding: 3px; ' title='" + title.toUpperCase() + "'/>");
|
---|
193 | });
|
---|
194 |
|
---|
195 | innertext += flags.join("");
|
---|
196 |
|
---|
197 | var ratio_id = "ratio-" + key.replace(":","_");
|
---|
198 | if (found_nz) {
|
---|
199 | currentRatio = ratio_id;
|
---|
200 | }
|
---|
201 |
|
---|
202 | var id = " id='" + ratio_id + "'";
|
---|
203 |
|
---|
204 | var style = "style='width:" + x_dim + "px; height:" + y_dim + "px;";
|
---|
205 | if (found_nz) {
|
---|
206 | style += "border-color:white; border-width:4px;"
|
---|
207 | }
|
---|
208 |
|
---|
209 | style += " overflow-y:scroll;";
|
---|
210 | style += "'";
|
---|
211 |
|
---|
212 | var onclick = " onclick='updateCanvasDimensions("+ratio_y + "," + ratio_x + ")'";
|
---|
213 |
|
---|
214 | items.push( "<div class='ratioDiv'" + id + style + onclick + ">"+ innertext + "</div>" );
|
---|
215 |
|
---|
216 | });
|
---|
217 |
|
---|
218 | $('#aspect-ratio-div').append("<div class='centredDiv' style='width: 100%'>"+items.join("\n")+"</div>");
|
---|
219 |
|
---|
220 |
|
---|
221 | // now change the flag tooltip labels into their country names
|
---|
222 | $.getJSON( "../similarity-2d/iso-3166-keyed-by-alpha-2-countrycodes.json", function(data) {
|
---|
223 |
|
---|
224 | $.each( data, function( iso2_key, country_name ) {
|
---|
225 | var $img = $('#aspect-ratio-'+iso2_key.toLowerCase());
|
---|
226 | var title = $img.attr("title") + ": " + country_name;
|
---|
227 | $img.attr("title",title);
|
---|
228 | }
|
---|
229 | )
|
---|
230 | });
|
---|
231 |
|
---|
232 | });
|
---|
233 | });
|
---|
234 |
|
---|
235 |
|
---|
236 |
|
---|
237 | $(window).resize(function() {
|
---|
238 | console.log("choose-canvas resize()");
|
---|
239 |
|
---|
240 | //console.log("*** aspect ratio div position top = " + $('#aspect-ratio-div').position().top);
|
---|
241 |
|
---|
242 | var window_height = $(window).height();
|
---|
243 | var div_top = $('#aspect-ratio-div').offset().top + 20;
|
---|
244 |
|
---|
245 | //var top = 0;
|
---|
246 |
|
---|
247 | var la_y_dim = window_height - div_top;
|
---|
248 | var la_x_dim = $(window).width() * 0.7;
|
---|
249 |
|
---|
250 | var scale_to_y_dim = calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim);
|
---|
251 |
|
---|
252 | // (top==0) ? la_y_dim/4 : la_y_dim/2.8;
|
---|
253 |
|
---|
254 | //console.log("*** scale to y dim = " + la_y_dim);
|
---|
255 |
|
---|
256 | $.each(aspectRatios, function(key, ratio_table) {
|
---|
257 | var freq = ratio_table.freq;
|
---|
258 | var ratio_array = key.split(":");
|
---|
259 | var ratio_x = ratio_array[0];
|
---|
260 | var ratio_y = ratio_array[1];
|
---|
261 | var ratio = ratio_y / ratio_x;
|
---|
262 |
|
---|
263 | var ratioScale = aspectMaxRatio/ratio;
|
---|
264 |
|
---|
265 | var x_dim = Math.round(scale_to_y_dim * ratioScale);
|
---|
266 | var y_dim = scale_to_y_dim;
|
---|
267 |
|
---|
268 | var ratio_id = "ratio-" + key.replace(":","_");
|
---|
269 |
|
---|
270 | //console.log("*** ratio_id = " + ratio_id + " xdim = " + x_dim + ", ydim = " + y_dim);
|
---|
271 |
|
---|
272 | $('#'+ratio_id).css("width",x_dim + "px");
|
---|
273 | $('#'+ratio_id).css("height",y_dim + "px");
|
---|
274 |
|
---|
275 |
|
---|
276 | });
|
---|
277 | });
|
---|
278 |
|
---|
279 |
|
---|
280 |
|
---|
281 | function updateCanvasDimensions(height, width) {
|
---|
282 |
|
---|
283 | // Show feedback by highlighting the chosen square
|
---|
284 | document.getElementById(currentRatio).style.borderColor = 'black';
|
---|
285 | document.getElementById(currentRatio).style.borderWidth = '1px';
|
---|
286 | currentRatio = "ratio-" + width + "_" + height;
|
---|
287 | document.getElementById(currentRatio).style.borderColor = 'white';
|
---|
288 | document.getElementById(currentRatio).style.borderWidth = '4px';
|
---|
289 |
|
---|
290 | // Changing the value of sizeConstant will change the
|
---|
291 | // size of the svg canvas without affecting the
|
---|
292 | // width/height ratios
|
---|
293 |
|
---|
294 | var sizeConstant = 400;
|
---|
295 |
|
---|
296 | var h = sizeConstant;
|
---|
297 | var w = (sizeConstant / height) * width;
|
---|
298 |
|
---|
299 | if (typeof flagCanvasSettings !== 'undefined') {
|
---|
300 | flagCanvasSettings.width = w;
|
---|
301 | flagCanvasSettings.height = h;
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|