2011-05-17 06:16:52 +02:00
|
|
|
(function() {
|
2011-05-18 05:28:11 +02:00
|
|
|
/* constructor */
|
|
|
|
function PhotoFloat() {
|
|
|
|
this.albumCache = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public member functions */
|
|
|
|
PhotoFloat.prototype.album = function(subalbum, callback, error) {
|
|
|
|
var cacheKey, ajaxOptions, self;
|
|
|
|
if (typeof subalbum.photos !== "undefined" && subalbum.photos !== null) {
|
|
|
|
callback(subalbum);
|
|
|
|
return;
|
2011-05-17 06:16:52 +02:00
|
|
|
}
|
2011-05-18 05:28:11 +02:00
|
|
|
if (Object.prototype.toString.call(subalbum).slice(8, -1) === "String")
|
|
|
|
cacheKey = subalbum;
|
|
|
|
else
|
|
|
|
cacheKey = PhotoFloat.cachePath(subalbum.parent.path + "/" + subalbum.path);
|
|
|
|
if (this.albumCache.hasOwnProperty(cacheKey)) {
|
|
|
|
callback(this.albumCache[cacheKey]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self = this;
|
|
|
|
ajaxOptions = {
|
|
|
|
type: "GET",
|
|
|
|
dataType: "json",
|
|
|
|
url: "cache/" + cacheKey + ".json",
|
|
|
|
success: function(album) {
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < album.albums.length; ++i)
|
|
|
|
album.albums[i].parent = album;
|
|
|
|
for (i = 0; i < album.photos.length; ++i)
|
|
|
|
album.photos[i].parent = album;
|
|
|
|
self.albumCache[cacheKey] = album;
|
|
|
|
callback(album);
|
2011-05-17 06:16:52 +02:00
|
|
|
}
|
|
|
|
};
|
2012-08-11 01:47:11 +02:00
|
|
|
if (typeof error !== "undefined" && error !== null) {
|
|
|
|
ajaxOptions.error = function(jqXHR, textStatus, errorThrown) {
|
|
|
|
error(jqXHR.status);
|
|
|
|
};
|
|
|
|
}
|
2011-05-18 05:28:11 +02:00
|
|
|
$.ajax(ajaxOptions);
|
|
|
|
};
|
|
|
|
PhotoFloat.prototype.albumPhoto = function(subalbum, callback, error) {
|
|
|
|
var nextAlbum, self;
|
|
|
|
self = this;
|
|
|
|
nextAlbum = function(album) {
|
|
|
|
var index = Math.floor(Math.random() * (album.photos.length + album.albums.length));
|
|
|
|
if (index >= album.photos.length) {
|
|
|
|
index -= album.photos.length;
|
|
|
|
self.album(album.albums[index], nextAlbum, error);
|
|
|
|
} else
|
|
|
|
callback(album, album.photos[index]);
|
2011-05-17 06:16:52 +02:00
|
|
|
};
|
2011-05-18 05:28:11 +02:00
|
|
|
if (typeof subalbum.photos !== "undefined" && subalbum.photos !== null)
|
|
|
|
nextAlbum(subalbum);
|
|
|
|
else
|
|
|
|
this.album(subalbum, nextAlbum, error);
|
|
|
|
};
|
|
|
|
PhotoFloat.prototype.parseHash = function(hash, callback, error) {
|
|
|
|
var index, album, photo;
|
|
|
|
hash = PhotoFloat.cleanHash(hash);
|
|
|
|
index = hash.lastIndexOf("/");
|
|
|
|
if (!hash.length) {
|
|
|
|
album = PhotoFloat.cachePath("root");
|
|
|
|
photo = null;
|
|
|
|
} else if (index !== -1 && index !== hash.length - 1) {
|
|
|
|
photo = hash.substring(index + 1);
|
|
|
|
album = hash.substring(0, index);
|
|
|
|
} else {
|
|
|
|
album = hash;
|
|
|
|
photo = null;
|
|
|
|
}
|
|
|
|
this.album(album, function(theAlbum) {
|
|
|
|
var i = -1;
|
|
|
|
if (photo !== null) {
|
|
|
|
for (i = 0; i < theAlbum.photos.length; ++i) {
|
|
|
|
if (PhotoFloat.cachePath(theAlbum.photos[i].name) === photo) {
|
|
|
|
photo = theAlbum.photos[i];
|
|
|
|
break;
|
2011-05-17 06:16:52 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-18 05:28:11 +02:00
|
|
|
if (i >= theAlbum.photos.length) {
|
|
|
|
photo = null;
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(theAlbum, photo, i);
|
|
|
|
}, error);
|
|
|
|
};
|
2012-08-11 01:47:11 +02:00
|
|
|
PhotoFloat.prototype.authenticate = function(password, result) {
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
dataType: "text",
|
2013-04-29 11:05:09 +02:00
|
|
|
url: "auth?username=photos&password=" + password,
|
2012-08-11 01:47:11 +02:00
|
|
|
success: function() {
|
|
|
|
result(true);
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
result(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2011-05-18 05:28:11 +02:00
|
|
|
|
|
|
|
/* static functions */
|
|
|
|
PhotoFloat.cachePath = function(path) {
|
|
|
|
if (path === "")
|
|
|
|
return "root";
|
2012-08-22 17:03:37 +02:00
|
|
|
if (path.charAt(0) === "/")
|
2011-05-18 05:28:11 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
PhotoFloat.photoHash = function(album, photo) {
|
|
|
|
return PhotoFloat.albumHash(album) + "/" + PhotoFloat.cachePath(photo.name);
|
|
|
|
};
|
|
|
|
PhotoFloat.albumHash = function(album) {
|
|
|
|
if (typeof album.photos !== "undefined" && album.photos !== null)
|
|
|
|
return PhotoFloat.cachePath(album.path);
|
|
|
|
return PhotoFloat.cachePath(album.parent.path + "/" + album.path);
|
|
|
|
};
|
|
|
|
PhotoFloat.photoPath = function(album, photo, size, square) {
|
2015-01-27 14:25:06 +01:00
|
|
|
var suffix, hash;
|
2011-05-18 05:28:11 +02:00
|
|
|
if (square)
|
|
|
|
suffix = size.toString() + "s";
|
|
|
|
else
|
|
|
|
suffix = size.toString();
|
2015-01-27 14:25:06 +01:00
|
|
|
hash = PhotoFloat.cachePath(PhotoFloat.photoHash(album, photo) + "_" + suffix + ".jpg");
|
|
|
|
if (hash.indexOf("root-") === 0)
|
|
|
|
hash = hash.substring(5);
|
|
|
|
return "cache/" + hash;
|
2011-05-18 05:28:11 +02:00
|
|
|
};
|
2013-12-21 04:40:25 +01:00
|
|
|
PhotoFloat.videoPath = function(album, video) {
|
2015-06-17 17:30:35 +02:00
|
|
|
return "cache/" + PhotoFloat.cachePath(PhotoFloat.photoHash(album, video) + ".mp4");
|
2013-12-21 04:40:25 +01:00
|
|
|
};
|
2011-05-18 05:28:11 +02:00
|
|
|
PhotoFloat.originalPhotoPath = function(album, photo) {
|
|
|
|
return "albums/" + album.path + "/" + photo.name;
|
|
|
|
};
|
|
|
|
PhotoFloat.trimExtension = function(name) {
|
|
|
|
var index = name.lastIndexOf(".");
|
|
|
|
if (index !== -1)
|
|
|
|
return name.substring(0, index);
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
PhotoFloat.cleanHash = function(hash) {
|
|
|
|
while (hash.length) {
|
2012-08-22 17:03:37 +02:00
|
|
|
if (hash.charAt(0) === "#")
|
2011-05-18 05:28:11 +02:00
|
|
|
hash = hash.substring(1);
|
2012-08-22 17:03:37 +02:00
|
|
|
else if (hash.charAt(0) === "!")
|
2011-05-18 05:28:11 +02:00
|
|
|
hash = hash.substring(1);
|
2012-08-22 17:03:37 +02:00
|
|
|
else if (hash.charAt(0) === "/")
|
2011-05-18 05:28:11 +02:00
|
|
|
hash = hash.substring(1);
|
2012-04-16 19:02:16 +02:00
|
|
|
else if (hash.substring(0, 3) === "%21")
|
|
|
|
hash = hash.substring(3);
|
2012-08-22 17:03:37 +02:00
|
|
|
else if (hash.charAt(hash.length - 1) === "/")
|
2011-05-18 05:28:11 +02:00
|
|
|
hash = hash.substring(0, hash.length - 1);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* make static methods callable as member functions */
|
|
|
|
PhotoFloat.prototype.cachePath = PhotoFloat.cachePath;
|
|
|
|
PhotoFloat.prototype.photoHash = PhotoFloat.photoHash;
|
|
|
|
PhotoFloat.prototype.albumHash = PhotoFloat.albumHash;
|
|
|
|
PhotoFloat.prototype.photoPath = PhotoFloat.photoPath;
|
2013-12-21 04:40:25 +01:00
|
|
|
PhotoFloat.prototype.videoPath = PhotoFloat.videoPath;
|
2011-05-18 05:28:11 +02:00
|
|
|
PhotoFloat.prototype.originalPhotoPath = PhotoFloat.originalPhotoPath;
|
|
|
|
PhotoFloat.prototype.trimExtension = PhotoFloat.trimExtension;
|
|
|
|
PhotoFloat.prototype.cleanHash = PhotoFloat.cleanHash;
|
|
|
|
|
|
|
|
/* expose class globally */
|
2011-05-17 06:16:52 +02:00
|
|
|
window.PhotoFloat = PhotoFloat;
|
2011-05-18 05:28:11 +02:00
|
|
|
}());
|