That's a neat library, but he didn't expose much of an API for doing what you want; it's all about the UI instead. I was going to suggest that we make our own array for this and generate the HTML from that array, but the library already makes an array from the HTML. So I added a method (below) to it.
But first I want to note a few things about the library's code. EDIT: Yeah, I am being rather pedantic but hopefully someone will learn if I detail his mistakes.
Least importantly, he doesn't use the standard event registration model. On that page, ppk lists both models as temporarily deprecated; this confuses me as scripts can easily achieve cross-browser compatibility in this area while being future-proof as well.
He also makes 5 global variables. The configurations could have been constructor arguments (that is, if Prototype has a certain simple feature in Class.create()). The initFrog function probably shouldn't exist in the library itself, but even so it could have been anonymous. EDIT: Oh, myFrog is also implied global (another poor practice) - so that's 6 of them.
Finally, he uses Array objects as associative. The library would work just as well and be more true to the nature of JavaScript if he had used literal {} objects instead.
Anyway, here's the method, to be inserted into the Frog prototype. It only does anything if the specified image is not the currently displayed one.
Code:
loadURL: function(image, neu){
//Determine what URL we're using.
var url;
if(typeof image == 'object'){
if(image.constructor == Array || image.constructor == ArrayObject)
image = {
full: image[0],
credit: image[1],
thumb: image[2],
caption: image[3],
link: image[4]
};
url = image.full;
}else
url = image;
//Find the URL in the array.
var imageNum;
for(var i = 0; i < imageArray.length; i++)
if(imageArray[i].full == url){
imageNum = i;
break;
}else{
var absolute;
var loc = window.location.toString();
if(imageArray[i].full.charAt(0) == '/'){
var site = loc.substring(0, loc.indexOf(window.location.pathname));
loc = site + imageArray[i].full;
}else{
var dir = loc.substring(0, loc.lastIndexOf('/') + 1);
loc = dir + imageArray[i].full;
}
if(loc == url){
imageNum = i;
break;
}
}
//Prepare the necessary variables.
if(typeof imageNum != 'undefined'){
if(typeof image != 'object')
image = imageArray[imageNum];
}else{
if(typeof image == 'object' && neu){
imageNum = imageArray.length;
imageArray.push(image);
}else
//A single URL won't work with FrogJS.
return false;
}
//What side are we coming from?
var side;
var current = $('FrogJSImage').src;
for(var i = 0; i < imageArray.length; i++){
if(imageArray[i].full == current){
side = imageNum - i;
break;
}else{
var absolute;
var loc = window.location.toString();
if(imageArray[i].full.charAt(0) == '/'){
var site = loc.substring(0, loc.indexOf(window.location.pathname));
loc = site + imageArray[i].full;
}else{
var dir = loc.substring(0, loc.lastIndexOf('/') + 1);
loc = dir + imageArray[i].full;
}
if(loc == current){
side = imageNum - i;
break;
}
}
}
if(side == 0) //We're already there.
return true;
if(side < 0)
side = 'left';
else if(side > 0)
side = 'right';
//How wide is the image?
var imgPreloader = new Image();
imgPreloader.onload = function(){
myFrog.loadImage(imageNum, side, imgPreloader.width);
};
imgPreloader.src = url;
return true;
},
Example usage, built around the example page distributed on Eric's site:
Code:
<a id='someId' href='images/3.jpg'>No, don't click here!</a>
// It's time for this frog to hop
function initFrog(){ myFrog = new Frog(); myFunc(myFrog); }
Event.observe(window, 'load', initFrog, false);
function myFunc(myFrog){
$('someId').onclick = function (){
myFrog.loadURL(this.href);
return false;
};
}
Bookmarks