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