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