Team:Cambridge/static/wiki.js

From 2011.igem.org

(Difference between revisions)
Line 183: Line 183:
   set_hash(url);
   set_hash(url);
   set_area(url);
   set_area(url);
-
   $('#cam_nav').introduction('setArea', area, subarea);
+
   $('#cam_desc').introduction('setArea', area, subarea);
   update_links();
   update_links();
};
};

Revision as of 15:09, 11 September 2011

console.log('/Team:Cambridge/static/wiki.js being Loaded.');

/*

*  Variables.
* 
* */

//current area/subarea var area = 'overview'; var subarea = 'home';

//the url that's loaded var the_url = ;//the url that's being displayed - initially nothing //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:\/\/2011.igem.org/i, ).replace(/\/Team:Cambridge/i, );
 if(hash == '/home') hash = ;
 
 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(hash == ) hash = '/home';

if(!absolute || (absolute == undefined)) return "/Team:Cambridge" + hash; return "https://2011.igem.org/Team:Cambridge" + hash; }

var check_hash = function() {

 if(the_url != get_hash(false))
 {
   int = clearInterval(int);//pause

console.log('the_url != get_hash(false) => "' + the_url + '" != "' + get_hash(false) + '" -- window.location.hash.substr(1) = "' + window.location.hash.substr(1) + '"')

   load_url(get_hash(false));
   int = setInterval('check_hash();', 500);
 }

}

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

 var current_url = get_hash(false);
 $('#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);

}

var is_internal = function(obj) {

 if($(obj).hasClass('cam-external')) return false;
 var href = $(obj).attr("href");
 if(/:\/\//.test(href))
 {
   return /http:\/\/2011.igem.org\/Team:Cambridge/i.test(href);
 }
 return true;

}


var areas = [

 [/\/Team:Cambridge\/project/i, 'project',],
 [/\/Team:Cambridge\/(experiments|constructs|protocols|safety|labwork)/i, 'labwork',],
 [/\/Team:Cambridge\/society/i, 'society',],
 [/\/Team:Cambridge\/team/i, 'team',],
 [/\/Team:Cambridge/i, 'overview'],
 [/./i, 'overview'],

];

var subareas = {

 'overview': [
   [/\/Team:Cambridge\/Media/i, 'media'],
   [/\/Team:Cambridge\/Reflectin/i, 'reflectin'],
   [/\/Team:Cambridge\/Blog/i, 'blog'],
   [/./i, 'home'],

],

 'project':  [
   [/\/Team:Cambridge\/Project\/prelim/i, 'preliminary'],
   [/\/Team:Cambridge\/Project\/in_vitro/i, 'invitro'],
   [/\/Team:Cambridge\/Project\/in_vivo/i, 'invivo'],
   [/\/Team:Cambridge\/Project\/gibthon/i, 'gibthon'],
   [/\/Team:Cambridge\/Project\/conclusion/i, 'conclusion'],
   [/\/Team:Cambridge\/Project/i, 'overview'],

],

 'labwork':  [
   [/\/Team:Cambridge\/Experiments\/Plasmid_Constructs/i, 'constructs'],
   [/\/Team:Cambridge\/experiments/i, 'experiments'],
   [/\/Team:Cambridge\/protocols/i, 'protocols'],
   [/\/Team:Cambridge\/safety/i, 'safety'],
   [/\/Team:Cambridge\/labwork/i, 'overview'],

],

 'society':  [
   [/\/Team:Cambridge\/Society\/igem/i, 'igem'],
   [/\/Team:Cambridge\/Society\/future_applications/i, 'future'],
   [/\/Team:Cambridge\/Society/i, 'overview'],

],

 'team':     [
   [/\/Team:Cambridge\/Team\/Students/i, 'students'],
   [/\/Team:Cambridge\/Team\/Academics/i, 'academics'],
   [/\/Team:Cambridge\/Team\/Sponsors/i, 'sponsors'],
   [/\/Team:Cambridge\/Team/i, 'overview'],

], };

var get_area = function(path) { //figure out which area of the site we are in var ar = ; var sub = ; if(path == undefined) var path = location.pathname;

for ( var a in areas) { if(areas[a][0].test(path)) { ar = areas[a][1]; break; } } for ( var a in subareas[ar]) { if(subareas[ar][a][0].test(path)) { sub = subareas[ar][a][1]; break; } } return [ar, sub]; }

var apply_area = function() { $('.cam-selected').removeClass('cam-selected'); $('.cam-menu-item.cam-' + area).addClass('cam-selected'); $('.cam-' + area + '-' + subarea).addClass('cam-selected'); }

var set_area = function(path) { var as = get_area(path); area = as[0]; subarea = as[1]; apply_area(); }

var load_url = function(url) {

 if(url == undefined)
   url = '/Team:Cambridge';
 var load = url + '?action=render';
 console.log('loading: ' + url);
 $.ajax({
   'url': load,
   'dataType': 'html',
   'error': function(jqXHR, textStatus, errorThrown) {
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);
   },
 });
 set_hash(url);
 set_area(url);
 $('#cam_desc').introduction('setArea', area, subarea);
 update_links();

};

$(window).ready(function() { console.log('static/wiki.js::$(window).ready()'); $('.cam-menu-item a').mouseenter( function(event) {//in $(event.currentTarget).next().children('.cam-menu-drop').slideDown('fast'); }); $('.cam-menu-item').mouseleave( function(event) {//out $(event.currentTarget).find('.cam-menu-drop').slideUp('fast'); });

$('.cam-social-link').hover( function(event) {; $(this).children().animate({top:0}, 75); }, function(event) { $(this).children().animate({'top':12}, 250); } ).click( function() { window.location = $(this).attr('href'); });

set_area();

   update_links();
 $('a').live('click', function(event) {
   if(is_internal(this))
   {
     event.preventDefault();
     var new_url = $(this).attr('href');
     if(new_url == '/Team:Cambridge') new_url = '/Team:Cambridge/home'; //silently redirect to the home page
     load_url(new_url);
     return false;
   }
   return true;
 });

$('#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); });