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