Need some help getting started
wellman
Registered Users Posts: 961 Major grins
I'm trying to write a little JS code that will ultimately place gallery-level Previous and Next buttons on my pages. In other words, if I'm viewing gallery Y within a particular category, I want links to galleries X and Z on the page, thus allowing users to reach those galleries w/o first having to return to the category page.
I've looked through the API help, and it seems like I could use the smugmug.albums.get method toward this goal. I just can't figure out how to call the method through JavaScript. I've looked through BigWebGuy's slideshow code and devbobo's bulk thumbnailer code, but neither seem to use the API, at least not to my novice eyes.
So, to wrap up, how do I make an API call through JS? Thanks!
I've looked through the API help, and it seems like I could use the smugmug.albums.get method toward this goal. I just can't figure out how to call the method through JavaScript. I've looked through BigWebGuy's slideshow code and devbobo's bulk thumbnailer code, but neither seem to use the API, at least not to my novice eyes.
So, to wrap up, how do I make an API call through JS? Thanks!
0
Comments
Unfortunately, the api is a lot of work, and I am not sure if it's the right way to go.
You could achieve the same thing using an array of album numbers, obviously this approach isn't automatic and would require you to add a new gallery number to the array each time one was created.
Otherwise, you could try the API, but you will first need to apply for an API key.
Either way you decide to go, I can help you out with some code.
Cheers,
David
SmugMug API Developer
My Photos
the api is a big investment to get yourself into...
the way that dev and i have been getting information back into our hacks is just by calling regular smugmug pages via XMLHttpRequest and then parsing out all the bits we need.
I looked for a page that would have a list of galleries, but unfortunately the only thing i found requires that you're logged in. sorry
I have this in my Javascript section:
And I have this in my footer:
I have a test gallery here, where I have a blank div ID'd gwGalleryNav. When I bring up the test gallery, I can see that something is going on by watching status messages fly by in the status bar, but I don't get the contents of the called page shown to me.
What am I borking? Thanks!
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
anyways, try a firefox extension called FireBug. it lets you see the XMLHttpResponse calls and responses. Saves you from having to try and put it in an element on your page. I can see your response just fine.
Sweet! Thanks for the extension. I don't have FF at work, so it will be a while before I'm able to do much. Hopefully more to come...
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
BWG, in your slideshow code, your slideshowHandler function looks like it's doing some parsing of a response and chunking the data into arrays. This is pretty much what I'd like to do, only I can't figure out the meaning of the re, re2, and re3 variables. (Maybe some character-escaping madness?) Is there a tutorial somewhere that would help me out with this task?
TIA...
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
there are a ton of regex tutorials out on the web
Thanks. I've heard that term before; looks like I get to do some learning. :
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
Greg,
if your having trouble, post some examples of what you are tryiny to parse and I will write some regexps for you.
Cheers,
David
SmugMug API Developer
My Photos
It looks like my best bet will be to use the albumTitle_XXXXXXX id attribute as a means of collecting gallery numbers. On category pages, the sample photo and the title are in separate link tags, meaning I'd get dupicates if I searched for href=/gallery/XXXXXXX strings. Also, I'm thinking using the albumTitle_XXXXXXX id will eliminate false hits from other links in people's headers, etc.
So, I need a regular expression to search for this. I think this is an easy one. I then need to match regexp to the response and eliminate the extraeneous portion of the matched string to get just the number. Does the code below look OK?
So finally, here's my real question. After I find the first match, how do I march through the rest of the response to find them all and build an array?
Thanks!
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
- Define the regexp such as above (which will match albumTitle_XXXXXXX)
- Use the split() method on the response along with the regexp as my "delimiter". This will give me an array of the extra stuff between the matches.
- Loop through the elements of the array, using each along with a replace() call, operating on the original response string. Use some common delmiter for the replacement, like a comma. This gives me a comma-delimited string of albumTitle_XXXXXXX's.
- Use the split() method again, this time with a comma, to get an array.
- substr() away the albumTitle_ portion of each element.
What would be truly sweet is if there were a "NOT" operator for regexps. That way I could apply the NOT to the albumTitle_XXXXXXX expression, call the split() method on the response, and end up with the final array after only one function call. The "delimiter" for the split() would in effect be "anything other than albumTitle_XXXXXXX." Is there such a thing as an anti-regexp?Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
Greg,
In this scenario, RegExp.$1 will return the album number.
David
SmugMug API Developer
My Photos
Try something like this...
The RegExp.$1 variable contains the album number, so do whatever you want with it.
Hope this is the kinda thing ur after.
Cheers,
David
SmugMug API Developer
My Photos
Ah, so that's what the RegExp.$1 is... Thanks very much. I have a busy few days coming up, so it will likely be a while before I get to work on an implementation. Hopefully I'll get something concrete done over the weekend.
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
I thought that would give me a series of gallery numbers popping up in little windows, just to verify that the response was getting loaded. The regexp-parsing sample code devbobo gave me worked just fine when I subbed in my own "fake" source, but when I try to pass the response variable, no worky-worky. :cry What am I doing wrong? Sorry for the silly question...
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
greg, try taking the slideshow code out and see what happens.
what you're describing is kinda what is happening on safari with the slideshow...the response from the httprequest is blank...maybe there's a common thread in here somewhere.
this code works for me...
footer code....
javascript code... this code doesn't cater for subcategories, but it should be pretty simple to do that.
Cheers,
David
SmugMug API Developer
My Photos
BWG:
When I remove the slideshow code, devbobo's code works. When I put the SS code back in, it borks. I can't see much about the JS since I'm on IE at work, but I can tell you that it gives me this error:
s.length is null or not an object
Hope this helps somewhat.
Devbobo, thanks! I think I'm getting lots of "false positives" for the first few matches due to the customization not being "internalized" by smugmug yet. I have the code commented out on my site for the time being - until I get time to do something with the results. Thanks again!
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
Make sure that the function isClass isn't defined twice, as it was used with the slideshow code...i think.
Cheers,
David
SmugMug API Developer
My Photos
The code basically adds links to the trailing end of the breadcrumb. The links are to the galleries (within the same category) adjacent to the one currently being viewed. If the first or last gallery is being viewed, only one link is shown. Demo here.
Known issues:
1. It's slow to load for categories with large numbers of galleries due to all the background parsing. I'm guessing one solution to this might be to save the array of gallery IDs in a cookie after the first parsing, but I don't know how to do that. :cry
2. Funky category names might bork the code. I'm handling spaces (ie My Buddies) and ampersands (ie Me & My Buddies), but other patterns might break the string transformation.
So here goes...
javascript:
function isBodyClass(sClass)
{
sClassName = document.body.className;
re = new RegExp(sClass + "( |$)")
if (!sClassName) return false;
return re.test(sClassName);
}
function getAlbumsInCategory(sCategory)
{
ajax_query(procAlbumsInCategory, webServer+"/"+sCategory, null, true);
}
function procAlbumsInCategory(response)
{
// Set up variables
arrGalleryID = new Array();
var re = /id="albumTitle_(\d+)/g;
// Find all matches
i=0;
while (re.exec(response) != null)
{
arrGalleryID = RegExp.$1;
i++;
}
N=i;
// Get current gallery ID
var re2 = /gallery_(\d+)/;
re2.exec(document.body.className);
var sCurGalleryID = RegExp.$1
// Find adjacent gallery IDs
for (i=0; i<N; p i++) <> if (arrGalleryID == sCurGalleryID)
break;
// Define texts
var sGnLeadingText = ' (';
var sGnSpacerText = ' | ';
var sGnTrailingText = ')';
var sGnPrevText = '«Older';
var sGnNextText = 'Newer»';
// Construct links
if (i+1<N) p <> sGnPrevText = ''+sGnPrevText+'';
else
sGnPrevText = '';
if (i-1>=0)
sGnNextText = ''+sGnNextText+'';
else
sGnNextText = '';
if (sGnPrevText == '' || sGnNextText == '')
sGnSpacerText = '';
// Alter breadcrumb text
var objGnEdit = document.getElementById("albumTitle");
var sGnTemp = objGnEdit.innerHTML;
sGnTemp = sGnTemp+''+sGnLeadingText+sGnPrevText+sGnSpacerText+sGnNextText+sGnTrailingText+'';
objGnEdit.innerHTML = sGnTemp;
}
Footer (in script tags):
// GalleryNavigator
if (isBodyClass('galleryPage'))
{
re1 = /category_(\S+)/;
re1.exec(document.body.className);
sCategory = new String(RegExp.$1);
sCategory = sCategory.replace(/__/, " & ");
sCategory = sCategory.replace(/_/, ' ');
getAlbumsInCategory(sCategory);
}
CSS:
#galleryNavigator {
font-weight:normal;
color: #000;
}
I'd love to hear improvement ideas. This is pretty much what I'd envisioned for my own needs, but it could use a lot of "generification."
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
I was able to get whatever conflict there was between the SlideShow code and the GalleryNavigator code worked out by placing the loadSlideshow() funcrtion in an "if" in my footer like below:
The isBodyClass() function is adapted from devbobo's isClass() function (actually not a bit different):
Maybe simultaneous ajax calls were being made by your code and mine and the responses got crossed? The slideshow didn't need to load anywhere for me but the homepage, so this fix works fine. Don't know what would happen if you wanted to have a slideshow and these gallery links in the same gallery, though...
Swim for Them | WellmanHouse.net | AlbumFetcher | SmugShowBuilder
it's odd though. the slideshow should exit if the specified slideshowContainerId isn't found. hmm....
looks great by the way, very cool idea.
when I first wrote that code for you, I did it like this....
in FF, it would only return the first RegExp.$1 in procAlbumsInCategory(), while it worked fine in IE. I tried numerous methods, and found that passing the variable into getAlbumsInCategory() was the only way it would work properly.
This may or may not be related, but I thought it would be worth mentioning.
The hack looks great Greg, you have done an awesome job, my only comment....i don't like the way it makes the breadcrumb wrap onto another line, personally, I would like to see it under the breadcrumb.
Cheers,
David
SmugMug API Developer
My Photos