aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/010-control.js
blob: 1c0478d880830bdc8a7908634d3454163fe1d8b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
$(document).ready(function() {
	function cachePath(path) {
		if (path == "")
			return "root";
		if (path[0] == '/')
			path = path.substring(1);
		path = path
			.replace(/ /g, "_")
			.replace(/\//g, "-")
			.replace(/\(/g, "")
			.replace(/\)/g, "")
			.replace(/#/g, "")
			.replace(/&/g, "")
			.replace(/,/g, "")
			.replace(/\[/g, "")
			.replace(/\]/g, "")
			.replace(/"/g, "")
			.replace(/'/g, "")
			.replace(/_-_/g, "-")
			.toLowerCase();
		while (path.indexOf("--") != -1)
			path = path.replace(/--/g, "-");
		while (path.indexOf("__") != -1)
			path = path.replace(/__/g, "_");
		return path;
	}
	function imagePath(image, path, size, square) {
		var suffix;
		if (square)
			suffix = size.toString() + "s";
		else
			suffix = size.toString();
		return "cache/" + cachePath(path + "/" + image + "_" + suffix + ".jpg");
	}
	function properId(path) {
		if (!path[0].match(/[a-zA-Z]/))
			path = "a" + path;
		return path.replace(/[^a-zA-Z0-9_:.\-]/g, "-");
	}
	function escapeId(id) {
		return id.replace(/\./g, "\\.").replace(/,/g, "\\,");
	}
	function loadAlbum() {
		if (current_album_cache in album_cache) {
			albumLoaded(album_cache[current_album_cache]);
			return;
		}
		$("#loading").show();
		$.ajax({
			type: "GET",
			dataType: "json",
			url: "cache/" + current_album_cache + ".json",
			error: die,
			success: albumLoaded
		});
	}
	function albumLoaded(album) {
		var albumCachePath = cachePath(album.path);
		album_cache[albumCachePath] = album;
		if (albumCachePath != current_album_cache)
			return;
		undie();
		$("#loading").hide();
		current_album = album;
		if (current_photo_cache != null)
			showPhoto();
		showAlbum(true);
		setTitle();
	}
	function trimExtension(title) {
		var index = title.lastIndexOf(".");
		if (index != -1)
			return title.substring(0, index)
		return title;
	}
	function setTitle() {
		var title = "";
		var documentTitle = "";
		var components;
		if (current_album.path.length == 0)
			components = [original_title];
		else {
			components = current_album.path.split("/");
			components.unshift(original_title);
		}
		if (current_photo_cache != null)
			documentTitle += trimExtension(current_photo.name);
		var last = "";
		for (var i = 0; i < components.length; ++i) {
			if (i || current_photo_cache != null)
				documentTitle += " \u00ab ";
			if (i)
				last += "/" + components[i];
			if (i < components.length - 1 || current_photo_cache != null)
				title += "<a href=\"#!/" + (i == 0 ? "" : cachePath(last.substring(1))) + "\">";
			title += components[i];
			documentTitle += components[components.length - 1 - i];
			if (i < components.length - 1 || current_photo_cache != null) {
				title += "</a>";
				title += " &raquo; ";
			}
		}
		if (current_photo_cache != null)
			title += trimExtension(current_photo.name);
		$("#title").html(title);
		document.title = documentTitle;
	}
	function showAlbum(populate) {
		if (current_photo_cache == null && previous_photo_cache == null)
			$("html, body").stop().animate({ scrollTop: 0 }, "slow");
		
		if (populate) {
			var photos = "";
			for (var i = 0; i < current_album.photos.length; ++i)
				photos += "<a href=\"#!/" + current_album_cache + "/" + cachePath(current_album.photos[i].name) + "\"><img title=\"" + trimExtension(current_album.photos[i].name) + "\" alt=\"" + trimExtension(current_album.photos[i].name) + "\" id=\"thumb-" + cachePath(current_album.photos[i].name) + "\" src=\"" + imagePath(current_album.photos[i].name, current_album.path, 150, true) + "\" height=\"150\" width=\"150\"></a>";
			$("#thumbs").html(photos);
			
			var subalbums = "";
			var thumbFinderList = new Array();
			for (var i = current_album.albums.length - 1; i >= 0; --i) {
				var path = cachePath(current_album.path + "/" + current_album.albums[i].path);
				var id = properId("album-" + path);
				subalbums += "<a href=\"#!/" + path + "\"><div title=\"" + current_album.albums[i].date + "\" id=\"" + id + "\" class=\"album-button\">" + current_album.albums[i].path + "</div></a>";
				thumbFinderList.push({ path: path, id: escapeId(id) });
			}
			$("#subalbums").html(subalbums);
			for (var i = 0; i < thumbFinderList.length; ++i)
				(function(thumb) {
					albumThumbFinder(thumb.path, function(photo, album) {
						$("#" + thumb.id).css("background-image", "url(" + imagePath(photo.name, album.path, 150, true) + ")");
					});
				})(thumbFinderList[i]);
		}
		
		if (current_photo_cache == null) {
			$("#thumbs img").removeClass("current-thumb");
			$("#album-view").removeClass("photo-view-container");
			$("#subalbums").show();
			$("#photo-view").hide();
		}
		setTimeout(scrollToThumb, 1);
	}
	function getDecimal(fraction) {
		if (fraction[0] < fraction[1])
			return fraction[0] + "/" + fraction[1];
		return (fraction[0] / fraction[1]).toString();
	}
	function scaleImage() {
		var image = $("#photo");
		if (image.get(0) == this)
			$(window).bind("resize", scaleImage);
		var container = $("#photo-view");
		if (image.css("width") != "100%" && container.height() * image.width() / image.height() > container.width())
			image.css("width", "100%").css("height", "auto");
		else if (image.css("height") != "100%")
			image.css("height", "100%").css("width", "auto");
	}
	function showPhoto() {
		currentPhoto();
		if (current_photo == null) {
			die();
			return;
		}
		undie();
		var maxSize = 800;
		var width = current_photo.size[0];
		var height = current_photo.size[1];
		if (width > height) {
			height = height / width * maxSize;
			width = maxSize;
		} else {
			width = width / height * maxSize;
			height = maxSize;
		}
		$(window).unbind("resize", scaleImage);
		var photoSrc = imagePath(current_photo.name, current_album.path, maxSize, false);
		$("#photo")
			.attr("width", width).attr("height", height)
			.attr("src", photoSrc)
			.attr("alt", current_photo.name)
			.attr("title", current_photo.date)
			.load(scaleImage);
		$("head").append("<link rel=\"image_src\" href=\"" + photoSrc + "\" />");
		
		var previousPhoto = current_album.photos[
			(current_photo_index - 1 < 0) ? (current_album.photos.length - 1) : (current_photo_index - 1)
		];
		var nextPhoto = current_album.photos[
			(current_photo_index + 1 >= current_album.photos.length) ? 0 : (current_photo_index + 1)
		];
		$.preloadImages(imagePath(nextPhoto.name, current_album.path, maxSize, false), imagePath(previousPhoto.name, current_album.path, maxSize, false));
		
		var nextLink = "#!/" + current_album_cache + "/" + cachePath(nextPhoto.name);
		$("#next-photo").attr("href", nextLink);
		$("#next").attr("href", nextLink);
		$("#back").attr("href", "#!/" + current_album_cache + "/" + cachePath(previousPhoto.name));
		$("#original-link").attr("target", "_blank").attr("href", "albums/" + current_album.path + "/" + current_photo.name);

		var text = "<table>";
		if (current_photo.make != undefined) text += "<tr><td>Camera Maker</td><td>" + current_photo.make + "</td></tr>";
		if (current_photo.model != undefined) text += "<tr><td>Camera Model</td><td>" + current_photo.model + "</td></tr>";
		if (current_photo.date != undefined) text += "<tr><td>Time Taken</td><td>" + current_photo.date + "</td></tr>";
		if (current_photo.size != undefined) text += "<tr><td>Resolution</td><td>" + current_photo.size[0] + " x " + current_photo.size[1] + "</td></tr>";
		if (current_photo.aperture != undefined) text += "<tr><td>Aperture</td><td> f/" + getDecimal(current_photo.aperture) + "</td></tr>";
		if (current_photo.focalLength != undefined) text += "<tr><td>Focal Length</td><td>" + getDecimal(current_photo.focalLength) + " mm</td></tr>";
		if (current_photo.iso != undefined) text += "<tr><td>ISO</td><td>" + current_photo.iso + "</td></tr>";
		if (current_photo.exposureTime != undefined) text += "<tr><td>Exposure Time</td><td>" + getDecimal(current_photo.exposureTime) + " sec</td></tr>";
		if (current_photo.exposureProgram != undefined) text += "<tr><td>Exposure Program</td><td>" + current_photo.exposureProgram + "</td></tr>";
		if (current_photo.exposureCompensation != undefined) text += "<tr><td>Exposure Compensation</td><td>" + getDecimal(current_photo.exposureCompensation) + "</td></tr>";
		if (current_photo.spectralSensitivity != undefined) text += "<tr><td>Spectral Sensitivity</td><td>" + current_photo.spectralSensitivity + "</td></tr>";
		if (current_photo.flash != undefined) text += "<tr><td>Flash</td><td>" + current_photo.flash + "</td></tr>";
		if (current_photo.orientation != undefined) text += "<tr><td>Orientation</td><td>" + current_photo.orientation + "</td></tr>";
		text += "</table>";
		$("#metadata").html(text);
		
		$("#album-view").addClass("photo-view-container");
		$("#subalbums").hide();
		$("#photo-view").show();
	}
	function scrollToThumb() {
		var photo = current_photo_cache
		if (photo == null) {
			photo = previous_photo_cache;
			if (photo == null)
				return;
		}
		var thumb = $("#thumb-" + escapeId(properId(photo)));
		if (!thumb.length)
			return;
		if (current_photo_cache != null) {
			var scroller = $("#album-view");
			scroller.stop().animate({ scrollLeft: thumb.position().left + scroller.scrollLeft() - scroller.width() / 2 + thumb.width() / 2 }, "slow");
		} else
			$("html, body").stop().animate({ scrollTop: thumb.offset().top - $(window).height() / 2 + thumb.height() }, "slow");
		
		if (current_photo_cache != null) {
			$("#thumbs img").removeClass("current-thumb");
			thumb.addClass("current-thumb");
		}
	}
	function currentPhoto() {
		for (current_photo_index = 0; current_photo_index < current_album.photos.length; ++current_photo_index) {
			if (cachePath(current_album.photos[current_photo_index].name) == current_photo_cache)
				break;
		}
		if (current_photo_index >= current_album.photos.length) {
			current_photo = null;
			current_photo_index = -1;
			return;
		}
		current_photo = current_album.photos[current_photo_index];
	}
	
	function albumForThumbIteration(album, callback) {
		album_cache[cachePath(album.path)] = album;
		var index = Math.floor(Math.random() * (album.photos.length + album.albums.length));
		if (index >= album.photos.length) {
			index -= album.photos.length;
			fetchAlbumForThumb(cachePath(album.path + "/" + album.albums[index].path), function(fetchedAlbum) {
				albumForThumbIteration(fetchedAlbum, callback);
			});
		} else
			callback(album.photos[index], album);
	}
	function fetchAlbumForThumb(album, callback) {
		if (album in album_cache) {
			callback(album_cache[album]);
			return;
		}
		$.ajax({
			type: "GET",
			dataType: "json",
			url: "cache/" + album + ".json",
			success: callback
		});
	}
	function albumThumbFinder(album, callback) {
		fetchAlbumForThumb(album, function(fetchedAlbum) { albumForThumbIteration(fetchedAlbum, callback); });
	}

	function die() {
		$("#error-overlay").fadeTo(500, 0.8);
		$("#error-text").fadeIn(2500);
		$("body, html").css("overflow", "hidden");
	}
	function undie() {
		$("#error-text, #error-overlay").fadeOut(500);
		$("body, html").css("overflow", "auto");
	}
	window.hashUrl = function() {
		var new_album_cache = location.hash.substring(1);
		if (new_album_cache.length) {
			if (new_album_cache[0] == "!")
				new_album_cache = new_album_cache.substring(1);
			if (new_album_cache[0] == "/")
				new_album_cache = new_album_cache.substring(1);
			if (new_album_cache[new_album_cache.length - 1] == "/")
				new_album_cache = new_album_cache.substring(0, new_album_cache.length - 1);
		}
		return new_album_cache;
	}
	
	var current_album_cache = null;
	var current_photo_cache = null;
	var previous_photo_cache = null;
	var current_album = null;
	var current_photo = null;
	var current_photo_index = -1;
	var original_title = document.title;
	var album_cache = new Array();
	$(window).hashchange(function() {
		$("link[rel=image_src]").remove();
		var new_album_cache = window.hashUrl();
		var index = new_album_cache.lastIndexOf("/");
		if (index != -1 && index != new_album_cache.length - 1) {
			previous_photo_cache = current_photo_cache;
			current_photo_cache = new_album_cache.substring(index + 1);
			new_album_cache = new_album_cache.substring(0, index);
		} else {
			previous_photo_cache = current_photo_cache;
			current_photo_cache = null;
		}
		if (!new_album_cache.length)
			new_album_cache = cachePath("root");
		if (new_album_cache != current_album_cache) {
			current_album_cache = new_album_cache;
			previous_photo_cache = null;
			loadAlbum();
		} else if (current_photo_cache != null) {
			showPhoto();
			showAlbum(false);
			setTitle();
		} else {
			showAlbum(false);
			setTitle();
		}
	});
	$(window).hashchange();
	$(document).keydown(function(e){
		if (current_photo_cache == null)
			return true;
		if (e.keyCode == 39) {
			window.location.href = $("#next").attr("href");
			return false;
		} else if (e.keyCode == 37) {
			window.location.href = $("#back").attr("href");
			return false;
		}
		return true;
	});
	$("#photo-box").mouseenter(function() {
		$("#photo-links").stop().fadeTo("slow", 0.50).css("display", "inline");
	});
	$("#photo-box").mouseleave(function() {
		$("#photo-links").stop().fadeOut("slow");
	});
	$("#next, #back").mouseenter(function() {
		$(this).fadeTo("slow", 1);
	});
	$("#next, #back").mouseleave(function() {
		$(this).fadeTo("slow", 0.35);
	});
	$("#metadata-link").click(function() {
		if (!$("#metadata").is(":visible"))
			$("#metadata").stop()
				.css("height", 0)
				.css("padding-top", 0)
				.css("padding-bottom", 0)
				.show()
				.animate({ height: $("#metadata > table").height(), paddingTop: 3, paddingBottom: 3 }, "slow", function() {
					$(this).css("height", "auto");
					$("#metadata-link").text($("#metadata-link").text().replace("show", "hide"));
				});
		else
			$("#metadata").stop().animate({ height: 0, paddingTop: 0, paddingBottom: 0 }, "slow", function() {
				$(this).hide();
				$("#metadata-link").text($("#metadata-link").text().replace("hide", "show"));
			});
	});
});