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, preload)
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 = preload ? "fsbox" : "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, preload);
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() { position_image(img, backend_width, backend_height, offset, preload); img.style.display = null; }, 1);
141 function display_image_num(num, offset)
143 var screen_size = find_width();
146 if (global_image_list[num][2] == -1) {
147 // no size information, use our pessimal guess
148 adjusted_size = max_image_size(screen_size);
150 adjusted_size = pick_image_size(screen_size, [ global_image_list[num][2], global_image_list[num][3] ]);
153 var evt = global_image_list[num][0];
154 var filename = global_image_list[num][1];
155 var backend_width = adjusted_size[0];
156 var backend_height = adjusted_size[1];
157 var url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/" + filename;
160 display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset, false);
162 if (global_infobox) {
164 var dpr = find_dpr();
165 var elem_id = num + "_box";
167 url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "/box/" + filename;
169 url = window.location.origin + "/" + evt + "/" + backend_width + "x" + backend_height + "@" + dpr.toFixed(2) + "/box/" + filename;
171 display_image(url, adjusted_size[2], adjusted_size[3], elem_id, offset, true);
175 // Update the "download original" link.
176 var original_url = window.location.origin + "/" + evt + "/original/" + filename;
177 document.getElementById("origdownload").href = original_url;
179 // If it's a raw image, show a JPEG link.
180 var fulldownload = document.getElementById("fulldownload");
181 if (filename.match(/\.(nef|cr2)$/i)) {
182 fulldownload.style.display = "block";
183 var full_url = window.location.origin + "/" + evt + "/" + filename;
184 document.getElementById("fulldownloadlink").href = full_url;
185 origdownload.innerHTML = "Download original image (RAW)";
187 fulldownload.style.display = "none";
188 origdownload.innerHTML = "Download original image";
191 // replace the anchor part (if any) with the image number
192 window.location.hash = "#" + (num+1);
196 function can_go_next()
198 return (global_image_num < global_image_list.length - 1);
201 function can_go_previous()
203 return (global_image_num > 0);
206 function set_opacity(id, amount)
208 var elem = document.getElementById(id);
210 // If optionmenu is visible, options is also visible.
211 if (id === "options" && amount < 0.7) {
212 var optionmenu = document.getElementById("optionmenu");
213 if (optionmenu.style.display === "block") {
217 elem.style.opacity = amount;
220 function position_image(img, backend_width, backend_height, offset, preload)
222 var screen_size = find_width();
223 var dpr = find_dpr();
226 if (backend_width == -1) {
227 // no size information, use our pessimal guess
228 var adjusted_size = max_image_size(screen_size);
229 width = adjusted_size[0];
230 height = adjusted_size[1];
232 // use the exact information
233 var adjusted_size = pick_image_size(screen_size, [ backend_width, backend_height ]);
234 width = adjusted_size[2];
235 height = adjusted_size[3];
238 var extra_x_offset = find_width()[0] * offset;
239 var left = (screen_size[0] - width) / 2;
240 var top = (screen_size[1] - height) / 2;
242 if (global_infobox) top -= dpr * (24/2);
244 // center the image on-screen
245 img.style.position = "absolute";
246 img.style.left = (left / dpr) + "px";
247 img.style.transform = "translate(" + extra_x_offset + "px,0px)";
250 img.style.top = (top + height) / dpr + "px";
252 img.style.top = (top / dpr) + "px";
253 img.style.lineHeight = (height / dpr) + "px";
254 img.style.width = (width / dpr) + "px";
255 img.style.height = (height / dpr) + "px";
259 function update_shown_images()
261 // Go through and remove all the elements that are not supposed to be there.
262 var main = document.getElementById("main");
263 var children = main.children;
265 for (var i = 0; i < children.length; i++) {
266 var child = children[i];
268 if (child.className === "fsimg") {
269 inum = parseInt(child.id);
270 } else if (child.className === "fsbox") {
271 inum = parseInt(child.id.replace("_box", ""));
276 // FIXME: For whatever reason, if we don't actually remove an item
277 // and then scroll it out of view, Chrome scrolls to it.
278 // So don't keep anything that's going to be a preload;
279 // we'll have to recreate the element entirely.
280 //if (inum !== global_image_num - 1 &&
281 // inum !== global_image_num &&
282 // inum !== global_image_num + 1) {
283 // to_remove.push(child);
285 if (inum !== global_image_num) {
286 to_remove.push(child);
289 for (let child of to_remove) {
290 child.parentNode.removeChild(child);
293 // Add any missing elements. Note that we add the main one first,
294 // so that the preloads have an element to fire onload off of.
295 display_image_num(global_image_num, 0);
296 if (can_go_previous()) {
297 display_image_num(global_image_num - 1, -1);
300 display_image_num(global_image_num + 1, 1);
306 update_shown_images();
308 set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
309 set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
310 set_opacity("close", global_default_opacity);
311 set_opacity("options", global_default_opacity);
314 function go_previous()
316 if (!can_go_previous()) {
321 update_shown_images();
322 if (can_go_previous()) {
323 set_opacity("previous", global_default_opacity);
325 set_opacity("previous", global_disabled_opacity);
327 set_opacity("next", can_go_next() ? global_default_opacity : global_disabled_opacity);
332 if (!can_go_next()) {
337 update_shown_images();
339 set_opacity("next", global_default_opacity);
341 set_opacity("next", global_disabled_opacity);
343 set_opacity("previous", can_go_previous() ? global_default_opacity : global_disabled_opacity);
348 if (global_image_num > 0) {
349 window.location = global_return_url + '#' + (global_image_num + 1);
351 window.location = global_return_url;
355 function toggle_optionmenu()
357 var optionmenu = document.getElementById("optionmenu");
358 if (optionmenu.style.display === "block") {
359 optionmenu.style.display = "none";
361 optionmenu.style.display = "block";
362 set_opacity("options", 0.7);
365 window['toggle_optionmenu'] = toggle_optionmenu;
367 function draw_text(msg)
369 // remove any text we might have left
370 var text = document.getElementById("text");
372 text.parentNode.removeChild(text);
375 text = document.createElement("p");
377 text.style.position = "absolute";
378 text.style.color = "white";
379 text.style.lineHeight = "24px";
380 text.style.font = "24px verdana, arial, sans-serif";
381 text.innerHTML = msg;
383 var main = document.getElementById("main");
384 main.appendChild(text);
386 text.style.left = (main.clientWidth - text.clientWidth) / 2 + "px";
387 text.style.top = (main.clientHeight - text.clientHeight) / 2 + "px";
390 function fade_text(opacity)
392 set_opacity("text", opacity);
398 setTimeout(function() { fade_text(opacity); }, 30);
400 var text = document.getElementById("text");
402 text.parentNode.removeChild(text);
407 function select_image(evt, filename, selected)
410 draw_text("Selecting " + filename + "...");
412 draw_text("Unselecting " + filename + "...");
415 var req = new XMLHttpRequest();
416 req.open("POST", window.location.origin + "/select", false);
417 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
418 req.send("event=" + evt + "&filename=" + filename + "&selected=" + selected);
420 setTimeout(function() { fade_text(0.99); }, 30);
423 function key_down(which)
425 if (which == 39) { // right
427 set_opacity("next", global_highlight_opacity);
429 } else if (which == 37) { // left
430 if (can_go_previous()) {
431 set_opacity("previous", global_highlight_opacity);
433 } else if (which == 27) { // escape
434 set_opacity("close", global_higlight_opacity);
436 check_for_hash_change();
440 function key_up(which) {
441 if (which == 39) { // right
443 set_opacity("next", global_default_opacity);
446 } else if (which == 37) { // left
447 if (can_go_previous()) {
448 set_opacity("previous", global_default_opacity);
451 } else if (which == 27) { // escape
452 set_opacity("close", global_default_opacity);
454 } else if (which == 32 && global_select) { // space
455 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 1);
456 } else if (which == 85 && global_select) { // u
457 select_image(global_image_list[global_image_num][0], global_image_list[global_image_num][1], 0);
459 check_for_hash_change();
463 function parse_image_num(default_value) {
464 var num = parseInt(window.location.hash.substr(1));
465 if (num >= 1 && num <= global_image_list.length) { // and then num != NaN
468 return default_value;
471 window['parse_image_num'] = parse_image_num;
473 function check_for_hash_change() {
474 var num = parse_image_num(-1);
475 if (num != -1 && num != global_image_num) {
476 global_image_num = num;
481 function toggle_immersive() {
482 if (global_default_opacity == 0.7) {
483 global_disabled_opacity = 0.0;
484 global_default_opacity = 0.0;
485 global_highlight_opacity = 0.2;
486 global_infobox = false;
487 document.getElementById('immersivetoggle').innerHTML = 'Show decorations';
489 global_disabled_opacity = 0.1;
490 global_default_opacity = 0.7;
491 global_highlight_opacity = 1.0;
492 global_infobox = true;
493 document.getElementById('immersivetoggle').innerHTML = 'Hide all decorations';
497 window['toggle_immersive'] = toggle_immersive;
499 window.onload = function() {
501 setInterval(check_for_hash_change, 1000);
503 var body = document.body;
504 body.onresize = function() { relayout(); };
505 body.onkeydown = function(evt) { key_down(evt.keyCode); };
506 body.onkeyup = function(evt) { key_up(evt.keyCode); };
507 body.onhashchange = function() { check_for_hash_change(); };
508 body.onclick = function() { check_for_hash_change(); };
510 var previous = document.getElementById('previous');
511 previous.onmousedown = function() { if (can_go_previous()) { set_opacity('previous', global_highlight_opacity); } };
512 previous.onmouseup = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); go_previous(); } };
513 previous.onmouseout = function() { if (can_go_previous()) { set_opacity('previous', global_default_opacity); } };
515 var next = document.getElementById('next');
516 next.onmousedown = function() { if (can_go_next()) { set_opacity('next', global_highlight_opacity); } };
517 next.onmouseup = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); go_next(); } };
518 next.onmouseout = function() { if (can_go_next()) { set_opacity('next', global_default_opacity); } };
520 var close = document.getElementById('close');
521 close.onmousedown = function() { set_opacity('close', global_highlight_opacity); };
522 close.onmouseup = function() { set_opacity('close', global_default_opacity); do_close(); };
523 close.onmouseout = function() { set_opacity('close', global_default_opacity); };
525 var options = document.getElementById('options');
526 options.onmousedown = function() { set_opacity('options', global_highlight_opacity); };
527 options.onmouseup = function() { set_opacity('options', global_default_opacity); toggle_optionmenu(); };
528 options.onmouseout = function() { set_opacity('options', global_default_opacity); };