45b60c6737470342d47a5ba57a5459ef9337ee0c
[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         [ 3840, 2880 ],
30         [ 3200, 2400 ],
31         [ 2800, 2100 ],
32         [ 2304, 1728 ],
33         [ 2048, 1536 ],
34         [ 1920, 1440 ],
35         [ 1600, 1200 ],
36         [ 1400, 1050 ],
37         [ 1280, 960 ],
38         [ 1152, 864 ],
39         [ 1024, 768 ],
40         [ 800, 600 ],
41         [ 640, 480 ],
42         [ 512, 384 ],
43         [ 320, 256 ],
44         [ 240, 192 ],
45         [ 120, 96 ],
46         [ 80, 64 ]
47 ];
48 function max_image_size(screen_size)
49 {
50         var i;
51         for (i = 0; i < fixed_sizes.length; ++i) {
52                 if (screen_size[0] >= fixed_sizes[i][0] && screen_size[1] >= fixed_sizes[i][1]) {
53                         return fixed_sizes[i];
54                 }
55         }
56         return [ 80, 64 ];
57 }
58
59 function pick_image_size(screen_size, image_size)
60 {
61         var i;
62         for (i = 0; i < fixed_sizes.length; ++i) {
63                 // this is a duplicate of pr0n's resizing code, hope for no floating-point
64                 // inaccuracies :-)
65                 var thumbxres = fixed_sizes[i][0];
66                 var thumbyres = fixed_sizes[i][1];
67                 var width = image_size[0];
68                 var height = image_size[1];
69
70                 if (!(thumbxres >= width && thumbyres >= height)) {
71                         var sfh = width / thumbxres;
72                         var sfv = height / thumbyres;
73                         if (sfh > sfv) {
74                                 width  /= sfh;
75                                 height /= sfh;
76                         } else {
77                                 width  /= sfv;
78                                 height /= sfv;
79                         }
80                         width = Math.floor(width);
81                         height = Math.floor(height);
82                 }
83
84                 if (screen_size[0] >= width && screen_size[1] >= height) {
85                         // be sure _not_ to return a reference
86                         return [ fixed_sizes[i][0], fixed_sizes[i][1], width, height ];
87                 }
88         }
89         return [ 80, 64 ];
90 }
91
92 function rename_element(old_name, new_name)
93 {
94         // Remove any element that's in the way.
95         var elem = document.getElementById(new_name);
96         if (elem !== null) {
97                 elem.parentNode.removeChild(elem);
98         }
99
100         elem = document.getElementById(old_name);
101         if (elem !== null) {
102                 elem.id = new_name;
103         }
104         return elem;
105 }
106
107 function display_image(url, backend_width, backend_height, elem_id, offset, box)
108 {
109         // See if this image already exists in the DOM; if not, add it.
110         var img = document.getElementById(elem_id);
111         if (img === null) {
112                 img = document.createElement("img");
113                 img.id = elem_id;
114                 img.alt = "";
115                 img.className = box ? "fsbox" : "fsimg";
116         }
117         img.style.position = "absolute";
118         img.style.transformOrigin = "top left";
119         document.getElementById("main").appendChild(img);
120
121         if (offset === 0) {
122                 img.src = url;
123                 position_image(img, backend_width, backend_height, offset, box);
124         } else {
125                 // This is a preload, so wait for the main image to be ready.
126                 // The test for .complete is an old IE hack, which I don't know if is relevant anymore.
127                 var main_img = document.getElementById(global_image_num);
128                 if (main_img === null || main_img.complete) {
129                         img.src = url;
130                 } else {
131                         main_img.addEventListener('load', function() { img.src = url; }, false);
132                 }
133
134                 // Seemingly one needs to delay position_image(), or Firefox will set the initial
135                 // scroll offset completely off.
136                 img.style.display = 'none';
137                 setTimeout(function() {
138                         position_image(img, backend_width, backend_height, offset, box);
139                         img.style.display = null;
140                 }, 1);
141         }
142 }
143
144 function display_image_num(num, offset)
145 {
146         var screen_size = find_width();
147         var adjusted_size;
148
149         if (global_image_list[num][2] == -1) {
150                 // no size information, use our pessimal guess
151                 adjusted_size = max_image_size(screen_size);
152         } else {
153                 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
154         }
155
156         var evt = global_image_list[num][0];
157         var filename = global_image_list[num][1];
158         var backend_width = adjusted_size[0];
159         var backend_height = adjusted_size[1];
160         var url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/" + filename;
161         var elem_id = num;
162
163         display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset, false);
164
165         if (global_infobox) {
166                 var url;
167                 var dpr = find_dpr();
168                 var elem_id = num + "_box";
169                 if (dpr == 1) {
170                         url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/box/" + filename;
171                 } else {
172                         url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "@" + dpr.toFixed(2) + "/box/" + filename;
173                 }
174                 display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset, true);
175                 document.getElementById(elem_id).style.transform += " scale(" + (1.0 / dpr) + ")";
176         }
177
178         if (offset === 0) {
179                 // Update the "download original" link.
180                 var original_url = window.location.origin + "/" + evt + "/original/" + filename;
181                 document.getElementById("origdownload").href = original_url;
182
183                 // If it's a raw image, show a JPEG link.
184                 var fulldownload = document.getElementById("fulldownload");
185                 if (filename.match(/\.(nef|cr2)$/i)) {
186                         fulldownload.style.display = "block";
187                         var full_url = window.location.origin + "/" + evt + "/" + filename;
188                         document.getElementById("fulldownloadlink").href = full_url;
189                         origdownload.innerHTML = "Download original image (RAW)";
190                 } else {
191                         fulldownload.style.display = "none";
192                         origdownload.innerHTML = "Download original image";
193                 }
194
195                 // replace the anchor part (if any) with the image number
196                 window.location.hash = "#" + (num+1);
197         }
198 }
199
200 function can_go_next()
201 {
202         return (global_image_num < global_image_list.length - 1);
203 }
204
205 function can_go_previous()
206 {
207         return (global_image_num > 0);
208 }
209
210 function set_opacity(id, amount)
211 {
212         var elem = document.getElementById(id);
213
214         // If optionmenu is visible, options is also visible.
215         if (id === "options" && amount < 0.7) {
216                 var optionmenu = document.getElementById("optionmenu");
217                 if (optionmenu.style.display === "block") {
218                         amount = 0.7;
219                 }
220         }
221         elem.style.opacity = amount;
222 }
223
224 function position_image(img, backend_width, backend_height, offset, box)
225 {
226         var screen_size = find_width();
227         var dpr = find_dpr();
228         var width, height;
229
230         if (backend_width == -1) {
231                 // no size information, use our pessimal guess
232                 var adjusted_size = max_image_size(screen_size);
233                 width = adjusted_size[0];
234                 height = adjusted_size[1];
235         } else {
236                 // use the exact information
237                 var adjusted_size = pick_image_size(screen_size, [ backend_width, backend_height ]);
238                 width = adjusted_size[2];
239                 height = adjusted_size[3];
240         }
241
242         var extra_x_offset = find_width()[0] * offset;
243         var left = (screen_size[0] - width) / 2;
244         var top = (screen_size[1] - height) / 2;
245
246         if (global_infobox) top -= dpr * (24/2);
247
248         // center the image on-screen
249         img.style.position = "absolute";
250         img.style.left = (left / dpr) + "px";
251         img.style.transform = "translate(" + extra_x_offset + "px,0px)";
252
253         if (box) {
254                 img.style.top = (top + height) / dpr + "px";
255         } else {
256                 img.style.top = (top / dpr) + "px";
257                 img.style.lineHeight = (height / dpr) + "px";
258                 img.style.width = (width / dpr) + "px";
259                 img.style.height = (height / dpr) + "px";
260         }
261 }
262
263 function update_shown_images()
264 {
265         // Go through and remove all the elements that are not supposed to be there.
266         var main = document.getElementById("main");
267         var children = main.children;
268         var to_remove = [];
269         for (var i = 0; i < children.length; i++) {
270                 var child = children[i];
271                 var inum = null;
272                 if (child.className === "fsimg") {
273                         inum = parseInt(child.id);
274                 } else if (child.className === "fsbox") {
275                         inum = parseInt(child.id.replace("_box", ""));
276                 } else {
277                         continue;
278                 }
279
280                 // FIXME: For whatever reason, if we don't actually remove an item
281                 // and then scroll it out of view, Chrome scrolls to it.
282                 // So don't keep anything that's going to be a preload;
283                 // we'll have to recreate the element entirely.
284                 //if (inum !== global_image_num - 1 &&
285                 //    inum !== global_image_num &&
286                 //    inum !== global_image_num + 1) {
287                 //      to_remove.push(child);
288                 //}
289                 if (inum !== global_image_num) {
290                         to_remove.push(child);
291                 }
292         }
293         for (let child of to_remove) {
294                 child.parentNode.removeChild(child);
295         }
296
297         // Add any missing elements. Note that we add the main one first,
298         // so that the preloads have an element to fire onload off of.
299         display_image_num(global_image_num, 0);
300         if (can_go_previous()) {
301                 display_image_num(global_image_num - 1, -1);
302         }
303         if (can_go_next()) {
304                 display_image_num(global_image_num + 1, 1);
305         }
306 }
307
308 function relayout()
309 {
310         update_shown_images();
311
312         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
313         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
314         set_opacity("close", global_default_opacity);
315         set_opacity("options", global_default_opacity);
316 }
317
318 function go_previous()
319 {
320         if (!can_go_previous()) {
321                 return;
322         }
323
324         --global_image_num;
325         update_shown_images();
326         if (can_go_previous()) {
327                 set_opacity("previous", global_default_opacity);
328         } else {
329                 set_opacity("previous", global_disabled_opacity);
330         }
331         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
332 }
333
334 function go_next()
335 {
336         if (!can_go_next()) {
337                 return;
338         }
339
340         ++global_image_num;
341         update_shown_images();
342         if (can_go_next()) {
343                 set_opacity("next", global_default_opacity);
344         } else {
345                 set_opacity("next", global_disabled_opacity);
346         }
347         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
348 }
349
350 function do_close()
351 {
352         if (global_image_num > 0) {
353                 window.location = global_return_url + '#' + (global_image_num + 1);
354         } else {
355                 window.location = global_return_url;
356         }
357 }
358
359 function toggle_optionmenu()
360 {
361         var optionmenu = document.getElementById("optionmenu");
362         if (optionmenu.style.display === "block") {
363                 optionmenu.style.display = "none";
364         } else {
365                 optionmenu.style.display = "block";
366                 set_opacity("options", 0.7);
367         }
368 }
369 window['toggle_optionmenu'] = toggle_optionmenu;
370
371 function draw_text(msg)
372 {
373         // remove any text we might have left
374         var text = document.getElementById("text");
375         if (text !== null) {
376                 text.parentNode.removeChild(text);
377         }
378
379         text = document.createElement("p");
380         text.id = "text";
381         text.style.position = "absolute";
382         text.style.color = "white";
383         text.style.lineHeight = "24px";
384         text.style.font = "24px verdana, arial, sans-serif";
385         text.innerHTML = msg;
386
387         document.getElementById("main").appendChild(text);
388
389         var screen_size = find_width();
390         text.style.left = (screen_size[0] - text.clientWidth) / 2 + "px";
391         text.style.top = (screen_size[1] - text.clientHeight) / 2 + "px";
392 }
393
394 function fade_text(opacity)
395 {
396         set_opacity("text", opacity);
397         if (opacity > 0.0) {
398                 opacity -= 0.03;
399                 if (opacity < 0.0) {
400                         opacity = 0.0;
401                 }
402                 setTimeout(function() { fade_text(opacity); }, 30);
403         } else {
404                 var text = document.getElementById("text");
405                 if (text !== null) {
406                         text.parentNode.removeChild(text);
407                 }
408         }
409 }
410
411 function select_image(evt, filename, selected)
412 {
413         if (selected) {
414                 draw_text("Selecting " + filename + "...");
415         } else {
416                 draw_text("Unselecting " + filename + "...");
417         }
418         
419         var req = new XMLHttpRequest();
420         req.open("POST", window.location.origin + "/select", true);
421         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
422         req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
423
424         setTimeout(function() { fade_text(0.99); }, 30);
425 }
426
427 function key_down(which)
428 {
429         if (which == 39) {   // right
430                 if (can_go_next()) {
431                         set_opacity("next", global_highlight_opacity);
432                 }
433         } else if (which == 37) {   // left
434                 if (can_go_previous()) {
435                         set_opacity("previous", global_highlight_opacity);
436                 }
437         } else if (which == 27) {   // escape
438                 set_opacity("close", global_higlight_opacity);
439         } else {
440                 check_for_hash_change();
441         }
442 }
443
444 function key_up(which) {
445         if (which == 39) {   // right
446                 if (can_go_next()) {
447                         set_opacity("next", global_default_opacity);
448                         go_next();
449                 }
450         } else if (which == 37) {   // left
451                 if (can_go_previous()) {
452                         set_opacity("previous", global_default_opacity);
453                         go_previous();
454                 }
455         } else if (which == 27) {   // escape
456                 set_opacity("close", global_default_opacity);
457                 do_close();
458         } else if (which == 32 && global_select) {   // space
459                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
460         } else if (which == 85 && global_select) {   // u
461                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
462         } else {
463                 check_for_hash_change();
464         }
465 }
466
467 function parse_image_num(default_value) {
468         var num = parseInt(window.location.hash.substr(1));
469         if (num >= 1 && num <= global_image_list.length) {  // and then num != NaN
470                 return (num - 1);
471         } else {
472                 return default_value;
473         }
474 }
475 window['parse_image_num'] = parse_image_num;
476
477 function check_for_hash_change() {
478         var num = parse_image_num(-1);
479         if (num != -1 && num != global_image_num) {
480                 global_image_num = num;
481                 relayout();
482         }
483 }
484
485 function toggle_immersive() {
486         if (global_default_opacity == 0.7) {
487                 global_disabled_opacity = 0.0;
488                 global_default_opacity = 0.0;
489                 global_highlight_opacity = 0.2;
490                 global_infobox = false;
491                 document.getElementById('immersivetoggle').innerHTML = 'Show decorations';
492         } else {
493                 global_disabled_opacity = 0.1;
494                 global_default_opacity = 0.7;
495                 global_highlight_opacity = 1.0;
496                 global_infobox = true;
497                 document.getElementById('immersivetoggle').innerHTML = 'Hide all decorations';
498         }
499         relayout();
500 }
501 window['toggle_immersive'] = toggle_immersive;
502
503 var swiping = false;
504 var swipe_start_x = 0;
505
506 function set_swipe_pos(x, transition)
507 {
508         x = Math.max(x, -window.innerWidth);
509         x = Math.min(x,  window.innerWidth);
510         if (!can_go_previous()) {
511                 x = Math.min(x, window.innerWidth / 8);
512         }
513         if (!can_go_next()) {
514                 x = Math.max(x, -window.innerWidth / 8);
515         }
516
517         var dpr = find_dpr();
518         var main = document.getElementById("main");
519         var children = main.children;
520         var to_remove = [];
521         for (var i = 0; i < children.length; i++) {
522                 var child = children[i];
523                 if (child.className === "fsimg") {
524                         var inum = parseInt(child.id);
525                         var offset = inum - global_image_num;
526                         child.style.transition = transition;
527                         child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px)";
528                 } else if (child.className === "fsbox") {
529                         var inum = parseInt(child.id.replace("_box", ""));
530                         var offset = inum - global_image_num;
531                         child.style.transition = transition;
532                         child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px) scale(" + (1.0 / dpr) + ")";
533                 }
534         }
535 }
536
537 function start_swipe(e)
538 {
539         swiping = true;
540         swipe_start_x = e.changedTouches[0].pageX;
541 }
542
543 function end_swipe(e)
544 {
545         if (swiping) {
546                 var new_x = (e.changedTouches[0].pageX - swipe_start_x);
547                 if (new_x < -window.innerWidth / 4 && can_go_next()) {
548                         set_swipe_pos(-window.innerWidth, "transform 0.1s ease-out");
549                         setTimeout(function() { go_next(); }, 100);
550                 } else if (new_x > window.innerWidth / 4 && can_go_previous()) {
551                         set_swipe_pos(window.innerWidth, "transform 0.1s ease-out");
552                         setTimeout(function() { go_previous(); }, 100);
553                 } else {
554                         set_swipe_pos(0, "transform 0.1s ease-out");
555                 }
556                 swiping = false;
557         }
558 }
559
560 function swipe(e)
561 {
562         if (swiping) {
563                 var new_x = (e.changedTouches[0].pageX - swipe_start_x);
564                 set_swipe_pos(new_x, null);
565         }
566 }
567
568 window.onload = function() {
569         relayout();
570         setInterval(check_for_hash_change, 1000);
571
572         document.addEventListener('touchstart', start_swipe, false);
573         document.addEventListener('touchend', end_swipe, false);
574         document.addEventListener('touchmove', swipe, false)
575
576         var body = document.body;
577         body.onresize = function() { relayout(); };
578         body.onkeydown = function(evt) { key_down(evt.keyCode); };
579         body.onkeyup = function(evt) { key_up(evt.keyCode); };
580         body.onhashchange = function() { check_for_hash_change(); };
581         body.onclick = function() { check_for_hash_change(); };
582
583         var previous = document.getElementById('previous');
584         previous.onmousedown = function() { if (can_go_previous()) { set_opacity('previous', global_highlight_opacity); } };
585         previous.onmouseup = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); go_previous(); } };
586         previous.onmouseout = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); } };
587
588         var next = document.getElementById('next');
589         next.onmousedown = function() { if (can_go_next()) { set_opacity('next', global_highlight_opacity); } };
590         next.onmouseup = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); go_next(); } };
591         next.onmouseout = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); } };
592
593         var close = document.getElementById('close');
594         close.onmousedown = function() { set_opacity('close', global_highlight_opacity); };
595         close.onmouseup = function() { set_opacity('close', global_default_opacity); do_close(); };
596         close.onmouseout = function() { set_opacity('close', global_default_opacity); };
597
598         var options = document.getElementById('options');
599         options.onmousedown = function() { set_opacity('options', global_highlight_opacity); };
600         options.onmouseup = function() { set_opacity('options', global_default_opacity); toggle_optionmenu(); };
601         options.onmouseout = function() { set_opacity('options', global_default_opacity); };
602 };
603
604 })();