Team:EPF-Lausanne/Template/script.js
From 2012.igem.org
Line 1: | Line 1: | ||
//Generate private namespace | //Generate private namespace | ||
- | + | window.EPFL = (function(){ | |
//Little hack to stop MediaWiki from replacing ampersands... | //Little hack to stop MediaWiki from replacing ampersands... | ||
var amp = unescape("%26"); | var amp = unescape("%26"); | ||
Line 117: | Line 117: | ||
}()); | }()); | ||
- | + | if(!window.console){ | |
+ | //Dummy console... | ||
+ | window.console = { | ||
+ | log: function(){ } | ||
+ | }; | ||
+ | } | ||
+ | |||
+ | window.Doodles = (function(){ | ||
var SPACE_MIN = 20; | var SPACE_MIN = 20; | ||
var SPACE_MAX = 100; | var SPACE_MAX = 100; | ||
Line 133: | Line 140: | ||
return { | return { | ||
onLoad: function(){ | onLoad: function(){ | ||
+ | console.log("Doodle onload"); | ||
contentHeight = $(".page-content").height() - 50; | contentHeight = $(".page-content").height() - 50; | ||
doodleBars = $(".doodle-bar"); | doodleBars = $(".doodle-bar"); | ||
Line 140: | Line 148: | ||
}, | }, | ||
addDoodles: function(){ | addDoodles: function(){ | ||
+ | console.log("Adding doodles to left"); | ||
for(var i = 0; i < MAX_DOODLES_PER_BAR; i++){ | for(var i = 0; i < MAX_DOODLES_PER_BAR; i++){ | ||
if(!this.addDoodle(0)) break; | if(!this.addDoodle(0)) break; | ||
} | } | ||
+ | console.log("Adding doodles to right"); | ||
for(var i = 0; i < MAX_DOODLES_PER_BAR; i++){ | for(var i = 0; i < MAX_DOODLES_PER_BAR; i++){ | ||
if(!this.addDoodle(1)) break; | if(!this.addDoodle(1)) break; | ||
Line 148: | Line 158: | ||
}, | }, | ||
addDoodle: function(barNumber){ | addDoodle: function(barNumber){ | ||
+ | console.log("Trying to add doodle to", barNumber); | ||
var bar = $(doodleBars[barNumber]); | var bar = $(doodleBars[barNumber]); | ||
var usage = $(barUsage[barNumber]); | var usage = $(barUsage[barNumber]); | ||
+ | console.log("Current usage vs contentHeight: ", usage.max, contentHeight); | ||
if(usage.max > contentHeight) return false; | if(usage.max > contentHeight) return false; | ||
Line 156: | Line 168: | ||
var nextImage = all[Math.floor(Math.random()*all.length)]; | var nextImage = all[Math.floor(Math.random()*all.length)]; | ||
+ | console.log("Next image: ", nextImage); | ||
if(usage.max + nextDistance + nextImage.height > contentHeight) return false; | if(usage.max + nextDistance + nextImage.height > contentHeight) return false; | ||
- | $("<img>") | + | console.log("Adding image"); |
- | + | ||
- | + | bar.append( | |
- | + | $("<img>") | |
- | + | .attr("src", nextImage.src) | |
- | + | .attr("width", WIDTH) | |
- | + | .attr("height", nextImage.height) | |
+ | .css({ | ||
+ | "margin-top": nextDistance+"px" | ||
+ | }) | ||
+ | ); | ||
usage.max += nextDistance + nextImage.height; | usage.max += nextDistance + nextImage.height; | ||
Revision as of 21:12, 5 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]*$/, 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); }, setup: function(){ EPFL.setupProtocols(); EPFL.setupCalendar(); EPFL.setupPlanning(); EPFL.setupTitle(); EPFL.setupTOC(); $('p') .filter(function(){ return $.trim($(this).text()) === && $.trim($(this).html()).length < 10; }) .remove(); }, setupTOC: function(){ var toc = $("#toc"); toc.remove(); $(".paper").prepend(toc); }, setupPlanning: function(){ if(location.href.match(/Team:EPF-Lausanne\/Planning/ig)){ $(".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(){ 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 = $($(".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 == ""){ result = "Home"; }
$("#pageTitle").text(result); } }; }());
if(!window.console){ //Dummy console... window.console = { log: function(){ } }; }
window.Doodles = (function(){ var SPACE_MIN = 20; var SPACE_MAX = 100; var WIDTH = 120; var MAX_DOODLES_PER_BAR = 20;
var all = [ { height: 114, src: "" } ];
var contentHeight = 0; var doodleBars = []; var barUsage = [{max: 0, images: []}, {max: 0, images: []}];
return { onLoad: function(){ 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"); 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){ 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;
var nextDistance = SPACE_MIN + Math.floor(Math.random()*(SPACE_MAX-SPACE_MIN+1)); var nextImage = all[Math.floor(Math.random()*all.length)];
console.log("Next image: ", nextImage); if(usage.max + nextDistance + nextImage.height > contentHeight) return false;
console.log("Adding image");
bar.append( $("<img>") .attr("src", nextImage.src) .attr("width", WIDTH) .attr("height", nextImage.height) .css({ "margin-top": nextDistance+"px" }) ); usage.max += nextDistance + nextImage.height;
return true; } }; }());
runOnloadHook = function(){ EPFL.setup(); };