Team:Cambridge/static/wiki.js

From 2011.igem.org

(Difference between revisions)
 
(40 intermediate revisions not shown)
Line 1: Line 1:
-
console.log('/Team:Cambridge/static/wiki.js being Loaded.');
 
-
 
/*
/*
-
  *  Variables.
+
  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.
 +
*/
//current area/subarea
//current area/subarea
var area = 'overview';
var area = 'overview';
var subarea = 'home';
var subarea = 'home';
-
 
//the url that's loaded
//the url that's loaded
-
var the_url = '';//the url that's being displayed - initially nothing
+
var the_url = window.location.pathname;//the url that's being displayed
//the interval
//the interval
var int;
var int;
-
 
var set_hash = function(path)
var set_hash = function(path)
Line 22: Line 26:
   //turn the path into a presentable hash
   //turn the path into a presentable hash
   var hash = path.replace(/http:\/\/2011.igem.org/i, '').replace(/\/Team:Cambridge/i, '');
   var hash = path.replace(/http:\/\/2011.igem.org/i, '').replace(/\/Team:Cambridge/i, '');
-
  if(hash == '/home') hash = '';
 
    
    
   window.location.hash = hash;
   window.location.hash = hash;
Line 32: Line 35:
var get_hash = function(absolute)
var get_hash = function(absolute)
{
{
-
var hash = window.location.hash.substr(1);
+
  var hash = window.location.hash.substr(1);
-
if(hash == '') hash = '/home';
+
  if(!absolute || (absolute == undefined))
-
+
    return "/Team:Cambridge" + hash;
-
if(!absolute || (absolute == undefined))
+
  return "https://2011.igem.org/Team:Cambridge" + hash;
-
return "/Team:Cambridge" + hash;
+
-
return "https://2011.igem.org/Team:Cambridge" + hash;
+
}
}
var check_hash = function()
var check_hash = function()
-
{
+
{//check if the hash has changed (e.g. back button press)
   if(the_url != get_hash(false))
   if(the_url != get_hash(false))
   {
   {
     int = clearInterval(int);//pause
     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) + '"')
+
    if((window.location.hash.charAt(1) == '/') || (window.location.hash == ''))
-
    load_url(get_hash(false));
+
      load_url(get_hash(false));
-
 
+
     int = setInterval('check_hash();', 500);
     int = setInterval('check_hash();', 500);
   }
   }
Line 53: Line 53:
//update the edit, login and logout links after page change
//update the edit, login and logout links after page change
-
var update_links = function()
+
var update_links = function(current_url)
{
{
-
 
-
  var current_url = get_hash(false);
 
   $('#edit-link').attr('href', current_url + '?action=edit');
   $('#edit-link').attr('href', current_url + '?action=edit');
   $('#edit-intro-link').attr('href', '/Team:Cambridge/desc/' + area + '/' + subarea + '?action=edit');
   $('#edit-intro-link').attr('href', '/Team:Cambridge/desc/' + area + '/' + subarea + '?action=edit');
Line 68: Line 66:
   return /\/Team:Cambridge/i.test($(obj).attr("href"));
   return /\/Team:Cambridge/i.test($(obj).attr("href"));
}
}
-
 
var is_external = function(obj) { return !is_internal(obj);}
var is_external = function(obj) { return !is_internal(obj);}
Line 78: Line 75:
   [/\/Team:Cambridge/i, 'overview'],
   [/\/Team:Cambridge/i, 'overview'],
   [/./i, 'overview'],
   [/./i, 'overview'],
-
];
+
];
var subareas = {
var subareas = {
   'overview': [
   'overview': [
     [/\/Team:Cambridge\/Media/i, 'media'],
     [/\/Team:Cambridge\/Media/i, 'media'],
-
     [/\/Team:Cambridge\/Reflectin/i, 'reflectin'],
+
     [/\/Team:Cambridge\/Parts/i, 'parts'],
     [/\/Team:Cambridge\/Blog/i, 'blog'],
     [/\/Team:Cambridge\/Blog/i, 'blog'],
     [/./i, 'home'],
     [/./i, 'home'],
],
],
   'project':  [
   'project':  [
-
     [/\/Team:Cambridge\/Project\/prelim/i, 'preliminary'],
+
     [/\/Team:Cambridge\/Project\/Background/i, 'background'],
     [/\/Team:Cambridge\/Project\/Microscopy/i, 'microscopy'],
     [/\/Team:Cambridge\/Project\/Microscopy/i, 'microscopy'],
     [/\/Team:Cambridge\/Project\/in_vitro/i, 'invitro'],
     [/\/Team:Cambridge\/Project\/in_vitro/i, 'invitro'],
     [/\/Team:Cambridge\/Project\/in_vivo/i, 'invivo'],
     [/\/Team:Cambridge\/Project\/in_vivo/i, 'invivo'],
     [/\/Team:Cambridge\/Project\/gibthon/i, 'gibthon'],
     [/\/Team:Cambridge\/Project\/gibthon/i, 'gibthon'],
-
     [/\/Team:Cambridge\/Project\/conclusion/i, 'conclusion'],
+
     [/\/Team:Cambridge\/Project\/future/i, 'future'],
     [/\/Team:Cambridge\/Project/i, 'overview'],
     [/\/Team:Cambridge\/Project/i, 'overview'],
],
],
Line 104: Line 101:
],
],
   'society':  [
   'society':  [
-
     [/\/Team:Cambridge\/Society\/survey/i, 'survey'],
+
     [/\/Team:Cambridge\/Society\/report/i, 'report'],
     [/\/Team:Cambridge\/Society\/interviews/i, 'interviews'],
     [/\/Team:Cambridge\/Society\/interviews/i, 'interviews'],
     [/\/Team:Cambridge\/Society/i, 'overview'],
     [/\/Team:Cambridge\/Society/i, 'overview'],
Line 145: Line 142:
   if(path == undefined)
   if(path == undefined)
var path = location.pathname;
var path = location.pathname;
-
 
 
var ar = get_main_area(path);
var ar = get_main_area(path);
var sub = get_sub_area(path, ar);
var sub = get_sub_area(path, ar);
-
 
return [ar, sub];
return [ar, sub];
}
}
Line 169: Line 164:
var colorize_links = function($container) //colourise all the links in the container based on where the go
var colorize_links = function($container) //colourise all the links in the container based on where the go
{
{
-
  console.log('colorize_links');
 
   $container.find('a').each( function(i) {
   $container.find('a').each( function(i) {
-
    console.log('  ' + i);
 
     var $this = $(this);
     var $this = $(this);
 +
    if($this.hasClass('cam-color-exempt'))
 +
      return;
     var ar = '';
     var ar = '';
     if(is_external($this))
     if(is_external($this))
Line 181: Line 176:
     $(this).addClass('cam-' + ar);
     $(this).addClass('cam-' + ar);
   });
   });
-
  console.log('done');
 
}
}
Line 187: Line 181:
var show_loading = function() {
var show_loading = function() {
-
   //only show the loading bar if it's taking more than 250ms to display
+
   //only show the loading bar if it's taking more than 500ms to display
-
   loading_timeout = setTimeout("$('#cam_loading').slideDown('fast');", 250);
+
   loading_timeout = setTimeout("$('#cam_loading').slideDown('fast');", 500);
}
}
Line 201: Line 195:
     url = '/Team:Cambridge';
     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';
   var load = url + '?action=render';
-
  console.log('loading: ' + url);
+
 
   show_loading();
   show_loading();
   $.ajax({
   $.ajax({
Line 208: Line 211:
     'dataType': 'html',
     'dataType': 'html',
     'error': function(jqXHR, textStatus, errorThrown) {
     'error': function(jqXHR, textStatus, errorThrown) {
 +
      hide_loading();
 +
      set_hash(url);
       var $c = $('<h2 class="error">AjaxError</h2><h4>Got error "' + errorThrown + '" while attempting to load url "' + load + '"</h4>');
       var $c = $('<h2 class="error">AjaxError</h2><h4>Got error "' + errorThrown + '" while attempting to load url "' + load + '"</h4>');
       $('#cam_pagecontent').hide().html($c).show();
       $('#cam_pagecontent').hide().html($c).show();
       window.scrollTo(0,0);
       window.scrollTo(0,0);
-
      hide_loading();
 
     },
     },
     'success': function(data){
     'success': function(data){
Line 219: Line 223:
       window.scrollTo(0,0);
       window.scrollTo(0,0);
       hide_loading();
       hide_loading();
 +
      if(after_hash != '')
 +
        window.location.hash = after_hash;
 +
      set_hash(url);
 +
     
 +
      //load the parts if we're on the parts page. This is hacky but I don't give a fark
 +
      if((area == 'overview') && (subarea == 'parts'))
 +
      {
 +
        $('#groupparts').load('/cgi/api/groupparts.cgi?t=iGEM011\&amp;g=Cambridge',function(){ $('#groupparts .tablesorter').tablesorter();} );
 +
      }
     },
     },
   });
   });
-
  set_hash(url);
 
   set_area(url);
   set_area(url);
   $('#cam_desc').introduction('setArea', area, subarea);
   $('#cam_desc').introduction('setArea', area, subarea);
-
   update_links();
+
   update_links(url);
};
};
var wiki_main = function() {
var wiki_main = function() {
-
console.log('static/wiki.js::$(window).ready()');
+
  $('.cam-menu-item a').mouseenter( function(event) {//in
-
$('.cam-menu-item a').mouseenter( function(event) {//in
+
$(event.currentTarget).next().children('.cam-menu-drop').slideDown('fast');
-
$(event.currentTarget).next().children('.cam-menu-drop').slideDown('fast');
+
  });
-
});
+
  $('.cam-menu-item').mouseleave( function(event) {//out
-
$('.cam-menu-item').mouseleave( function(event) {//out
+
    $(event.currentTarget).find('.cam-menu-drop').slideUp('fast');
-
$(event.currentTarget).find('.cam-menu-drop').slideUp('fast');
+
  }).click( function(event) {//hide on click
-
});
+
    $(event.currentTarget).find('.cam-menu-drop').slideUp('fast');
 +
  });
-
$('.cam-social-link').hover(
+
  $('.cam-social-link').hover(
-
function(event) {;
+
    function(event) {
-
$(this).children().animate({top:0}, 75);
+
      $(this).children().animate({top:0}, 75);
-
},
+
    },
-
function(event) {
+
    function(event) {
-
$(this).children().animate({'top':12}, 250);
+
      $(this).children().animate({'top':12}, 250);
-
}
+
    }
-
).click( function() {
+
  ).click( function() {
-
  window.location = $(this).attr('href');
+
    window.location = $(this).attr('href');
-
});
+
  });
-
+
-
set_area();
+
-
    update_links();
+
   $('a').live('click', function(event) {
   $('a').live('click', function(event) {
 +
    if(event.ctrlKey) return true;
     if(is_internal(this))
     if(is_internal(this))
     {
     {
       event.preventDefault();
       event.preventDefault();
-
       var new_url = $(this).attr('href');
+
       load_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 false;
     }
     }
Line 270: Line 279:
}
}
);
);
-
+
 
-
int = window.setInterval('check_hash();', 500);
+
  int = window.setInterval('check_hash();', 500);
-
console.log('static/wiki.js::$(window).ready() Return');
+
  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);
 +
    colorize_links($('#wiki_content'));
 +
    $('#cam_desc').introduction('setArea', area, subarea);
 +
  }
 +
  else
 +
  {
 +
    load_url(get_hash());
 +
  }
}
}

Latest revision as of 03:25, 22 September 2011

/*

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. 
  • /

//current area/subarea var area = 'overview'; var subarea = 'home'; //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:\/\/2011.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://2011.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');
 $('#edit-intro-link').attr('href', '/Team:Cambridge/desc/' + area + '/' + subarea + '?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;
 return /\/Team:Cambridge/i.test($(obj).attr("href"));

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

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\/Parts/i, 'parts'],
   [/\/Team:Cambridge\/Blog/i, 'blog'],
   [/./i, 'home'],

],

 'project':  [
   [/\/Team:Cambridge\/Project\/Background/i, 'background'],
   [/\/Team:Cambridge\/Project\/Microscopy/i, 'microscopy'],
   [/\/Team:Cambridge\/Project\/in_vitro/i, 'invitro'],
   [/\/Team:Cambridge\/Project\/in_vivo/i, 'invivo'],
   [/\/Team:Cambridge\/Project\/gibthon/i, 'gibthon'],
   [/\/Team:Cambridge\/Project\/future/i, 'future'],
   [/\/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\/report/i, 'report'],
   [/\/Team:Cambridge\/Society\/interviews/i, 'interviews'],
   [/\/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_main_area = function(path) {

 for ( var a in areas)

{ if(areas[a][0].test(path)) { return areas[a][1]; } }

 return 'default';

}

var get_sub_area = function(path, area) {

 if(area == 'default') return 'default';
 for ( var a in subareas[area])

{ if(subareas[area][a][0].test(path)) { return subareas[area][a][1]; } }

 return 'overview';

}

var get_area = function(path) { //figure out which area of the site we are in

 if(path == undefined)

var path = location.pathname; var ar = get_main_area(path); var sub = get_sub_area(path, ar); 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 colorize_links = function($container) //colourise all the links in the container based on where the go {

 $container.find('a').each( function(i) {
   var $this = $(this);
   if($this.hasClass('cam-color-exempt'))
     return;
   var ar = ;
   if(is_external($this))
     ar = 'ext';
   else 
     ar = get_main_area($this.attr('href'));
   
   $(this).addClass('cam-' + ar);
 });

}

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();	
     colorize_links($('#cam_pagecontent'));
     window.scrollTo(0,0);
     hide_loading();
     if(after_hash != )
       window.location.hash = after_hash;
     set_hash(url);
     
     //load the parts if we're on the parts page. This is hacky but I don't give a fark
     if((area == 'overview') && (subarea == 'parts'))
     {
       $('#groupparts').load('/cgi/api/groupparts.cgi?t=iGEM011\&g=Cambridge',function(){ $('#groupparts .tablesorter').tablesorter();} );
     }
   },
 });
 set_area(url);
 $('#cam_desc').introduction('setArea', area, subarea);
 update_links(url);

};

var wiki_main = function() {

 $('.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');
 }).click( function(event) {//hide on click
   $(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');
 });
 $('a').live('click', function(event) {
   if(event.ctrlKey) return true;
   if(is_internal(this))
   {
     event.preventDefault();
     load_url($(this).attr('href'));
     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);
 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);
   colorize_links($('#wiki_content'));
   $('#cam_desc').introduction('setArea', area, subarea);
 }
 else
 {
   load_url(get_hash());
 }

}