Book Image

Instant Galleria How-to

By : Nathan Van Gheem
Book Image

Instant Galleria How-to

By: Nathan Van Gheem

Overview of this book

Providing beautiful and usable galleries is an important aspect of the Web today. A gallery solution needs to integrate into your web application easily and seamlessly. Users expect mobile sites that function on par with their desktop counterparts‚Äîespecially for image galleries. In order to accomplish these tasks, you need to use a JavaScript (not Flash) that is extensible and scales to mobile devices. "Instant Galleria How-to" will teach you how to use Galleria in advanced scenarios to become an expert and integrate Galleria into your next project using themes and plugins to accomplish any task. This book teaches you how to use and create themes and plugins,  using the Galleria API through a series of recipes that include a plethora of code examples. You'll be taken through detailed instructions on the usage of JavaScript to customize Galleria. You will create your own theme (mobile friendly), plugin, and learn all the configuration options of Galleria. You'll learn how to customize Galleria by using its extensive API, optimize Galleria, integrate with Google Analytics, create tests for your customization, and integrate into your web application. You'll become an expert user of the Galleria framework, which will enable you to deploy beautiful, mobile friendly galleries for your next web project.
Table of Contents (7 chapters)

Alternative image data structures (Simple)


This recipe provides an overview of the various methods used to provide alternative data structures for the Galleria framework to consume.

Getting ready

The data used to display images and other media for a gallery has a number of attributes that can be set up. Typically, Galleria just finds relevant image URLs, title, and description for the image and displays the gallery with that; however, there are more options possible to create a more robust gallery than what is typically used.

The full list of available image properties that can be set through HTML data attributes (HTML tag attributes that begin with data-) or JavaScript is as follows:

  • thumb: This property provides the thumbnail image

  • image: This property provides the main image (required)

  • big: This property provides the big image for fullscreen mode

  • title: This property provides the image title

  • description: This property provides the image description

  • link: This property provides the image URL link

  • layer: This property provides a layer of HTML that will be displayed on top of the content

  • video: This property provides a URL to a video; as of Version 1.2.7 we support Vimeo, Youtube, and Dailymotion URLs

  • iframe: This property provides a URL to be displayed in an iframe

  • original: This property provides a reference to the original IMG element

Full documentation covering image properties can be found via Galleria's website at http://galleria.io/docs/references/data/.

Before we start using the different image data properties, we'll create a file in the development folder called data-structures.html. To start off, we'll create the basic HTML structure for our gallery using the following code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="http://code.jquery.com/jquery.min.js">
    </script>
    <script src="../galleria-1.2.8.min.js"></script>
    <script>
      // Galleria initialization will be placed here
    </script>
  </head>
<body>
  <div id="galleria" style="height: 500px;">
    <!-- html image structure will be placed here -->
  </div>
</body>
</html>

Specifying extra parameters via IMG tag

The usual HTML structure for image data is to provide an anchor tag linking to the image displayed in the gallery, which then wraps an img tag that specifies the thumbnail and additional image properties for the gallery.

The following code is an example of this in practice:

<div id="galleria-img-tag" style="height: 500px;">
  <!—- anchor tag wrapping img tag -->
  <a href="images/image1-1600px.jpg">
    <img data-title="Image 1"
         data-description="Image 1 description"
         src="images/image1-200px.jpg"
         data-link="images/image1-1600px.jpg" />
  </a>
  <!-- More images defined in this way -->
</div>

This code will be initialized with the following JavaScript code:

<script>
  $(document).ready(function(){
    Galleria.loadTheme(
      '../themes/classic/galleria.classic.min.js');
    Galleria.run('#galleria-img-tag');
  });
</script>

Galleria understands data- prefixed attributes as settings for Galleria on that image. So data-title, data-description, and data-link are all Galleria image properties that are set via HTML data attributes. Additional properties could be provided here such as data-big or data-original.

Alternative media

In addition to images, Galleria can also display video and iframes inside the gallery. The method by which this is accomplished is mostly the same as the way images are specified, with the only difference being that the image URL is replaced with the video or iframe URL. Galleria supports YouTube, Vimeo, and Dailymotion video URLs.

The following code is an example of adding just a YouTube video:

<div id="galleria-media" style="height: 500px;">
  <a href="http://www.youtube.com/watch?v=8gSPbmgWr_Y">
    <img data-title="Youtube video"
         data-description="Youtube description"
        src="http://i3.ytimg.com/i/vC4D8onUfXzvjTOM-dBfEA/1.jpg?v=50aaaeea"
        data-link="http://www.youtube.com/watch?v=8gSPbmgWr_Y" />
  </a>
</div>

This code should be initialized with the following JavaScript code:

<script>
  $(document).ready(function(){
    Galleria.loadTheme('../themes/classic/galleria.classic.min.js');
    Galleria.run('#galleria-media');
  });
</script>

As we can see, the HTML structure is basically identical. The only difference here is that we're using a YouTube video URL instead of an image URL.

Using JSON

It's also possible to use JSON data to initialize Galleria. This is useful when we want to initialize Galleria programmatically with JavaScript or when the image data is gathered on a remote server.

In our example, we'll load the same images as previously, just with a JSON structure instead of HTML, using the following code:

<script>
$(document).ready(function(){
    Galleria.loadTheme(
      '../themes/classic/galleria.classic.min.js');
    var data = [
        {
            thumb: 'images/image1-200px.jpg',
            image: 'images/image1-1600px.jpg',
            title: 'Image 1',
            description: 'Image 1 description'
        },{
            thumb: 'images/image2-200px.jpg',
            image: 'images/image2-1600px.jpg',
            title: 'Image 2',
            description: 'Image 2 description'
        }];
    Galleria.run('#galleria-json', {
        dataSource: data });
});
</script>

Here, we're just using the dataSource property to specify an array of images that Galleria will use for the gallery.

HTML structure parsing customization

It's possible that you'll have an existing HTML markup that doesn't fit in the HTML pattern that Galleria expects. Here, we'll look into customizing how Galleria gathers the image properties for the gallery from the DOM.

For example, if working with a definition list (dl) of images, titles, and descriptions, all the required properties are available in order to create a gallery.

<div id="galleria-dataconfig" style="height: 500px;">
  <dl>
    <dt><img src="images/image1-1600px.jpg"
             data-thumb="images/image1-200px.jpg" />
    </dt>
    <dd>
      <span class="title">Image 1</span>
      <span class="description">Image 1 Description</span>
    </dd>
    <dt><img src="images/image2-1600px.jpg"
             data-thumb="images/image2-200px.jpg"/>
    </dt>
    <dd>
      <span class="title">Image 2</span>
      <span class="description">Image 2 Description</span>
    </dd>
  </dl>
</div>

All that's left is instructing Galleria how to gather those properties from the DOM, which can be done with the following JavaScript code:

$(document).ready(function(){
    Galleria.loadTheme(
      '../themes/classic/galleria.classic.min.js');
    Galleria.run('#galleria-dataconfig', {
        dataConfig: function(img) {
            var img = $(img);
            return {
                thumb: img.data('thumb'),
                title: img.parent().next().
                           find('.title').html(),
                description: img.parent().next().
                                 find('.description').html()
            }
        }
    });
});

What is being done here is that Galleria is finding every instance of an img tag and passing it into the provided dataConfig function, which then returns the rest of the image properties via simple jQuery DOM traversal.

How it works...

Before an image is displayed in a gallery, Galleria uses the various methods we described previously to parse image data so that it can easily be customized.