Move all the fullscreen events to the JavaScript file.
[pr0n] / files / pr0n-fullscreen.js
1 function find_width()
2 {
3         var dpr = find_dpr();
4         return [window.innerWidth * dpr, window.innerHeight * dpr];
5 }
6
7 function find_dpr()
8 {
9         return window.devicePixelRatio || 1;
10 }
11
12 /*
13  * pr0n can resize to any size we'd like, but we're much more likely
14  * to have this set of fixed-resolution screens cached, so to increase
15  * performance, we round down to the closest fit and use that. This 
16  * function is a pessimal estimate of what thumbnail size we can _always_
17  * fit on the screen -- it's right if and only if all images are 4:3
18  * (and landscape). If individual size information is available, use
19  * pick_image_size, below.
20  */
21 var fixed_sizes = [
22         [ 2304, 1728 ],
23         [ 2048, 1536 ],
24         [ 1920, 1440 ],
25         [ 1600, 1200 ],
26         [ 1400, 1050 ],
27         [ 1280, 960 ],
28         [ 1152, 864 ],
29         [ 1024, 768 ],
30         [ 800, 600 ],
31         [ 640, 480 ],
32         [ 512, 384 ],
33         [ 320, 256 ],
34         [ 240, 192 ],
35         [ 120, 96 ],
36         [ 80, 64 ]
37 ];
38 function max_image_size(screen_size)
39 {
40         var i;
41         for (i = 0; i < fixed_sizes.length; ++i) {
42                 if (screen_size[0] >= fixed_sizes[i][0] && screen_size[1] >= fixed_sizes[i][1]) {
43                         return fixed_sizes[i];
44                 }
45         }
46         return [ 80, 64 ];
47 }
48
49 function pick_image_size(screen_size, image_size)
50 {
51         var i;
52         for (i = 0; i < fixed_sizes.length; ++i) {
53                 // this is a duplicate of pr0n's resizing code, hope for no floating-point
54                 // inaccuracies :-)
55                 var thumbxres = fixed_sizes[i][0];
56                 var thumbyres = fixed_sizes[i][1];
57                 var width = image_size[0];
58                 var height = image_size[1];
59
60                 if (!(thumbxres >= width && thumbyres >= height)) {
61                         var sfh = width / thumbxres;
62                         var sfv = height / thumbyres;
63                         if (sfh > sfv) {
64                                 width  /= sfh;
65                                 height /= sfh;
66                         } else {
67                                 width  /= sfv;
68                                 height /= sfv;
69                         }
70                         width = Math.floor(width);
71                         height = Math.floor(height);
72                 }
73
74                 if (screen_size[0] >= width && screen_size[1] >= height) {
75                         // be sure _not_ to return a reference
76                         return [ fixed_sizes[i][0], fixed_sizes[i][1], width, height ];
77                 }
78         }
79         return [ 80, 64 ];
80 }
81
82 function replace_image_element(url, element_id, parent_node)
83 {
84         var img = document.getElementById(element_id);
85         if (img !== null) {
86                 if (img.src === url) {
87                         return img;
88                 }
89                 img.parentNode.removeChild(img);
90         }
91
92         img = document.createElement("img");
93         img.id = element_id;
94         img.alt = "";
95         img.src = url;
96         parent_node.appendChild(img);
97         return img;
98 }
99
100 function rename_element(old_name, new_name)
101 {
102         // Remove any element that's in the way.
103         var elem = document.getElementById(new_name);
104         if (elem !== null) {
105                 elem.parentNode.removeChild(elem);
106         }
107
108         elem = document.getElementById(old_name);
109         if (elem !== null) {
110                 elem.id = new_name;
111         }
112         return elem;
113 }
114
115 function display_image(width, height, evt, filename, element_id)
116 {
117         var url = window.location.origin + "/" + evt + "/" + width + "x" + height + "/" + filename;
118         var main = document.getElementById("iehack");
119         var preload = document.getElementById("preload");
120         var dpr = find_dpr();
121         var img;
122         // See if we have a preload going on that we can reuse.
123         if (element_id == "image" && preload !== null && preload.src == url) {
124                 rename_element("preload_box", "image_box");
125                 img = rename_element("preload", "image");
126         } else {
127                 img = replace_image_element(url, element_id, main);
128         }
129         img.style.position = "absolute";
130         img.style.left = "0px";
131         img.style.top = "0px";
132         img.style.transformOrigin = "top left";
133         img.style.transform = "scale(" + (1.0 / dpr) + ")";
134
135         // Update the "download original" link.
136         var original_url = window.location.origin + "/" + evt + "/original/" + filename;
137         document.getElementById("origdownload").href = original_url;
138
139         if (global_infobox) {
140                 var url;
141                 if (dpr == 1) {
142                         url = window.location.origin + "/" + evt + "/" + width + "x" + height + "/box/" + filename;
143                 } else {
144                         url = window.location.origin + "/" + evt + "/" + width + "x" + height + "@" + dpr.toFixed(2) + "/box/" + filename;
145                 }
146                 var boximg = replace_image_element(url, element_id + "_box", main);
147
148                 boximg.style.position = "absolute";
149                 boximg.style.left = "0px";
150                 boximg.style.bottom = "-1px";
151                 boximg.style.transformOrigin = "bottom left";
152                 boximg.style.transform = "scale(" + (1.0 / dpr) + ")";
153         } else {
154                 var boximg = document.getElementById(element_id + "_box");
155                 if (boximg !== null) {
156                         boximg.parentNode.removeChild(boximg);
157                 }
158         }
159
160         return img;
161 }
162
163 function display_image_num(num, element_id)
164 {
165         var screen_size = find_width();
166         var adjusted_size;
167
168         if (global_image_list[num][2] == -1) {
169                 // no size information, use our pessimal guess
170                 adjusted_size = max_image_size(screen_size);
171         } else {
172                 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
173         }
174
175         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[num][0], global_image_list[num][1], element_id);
176         
177         if (element_id == "image") {
178                 // we want to shrink the box as much as possible if we know the true
179                 // size of the image
180                 center_image(num);
181                 
182                 // replace the anchor part (if any) with the image number
183                 window.location.hash = "#" + (num+1);
184         }
185
186         return img;
187 }
188
189 function prepare_preload(img, num)
190 {
191         // cancel any pending preload
192         var preload = document.getElementById("preload");
193         if (preload !== null) {
194                 preload.parentNode.removeChild(preload);
195         }
196         
197         var preload_box = document.getElementById("preload_box");
198         if (preload_box !== null) {
199                 preload_box.parentNode.removeChild(preload_box);
200         }
201
202         // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
203         // completeness first; should at least be _somewhat_ better
204         if (img.complete) {
205                 display_image_num(num, "preload");
206         } else {
207                 img.onload = function() { display_image_num(num, "preload"); };
208         }       
209 }
210
211 function can_go_next()
212 {
213         return (global_image_num < global_image_list.length - 1);
214 }
215
216 function can_go_previous()
217 {
218         return (global_image_num > 0);
219 }
220
221 function set_opacity(id, amount)
222 {
223         var elem = document.getElementById(id);
224
225         // If optionmenu is visible, options is also visible.
226         if (id === "options" && amount < 0.7) {
227                 var optionmenu = document.getElementById("optionmenu");
228                 if (optionmenu.style.display === "block") {
229                         amount = 0.7;
230                 }
231         }
232         elem.style.opacity = amount;
233 }
234
235 function center_image(num)
236 {
237         var screen_size = find_width();
238         var dpr = find_dpr();
239         var width, height;
240         
241         if (global_image_list[num][2] == -1) {
242                 // no size information, use our pessimal guess
243                 var adjusted_size = max_image_size(screen_size);
244                 width = adjusted_size[0];
245                 height = adjusted_size[1];
246         } else {
247                 // use the exact information
248                 var adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
249                 width = adjusted_size[2];
250                 height = adjusted_size[3];
251         }
252
253         // center the image on-screen
254         var main = document.getElementById("main");
255         main.style.position = "absolute";
256         main.style.left = (((screen_size[0] - width) / 2) / dpr) + "px";
257         main.style.top = (((screen_size[1] - height) / 2) / dpr) + "px";
258         main.style.width = (width / dpr) + "px";
259         main.style.height = (height / dpr) + "px";
260         main.style.lineHeight = (height / dpr) + "px";
261 }
262
263 function relayout()
264 {
265         var img = display_image_num(global_image_num, "image");
266         if (can_go_next()) {
267                 prepare_preload(img, global_image_num + 1);
268         }
269
270         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
271         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
272         set_opacity("close", global_default_opacity);
273         set_opacity("options", global_default_opacity);
274 }
275
276 function go_previous()
277 {
278         if (!can_go_previous()) {
279                 return;
280         }
281
282         var img = display_image_num(--global_image_num, "image");
283         if (can_go_previous()) {
284                 set_opacity("previous", global_default_opacity);
285                 prepare_preload(img, global_image_num - 1);
286         } else {
287                 set_opacity("previous", global_disabled_opacity);
288         }
289         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
290 }
291
292 function go_next()
293 {
294         if (!can_go_next()) {
295                 return;
296         }
297
298         var img = display_image_num(++global_image_num, "image");
299         if (can_go_next()) {
300                 set_opacity("next", global_default_opacity);
301                 prepare_preload(img, global_image_num + 1);
302         } else {
303                 set_opacity("next", global_disabled_opacity);
304         }
305         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
306 }
307
308 function do_close()
309 {
310         if (global_image_num > 0) {
311                 window.location = global_return_url + '#' + (global_image_num + 1);
312         } else {
313                 window.location = global_return_url;
314         }
315 }
316
317 function toggle_optionmenu()
318 {
319         var optionmenu = document.getElementById("optionmenu");
320         if (optionmenu.style.display === "block") {
321                 optionmenu.style.display = "none";
322         } else {
323                 optionmenu.style.display = "block";
324                 set_opacity("options", 0.7);
325         }
326 }
327
328 function draw_text(msg)
329 {
330         // remove any text we might have left
331         var text = document.getElementById("text");
332         if (text !== null) {
333                 text.parentNode.removeChild(text);
334         }
335
336         text = document.createElement("p");
337         text.id = "text";
338         text.style.position = "absolute";
339         text.style.color = "white";
340         text.style.lineHeight = "24px";
341         text.style.font = "24px verdana, arial, sans-serif";
342         text.innerHTML = msg;
343
344         var main = document.getElementById("main");
345         main.appendChild(text);
346
347         text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
348         text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
349 }
350
351 function fade_text(opacity)
352 {
353         set_opacity("text", opacity);
354         if (opacity > 0.0) {
355                 opacity -= 0.03;
356                 if (opacity < 0.0) {
357                         opacity = 0.0;
358                 }
359                 setTimeout("fade_text(" + opacity + ")", 30);
360         } else {
361                 var text = document.getElementById("text");
362                 if (text !== null) {
363                         text.parentNode.removeChild(text);
364                 }
365         }
366 }
367
368 function select_image(evt, filename, selected)
369 {
370         if (selected) {
371                 draw_text("Selecting " + filename + "...");
372         } else {
373                 draw_text("Unselecting " + filename + "...");
374         }
375         
376         var req = new XMLHttpRequest();
377         req.open("POST", window.location.origin + "/select", false);
378         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
379         req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
380
381         setTimeout("fade_text(0.99)", 30);
382 }
383
384 function key_down(which)
385 {
386         if (which == 39) {   // right
387                 if (can_go_next()) {
388                         set_opacity("next", global_highlight_opacity);
389                 }
390         } else if (which == 37) {   // left
391                 if (can_go_previous()) {
392                         set_opacity("previous", global_highlight_opacity);
393                 }
394         } else if (which == 27) {   // escape
395                 set_opacity("close", global_higlight_opacity);
396         } else {
397                 check_for_hash_change();
398         }
399 }
400
401 function key_up(which) {
402         if (which == 39) {   // right
403                 if (can_go_next()) {
404                         set_opacity("next", global_default_opacity);
405                         go_next();
406                 }
407         } else if (which == 37) {   // left
408                 if (can_go_previous()) {
409                         set_opacity("previous", global_default_opacity);
410                         go_previous();
411                 }
412         } else if (which == 27) {   // escape
413                 set_opacity("close", global_default_opacity);
414                 do_close();
415         } else if (which == 32 && global_select) {   // space
416                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
417         } else if (which == 85 && global_select) {   // u
418                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
419         } else {
420                 check_for_hash_change();
421         }
422 }
423
424 function parse_image_num(default_value) {
425         var num = parseInt(window.location.hash.substr(1));
426         if (num >= 1 && num <= global_image_list.length) {  // and then num != NaN
427                 return (num - 1);
428         } else {
429                 return default_value;
430         }
431 }
432
433 function check_for_hash_change() {
434         var num = parse_image_num(-1);
435         if (num != -1 && num != global_image_num) {
436                 global_image_num = num;
437                 relayout();
438         }
439 }
440
441 function toggle_immersive() {
442         if (global_default_opacity == 0.7) {
443                 global_disabled_opacity = 0.0;
444                 global_default_opacity = 0.0;
445                 global_highlight_opacity = 0.2;
446                 global_infobox = false;
447                 document.getElementById('immersivetoggle').innerHTML = 'Show decorations';
448         } else {
449                 global_disabled_opacity = 0.1;
450                 global_default_opacity = 0.7;
451                 global_highlight_opacity = 1.0;
452                 global_infobox = true;
453                 document.getElementById('immersivetoggle').innerHTML = 'Hide all decorations';
454         }
455         relayout();
456 }
457
458 window.onload = function() {
459         relayout();
460         setInterval(check_for_hash_change, 1000);
461
462         var body = document.body;
463         body.onresize = function() { relayout(); };
464         body.onkeydown = function() { key_down(event.keyCode); };
465         body.onkeyup = function() { key_up(event.keyCode); };
466         body.onhashchange = function() { check_for_hash_change(); };
467         body.onclick = function() { check_for_hash_change(); };
468
469         var previous = document.getElementById('previous');
470         previous.onmousedown = function() { if (can_go_previous()) { set_opacity('previous', global_highlight_opacity); } };
471         previous.onmouseup = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); go_previous(); } };
472         previous.onmouseout = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); } };
473
474         var next = document.getElementById('next');
475         next.onmousedown = function() { if (can_go_next()) { set_opacity('next', global_highlight_opacity); } };
476         next.onmouseup = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); go_next(); } };
477         next.onmouseout = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); } };
478
479         var close = document.getElementById('close');
480         close.onmousedown = function() { set_opacity('close', global_highlight_opacity); };
481         close.onmouseup = function() { set_opacity('close', global_default_opacity); do_close(); };
482         close.onmouseout = function() { set_opacity('close', global_default_opacity); };
483
484         var options = document.getElementById('options');
485         options.onmousedown = function() { set_opacity('options', global_highlight_opacity); };
486         options.onmouseup = function() { set_opacity('options', global_default_opacity); toggle_optionmenu(); };
487         options.onmouseout = function() { set_opacity('options', global_default_opacity); };
488 };