Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Lightbox = (function (Helpers) {
  2.  
  3.   var options = {
  4.     template: '/build/javascripts/templates/lightbox.html',
  5.     selector: '[rel="lightbox"]'
  6.   }
  7.  
  8.   /**
  9.    * Build lightbox
  10.    */
  11.  
  12.   var _buildLightbox = function () {
  13.  
  14.     // grab the contents of the template
  15.     // and insert it into the DOM
  16.     Helpers.getFileContents(options.template, function (response) {
  17.       document.body.innerHTML += response;
  18.       document.body.innerHTML += '<div class="overlay"></div>';
  19.  
  20.       // add a click event for the close icon
  21.       var close = document.getElementById('close');
  22.       close.onclick = _destroyLightbox;
  23.     });
  24.  
  25.   }
  26.  
  27.   /**
  28.    * Destroy lightbox
  29.    */
  30.  
  31.   var _destroyLightbox = function () {
  32.     var lightbox = document.getElementsByClassName('lightbox');
  33.     for(var i = 0; i < lightbox.length; i++) {
  34.       lightbox[i].parentNode.removeChild(lightbox[i]);
  35.     }
  36.  
  37.     var overlay = document.getElementsByClassName('overlay');
  38.     for(var i = 0; i < overlay.length; i++) {
  39.       overlay[i].parentNode.removeChild(overlay[i]);
  40.     }
  41.   }
  42.  
  43.  
  44.   /**
  45.    * Init
  46.    */
  47.   var init = function () {
  48.     var links = document.querySelectorAll(options.selector);
  49.     for(var i = 0; i < links.length; i++) {
  50.       links[i].addEventListener('click', _buildLightbox, false);
  51.     }
  52.  
  53.     document.onkeydown = function(evt) {
  54.       evt = evt || window.event;
  55.       if (evt.keyCode == 27) {
  56.         _destroyLightbox();
  57.       }
  58.     };
  59.   };
  60.  
  61.   return {
  62.     init: init
  63.   };
  64.  
  65. })(Helpers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement