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;
11 return [window.innerWidth * dpr, window.innerHeight * dpr];
16 return window.devicePixelRatio || 1;
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.
48 function max_image_size(screen_size)
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];
59 function pick_image_size(screen_size, image_size)
62 for (i = 0; i < fixed_sizes.length; ++i) {
63 // this is a duplicate of pr0n's resizing code, hope for no floating-point
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];
70 if (!(thumbxres >= width && thumbyres >= height)) {
71 var sfh = width / thumbxres;
72 var sfv = height / thumbyres;
80 width = Math.floor(width);
81 height = Math.floor(height);
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 ];
92 function rename_element(old_name, new_name)
94 // Remove any element that's in the way.
95 var elem = document.getElementById(new_name);
97 elem.parentNode.removeChild(elem);
100 elem = document.getElementById(old_name);
107 function display_image(url, backend_width, backend_height, elem_id, offset)
109 // See if this image already exists in the DOM; if not, add it.
110 var img = document.getElementById(elem_id);
112 img = document.createElement("img");
115 img.className = "fsimg";
117 img.style.position = "absolute";
118 img.style.transformOrigin = "top left";
119 document.getElementById("main").appendChild(img);
123 position_image(img, backend_width, backend_height, offset, false);
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) {
131 main_img.addEventListener('load', function() { img.src = url; }, false);
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() {
138 position_image(img, backend_width, backend_height, offset, false);
139 img.style.display = null;
144 function display_infobox(html, backend_width, backend_height, elem_id, offset)
146 // See if this image already exists in the DOM; if not, add it.
147 var box = document.getElementById(elem_id);
149 box = document.createElement("div");
152 box.className = "fsbox";
154 box.style.position = "absolute";
155 box.style.transformOrigin = "top left";
156 box.innerHTML = html;
157 document.getElementById("main").appendChild(box);
160 position_image(box, backend_width, backend_height, offset, true);
162 // This is a preload.
163 // Seemingly one needs to delay position_image(), or Firefox will set the initial
164 // scroll offset completely off.
165 box.style.display = 'none';
166 setTimeout(function() {
167 position_image(box, backend_width, backend_height, offset, true);
168 box.style.display = null;
173 function display_image_num(num, offset)
175 var screen_size = find_width();
178 if (global_image_list[num][2] == -1) {
179 // no size information, use our pessimal guess
180 adjusted_size = max_image_size(screen_size);
182 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
185 var evt = global_image_list[num][0];
186 var filename = global_image_list[num][1];
187 var backend_width = adjusted_size[0];
188 var backend_height = adjusted_size[1];
189 var url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/" + filename;
192 display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset);
194 if (global_infobox) {
195 var elem_id = num + "_box";
196 display_infobox(global_image_list[num][4], adjusted_size[2], adjusted_size[3], elem_id, offset);
200 // Update the "download original" link.
201 var original_url = window.location.origin + "/" + evt + "/original/" + filename;
202 document.getElementById("origdownload").href = original_url;
204 // If it's a raw image, show a JPEG link.
205 var fulldownload = document.getElementById("fulldownload");
206 if (filename.match(/\.(nef|cr2)$/i)) {
207 fulldownload.style.display = "block";
208 var full_url = window.location.origin + "/" + evt + "/" + filename;
209 document.getElementById("fulldownloadlink").href = full_url;
210 origdownload.innerHTML = "Download original image (RAW)";
212 fulldownload.style.display = "none";
213 origdownload.innerHTML = "Download original image";
216 // replace the anchor part (if any) with the image number
217 window.location.hash = "#" + (num+1);
221 function can_go_next()
223 return (global_image_num < global_image_list.length - 1);
226 function can_go_previous()
228 return (global_image_num > 0);
231 function set_opacity(id, amount)
233 var elem = document.getElementById(id);
235 // If optionmenu is visible, options is also visible.
236 if (id === "options" && amount < 0.7) {
237 var optionmenu = document.getElementById("optionmenu");
238 if (optionmenu.style.display === "block") {
242 elem.style.opacity = amount;
245 function position_image(img, backend_width, backend_height, offset, box)
247 var screen_size = find_width();
248 var dpr = find_dpr();
251 if (backend_width == -1) {
252 // no size information, use our pessimal guess
253 var adjusted_size = max_image_size(screen_size);
254 width = adjusted_size[0];
255 height = adjusted_size[1];
257 // use the exact information
258 var adjusted_size = pick_image_size(screen_size, [ backend_width, backend_height ]);
259 width = adjusted_size[2];
260 height = adjusted_size[3];
263 var extra_x_offset = find_width()[0] * offset;
264 var left = (screen_size[0] - width) / 2;
265 var top = (screen_size[1] - height) / 2;
267 if (global_infobox) {
268 top = Math.max(top - dpr * (24/2), 0);
271 // center the image on-screen
272 img.style.position = "absolute";
273 img.style.left = (left / dpr) + "px";
274 img.style.transform = "translate(" + extra_x_offset + "px,0px)";
277 img.style.top = Math.min(top + height, screen_size[1] - 24) / dpr + "px";
278 img.style.height = "24px";
279 img.style.width = null;
280 img.style.whiteSpace = 'nowrap';
282 // Hide the box if there's no room for all the text.
283 img.style.opacity = (img.clientWidth < width / dpr + 10) ? null : 0.0;
285 img.style.width = (width / dpr) + "px";
287 img.style.top = (top / dpr) + "px";
288 img.style.lineHeight = (height / dpr) + "px";
289 img.style.width = (width / dpr) + "px";
290 img.style.height = (height / dpr) + "px";
294 function update_shown_images()
296 // Go through and remove all the elements that are not supposed to be there.
297 var main = document.getElementById("main");
298 var children = main.children;
300 for (var i = 0; i < children.length; i++) {
301 var child = children[i];
303 if (child.className === "fsimg") {
304 inum = parseInt(child.id);
305 } else if (child.className === "fsbox") {
306 inum = parseInt(child.id.replace("_box", ""));
311 // FIXME: For whatever reason, if we don't actually remove an item
312 // and then scroll it out of view, Chrome scrolls to it.
313 // So don't keep anything that's going to be a preload;
314 // we'll have to recreate the element entirely.
315 //if (inum !== global_image_num - 1 &&
316 // inum !== global_image_num &&
317 // inum !== global_image_num + 1) {
318 // to_remove.push(child);
320 if (inum !== global_image_num || (child.className === "fsbox" && !global_infobox)) {
321 to_remove.push(child);
324 for (let child of to_remove) {
325 child.parentNode.removeChild(child);
328 // Add any missing elements. Note that we add the main one first,
329 // so that the preloads have an element to fire onload off of.
330 display_image_num(global_image_num, 0);
331 if (can_go_previous()) {
332 display_image_num(global_image_num - 1, -1);
335 display_image_num(global_image_num + 1, 1);
341 update_shown_images();
343 set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
344 set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
345 set_opacity("close", global_default_opacity);
346 set_opacity("options", global_default_opacity);
349 function go_previous()
351 if (!can_go_previous()) {
356 update_shown_images();
357 if (can_go_previous()) {
358 set_opacity("previous", global_default_opacity);
360 set_opacity("previous", global_disabled_opacity);
362 set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
367 if (!can_go_next()) {
372 update_shown_images();
374 set_opacity("next", global_default_opacity);
376 set_opacity("next", global_disabled_opacity);
378 set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
383 if (global_image_num > 0) {
384 window.location = global_return_url + '#' + (global_image_num + 1);
386 window.location = global_return_url;
390 function toggle_optionmenu()
392 var optionmenu = document.getElementById("optionmenu");
393 if (optionmenu.style.display === "block") {
394 optionmenu.style.display = "none";
396 optionmenu.style.display = "block";
397 set_opacity("options", 0.7);
400 window['toggle_optionmenu'] = toggle_optionmenu;
402 function draw_text(msg)
404 // remove any text we might have left
405 var text = document.getElementById("text");
407 text.parentNode.removeChild(text);
410 text = document.createElement("p");
412 text.style.position = "absolute";
413 text.style.color = "white";
414 text.style.lineHeight = "24px";
415 text.style.font = "24px verdana, arial, sans-serif";
416 text.innerHTML = msg;
418 document.getElementById("main").appendChild(text);
420 var screen_size = find_width();
421 text.style.left = (screen_size[0] - text.clientWidth) / 2 + "px";
422 text.style.top = (screen_size[1] - text.clientHeight) / 2 + "px";
425 function fade_text(opacity)
427 set_opacity("text", opacity);
433 setTimeout(function() { fade_text(opacity); }, 30);
435 var text = document.getElementById("text");
437 text.parentNode.removeChild(text);
442 function select_image(evt, filename, selected)
445 draw_text("Selecting " + filename + "...");
447 draw_text("Unselecting " + filename + "...");
450 var req = new XMLHttpRequest();
451 req.open("POST", window.location.origin + "/select", true);
452 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
453 req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
455 setTimeout(function() { fade_text(0.99); }, 30);
458 function key_down(which)
460 if (which == 39) { // right
462 set_opacity("next", global_highlight_opacity);
464 } else if (which == 37) { // left
465 if (can_go_previous()) {
466 set_opacity("previous", global_highlight_opacity);
468 } else if (which == 27) { // escape
469 set_opacity("close", global_higlight_opacity);
471 check_for_hash_change();
475 function key_up(which) {
476 if (which == 39) { // right
478 set_opacity("next", global_default_opacity);
481 } else if (which == 37) { // left
482 if (can_go_previous()) {
483 set_opacity("previous", global_default_opacity);
486 } else if (which == 27) { // escape
487 set_opacity("close", global_default_opacity);
489 } else if (which == 32 && global_select) { // space
490 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
491 } else if (which == 85 && global_select) { // u
492 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
494 check_for_hash_change();
498 function parse_image_num(default_value) {
499 var num = parseInt(window.location.hash.substr(1));
500 if (num >= 1 && num <= global_image_list.length) { // and then num != NaN
503 return default_value;
506 window['parse_image_num'] = parse_image_num;
508 function check_for_hash_change() {
509 var num = parse_image_num(-1);
510 if (num != -1 && num != global_image_num) {
511 global_image_num = num;
516 function toggle_immersive() {
517 if (global_default_opacity == 0.7) {
518 global_disabled_opacity = 0.0;
519 global_default_opacity = 0.0;
520 global_highlight_opacity = 0.2;
521 global_infobox = false;
522 document.getElementById('immersivetoggle').innerHTML = 'Show decorations';
524 global_disabled_opacity = 0.1;
525 global_default_opacity = 0.7;
526 global_highlight_opacity = 1.0;
527 global_infobox = true;
528 document.getElementById('immersivetoggle').innerHTML = 'Hide all decorations';
532 window['toggle_immersive'] = toggle_immersive;
535 var swipe_start_x = 0;
537 function set_swipe_pos(x, transition)
539 x = Math.max(x, -window.innerWidth);
540 x = Math.min(x, window.innerWidth);
541 if (!can_go_previous()) {
542 x = Math.min(x, window.innerWidth / 8);
544 if (!can_go_next()) {
545 x = Math.max(x, -window.innerWidth / 8);
548 var dpr = find_dpr();
549 var main = document.getElementById("main");
550 var children = main.children;
552 for (var i = 0; i < children.length; i++) {
553 var child = children[i];
554 if (child.className === "fsimg") {
555 var inum = parseInt(child.id);
556 var offset = inum - global_image_num;
557 child.style.transition = transition;
558 child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px)";
559 } else if (child.className === "fsbox") {
560 var inum = parseInt(child.id.replace("_box", ""));
561 var offset = inum - global_image_num;
562 child.style.transition = transition;
563 child.style.transform = "translate(" + (x + find_width()[0] * offset / dpr) + "px,0px)";
568 function start_swipe(e)
571 swipe_start_x = e.changedTouches[0].pageX;
574 function end_swipe(e)
577 var new_x = (e.changedTouches[0].pageX - swipe_start_x);
578 if (new_x < -window.innerWidth / 4 && can_go_next()) {
579 set_swipe_pos(-window.innerWidth, "transform 0.1s ease-out");
580 setTimeout(function() { go_next(); }, 100);
581 } else if (new_x > window.innerWidth / 4 && can_go_previous()) {
582 set_swipe_pos(window.innerWidth, "transform 0.1s ease-out");
583 setTimeout(function() { go_previous(); }, 100);
585 set_swipe_pos(0, "transform 0.1s ease-out");
594 var new_x = (e.changedTouches[0].pageX - swipe_start_x);
595 set_swipe_pos(new_x, null);
599 window.onload = function() {
601 setInterval(check_for_hash_change, 1000);
603 document.addEventListener('touchstart', start_swipe, false);
604 document.addEventListener('touchend', end_swipe, false);
605 document.addEventListener('touchmove', swipe, false)
607 var body = document.body;
608 body.onresize = function() { relayout(); };
609 body.onkeydown = function(evt) { key_down(evt.keyCode); };
610 body.onkeyup = function(evt) { key_up(evt.keyCode); };
611 body.onhashchange = function() { check_for_hash_change(); };
612 body.onclick = function() { check_for_hash_change(); };
614 var previous = document.getElementById('previous');
615 previous.onmousedown = function() { if (can_go_previous()) { set_opacity('previous', global_highlight_opacity); } };
616 previous.onmouseup = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); go_previous(); } };
617 previous.onmouseout = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); } };
619 var next = document.getElementById('next');
620 next.onmousedown = function() { if (can_go_next()) { set_opacity('next', global_highlight_opacity); } };
621 next.onmouseup = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); go_next(); } };
622 next.onmouseout = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); } };
624 var close = document.getElementById('close');
625 close.onmousedown = function() { set_opacity('close', global_highlight_opacity); };
626 close.onmouseup = function() { set_opacity('close', global_default_opacity); do_close(); };
627 close.onmouseout = function() { set_opacity('close', global_default_opacity); };
629 var options = document.getElementById('options');
630 options.onmousedown = function() { set_opacity('options', global_highlight_opacity); };
631 options.onmouseup = function() { set_opacity('options', global_default_opacity); toggle_optionmenu(); };
632 options.onmouseout = function() { set_opacity('options', global_default_opacity); };