Team:LMU-Munich/ajaxload.js

From 2014.igem.org

Revision as of 21:08, 17 October 2014 by Hoerldavid (Talk | contribs)

/* AJAX-loading adapted from Team TU-Munich 2013

  • /

/* GLOABAL VARIABLES for AJAX-anchor loading */ var lastAnchor = ""; var anchorClicked = false;

function ajaxBinding() {

 var links = new Array();
 var externalLinks = new Array();
 console.log("AJAX setup!");
 //var candidates = $('#bodyContent a, #footer-box a').not('.bx-controls a');
 //var candidates = $('#headerWrapper a, #footer a');
 var candidates = $('a').not('#top-section a, .editsection a');
 for (i = 0; i < candidates.length; i++) {
   if ( candidates[i].href.search('2014.igem.org/Team:LMU-Munich') != -1)// && candidates[i].href.search('#') == -1)
   {
     links.push(candidates[i]);
     //console.log(candidates[i]);
   }else {
     externalLinks.push(candidates[i]);
   }
 }
 $(links).click(function(e){
   console.log(this.href);
   if(this.href.search('#') == -1){
     if(document.URL.indexOf(this.href) > -1){
       console.log('scroll to top!' );
       window.scrollTo(0,0);
     }
     e.preventDefault();
     History.pushState(null, this.href.substring(21) + ' - 2014.igem.org', this.href);
   }else{
     if(document.URL.indexOf(this.href.split('#')[0]) > -1){
       e.preventDefault();
      // $(this.href.split('#')[1]).scrollTo();
       $('html, body').animate({
       scrollTop: (Number($("#" + this.href.split('#')[1]).offset().top) - Number(105))
   }, 500);
     }else{
       e.preventDefault();
       
       anchorClicked = true;
       lastAnchor = "#" + this.href.split('#')[1]; 
       
       History.pushState(null, this.href.substring(21) + ' - 2014.igem.org', this.href.split('#')[0]);
     }
   }
 });
 
 $(externalLinks).attr("target","_blank");


// mercilessly copied from iGEM TUM 2013 // Slideshows

$('.bxslider').bxSlider({

   responsive: false,
   auto: true,
   autoHover: true,
   captions: true

});

$('.bxgallery').bxSlider({

   captions: true,
   slideMargin: 10,
   minSlides: 3,
   maxSlides: 3,
   moveSlides: 1,
   slideWidth: 5000

});

// Lightbox

$("div.thumbinner > a img").slimbox({}, function(el) { url = el.src; if (url.indexOf('thumb') != -1) { url = url.substring(0, url.lastIndexOf('/')); url = url.replace('/thumb/', '/'); } description = $(el).parents("div.thumbinner").children("div.thumbcaption").text(); return [url, description]; }, function(el) { return (this == el); });

$("ul.bxgallery img").slimbox({ loop: true }, function(el) { url = el.src; if (url.indexOf('thumb') != -1) { url = url.substring(0, url.lastIndexOf('/')); url = url.replace('/thumb/', '/'); } description = el.title + el.alt; return [url, description]; }, function(el) { return (this == el) || (this.parentNode.parentNode && (this.parentNode.parentNode == el.parentNode.parentNode)); });

$('.team-img div').mouseover(function() {

   $(".team-data#" + $(this).parent().parent().attr('id') + " div.member").addClass('invisible-team');
   $(".team-data#" + $(this).parent().parent().attr('id') + " div#" + $(this).attr('id')).removeClass('invisible-team')
   $(".team-data#" + $(this).parent().parent().attr('id') + " div#" + $(this).attr('id'))..css('visibility', 'visible');

}); $('.team-img div').click(function() {

   $(".team-data#" + $(this).parent().parent().attr('id') + " div.member").addClass('invisible-team');
   $(".team-data#" + $(this).parent().parent().attr('id') + " div#" + $(this).attr('id')).removeClass('invisible-team');
   $(".team-data#" + $(this).parent().parent().attr('id') + " div#" + $(this).attr('id'))..css('visibility', 'visible');

}); $('.team-img div').on("tap",function() {

   $(".team-data#" + $(this).parent().parent().attr('id') + " div.member").addClass('invisible-team');
   $(".team-data#" + $(this).parent().parent().attr('id') + " div#" + $(this).attr('id')).removeClass('invisible-team');
   $(".team-data#" + $(this).parent().parent().attr('id') + " div#" + $(this).attr('id'))..css('visibility', 'visible');

});


}


function ajaxLoad(url) {

 ajaxSrc = url;
 $.ajax({
   url: ajaxSrc,
   type: "GET",
   error: function(xhr, status) {
     window.location = ajaxSrc;
   },
   success: function(render) {
     oldContent = $('#lmu-content').children();
     newContent = $(render).find('#lmu-content').children();
     $('#top-section').replaceWith($(render).find('#top-section'));
     $(oldContent).remove();
     $('#lmu-content').prepend(newContent);
     window.scrollTo(0,0);
     
     // external anchor link simulation
     if (anchorClicked)
     {
       $('html, body').animate({
       scrollTop: (Number($(lastAnchor).offset().top) - Number(105))
       }, 1);
       anchorClicked = false;
     }
     
    
     
 
     
     // TODO prepare menu
     // TODO update tour
     
     
     /*
     $('#ajax-overlay').fadeOut(400, function(){
       $('#ajax-overlay').remove();
     });
     */
     ajaxReady();
   }
 });
 /*
overlay = $('
<img src="TUM13_ajax-loading.gif" />
');
 $(overlay).hide();
 $('#wikicontent').append(overlay);
 $(window).off('scroll', gotop);
 $('html, body').animate({scrollTop : 0},400,'swing',function(){
   $("a#gotop").fadeOut(400);
 });
 $(overlay).fadeIn(400);
 */

}

function ajaxStateChange(e) {

 //alert(History.getState().url);
 ajaxLoad(History.getState().url);

}

History.Adapter.bind(window, 'statechange', ajaxStateChange);

$(document).ready(initiateStuff);

function initiateStuff() {

 //$('#sidr').sidr();
 $('#mobile-navigation').slicknav({label:'LMU iGEM',closeOnClick:true,allowParentLinks:true,prependTo:'#slick-navigation'});
 $('#mobile-navigation').hide();
 $('.slicknav_menu').addClass('hidden-md');
 $('.slicknav_menu').addClass('hidden-lg');
   $('.thumbinner').each(function() {
    $(this).width($(this).width() + 6);
 });
 ajaxBinding();
 scrollSpy();

}

function scrollSpy(){

  // scroll spy initiation
     $('.scroll-spy').xscrolly({
       change: function($el) {
         //$el.addClass('active');
         
         // remove active
         $(".active-item").removeClass("active-item");
         
         // reset navigation
         $(".nav-item.active").removeClass("active");
         $(".current-top-nav").addClass("active");
         $("#sub-navigation > ul").hide();
         $("#" + $(".current-top-nav").attr('id') + "-sub").show();
         
     
         // scroll spy
         console.log("#inpage-nav-" + $el.data("scroll"));
         $("#inpage-nav-" + $el.data("scroll")).addClass("active-item");
       }
     });

}

function ajaxReady() {

   $('.thumbinner').each(function() {
    $(this).width($(this).width() + 6);
 });
 ajaxBinding();
     MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
 scrollSpy();
 //myDocumentReady();

}