Team:Cambridge/static/wiki.js

From 2012.igem.org

/*

This file is part of the cambridge 2011 igem teams wiki. nothing is guaranteed.
DO WHAT THE F*CK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004
Copyright (C) 2011 Haydn King hjking734@gmail.com
           DO WHAT THE F*CK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 0. You just DO WHAT THE F*CK YOU WANT TO. 
  • /

//the url that's loaded var the_url = window.location.pathname;//the url that's being displayed //the interval var int;


var set_hash = function(path) {//set the hash to the path given

 int = window.clearInterval(int);
 
 //turn the path into a presentable hash
 var hash = path.replace(/http:\/\/2012.igem.org/i, ).replace(/\/Team:Cambridge/i, );
 
 window.location.hash = hash;
 the_url = get_hash(false);
 
 int = setInterval('check_hash();', 500);

}

var get_hash = function(absolute) {

 var hash = window.location.hash.substr(1);
 if(!absolute || (absolute == undefined))
   return "/Team:Cambridge" + hash;
 return "https://2012.igem.org/Team:Cambridge" + hash;

}

var check_hash = function() {//check if the hash has changed (e.g. back button press)

 if(the_url != get_hash(false))
 {
   int = clearInterval(int);//pause
   if((window.location.hash.charAt(1) == '/') || (window.location.hash == ))
     load_url(get_hash(false));
   int = setInterval('check_hash();', 500);
 }

}

//update the edit, login and logout links after page change var update_links = function(current_url) {

 $('#edit-link').attr('href', current_url + '?action=edit');
 $('#login-link' ).attr('href', '/wiki/index.php?title=Special:UserLogin&returnto=' + current_url);
 $('#logout-link').attr('href', '/wiki/index.php?title=Special:UserLogout&returnto=' + current_url);

}

//define external internal links

var is_internal = function(obj) {

 if($(obj).hasClass('cam-external')) return false;
 return /\/Team:Cambridge/i.test($(obj).attr("href"));

} var is_external = function(obj) { return !is_internal(obj);}

var loading_timeout;

var show_loading = function() {

 //only show the loading bar if it's taking more than 500ms to display
 loading_timeout = setTimeout("$('#cam_loading').slideDown('fast');", 500);

}

var hide_loading = function() {

 clearTimeout(loading_timeout);
 $('#cam_loading').slideUp('fast');

}

var load_url = function(url) {

 if(url == undefined)
   url = '/Team:Cambridge';
 var after_hash = ;
 var match = url.match(/#.+/i);
 if(match != null)
   after_hash = match[0].substr(1);
 var hash_start = url.search('#');
 if(hash_start > 0)
 {
   url = url.substr(0, hash_start);
 }
 var load = url + '?action=render';
 show_loading();
 $.ajax({
   'url': load,
   'dataType': 'html',
   'error': function(jqXHR, textStatus, errorThrown) {
     hide_loading();
     set_hash(url);
var $c = $('

AjaxError

Got error "' + errorThrown + '" while attempting to load url "' + load + '"

');
     $('#cam_pagecontent').hide().html($c).show();	
     window.scrollTo(0,0);	
   },
   'success': function(data){
     var $c = $('#wiki_content', data);
     $('#cam_pagecontent').hide().html($c.html()).show();	
     window.scrollTo(0,0);
     hide_loading();
     if(after_hash != )
       window.location.hash = after_hash;
     set_hash(url);
   },
 });
 set_area(url);
 update_links(url);

};

$('#cam_topbar a').hover( function() { $(this).prev('.cam-ui-icon').addClass('cam-hover'); }, function() { $(this).prev('.cam-ui-icon').removeClass('cam-hover'); } );

 int = window.setInterval('check_hash();', 500);
 if((window.location.hash == ) || (window.location.hash == undefined) || !(window.location.hash.charAt(1) == '/'))
 {
   set_area(the_url);
   set_hash(the_url);
   update_links(the_url);
 }
 else
 {
   load_url(get_hash());
 }