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