views:

172

answers:

1

Hi guys, I would like to know if it is possible to re-size and re-position a greybox or shadowbox window to display in a certain div element?

I am creating a template for a wordpress site, and I am using the default calendar, but when you click on a date inside of the calendar, the default action is to open it up in a new window.

Would it be possible to mod greybox or shadowbox in such a way that when you click on a date, the box opens up over the calendar displaying the contents in it, instead of a new page?

Or is there another way of achieving the desired result, maybe with jQuery or Javascript?

Any help would be greatly appreciated, thanx in advance!

A: 

jQuery UI has a widget called Dialog that can achieve your result. You could use it like this:

$(document).ready( function() {

    // This will initialize the dialog box and hide it by default.
    // You can find a list of most options here: http://docs.jquery.com/UI/Dialog
    $("#dialog").dialog({
        autoOpen: false,
        modal : true,
    });

    // This will select the links like you specified
    $('#wp-calendar a').click( function(e) {
        e.preventDefault() // Disable the link from going to a new page
        var url = $(this).attr("href"); // Pull the HRef for the link for making the AJAX request

        // This will make an AJAX request and put the result in the dialog.
        $("#dialog").empty().load(url, function() {

            // This is the callback for when the AJAX request finishes
            // All it does it open the dialog, which now has data in it
            $("#dialog").dialog("open");
        });
    });
});

Note: You'd need to install jQuery UI for this, and you'll probably want to read up on Dialog here. You'll need to take more steps if you'd want this to degrade gracefully, and depending on your markup. If you could post a snippet of a "day" on the calendar, I can try to tailor it to your needs.


Edit: Lets say that the file that return the rendered page for the event is "/ajax/event.php?id=123", you'd first want to prevent the link's default behavior (of navigating to a new page); then you'd want to use jQuery's load function to load an assembled URL's data into the dialog; once that request completes, you'd want to open the dialog. I've edited to code to do so, and it will degrade gracefully for user that don't have JavaScript enabled.

Zack
Awesome, I'll give it a go, thanx! I'll report back asap.
You'll need to take some more steps to make it work with your code, specifically. Just post a snippet of the "day" in the calendar that triggers this and I'll see if I can help with that. I've added some comments to the script, also.
Zack
Cool, will do, thanx!
I have managed to assign a class to all the link tags in the wordpress calendar, using this jquery code, $(function() { $('#wp-calendar').find('a').each( function() { $(this).addClass( 'testing' ); });});Is there a way to apply the jquery dialog, to open all of the links that have that class in a dialog box?
I've updated the answer, if you can find all the `<a>`'s like that, then you can just apply the click event handler to them immediately. Also `$("#wp-calendar a") should select them all just fine. If you want the Dialog to be resizable or specify the size manually, check out the options that can be passed the a jQuery dialog, I've added a link to the page with all of them in the comments.
Zack
Awesome, thanx a million! I'll keep you posted on my progress.
It works ok in FF but it freaks out in IE, it seems to be in a endless loop.I can see the scroll-bar getting smaller, but nothing displays.
Ok, now it's freaking out in FF as well, when I open a window and close it the first time, it's ok, but when I open another one, it opens the previous one below that one as well.The calendar is in jquery tabs at the moment, I am assuming that could be a problem?
Ah, it looks like it's appending the data to the dialog, I've updated the answer to empty the dialog before loading the data into it.
Zack
Still seems a bit buggy, when I open it up once, it works ok, but as soon as I open it up again, it loads twice, and if I open it up a third time, 3 or more windows pop up over each other.Thanx so much for the help, much appreciated.