GreggHz

Mootools Video Gallery Tutorial

Recently I did some work for the folks over at The Good Line. As part of the project, I built a fairly simple video gallery for them. The gallery handles vimeo videos and uses the mootools javascript framework. Today I'd like to take a few minutes and outline how the gallery was created.

I should note that is a bit of server side magic going on that makes some of what I'm doing here quite a bit easier, however, in this post I would like to focus on the client side issues since the server side stuff is less interesting and any average programmer would come to similar solutions that I have found.

So to start, let's take a look at the html for the gallery. First the container for the actual player (this is the part that shows up after you click a video thumbnail):

<div id="video_wrap" style="display: none;">
    <div id="video">
        <iframe id="vid_frame"
                src=""
                width="990"
                height="555"
                frameborder="0"
                webkitAllowFullScreen
                allowFullScreen></iframe>
    </div>
    <div id="title"></div>
    <div id="description"></div>
</div>

The `display: none` makes sure that the player is hidden when the page loads. The three children divs (video, title and description) are what will hold the data from each video. When a video is loaded, the iframe's src attribute will be updated to be the appropriate Vimeo url, the title div will be updated with the title of the video and the description div will be updated to contain the description.

Now let's look at the html for each thumbnail.

<div class="thumb alpha grid_1" data-vid-id="28961992"
                                data-title="Fairbourne Consulting - SPEC AD"
                                data-description="">
    <img src="http://thegoodline.com/images/slir/w188-h106-c188:106/images/thumbs/28961992_200.jpg" title="Fairbourne Consulting" alt="Fairbourne Consulting" />
    <div class="vid_title">Fairbourne Consulting</div>
</div>

There are minor things worth mentioning here. First, the main div here uses the data-* attributes that html5 allows for storing arbitrary data. While it's not quite standards compliant if you're not using html, any browser will handle this data correctly. These data attributes hold the video id, title and description of the video. The image here is just a normal thumbnail. In my case, I used SLIR to dynamically generate the thumbnails from base images. This is required, but it makes life much easier (unfortunately, it's also beyond the scope of this article).

Each thumbnail uses the same markup with the data modified to match the video it is a thumbnail for. There are some important css classes I'm using though. You can see three of the classes used above (thumb, alpha, grid_1) and one not used above (omega). Below is the css for these classes:

.grid_1 {
    width: 188px;
    float: left;
    margin-left: 6px;
    margin-right: 6px;
}
.alpha {
    margin-left: 0px;
}
.omega {
    margin-right: 0px;
}
.thumb {
    width: 188px;
    cursor: pointer;
}

`grid_1` is what places each thumbnail into a column. `alpha` should be used when the thumbnail starts a new row. `omega` should be used when a thumbnail ends a row. `thumb` is used to make sure the thumbnails behave like links (the cursor turns into the pointer when hovered).

The remaining css is mostly cosmetic issues and should be much trouble to work out.

Now the interesting part. First of all, mootools must be included for this to work properly. A simple and quick way to do this is to use the Google Libraries API like this:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.1/mootools-yui-compressed.js"></script>

The rest of the javascript will be inserted inside script tags just before the closing </body> tag.

var setVideo = function (src, title, description, callback) {
    $('vid_frame').addEvent('load', function (event) {
        callback(event);
        $('vid_frame').removeEvents('load');
    });

    $('vid_frame').set('src', src);
    $('title').set('html', title);
    $('description').set('html', description);
}

setVideo is a helper function that loads a new video into the player and issues the passed in callback. The parameters are simple enough to figure out I think. It also sets up an event listener so that when the new video finishes loading, an arbitrary callback is called. I'll explain what to do with that shortly.

Here's the function that will run when a thumbnail is clicked:

var onThumbClick = function (event) {
    var src = 'http://player.vimeo.com/video/'+this.get('data-vid-id')+'?title=0&byline=0&portrait=0&autoplay=0&color=ffffff';
    var title = this.get('data-title');
    var desc = this.get('data-description');
    
    if ($('video_wrap').getStyle('display') == 'none') {
        setVideo(src, title, desc, function () {
            $('video_wrap').reveal();
        });
    } else {
        var fade = new Fx.Tween($('video_wrap'), {
            property: 'opacity',
            link: 'chain',
            onComplete: function () {
                setVideo(src, title, desc, function () {
                    $('video_wrap').fade('in');
                    new Fx.Tween($('video_wrap'), {
                        property: 'opacity',
                        link: 'chain'
                    }).start(100);
                });
            }
        }).start(0);
    }
};

Now we need to make sure all of the thumbnails are clickable and that we call onThumbClick when the thumbnails are clicked. We're also binding thumb to the this variable for convenience.

$$('.thumb').each(function (thumb) {
    addEvent('click', onThumbClick.bind(this));
});

And that's it. Not so bad. You can see the finished result here: The Good Line

Google Docs Sync for Linux

Google-Docs-Sync is a small python project I started back in January of this year. Since then, I think it has grown into a somewhat useful (although incomplete) tool. It's Ubuntu only for now, but I occasionally bug a Mac user friend and a Windows user friend to help with the porting. I haven't had any luck yet.

This blog post will serve as a sort of quick start guide. While this information will probably stay relevant, the most up to date info will be found in the project's README file on github.

You can download everything you need here: Google-Docs-Sync.

  1. Download the file using the link above and extract it where ever you would like. I use ...

read more

Teach Yourself HTML and CSS

See the actual app here.

My wife is a designer. Recently she has expressed interest in learning html and css since it would help her tremendously at work. So we agreed that I would teach her. I'm not a great teacher, so I wanted to make sure everything was as painless as possible. In my mind, the least useful/fun part when learning to work with html and css is dealing with the editors and opening the page in a browser and refreshing the page and checking for errors and refreshing the page and making changes and saving your work and refreshing the browser and so on. I decided that I can get rid of most of the grid ...

read more

Syft.co and Image Search

Yes. Syft.co. Not Syft.com. Now that we have that out of the way, we can move on to more interesting issues.

Syft is a joint project between myself and Keenan Cummings. I handle the implementation and technical aspects of the site. In the current state, Syft is relatively simple (by most any standard). It uses the Google Image Search API and runs a user-supplied query against a number of pre-selected sites and returns the results. So right now, not much all that interesting is going on. It seems to be a pretty standard use of Google's API. Unfortunately Google recently announced that they are deprecating this API. And they aren't providing an alternative. I believe that ...

read more

contact

google+: Greggory Hernandez
twitter: @gregghz
email: greggory.hz@gmail.com