Hi Manuel!
Found a bug in galleryformatter.js. Have a look at the following lines of code.
var $locationHash = window.location.hash; // if we are being deeplinked to a specific slide, capture that
// if we have a hash in the url
if ($locationHash) {
$slides.filter($locationHash).show(); // show that slide
$thumbsLi.not($(".cloned")).find("a[href="+ $locationHash +"]").parent().addClass('active'); // activate that thumbnail
}
// otherwise the default
else {
$slides.filter(':first').show(); // show the first one
$thumbsLi.filter('.slide-0:not("cloned")').addClass('active'); // activate the first thumbnail
}
Now we have a node with the image gallery and comments enabled, imagine a link like /node/1234#comment-1. Maybe you already know what happens. The browser will jump directly to the comment but if the user now scrolls to the image gallery no image is selected. Because we never check if the anchor from the URI is really a image slide.
My solution:
var $locationHash = window.location.hash; // if we are being deeplinked to a specific slide, capture that
// if we have a hash in the url
if ($locationHash && $thumbsLi.not($(".cloned")).find("a[href="+ $locationHash +"]").length !== 0) {
$slides.filter($locationHash).show(); // show that slide
$thumbsLi.not($(".cloned")).find("a[href="+ $locationHash +"]").parent().addClass('active'); // activate that thumbnail
}
// otherwise the default
else {
$slides.filter(':first').show(); // show the first one
$thumbsLi.filter('.slide-0:not("cloned")').addClass('active'); // activate the first thumbnail
}
By the way, at the end of the file a semicolon is missing and the variable $slideContainer is never used within the script.
Comments
Comment #1
manuel garcia commentedGood catch Fleshgrinder!
Your solution is the way to go, only we should probably do that selection once and cache it, since we use it again later. I'll get to it when i get a minute.
I'll also take a look at what you mention in the end - Thanks!
Comment #2
fleshgrinder commentedMake use of JSLint to find such little “errors” like the two things I mentioned at last.
Comment #3
manuel garcia commentedYep, that's a nice tool - I've made a commit that fixes these little "errors".
More on the real bug tomorrow.
Comment #4
manuel garcia commentedOK, here is my take on the problem.
I check wether we have a slide to show based on that hash first, if so we show it, otherwise we show the first slide.
In order to avoid duplicate code, I created a helper function called showFirstSlide();
Please test attached patch and let me know how it goes!
Comment #5
fleshgrinder commented.size()is slower then.find(), see this comment on jQuery API.If there are more elements found then exactly one, it's the problem of the browser to which element it will set the focus, not our's.
EDIT: My full script I'm using at the moment (with your patch incorporated):
It's working perfectly, you have the link to my current project and you can see it there in action.
Comment #6
manuel garcia commentedOK, well, I think it actualy makes more sense to just use the length property directly, since thats all that size is returning anyway. It's a minimal difference but oh well.
I've updated the patch with a little more commenting. BTW, I dont think it makes sense to call find() there, the $slideToShow already has length in it that tells u how many elements it contains.
I've also changed the if statement for > 0.
Comment #7
manuel garcia commentedcommitted - thanks!
Comment #8
fleshgrinder commented.lengthis perfect, no function call at all! I think the second patch is great. :-)