Team:LMU-Munich/ajaxload.js
From 2014.igem.org
Revision as of 17:09, 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");
}
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; } MathJax.Hub.Queue(["Typeset",MathJax.Hub]); // TODO prepare menu // TODO update tour /* $('#ajax-overlay').fadeOut(400, function(){ $('#ajax-overlay').remove(); }); */ ajaxReady(); } });
/*overlay = $('
<img src="" />
');
$(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'); 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').width(this.width()+6); ajaxBinding();
scrollSpy(); myDocumentReady();
}