eternalistic
10/22/2015 - 4:18 AM

aspPhotoGallery.js

/**
 * @file
 * A JavaScript file for the theme.
 *
 * In order for this JavaScript to be loaded on pages, see the instructions in
 * the README.txt next to this file.
 */

(function($, window, document, undefined) {

  // Photo Gallery.
  Drupal.behaviors.aspPhotoGallery = {
    attach: function(context, settings) {
      $('a.aspPhotoGallery', context).colorbox({
        rel: 'aspPhotoGallery',
        maxWidth: '100%',
        fixed: true,
        title: function() {

          // URL.
          var url = $(this).attr('href');

          // Title.
          var title = $(this).attr('data-title');
          if (title) {
            var title = '<h2 class="title">' + $(this).attr('data-title') + '</h2>';
          } else {
            var title = '';
          }

          // Photographer.
          var photographer = '<div class="photographer">' + $(this).attr('data-photographer') + '</div>';

          // Caption.
          var caption = $(this).attr('data-caption');
          if (caption) {
            var caption = '<div class="column-second column"><div class="caption">' + $(this).attr('data-caption') + '</div></div>';
          } else {
            var caption = '';
          }

          // Share.
          var share = '<div class="share">' + $(this).attr('data-share') + '</div>';

          // Markup.
          return '<div class="column-wrapper"><div class="column-first column">' + title + photographer + '</div>' + caption + '<div class="column-last column">' + share + '</div></div>';
        },

        // On open, add "fixed" class to body to disable page scrolling.
        onOpen: function() {
          $('body').addClass('fixed');
        },

        // On close, remove "fixed" class to body to re-enable page scrolling.
        onCleanup: function() {
          $('body').removeClass('fixed');
        },
      });
    }
  };

  // Read a page's GET URL variables and return them as an associative array.
  function getUrlVars() {
    var vars = [],
      hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  }

  // Force click if image ID is in query string.
  Drupal.behaviors.aspPhotoGalleryQuery = {
    attach: function(context, settings) {
      var aspPhotoGalleryPeopleFID = getUrlVars()["aspPhotoGallery"];
      if (aspPhotoGalleryPeopleFID, context) {
        $('a.aspPhotoGallery#aspPhotoGallery-' + aspPhotoGalleryPeopleFID, context).trigger('click');
      }
    }
  };

})(jQuery, this, this.document);