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