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)

Creating a plugin (Advanced)


In this recipe we'll learn how to create a plugin that retrieves images from Facebook.

Getting ready

For this exercise we're going to walk through the process of creating a Facebook plugin that will get all the public photos for the user or page provided in a Galleria option.

Before we get started, we need to create a new folder named facebook. Inside the folder, please create empty files with the names galleria.facebook.js and galleria.facebook-example.html.

How to do it...

Creating a plugin can include hooking into Galleria events, customizing options, or overriding Galleria methods. In the later stage, we need to make sure we still allow Galleria to function properly if the plugin isn't used, even though it might be loaded.

  1. The following code with inline comments is used to create the galleria.facebook.js plugin:

    (function($) {
    
    var FACEBOOK_URL = 'https://graph.facebook.com';
    
    /* Store the original load method so we can later call it if
       no facebook option is provided to Galleria. */
    var load = Galleria.prototype.load;
    
    /* Here we override the load method with our own that will
       check for facebook option. */
    Galleria.prototype.load = function() {
      /* check if facebook option is provided. Call the
    	  original load if not. */
      if(typeof this._options.facebook !== 'string'){
        load.apply(this, Galleria.utils.array(arguments));
        return;
      }
    
      /* "this" in this context is the Galleria instance. 
         We assign it to "galleria" so we can still have 
         access to it within the context of the ajax 
         callback */
      var galleria = this,
      facebook = this._options.facebook;
    
      $.ajax({
        /* Use the facebook graph API to get all the public
           photos for the provided facebook user or page */
        url: FACEBOOK_URL + '/' + facebook +
             '/photos?fields=picture,source&limit=50',
        /* returned data is in JSON format */
        dataType: 'json',
        success: function(data){
          var images = [];
          for(var i=0; i<data.data.length; i++){
           /* go through each image and add it to
              new images array in JSON format that
              Galleria expects */
            var _image = data.data[i];
            images.push({
              image: _image.source,
              thumb: _image.picture
            });
          }
         /* manually set the data for Galleria */
          galleria._data = images;
         /* And trigger Galleria to load with the newly
            set data */
         galleria.trigger(Galleria.DATA);
       }
      });
    };
    }(jQuery));
  2. Finally, we need to load the gallery using the plugin. For this example, we're using the "Marvel" Facebook page to retrieve images:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <script src="http://code.jquery.com/jquery.min.js"></script>
     <script src="../../galleria-1.2.8.js"></script>
     <script src="galleria.facebook.js"></script>
     <script>
       $(document).ready(function(){
         Galleria.loadTheme(
           '../../themes/classic/galleria.classic.min.js');
         Galleria.run('#galleria', { facebook: 'Marvel' });
       });
     </script>
      </head>
      <body>
     <div id="galleria" style="height: 500px;">
     </div>
      </body>
    </html>

    Here, all we're doing differently is including the galleria.facebook.js file we just worked on and initializing the gallery with the facebook option. If everything worked properly, the result should be a nice gallery retrieving images from Facebook, as shown in the following screenshot:

How it works...

The plugin overrides the original load behavior of Galleria, retaining the original load behavior to fall back on if the facebook plugin is not used. Then, we use the Facebook Graph API with AJAX to retrieve the images.

What else can we do?

The possibilities for customizing Galleria are vast. In the next section, we'll be going through using the Galleria API, which will help us to understand the various places we can customize Galleria and methods we can use to dig into the Galleria framework.