aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/010-control.js
blob: 37ca797d83c0797c5343b7c476111b4ac5a36792 (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
$(document).ready(function() {
	function cachePath(path) {
		if (path == "")
			return "root";
		if (path[0] == '/')
			path = path.substring(1);
		return path.replace(/ /g, "_").replace(/\//g, "-");
	}
	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 loadAlbum() {
		if (current_album_cache in album_cache) {
			albumLoaded(album_cache[current_album_cache]);
			return;
		}
		$("#loading").show();
		$.ajax({
			type: "GET",
			url: "cache/" + current_album_cache + ".json",
			error: function() { $(document.body).html("Couldn't fetch it."); },
			success: albumLoaded
		});
	}
	function albumLoaded(album) {
		$("#loading").hide();
		album_cache[cachePath(album.path)] = album;
		current_album = album;
		if (cachePath(album.path) == current_album_cache)
			showAlbum();
		if (current_photo_cache != null)
			showPhoto();
		setTitle();
	}
	function setTitle() {
		var title = "";
		var components;
		if (current_album.path.length == 0)
			components = [document.title];
		else {
			components = current_album.path.split("/");
			components.unshift(document.title);
		}
		var last = "";
		for (var i = 0; i < components.length; ++i) {
			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];
			if (i < components.length - 1 || current_photo_cache != null) {
				title += "</a>";
				title += " &raquo; ";
			}
		}
		if (current_photo_cache != null)
			title += current_photo.name;
		$("#title").html(title);
	}
	function showAlbum() {
		$("html, body").animate({ scrollTop: 0 }, "slow");
		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 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);
		if (current_album.albums.length)
			$("#subalbums-title").show();
		else
			$("#subalbums-title").hide();
		var subalbums = "";
		for (var i = 0; i < current_album.albums.length; ++i)
			subalbums += "<a href=\"#" + cachePath(current_album.path + "/" + current_album.albums[i].path) + "\"><div class=\"album-button\">" + current_album.albums[i].path + "</div></a>";
		$("#subalbums").html(subalbums);
		
		$("#album-view").removeClass("photo-view-container");
		$("#subalbums").show();
		$("#photo-view").hide();
	}
	function showPhoto() {
		currentPhoto();
		if (current_photo == null) {
			$(document.body).html("Wrong picture.");
			return;
		}
		var maxSize = 640;
		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;
		}
		$("#photo")
			.attr("width", width).attr("height", height)
			.attr("src", imagePath(current_photo.name, current_album.path, maxSize, false))
			.attr("alt", current_photo.name)
			.attr("title", current_photo.name);
		var nextLink = "#" + current_album_cache + "/" + cachePath(current_album.photos[
			(current_photo_index + 1 >= current_album.photos.length) ? 0 : (current_photo_index + 1)
		].name);
		$("#next-photo").attr("href", nextLink);
		$("#next").attr("href", nextLink);
		$("#back").attr("href", "#" + current_album_cache + "/" + cachePath(current_album.photos[
			(current_photo_index - 1 < 0) ? (current_album.photos.length - 1) : (current_photo_index - 1)
		].name));
		
		$("#album-view").addClass("photo-view-container");
		$("#subalbums").hide();
		$("#photo-view").show();
		var thumb = $("#thumb-" + current_photo_cache.replace(/\./g, "\\."));
		var scroller = $("#album-view");
		scroller.stop();
		scroller.animate({ scrollLeft: thumb.position().left + scroller.scrollLeft() - scroller.width() / 2 + thumb.width() / 2 }, "slow");
	}
	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];
	}
	var current_album_cache = null;
	var current_photo_cache = null;
	var current_album = null;
	var current_photo = null;
	var current_photo_index = -1;
	var album_cache = new Array();
	$(window).hashchange(function() {
		var new_album_cache = location.hash.substring(1);
		var index = new_album_cache.lastIndexOf("/");
		if (index != -1 && index != new_album_cache.length - 1) {
			current_photo_cache = new_album_cache.substring(index + 1);
			new_album_cache = new_album_cache.substring(0, index);
		} else
			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;
			loadAlbum();
		} else if (current_photo_cache != null) {
			showAlbum();
			showPhoto();
			setTitle();
		} else {
			showAlbum();
			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;
	});
});