Get rid of CoffeeScript-style weirdness and make it plain and simple.

This commit is contained in:
Jason A. Donenfeld 2011-05-17 23:28:11 -04:00
parent fc2f923de1
commit 264738009b

View File

@ -1,161 +1,167 @@
(function() { (function() {
var PhotoFloat = (function() { /* constructor */
PhotoFloat.cachePath = function(path) { function PhotoFloat() {
if (path === "") this.albumCache = [];
return "root"; }
if (path[0] === "/")
path = path.substring(1); /* public member functions */
path = path PhotoFloat.prototype.album = function(subalbum, callback, error) {
.replace(/ /g, "_") var cacheKey, ajaxOptions, self;
.replace(/\//g, "-") if (typeof subalbum.photos !== "undefined" && subalbum.photos !== null) {
.replace(/\(/g, "") callback(subalbum);
.replace(/\)/g, "") return;
.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) {
var suffix;
if (square)
suffix = size.toString() + "s";
else
suffix = size.toString();
return "cache/" + PhotoFloat.cachePath(PhotoFloat.photoHash(album, photo) + "_" + suffix + ".jpg");
};
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) {
if (hash[0] === "#")
hash = hash.substring(1);
else if (hash[0] === "!")
hash = hash.substring(1);
else if (hash[0] === "/")
hash = hash.substring(1);
else if (hash[hash.length - 1] === "/")
hash = hash.substring(0, hash.length - 1);
else
break;
}
return hash;
};
function PhotoFloat() {
this.albumCache = [];
} }
PhotoFloat.prototype.cachePath = PhotoFloat.cachePath; if (Object.prototype.toString.call(subalbum).slice(8, -1) === "String")
PhotoFloat.prototype.photoHash = PhotoFloat.photoHash; cacheKey = subalbum;
PhotoFloat.prototype.albumHash = PhotoFloat.albumHash; else
PhotoFloat.prototype.photoPath = PhotoFloat.photoPath; cacheKey = PhotoFloat.cachePath(subalbum.parent.path + "/" + subalbum.path);
PhotoFloat.prototype.originalPhotoPath = PhotoFloat.originalPhotoPath; if (this.albumCache.hasOwnProperty(cacheKey)) {
PhotoFloat.prototype.trimExtension = PhotoFloat.trimExtension; callback(this.albumCache[cacheKey]);
PhotoFloat.prototype.cleanHash = PhotoFloat.cleanHash; return;
PhotoFloat.prototype.album = function(subalbum, callback, error) { }
var cacheKey, ajaxOptions, self; self = this;
if (typeof subalbum.photos !== "undefined" && subalbum.photos !== null) { ajaxOptions = {
callback(subalbum); type: "GET",
return; 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);
} }
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);
}
};
if (typeof error !== "undefined" && error !== null)
ajaxOptions.error = error;
$.ajax(ajaxOptions);
}; };
PhotoFloat.prototype.albumPhoto = function(subalbum, callback, error) { if (typeof error !== "undefined" && error !== null)
var nextAlbum, self; ajaxOptions.error = error;
self = this; $.ajax(ajaxOptions);
nextAlbum = function(album) { };
var index = Math.floor(Math.random() * (album.photos.length + album.albums.length)); PhotoFloat.prototype.albumPhoto = function(subalbum, callback, error) {
if (index >= album.photos.length) { var nextAlbum, self;
index -= album.photos.length; self = this;
self.album(album.albums[index], nextAlbum, error); nextAlbum = function(album) {
} else var index = Math.floor(Math.random() * (album.photos.length + album.albums.length));
callback(album, album.photos[index]); if (index >= album.photos.length) {
}; index -= album.photos.length;
if (typeof subalbum.photos !== "undefined" && subalbum.photos !== null) self.album(album.albums[index], nextAlbum, error);
nextAlbum(subalbum); } else
else callback(album, album.photos[index]);
this.album(subalbum, nextAlbum, error);
}; };
PhotoFloat.prototype.parseHash = function(hash, callback, error) { if (typeof subalbum.photos !== "undefined" && subalbum.photos !== null)
var index, album, photo; nextAlbum(subalbum);
hash = PhotoFloat.cleanHash(hash); else
index = hash.lastIndexOf("/"); this.album(subalbum, nextAlbum, error);
if (!hash.length) { };
album = PhotoFloat.cachePath("root"); PhotoFloat.prototype.parseHash = function(hash, callback, error) {
photo = null; var index, album, photo;
} else if (index !== -1 && index !== hash.length - 1) { hash = PhotoFloat.cleanHash(hash);
photo = hash.substring(index + 1); index = hash.lastIndexOf("/");
album = hash.substring(0, index); if (!hash.length) {
} else { album = PhotoFloat.cachePath("root");
album = hash; photo = null;
photo = null; } else if (index !== -1 && index !== hash.length - 1) {
} photo = hash.substring(index + 1);
this.album(album, function(theAlbum) { album = hash.substring(0, index);
var i = -1; } else {
if (photo !== null) { album = hash;
for (i = 0; i < theAlbum.photos.length; ++i) { photo = null;
if (PhotoFloat.cachePath(theAlbum.photos[i].name) === photo) { }
photo = theAlbum.photos[i]; this.album(album, function(theAlbum) {
break; var i = -1;
} if (photo !== null) {
} for (i = 0; i < theAlbum.photos.length; ++i) {
if (i >= theAlbum.photos.length) { if (PhotoFloat.cachePath(theAlbum.photos[i].name) === photo) {
photo = null; photo = theAlbum.photos[i];
i = -1; break;
} }
} }
callback(theAlbum, photo, i); if (i >= theAlbum.photos.length) {
}, error); photo = null;
}; i = -1;
return PhotoFloat; }
})(); }
callback(theAlbum, photo, i);
}, error);
};
/* static functions */
PhotoFloat.cachePath = function(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;
};
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) {
var suffix;
if (square)
suffix = size.toString() + "s";
else
suffix = size.toString();
return "cache/" + PhotoFloat.cachePath(PhotoFloat.photoHash(album, photo) + "_" + suffix + ".jpg");
};
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) {
if (hash[0] === "#")
hash = hash.substring(1);
else if (hash[0] === "!")
hash = hash.substring(1);
else if (hash[0] === "/")
hash = hash.substring(1);
else if (hash[hash.length - 1] === "/")
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;
PhotoFloat.prototype.originalPhotoPath = PhotoFloat.originalPhotoPath;
PhotoFloat.prototype.trimExtension = PhotoFloat.trimExtension;
PhotoFloat.prototype.cleanHash = PhotoFloat.cleanHash;
/* expose class globally */
window.PhotoFloat = PhotoFloat; window.PhotoFloat = PhotoFloat;
}).call(this); }());