]> git.sesse.net Git - pr0n/blob - files/pr0n-fullscreen.js
Fix an (irrelevant) confusion about addEventListener.
[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)
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 = "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, false);
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; }, { 'once': true });
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                         img.style.display = null;  // Must be done before position_image(), for measurement.
139                         position_image(img, backend_width, backend_height, offset, false);
140                 }, 1);
141         }
142 }
143
144 function display_infobox(html, backend_width, backend_height, elem_id, offset)
145 {
146         // See if this image already exists in the DOM; if not, add it.
147         var box = document.getElementById(elem_id);
148         if (box === null) {
149                 box = document.createElement("div");
150                 box.id = elem_id;
151                 box.alt = "";
152                 box.className = "fsbox";
153         }
154         box.style.position = "absolute";
155         box.style.transformOrigin = "top left";
156         box.innerHTML = html;
157         document.getElementById("main").appendChild(box);
158
159         if (offset === 0) {
160                 position_image(box, backend_width, backend_height, offset, true);
161         } else {
162                 // This is a preload.
163                 // Seemingly one needs to delay position_image(), or Firefox will set the initial
164                 // scroll offset completely off.
165                 box.style.display = 'none';
166                 setTimeout(function() {
167                         box.style.display = null;  // Must be done before position_image(), for measurement.
168                         position_image(box, backend_width, backend_height, offset, true);
169                 }, 1);
170         }
171 }
172
173 function display_image_num(num, offset)
174 {
175         var screen_size = find_width();
176         var adjusted_size;
177
178         if (global_image_list[num][2] == -1) {
179                 // no size information, use our pessimal guess
180                 adjusted_size = max_image_size(screen_size);
181         } else {
182                 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
183         }
184
185         var evt = global_image_list[num][0];
186         var filename = global_image_list[num][1];
187         var backend_width = adjusted_size[0];
188         var backend_height = adjusted_size[1];
189         var url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/" + filename;
190         var elem_id = num;
191
192         display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset);
193
194         if (global_infobox) {
195                 var elem_id = num + "_box";
196                 display_infobox(global_image_list[num][4], adjusted_size[2], adjusted_size[3], elem_id, offset);
197         }
198
199         if (offset === 0) {
200                 // Update the "download original" link.
201                 var original_url = window.location.origin + "/" + evt + "/original/" + filename;
202                 document.getElementById("origdownload").href = original_url;
203
204                 // If it's a raw image, show a JPEG link.
205                 var fulldownload = document.getElementById("fulldownload");
206                 if (filename.match(/\.(nef|cr2)$/i)) {
207                         fulldownload.style.display = "block";
208                         var full_url = window.location.origin + "/" + evt + "/" + filename;
209                         document.getElementById("fulldownloadlink").href = full_url;
210                         origdownload.innerHTML = "Download original image (RAW)";
211                 } else {
212                         fulldownload.style.display = "none";
213                         origdownload.innerHTML = "Download original image";
214                 }
215
216                 // replace the anchor part (if any) with the image number
217                 window.location.hash = "#" + (num+1);
218         }
219 }
220
221 function can_go_next()
222 {
223         return (global_image_num < global_image_list.length - 1);
224 }
225
226 function can_go_previous()
227 {
228         return (global_image_num > 0);
229 }
230
231 function set_opacity(id, amount)
232 {
233         var elem = document.getElementById(id);
234
235         // If optionmenu is visible, options is also visible.
236         if (id === "options" && amount < 0.7) {
237                 var optionmenu = document.getElementById("optionmenu");
238                 if (optionmenu.style.display === "block") {
239                         amount = 0.7;
240                 }
241         }
242         elem.style.opacity = amount;
243 }
244
245 function position_image(img, backend_width, backend_height, offset, box)
246 {
247         var screen_size = find_width();
248         var dpr = find_dpr();
249         var width, height;
250
251         if (backend_width == -1) {
252                 // no size information, use our pessimal guess
253                 var adjusted_size = max_image_size(screen_size);
254                 width = adjusted_size[0];
255                 height = adjusted_size[1];
256         } else {
257                 // use the exact information
258                 var adjusted_size = pick_image_size(screen_size, [ backend_width, backend_height ]);
259                 width = adjusted_size[2];
260                 height = adjusted_size[3];
261         }
262
263         var extra_x_offset = find_width()[0] * offset;
264         var left = (screen_size[0] - width) / 2;
265         var top = (screen_size[1] - height) / 2;
266
267         if (global_infobox) {
268                 top = Math.max(top - dpr * (24/2), 0);
269         }
270
271         // center the image on-screen
272         img.style.position = "absolute";
273         img.style.left = (left / dpr) + "px";
274         img.style.transform = "translate(" + extra_x_offset + "px,0px)";
275
276         if (box) {
277                 img.style.top = Math.min(top + height, screen_size[1] - 24 * dpr) / dpr + "px";
278                 img.style.height = "24px";
279                 img.style.width = null;
280                 img.style.whiteSpace = 'nowrap';
281                 // Hide the box if there's no room for all the text.
282                 img.style.opacity = (img.clientWidth < width / dpr + 10) ? null : 0.0;
283                 img.style.width = (width / dpr) + "px";
284         } else {
285                 img.style.top = (top / dpr) + "px";
286                 img.style.lineHeight = (height / dpr) + "px";
287                 img.style.width = (width / dpr) + "px";
288                 img.style.height = (height / dpr) + "px";
289         }
290 }
291
292 function update_shown_images()
293 {
294         // Go through and remove all the elements that are not supposed to be there.
295         var main = document.getElementById("main");
296         var children = main.children;
297         var to_remove = [];
298         for (var i = 0; i < children.length; i++) {
299                 var child = children[i];
300                 var inum = null;
301                 if (child.className === "fsimg") {
302                         inum = parseInt(child.id);
303                 } else if (child.className === "fsbox") {
304                         inum = parseInt(child.id.replace("_box", ""));
305                 } else {
306                         continue;
307                 }
308
309                 // FIXME: For whatever reason, if we don't actually remove an item
310                 // and then scroll it out of view, Chrome scrolls to it.
311                 // So don't keep anything that's going to be a preload;
312                 // we'll have to recreate the element entirely.
313                 //if (inum !== global_image_num - 1 &&
314                 //    inum !== global_image_num &&
315                 //    inum !== global_image_num + 1) {
316                 //      to_remove.push(child);
317                 //}
318                 if (inum !== global_image_num || (child.className === "fsbox" && !global_infobox)) {
319                         to_remove.push(child);
320                 }
321         }
322         for (let child of to_remove) {
323                 child.parentNode.removeChild(child);
324         }
325
326         // Add any missing elements. Note that we add the main one first,
327         // so that the preloads have an element to fire onload off of.
328         display_image_num(global_image_num, 0);
329         if (can_go_previous()) {
330                 display_image_num(global_image_num - 1, -1);
331         }
332         if (can_go_next()) {
333                 display_image_num(global_image_num + 1, 1);
334         }
335 }
336
337 function relayout()
338 {
339         update_shown_images();
340
341         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
342         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
343         set_opacity("close", global_default_opacity);
344         set_opacity("options", global_default_opacity);
345 }
346
347 function go_previous()
348 {
349         if (!can_go_previous()) {
350                 return;
351         }
352
353         --global_image_num;
354         update_shown_images();
355         if (can_go_previous()) {
356                 set_opacity("previous", global_default_opacity);
357         } else {
358                 set_opacity("previous", global_disabled_opacity);
359         }
360         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
361 }
362
363 function go_next()
364 {
365         if (!can_go_next()) {
366                 return;
367         }
368
369         ++global_image_num;
370         update_shown_images();
371         if (can_go_next()) {
372                 set_opacity("next", global_default_opacity);
373         } else {
374                 set_opacity("next", global_disabled_opacity);
375         }
376         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
377 }
378
379 function do_close()
380 {
381         if (global_image_num > 0) {
382                 window.location = global_return_url + '#' + (global_image_num + 1);
383         } else {
384                 window.location = global_return_url;
385         }
386 }
387
388 function toggle_optionmenu()
389 {
390         var optionmenu = document.getElementById("optionmenu");
391         if (optionmenu.style.display === "block") {
392                 optionmenu.style.display = "none";
393         } else {
394                 optionmenu.style.display = "block";
395                 set_opacity("options", 0.7);
396         }
397 }
398 window['toggle_optionmenu'] = toggle_optionmenu;
399
400 function draw_text(msg)
401 {
402         // remove any text we might have left
403         var text = document.getElementById("text");
404         if (text !== null) {
405                 text.parentNode.removeChild(text);
406         }
407
408         text = document.createElement("p");
409         text.id = "text";
410         text.style.position = "absolute";
411         text.style.color = "white";
412         text.style.lineHeight = "24px";
413         text.style.font = "24px verdana, arial, sans-serif";
414         text.innerHTML = msg;
415
416         document.getElementById("main").appendChild(text);
417
418         var screen_size = find_width();
419         text.style.left = (screen_size[0] - text.clientWidth) / 2 + "px";
420         text.style.top = (screen_size[1] - text.clientHeight) / 2 + "px";
421 }
422
423 function fade_text(opacity)
424 {
425         set_opacity("text", opacity);
426         if (opacity > 0.0) {
427                 opacity -= 0.03;
428                 if (opacity < 0.0) {
429                         opacity = 0.0;
430                 }
431                 setTimeout(function() { fade_text(opacity); }, 30);
432         } else {
433                 var text = document.getElementById("text");
434                 if (text !== null) {
435                         text.parentNode.removeChild(text);
436                 }
437         }
438 }
439
440 function select_image(evt, filename, selected)
441 {
442         if (selected) {
443                 draw_text("Selecting " + filename + "...");
444         } else {
445                 draw_text("Unselecting " + filename + "...");
446         }
447         
448         var req = new XMLHttpRequest();
449         req.open("POST", window.location.origin + "/select", true);
450         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
451         req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
452
453         setTimeout(function() { fade_text(0.99); }, 30);
454 }
455
456 function key_down(which)
457 {
458         if (which == 39) {   // right
459                 if (can_go_next()) {
460                         set_opacity("next", global_highlight_opacity);
461                 }
462         } else if (which == 37) {   // left
463                 if (can_go_previous()) {
464                         set_opacity("previous", global_highlight_opacity);
465                 }
466         } else if (which == 27) {   // escape
467                 set_opacity("close", global_higlight_opacity);
468         } else {
469                 check_for_hash_change();
470         }
471 }
472
473 function key_up(which) {
474         if (which == 39) {   // right
475                 if (can_go_next()) {
476                         set_opacity("next", global_default_opacity);
477                         go_next();
478                 }
479         } else if (which == 37) {   // left
480                 if (can_go_previous()) {
481                         set_opacity("previous", global_default_opacity);
482                         go_previous();
483                 }
484         } else if (which == 27) {   // escape
485                 set_opacity("close", global_default_opacity);
486                 do_close();
487         } else if (which == 32 && global_select) {   // space
488                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
489         } else if (which == 85 && global_select) {   // u
490                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
491         } else {
492                 check_for_hash_change();
493         }
494 }
495
496 function parse_image_num(default_value) {
497         var num = parseInt(window.location.hash.substr(1));
498         if (num >= 1 && num <= global_image_list.length) {  // and then num != NaN
499                 return (num - 1);
500         } else {
501                 return default_value;
502         }
503 }
504 window['parse_image_num'] = parse_image_num;
505
506 function check_for_hash_change() {
507         var num = parse_image_num(-1);
508         if (num != -1 && num != global_image_num) {
509                 global_image_num = num;
510                 relayout();
511         }
512 }
513
514 function toggle_immersive() {
515         if (global_default_opacity == 0.7) {
516                 global_disabled_opacity = 0.0;
517                 global_default_opacity = 0.0;
518                 global_highlight_opacity = 0.2;
519                 global_infobox = false;
520                 document.getElementById('immersivetoggle').innerHTML = 'Show decorations';
521         } else {
522                 global_disabled_opacity = 0.1;
523                 global_default_opacity = 0.7;
524                 global_highlight_opacity = 1.0;
525                 global_infobox = true;
526                 document.getElementById('immersivetoggle').innerHTML = 'Hide all decorations';
527         }
528         relayout();
529 }
530 window['toggle_immersive'] = toggle_immersive;
531
532 var swiping = false;
533 var swipe_start_x = 0;
534
535 function set_swipe_pos(x, transition)
536 {
537         x = Math.max(x, -window.innerWidth);
538         x = Math.min(x,  window.innerWidth);
539         if (!can_go_previous()) {
540                 x = Math.min(x, window.innerWidth / 8);
541         }
542         if (!can_go_next()) {
543                 x = Math.max(x, -window.innerWidth / 8);
544         }
545
546         var dpr = find_dpr();
547         var main = document.getElementById("main");
548         var children = main.children;
549         var to_remove = [];
550         for (var i = 0; i < children.length; i++) {
551                 var child = children[i];
552                 if (child.className === "fsimg") {
553                         var inum = parseInt(child.id);
554                         var offset = inum - global_image_num;
555                         child.style.transition = transition;
556                         child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px)";
557                 } else if (child.className === "fsbox") {
558                         var inum = parseInt(child.id.replace("_box", ""));
559                         var offset = inum - global_image_num;
560                         child.style.transition = transition;
561                         child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px)";
562                 }
563         }
564 }
565
566 function start_swipe(e)
567 {
568         swiping = true;
569         swipe_start_x = e.changedTouches[0].pageX;
570 }
571
572 function end_swipe(e)
573 {
574         if (swiping) {
575                 var new_x = (e.changedTouches[0].pageX - swipe_start_x);
576                 if (new_x < -window.innerWidth / 4 && can_go_next()) {
577                         set_swipe_pos(-window.innerWidth, "transform 0.1s ease-out");
578                         setTimeout(function() { go_next(); }, 100);
579                 } else if (new_x > window.innerWidth / 4 && can_go_previous()) {
580                         set_swipe_pos(window.innerWidth, "transform 0.1s ease-out");
581                         setTimeout(function() { go_previous(); }, 100);
582                 } else {
583                         set_swipe_pos(0, "transform 0.1s ease-out");
584                 }
585                 swiping = false;
586         }
587 }
588
589 function swipe(e)
590 {
591         if (swiping) {
592                 var new_x = (e.changedTouches[0].pageX - swipe_start_x);
593                 set_swipe_pos(new_x, null);
594         }
595 }
596
597 window.onload = function() {
598         relayout();
599         setInterval(check_for_hash_change, 1000);
600
601         document.addEventListener('touchstart', start_swipe, false);
602         document.addEventListener('touchend', end_swipe, false);
603         document.addEventListener('touchmove', swipe, false)
604
605         var body = document.body;
606         body.onresize = function() { relayout(); };
607         body.onkeydown = function(evt) { key_down(evt.keyCode); };
608         body.onkeyup = function(evt) { key_up(evt.keyCode); };
609         body.onhashchange = function() { check_for_hash_change(); };
610         body.onclick = function() { check_for_hash_change(); };
611
612         var previous = document.getElementById('previous');
613         previous.onmousedown = function() { if (can_go_previous()) { set_opacity('previous', global_highlight_opacity); } };
614         previous.onmouseup = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); go_previous(); } };
615         previous.onmouseout = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); } };
616
617         var next = document.getElementById('next');
618         next.onmousedown = function() { if (can_go_next()) { set_opacity('next', global_highlight_opacity); } };
619         next.onmouseup = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); go_next(); } };
620         next.onmouseout = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); } };
621
622         var close = document.getElementById('close');
623         close.onmousedown = function() { set_opacity('close', global_highlight_opacity); };
624         close.onmouseup = function() { set_opacity('close', global_default_opacity); do_close(); };
625         close.onmouseout = function() { set_opacity('close', global_default_opacity); };
626
627         var options = document.getElementById('options');
628         options.onmousedown = function() { set_opacity('options', global_highlight_opacity); };
629         options.onmouseup = function() { set_opacity('options', global_default_opacity); toggle_optionmenu(); };
630         options.onmouseout = function() { set_opacity('options', global_default_opacity); };
631 };
632
633 })();