Team:EPF-Lausanne/Template/script.js
From 2012.igem.org
(Difference between revisions)
Line 97: | Line 97: | ||
var next = buildString(getCorrectDate(day+1, month, year)); | var next = buildString(getCorrectDate(day+1, month, year)); | ||
+ | $(".page-content").prepend( | ||
+ | $("<div>") | ||
+ | .css({"clear": "both"})); | ||
+ | |||
$(".page-content").prepend( | $(".page-content").prepend( | ||
$("<a>") | $("<a>") | ||
.css({"float": "left"}) | .css({"float": "left"}) | ||
.attr("href", "/Team:EPF-Lausanne/Notebook/"+prev) | .attr("href", "/Team:EPF-Lausanne/Notebook/"+prev) | ||
- | . | + | .html(amp+"larrow; "+prev)); |
$(".page-content").prepend( | $(".page-content").prepend( | ||
Line 107: | Line 111: | ||
.css({"float": "right"}) | .css({"float": "right"}) | ||
.attr("href", "/Team:EPF-Lausanne/Notebook/"+next) | .attr("href", "/Team:EPF-Lausanne/Notebook/"+next) | ||
- | . | + | .html(next+" "+amp+"rarrow;")); |
} | } | ||
} | } |
Revision as of 22:09, 23 September 2012
//Generate private namespace window.EPFL = (function(){
//Little hack to stop MediaWiki from replacing ampersands... var amp = unescape("%26"); return { namingRegex: /^(\/|([A-Z][a-z]*))*[0-9]*$/, dateRegex: /^([0-9]+) ([A-Z][a-z]+) ([0-9]+)$/, registerNewPageButton: function(titleID, buttonID, base, preload, check){ $(buttonID).click(function(){ if(check != undefined){ if(!check(base, $(titleID).val())){ if(!confirm("The title doesn't seem to follow the naming conventions.\nAre you sure you want to create this page?")){ return; } } } window.location.href = "/wiki/index.php?title="+base+"/"+$(titleID).val()+amp+"action=edit"+amp+"preload="+preload; }); }, checkNamingConventions: function(base, title){ return EPFL.namingRegex.test(title); }, getPage: function(){ var title = $(".firstHeading").text(); var prefix = "Team:EPF-Lausanne/"; title = title.substr(prefix.length); titleParts = title.split("/"); if(titleParts.length > 0 && titleParts[0] == "Template"){ titleParts.shift(); } return titleParts; }, setup: function(){ EPFL.setupProtocols(); EPFL.setupCalendar(); EPFL.setupPlanning(); EPFL.setupTitle(); EPFL.setupTOC(); EPFL.setupNotebookNavButtons(); $('p') .filter(function(){ return $.trim($(this).text()) === && $.trim($(this).html()).length < 10; }) .remove(); }, setupTOC: function(){ var toc = $("#toc"); toc.remove(); $(".paper").prepend(toc); }, setupNotebookNavButtons: function(){ var months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; //Note: Valid for 2012, if you're reusing this script, update this: var days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var getCorrectDate = function(day, month, year){ if(day < 1){ month--; day = days[month]; } if(day > days[month]){ month++; day = 1; } if(month < 1){ year--; month = 12; } if(month > 12){ year++; month = 1; } return [day, month, year]; } var buildString = function(d){ return d[0]+" "+months[d[1]]+" "+d[2]; } if(EPFL.getPage().length == 2 && EPFL.getPage()[0] == "Notebook"){ var date = EPFL.getPage()[1]; var parts = EPFL.dateRegex.exec(date); if(parts != null){ var day = parseInt(parts[1], 10); var month = 6; for(var i = 1; i < 12; i++){ if(months[i] == parts[2]){ month = i; } } var year = parseInt(parts[3], 10); var prev = buildString(getCorrectDate(day-1, month, year)); var next = buildString(getCorrectDate(day+1, month, year)); $(".page-content").prepend($("
")
.css({"clear": "both"})); $(".page-content").prepend( $("<a>") .css({"float": "left"}) .attr("href", "/Team:EPF-Lausanne/Notebook/"+prev) .html(amp+"larrow; "+prev)); $(".page-content").prepend( $("<a>") .css({"float": "right"}) .attr("href", "/Team:EPF-Lausanne/Notebook/"+next) .html(next+" "+amp+"rarrow;")); } } }, setupPlanning: function(){ if(EPFL.getPage()[0] == "Planning"){ $(".toctext, .mw-headline").each(function(){ var text = $(this).text().trim(); if(text.substr(0, 1) == "\u2713"){ $(this).addClass("planning-finished"); }else{ var parts = text.split(" - "); if(parts.length == 1){ $(this).addClass("planning-todo"); }else{ $(this).addClass("planning-taken"); } } }); } }, setupProtocols: function(){ var hide = false; $(".protocol-title").each(function(){ if($.trim($(this).parent().parent().attr("class")) != "protocol-remove-style"){ hide = true; var link = $("<a>") .attr("href", "#") .click( function(){ $(this).parent().parent().children().last().toggle() return false; }); var children = $(this).children().children(); $(this).empty(); link.append(children); $(this).append(link); }else{ var txt = $(this).text(); $(this).empty(); $(this).text(txt); } }); if(hide) $(".protocol-inner-box").hide(); }, setupCalendar: function(){ if(EPFL.getPage().length == 1 && EPFL.getPage()[0] == "Notebook"){ var toAdd = amp+"preload=Team:EPF-Lausanne/Notebook/Template/NewEntry"; $(".month tr td a") .filter( function(){ return $(this).attr("href").length > 80; }) .each( function(){ $(this).attr("href", $(this).attr("href")+toAdd); } ); } }, setupTitle: function(){ var title = ""; if(window.titleOverride !== undefined && window.titleOverride !== "" && window.titleOverride !== " " && window.titleOverride !== "{{{1}}}"){ title = window.titleOverride; }else{ var title = $($(".firstHeading")[0]).text(); var prefix = "Team:EPF-Lausanne/"; title = title.substr(prefix.length); titleParts = title.split("/"); switch(titleParts[0]){ case "Template": titleParts.shift(); break; } var result = titleParts.join(" "); if(titleParts.length > 1){ result = titleParts.shift()+": "+titleParts.join(" "); } if(result == ""){ result = "Home"; } title = result; } $("#pageTitle").text(title); } };
}());
if(!window.console){
//Dummy console... window.console = { log: function(){ } };
}
window.Doodles = (function(){
var SPACE_MIN = 80; var SPACE_MAX = 400; var WIDTH = 120; var MAX_DOODLES_PER_BAR = 100; var MAX_RETRIES = 100; var all = [ //{ height: 114, src: "" }, { height: 27, src: "" }, { height: 131, src: "" }, { height: 87, src: "" }, { height: 138, src: "" }, { height: 76, src: "" }, { height: 156, src: "" }, { height: 175, src: "" }, { height: 141, src: "" }, { height: 71, src: "" }, { height: 128, src: "" }, { height: 98, src: "" } ]; var isHomePage = function(){ var title = $($(".firstHeading")[0]).text(); var prefix = "Team:EPF-Lausanne/"; title = title.substr(prefix.length); titleParts = title.split("/"); switch(titleParts[0]){ case "Template": titleParts.shift(); break; } var result = titleParts.join(" "); if(result == ""){ return true; } return false; } var contentHeight = 0; var doodleBars = []; var barUsage = [{max: 0, images: []}, {max: 0, images: []}]; var previous = undefined; var firstLoad = true; return { onLoad: function(){ if(firstLoad){ var that = this; setTimeout(function(){ that.onLoad(); }, 1000); firstLoad = false; } //console.log("Doodle onload"); contentHeight = $(".page-content").height() - 50; doodleBars = $(".doodle-bar"); //doodleBars.height(contentHeight); this.addDoodles(); }, addDoodles: function(){ //console.log("Adding doodles to left"); if(isHomePage()){ this.addDoodle(1, 400); } //Add a doodle at one of the sides (random) at y = 0 this.addDoodle(Math.round(Math.random()), 0); for(var i = 0; i < MAX_DOODLES_PER_BAR; i++){ if(!this.addDoodle(0)) break; } //console.log("Adding doodles to right"); for(var i = 0; i < MAX_DOODLES_PER_BAR; i++){ if(!this.addDoodle(1)) break; } }, addDoodle: function(barNumber, nextDistance){ //console.log("Trying to add doodle to", barNumber); var bar = $(doodleBars[barNumber]); var usage = barUsage[barNumber]; //console.log("Current usage vs contentHeight: ", usage.max, contentHeight); if(usage.max > contentHeight) return false; if(nextDistance === undefined) nextDistance = SPACE_MIN + Math.floor(Math.random()*(SPACE_MAX-SPACE_MIN+1)); var nextImage = all[Math.floor(Math.random()*all.length)]; for(var i = 0; i < MAX_RETRIES; i++){ if(previous == nextImage){ nextImage = all[Math.floor(Math.random()*all.length)]; }else{ break; } } if(previous == nextImage) return false; previous = nextImage; if(usage.max + nextDistance + nextImage.height > contentHeight) return false;var container = $("
")
").attr("class", "left").append($("")));
for(var i = 0; i < subMenu.length; i++){
sub.append($(" ")
.append($("<a>")
.attr("href", subMenu[i][1])
.append($("").text(subMenu[i][0]))));
}
sub.append($(" ").attr("class", "right").append($("")));
var center = positions[current];
width = sub.width();
var x = 330 + center - width/2;
var dx = 0;
if(width > 660){
dx = -center;
}else if(x < -40){
dx = -x - 40;
}else if(x + width > 660){
dx = 660 - width - x;
}
sub.css({"position": "relative", "left": (center+dx)+"px"});
}
}
};
})();
$(function(){
Menu.onLoad();
Doodles.onLoad();
});
runOnloadHook = function(){
EPFL.setup();
};
.css({ "background": "url('"+nextImage.src+"')", "width": WIDTH+"px", "height": nextImage.height+"px", "margin-top": nextDistance+"px" }); bar.append(container); /* var img = $("<img>") .load(function(){ container.append(img); }) .attr("width", WIDTH) .attr("height", nextImage.height) .attr("src", nextImage.src);*/ usage.max += nextDistance + nextImage.height; return true; } };
})();
window.Menu = (function(){
var items = ["introduction", "project", "notebook", "human", "acknowledgments"]; var positions = {"introduction": -300, "project": -170, "notebook": -60, "human": 75, "acknowledgments": 240}; var subMenus = { introduction: [ ["Home", "/Team:EPF-Lausanne"], ["Team", "/Team:EPF-Lausanne/Team"], ["Photos", "/Team:EPF-Lausanne/Photos"] ], project: [ ["Project Contents", "/Team:EPF-Lausanne/Project"], ["Results", "/Team:EPF-Lausanne/Results"], ["Parts Submitted to the Registry", "https://2012.igem.org/Team:EPF-Lausanne/Parts"], ["Modeling", "/Team:EPF-Lausanne/Modeling"] ], notebook: [ ["Notebook", "/Team:EPF-Lausanne/Notebook"], ["Protocols", "/Team:EPF-Lausanne/Protocol"] ], human: [ ["Talks", "/Team:EPF-Lausanne/Human/Talks"], ["Survey", "/Team:EPF-Lausanne/Human/Survey"], ["Primer Design", "/Team:EPF-Lausanne/PrimerDesignHelper"], ["Cell Creator", "/Team:EPF-Lausanne/CellCreator"], ["Safety", "/Team:EPF-Lausanne/Safety"], ], acknowledgments: [ ["Attributions", "/Team:EPF-Lausanne/Acknowledgments"], ["Sponsors", "/Team:EPF-Lausanne/Sponsors"] ] }; // Default to notebook var current = "notebook"; var leaveTimer = undefined; var leaveMenu = function(){ current = window.menu != "" ? window.menu.toLowerCase() : "notebook"; window.Menu.update(); }; var attachEvents = function(name){ $(".menu-"+name).mouseover(function(){ if(leaveTimer !== undefined){ clearTimeout(leaveTimer); leaveTimer = undefined; } current = name; window.Menu.update(); }); $(".menu-"+name).mouseout(function(){ if(leaveTimer === undefined){ leaveTimer = setTimeout(leaveMenu, 1000); } }); }; return { onLoad: function(){ // Default to notebook if(window.menu === undefined || window.menu === "" || window.menu === " " || window.menu === "{{{1}}}"){ window.menu = "notebook"; } current = window.menu != "" ? window.menu.toLowerCase() : "notebook"; for(var i = 0; i < items.length; i++){ attachEvents(items[i]); } $(".sub-menu").mouseover(function(){ if(leaveTimer !== undefined){ clearTimeout(leaveTimer); leaveTimer = undefined; } }); $(".sub-menu").mouseout(function(){ if(leaveTimer === undefined){ leaveTimer = setTimeout(leaveMenu, 1000); } }); $(".paper-arrow").show(); window.Menu.update(); }, update: function(){ if(current === undefined || current === "" || current === " "|| current === "{{{1}}}" || !positions[current]){ $(".paper-arrow").hide(); }else{ $(".paper-arrow").show(); var newPos = positions[current]+"px"; $(".paper-arrow").css({left: newPos}); } this.updateSubMenu(); }, updateSubMenu: function(){ var subMenu = subMenus[current]; var sub = $(".sub-menu .simple-navi ul"); sub.empty(); if(subMenu !== undefined && subMenu.length > 0){sub.append($("