a52bf8bddf930d3043722e2d0a790e62e3fc42df
[pr0n] / files / pr0n-fullscreen.js
1 var req;
2
3 function init_ajax()
4 {
5         req = new XMLHttpRequest();
6 }
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         // Update the "download original" link.
143         var original_url = window.location.origin + "/" + evt + "/original/" + filename;
144         document.getElementById("origdownload").href = original_url;
145
146         if (global_infobox) {
147                 var url;
148                 if (dpr == 1) {
149                         url = window.location.origin + "/" + evt + "/" + width + "x" + height + "/box/" + filename;
150                 } else {
151                         url = window.location.origin + "/" + evt + "/" + width + "x" + height + "@" + dpr.toFixed(2) + "/box/" + filename;
152                 }
153                 var boximg = replace_image_element(url, element_id + "_box", main);
154
155                 boximg.style.position = "absolute";
156                 boximg.style.left = "0px";
157                 boximg.style.bottom = "-1px";
158                 boximg.style.transformOrigin = "bottom left";
159                 boximg.style.transform = "scale(" + (1.0 / dpr) + ")";
160         } else {
161                 var boximg = document.getElementById(element_id + "_box");
162                 if (boximg !== null) {
163                         boximg.parentNode.removeChild(boximg);
164                 }
165         }
166
167         return img;
168 }
169
170 function display_image_num(num, element_id)
171 {
172         var screen_size = find_width();
173         var adjusted_size;
174
175         if (global_image_list[num][2] == -1) {
176                 // no size information, use our pessimal guess
177                 adjusted_size = max_image_size(screen_size);
178         } else {
179                 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
180         }
181
182         var img = display_image(adjusted_size[0], adjusted_size[1], global_image_list[num][0], global_image_list[num][1], element_id);
183         
184         if (element_id == "image") {
185                 // we want to shrink the box as much as possible if we know the true
186                 // size of the image
187                 center_image(num);
188                 
189                 // replace the anchor part (if any) with the image number
190                 window.location.hash = "#" + (num+1);
191         }
192
193         return img;
194 }
195
196 function prepare_preload(img, num)
197 {
198         // cancel any pending preload
199         var preload = document.getElementById("preload");
200         if (preload !== null) {
201                 preload.parentNode.removeChild(preload);
202         }
203         
204         var preload_box = document.getElementById("preload_box");
205         if (preload_box !== null) {
206                 preload_box.parentNode.removeChild(preload_box);
207         }
208
209         // grmf -- IE doesn't fire onload if the image was loaded from cache, so check for
210         // completeness first; should at least be _somewhat_ better
211         if (img.complete) {
212                 display_image_num(num, "preload");
213         } else {
214                 img.onload = function() { display_image_num(num, "preload"); };
215         }       
216 }
217
218 function can_go_next()
219 {
220         return (global_image_num < global_image_list.length - 1);
221 }
222
223 function can_go_previous()
224 {
225         return (global_image_num > 0);
226 }
227
228 function set_opacity(id, amount)
229 {
230         var elem = document.getElementById(id);
231
232         // If optionmenu is visible, options is also visible.
233         if (id === "options" && amount < 0.7) {
234                 var optionmenu = document.getElementById("optionmenu");
235                 if (optionmenu.style.display === "block") {
236                         amount = 0.7;
237                 }
238         }
239         elem.style.opacity = amount;
240 }
241
242 function center_image(num)
243 {
244         var screen_size = find_width();
245         var dpr = find_dpr();
246         var width, height;
247         
248         if (global_image_list[num][2] == -1) {
249                 // no size information, use our pessimal guess
250                 var adjusted_size = max_image_size(screen_size);
251                 width = adjusted_size[0];
252                 height = adjusted_size[1];
253         } else {
254                 // use the exact information
255                 var adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
256                 width = adjusted_size[2];
257                 height = adjusted_size[3];
258         }
259
260         // center the image on-screen
261         var main = document.getElementById("main");
262         main.style.position = "absolute";
263         main.style.left = (((screen_size[0] - width) / 2) / dpr) + "px";
264         main.style.top = (((screen_size[1] - height) / 2) / dpr) + "px";
265         main.style.width = (width / dpr) + "px";
266         main.style.height = (height / dpr) + "px";
267         main.style.lineHeight = (height / dpr) + "px";
268 }
269
270 function relayout()
271 {
272         var img = display_image_num(global_image_num, "image");
273         if (can_go_next()) {
274                 prepare_preload(img, global_image_num + 1);
275         }
276
277         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
278         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
279         set_opacity("close", global_default_opacity);
280         set_opacity("options", global_default_opacity);
281 }
282
283 function go_previous()
284 {
285         if (!can_go_previous()) {
286                 return;
287         }
288
289         var img = display_image_num(--global_image_num, "image");
290         if (can_go_previous()) {
291                 set_opacity("previous", global_default_opacity);
292                 prepare_preload(img, global_image_num - 1);
293         } else {
294                 set_opacity("previous", global_disabled_opacity);
295         }
296         set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
297 }
298
299 function go_next()
300 {
301         if (!can_go_next()) {
302                 return;
303         }
304
305         var img = display_image_num(++global_image_num, "image");
306         if (can_go_next()) {
307                 set_opacity("next", global_default_opacity);
308                 prepare_preload(img, global_image_num + 1);
309         } else {
310                 set_opacity("next", global_disabled_opacity);
311         }
312         set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
313 }
314
315 function do_close()
316 {
317         window.location = global_return_url;
318 }
319
320 function toggle_optionmenu()
321 {
322         var optionmenu = document.getElementById("optionmenu");
323         if (optionmenu.style.display === "block") {
324                 optionmenu.style.display = "none";
325         } else {
326                 optionmenu.style.display = "block";
327                 set_opacity("options", 0.7);
328         }
329 }
330
331 function draw_text(msg)
332 {
333         // remove any text we might have left
334         var text = document.getElementById("text");
335         if (text !== null) {
336                 text.parentNode.removeChild(text);
337         }
338
339         text = document.createElement("p");
340         text.id = "text";
341         text.style.position = "absolute";
342         text.style.color = "white";
343         text.style.lineHeight = "24px";
344         text.style.font = "24px verdana, arial, sans-serif";
345         text.innerHTML = msg;
346
347         var main = document.getElementById("main");
348         main.appendChild(text);
349
350         text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
351         text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
352 }
353
354 function fade_text(opacity)
355 {
356         set_opacity("text", opacity);
357         if (opacity > 0.0) {
358                 opacity -= 0.03;
359                 if (opacity < 0.0) {
360                         opacity = 0.0;
361                 }
362                 setTimeout("fade_text(" + opacity + ")", 30);
363         } else {
364                 var text = document.getElementById("text");
365                 if (text !== null) {
366                         text.parentNode.removeChild(text);
367                 }
368         }
369 }
370
371 function select_image(evt, filename, selected)
372 {
373         if (!req) {
374                 return;
375         }
376
377         if (selected) {
378                 draw_text("Selecting " + filename + "...");
379         } else {
380                 draw_text("Unselecting " + filename + "...");
381         }
382         
383         req.open("POST", window.location.origin + "/select", false);
384         req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
385         req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
386
387         setTimeout("fade_text(0.99)", 30);
388 }
389
390 function key_down(which)
391 {
392         if (which == 39) {   // right
393                 if (can_go_next()) {
394                         set_opacity("next", global_highlight_opacity);
395                 }
396         } else if (which == 37) {   // left
397                 if (can_go_previous()) {
398                         set_opacity("previous", global_highlight_opacity);
399                 }
400         } else if (which == 27) {   // escape
401                 set_opacity("close", global_higlight_opacity);
402         } else {
403                 check_for_hash_change();
404         }
405 }
406
407 function key_up(which) {
408         if (which == 39) {   // right
409                 if (can_go_next()) {
410                         set_opacity("next", global_default_opacity);
411                         go_next();
412                 }
413         } else if (which == 37) {   // left
414                 if (can_go_previous()) {
415                         set_opacity("previous", global_default_opacity);
416                         go_previous();
417                 }
418         } else if (which == 27) {   // escape
419                 set_opacity("close", global_default_opacity);
420                 do_close();
421         } else if (which == 32 && global_select) {   // space
422                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
423         } else if (which == 85 && global_select) {   // u
424                 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
425         } else {
426                 check_for_hash_change();
427         }
428 }
429
430 function parse_image_num(default_value) {
431         var num = parseInt(window.location.hash.substr(1));
432         if (num >= 1 && num <= global_image_list.length) {  // and then num != NaN
433                 return (num - 1);
434         } else {
435                 return default_value;
436         }
437 }
438
439 function check_for_hash_change() {
440         var num = parse_image_num(-1);
441         if (num != -1 && num != global_image_num) {
442                 global_image_num = num;
443                 relayout();
444         }
445 }
446
447 function toggle_immersive() {
448         if (global_default_opacity == 0.7) {
449                 global_disabled_opacity = 0.0;
450                 global_default_opacity = 0.0;
451                 global_highlight_opacity = 0.2;
452                 global_infobox = false;
453                 document.getElementById('immersivetoggle').innerHTML = 'Show decorations';
454         } else {
455                 global_disabled_opacity = 0.1;
456                 global_default_opacity = 0.7;
457                 global_highlight_opacity = 1.0;
458                 global_infobox = true;
459                 document.getElementById('immersivetoggle').innerHTML = 'Hide all decorations';
460         }
461         relayout();
462 }