Team:TU-Munich/TUM13 ajax.js

From 2013.igem.org

(Difference between revisions)
 
(46 intermediate revisions not shown)
Line 2: Line 2:
   var links = new Array();
   var links = new Array();
-
   for (i = 0; i < $('#bodyContent a').length; i++) {
+
   var candidates = $('#bodyContent a, #footer-box a').not('.bx-controls a');
-
     if ( $('#bodyContent a')[i].href.search('https://2013.igem.org/Team:TU-Munich') != -1 && $('#bodyContent a')[i].href.search('Labjournal') == -1 ) {
+
  for (i = 0; i < candidates.length; i++) {
-
       links.push($('#bodyContent a')[i]);
+
     if ( candidates[i].href.search('https://2013.igem.org/Team:TU-Munich') != -1 && candidates[i].href.search('#') == -1 && candidates[i].href.search('Labjournal') == -1 && candidates[i].href.search('.ph') == -1 ) {
 +
       links.push(candidates[i]);
     }
     }
   }
   }
Line 16: Line 17:
function ajaxLoad(url) {
function ajaxLoad(url) {
-
    ajaxSrc = url;
+
  ajaxSrc = url;
-
    $.ajax({
+
  $.ajax({
-
      url: ajaxSrc,
+
    url: ajaxSrc,
-
      data: { action: 'render' },
+
    type: "GET",
-
      type: "GET",
+
    error: function(xhr, status) {
-
      error: function(xhr, status) {
+
      window.location = ajaxSrc;
-
        window.location = ajaxSrc;
+
    },
-
      },
+
    success: function(render) {
-
      success: function(render) {
+
      oldContent = $('#wikicontent').children().not('#ajax-overlay');
-
        tmp = $('<div />').html(render);
+
      newContent = $(render).find('#wikicontent').children();
-
        $(tmp).find('#wikicontent').children().appendTo($('#wikicontent'));
+
      $('#top-section').replaceWith($(render).find('#top-section'));
-
         ajaxReady();
+
      $(oldContent).remove();
-
       }
+
      $('#wikicontent').prepend(newContent);
-
     });
+
      $('#ajax-overlay').fadeOut(400, function(){
-
    $(window).off('scroll', gotop);
+
         $('#ajax-overlay').remove();
-
    $('html').animate({scrollTop : 0},400,'swing',function(){
+
       });
-
      $(window).scroll(gotop);
+
      ajaxReady();
-
      $("a#gotop").fadeOut(400);
+
     }
-
      $('#wikicontent').children().remove();
+
  });
-
    });
+
  overlay = $('<div id="ajax-overlay"><img src="https://static.igem.org/mediawiki/2013/b/b4/TUM13_ajax-loading.gif" /></div>');
-
  }
+
  $(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) {
+
function ajaxStateChange(e) {
-
    ajaxLoad(History.getState().url);
+
  //alert(History.getState().url);
-
   }
+
   ajaxLoad(History.getState().url);
 +
}
History.Adapter.bind(window, 'statechange', ajaxStateChange);
History.Adapter.bind(window, 'statechange', ajaxStateChange);
Line 48: Line 57:
function ajaxReady() {
function ajaxReady() {
   ajaxBinding();
   ajaxBinding();
 +
  myWikiReady();
}
}

Latest revision as of 17:22, 28 October 2013

function ajaxBinding() {

 var links = new Array();
 var candidates = $('#bodyContent a, #footer-box a').not('.bx-controls a');
 for (i = 0; i < candidates.length; i++) {
   if ( candidates[i].href.search('https://2013.igem.org/Team:TU-Munich') != -1 && candidates[i].href.search('#') == -1 && candidates[i].href.search('Labjournal') == -1 && candidates[i].href.search('.ph') == -1 ) {
     links.push(candidates[i]);
   }
 }
 $(links).click(function(e){
   e.preventDefault();
   History.pushState(null, this.href.substring(21) + ' - 2013.igem.org', this.href);
 });

}

function ajaxLoad(url) {

 ajaxSrc = url;
 $.ajax({
   url: ajaxSrc,
   type: "GET",
   error: function(xhr, status) {
     window.location = ajaxSrc;
   },
   success: function(render) {
     oldContent = $('#wikicontent').children().not('#ajax-overlay');
     newContent = $(render).find('#wikicontent').children();
     $('#top-section').replaceWith($(render).find('#top-section'));
     $(oldContent).remove();
     $('#wikicontent').prepend(newContent);
     $('#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(ajaxBinding);

function ajaxReady() {

 ajaxBinding();
 myWikiReady();

}