Template:UBonn HBRS/js/main

/**

* Automatically sidebar nav with bootstrap scrollspy
*
* @version	0.23
* @author	mictes
* @date	2016-10-14
*/

function autoNav() {


var _that = this;

var _navElement = $( "#sideNav" ); var _idPrefix = "";

var _showDelay = 580; var _topOffset = 148; var _bottomOffset = $( "footer" ).outerHeight( true );


/** * Text to Id * * @param {string} text * * @return {string} * */ this.text2id = function( text ) {

if( typeof( text ) == "string" && text != "" ) { id = text.replace( /[^\w\s]/gi, ).replace( / /g, '_' ).toLowerCase(); } else { id = false; }


return id;

}


/** * Order Object by keys * (JS is not reliable enough!) * * @param {object} * * @return {object} * */ this.orderObject = function( unordered ) {

var ordered = {};

if( Object.keys( unordered ).length > 0 ) { // https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key Object.keys( unordered ).sort().forEach( function( key ) { ordered[key] = unordered[key]; } ); }


return ordered;

}


/** * Get Titles * Scan DOM for title-elements, add id's to them and return an object with the id's and text-contents * * @return {object} * */ var _getTitles = function() {

var list = {};

var titles = $( "h1, h2, h3, h4" );

if( titles.length > 0 ) { $.each( titles, function( index, value ) { var title = $( value ); var text = title.text().replace( '[edit]', ); var id = _idPrefix + _that.text2id( text );

// skip if loading... h3, could not be fixed with css if( text == " Loading ... " || text == "Contents" ) { return true; }

title.attr( "id", id ); list[index] = { type: title.prop( "tagName" ).toLowerCase(), text: text, id: id }; } ); }

var listOrdered = _that.orderObject( list );


return listOrdered;

}


/** * Initialize * */ this.initialize = function() {

if( _navElement.length > 0 ) { _navElement.css( "opacity", "0" );

var titles = _getTitles();

if( Object.keys( titles ).length > 0 ) { $.each( titles, function( index, value ) { if( value.type == "h2" ) {

_navElement.find( "ul" ).first().append( '
  • <a href="#' + value.id + '">' + value.text + '</a>
    • ' );

      } else if( value.type == "h3" ) { var ulElement = _navElement.find( "li.sideNav_h2:last > ul" );

      ulElement.append( '
    • <a href="#' + value.id + '">' + value.text + '</a>
    • ' );

      } } );

      $( "body" ).scrollspy( { target: "#" + _navElement.attr( "id" ) } );

      _navElement.affix( { offset: { top: _topOffset, bottom: _bottomOffset } } ).css( "top", "0" );

      _navElement.animate( { "opacity" : 1 }, _showDelay ); } }

      };


      }


      /**

      * Scroll to top button
      *
      * @version	0.1
      * @author	mictes
      * @date	2016-10-04
      *
      */
      

      function topScroll() {


      var _that = this;

      var _button = $( "#scrollTop" ); var _duration = 580;


      /** * Initialize * Attach event and functionality to button * */ this.initialize = function() {

      if( _button.length > 0 ) { $( document ).on( "click", "#" + _button.attr( "id" ), function( event ) { event.preventDefault();

      $( "html, body" ).animate( { scrollTop : 0 }, _duration ); } ); }

      }


      }


      /**

      * Init lightbox
      * (requires ekkoLightbox)
      *
      * @version	0.1
      * @author	mictes
      * @date	2016-10-04
      *
      */
      

      function initLightbox() {


      var _that = this;


      /** * Initialize * */ this.initialize = function() {

      $( document ).on( "click", '[data-toggle="lightbox"]', function( event ) { event.preventDefault(); $( this ).ekkoLightbox(); } );

      }


      }


      $( document ).ready( function() {

      autoNav = new autoNav(); autoNav.initialize();

      topScroll = new topScroll(); topScroll.initialize();

      initLightbox = new initLightbox(); initLightbox.initialize();

      } );