Team:EPF Lausanne/template/script.js
From 2013.igem.org
(Difference between revisions)
Line 41: | Line 41: | ||
var obj = $(row); | var obj = $(row); | ||
- | |||
if (obj.hasClass("image-row-loaded")) { | if (obj.hasClass("image-row-loaded")) { | ||
return; | return; | ||
} | } | ||
- | |||
obj.css({ | obj.css({ | ||
Line 60: | Line 58: | ||
images.each(function() { | images.each(function() { | ||
$(this).load(function() { | $(this).load(function() { | ||
- | |||
EPFL.setupImageRow(row); | EPFL.setupImageRow(row); | ||
}); | }); | ||
Line 69: | Line 66: | ||
allLoaded = false; | allLoaded = false; | ||
} | } | ||
- | |||
total += w/h; | total += w/h; | ||
}); | }); | ||
- | |||
var height = Math.floor(width / total); | var height = Math.floor(width / total); | ||
Line 79: | Line 74: | ||
images.each(function() { | images.each(function() { | ||
var w = $(this).width(); | var w = $(this).width(); | ||
+ | var h = $(this).height(); | ||
$(this).css({ | $(this).css({ | ||
- | width: w*height+"px", | + | width: w/h*height+"px", |
height: height+"px" | height: height+"px" | ||
}); | }); | ||
- | |||
}); | }); | ||
- | |||
obj.addClass("image-row-loaded"); | obj.addClass("image-row-loaded"); | ||
} | } |
Revision as of 12:27, 3 October 2013
//Generate private namespace window.EPFL = (function(){
//Little hack to stop MediaWiki from replacing ampersands... var amp = unescape("%26"); return { init: function(){ EPFL.setupTitle(); EPFL.setupTOC(); EPFL.startTaxi(); EPFL.setupNotebookNavButtons(); EPFL.setupImageRows(); $('p') .filter(function(){ return $.trim($(this).text()) === && $.trim($(this).html()).length < 10; }) .remove(); }, dateRegex: /^([0-9]+) ([A-Z][a-z]+) ([0-9]+)$/, onLoad: function() { }, 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; }, setupImageRows: function(){ $(".image-row").each(function() { var obj = $(this); console.log(obj); EPFL.setupImageRow(obj); }); }, setupImageRow: function(row){ var obj = $(row); if (obj.hasClass("image-row-loaded")) { return; } obj.css({ width: "100%", position: "relative" }); var width = obj.width(); var images = $("img", obj); var total = 0; var allLoaded = true; images.each(function() { $(this).load(function() { EPFL.setupImageRow(row); }); var h = $(this).height(); var w = $(this).width(); if (isNaN(w)) { allLoaded = false; } total += w/h; }); var height = Math.floor(width / total); if (!isNaN(height) && allLoaded) { images.each(function() { var w = $(this).width(); var h = $(this).height(); $(this).css({ width: w/h*height+"px", height: height+"px" }); }); obj.addClass("image-row-loaded"); } }, setupNotebookNavButtons: function(){ var months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; //Note: Valid for 2013, if you're reusing this script, update this: var days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var exists = function(day, month, year){ return year == 2013 && month >= 7 && month <= 11; }; 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){ if(exists(d[0], d[1], d[2])){ return d[0]+" "+months[d[1]]+" "+d[2]; }else{ return ""; } } if(EPFL.getPage().length == 2 && EPFL.getPage()[0] == "Calendar"){ 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 prevDate = getCorrectDate(day-1, month, year); while(prevDate[2] == 2013 && !exists(prevDate[0], prevDate[1], prevDate[2])){ prevDate = getCorrectDate(prevDate[0]-1, prevDate[1], prevDate[2]); } var nextDate = getCorrectDate(day+1, month, year); while(nextDate[2] == 2013 && !exists(nextDate[0], nextDate[1], nextDate[2])){ nextDate = getCorrectDate(nextDate[0]+1, nextDate[1], nextDate[2]); } var prev = buildString(prevDate); var next = buildString(nextDate); $(".page-content").prepend($("
")
.css({"clear": "both"})); if(prev.length > 0){ $(".page-content").prepend( $("<a>") .css({"float": "left"}) .attr("href", "/Team:EPF_Lausanne/Calendar/"+prev) .html("\u2190 "+prev)); } if(next.length > 0){ $(".page-content").prepend( $("<a>") .css({"float": "right"}) .attr("href", "/Team:EPF_Lausanne/Calendar/"+next) .html(next+" \u2192")); } } } }, setupTOC: function(){ var toc = $("#toc"); toc.remove(); $(".paper").prepend(toc); }, 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); }, startTaxi: function() { var taxi = $("#taxi"); var menu = $($("nav")[0]); var DURATION = 400; var FLIP_DURATION = 200; var EASING = "swing"; var DELAY = 800; var OFFSET = 30; var animate = true; $("#taxi").css({perspective: "400px"}); var base = $(menu.find(".menu-item")[0]); var current = $(menu.find(".menu-item")[0]); var currentTarget = null; var facingRight = true; var resizedWindow = false; current.addClass("menu-item-selected"); $(window).resize(function() { resizedWindow = true; gotoMenuItem(current); }); var returnTimeout = null; var gotoMenuItem = function(item, returningToBase) { if (!resizedWindow && !returningToBase && current[0] == item[0]){ current.removeClass("menu-item-selected"); current = item; if(!returningToBase) { item.addClass("menu-item-selected"); } return; } if (!resizedWindow && currentTarget != null && item[0] == currentTarget[0]) return; current.removeClass("menu-item-selected"); current = item; if(!returningToBase) { current.addClass("menu-item-selected"); } var currentPosition = $("#taxi").position().left; var menuItemPosition = item.position().left; var targetPosition = menuItemPosition + OFFSET; var flipNeeded = !facingRight; var flipCSS = {transform: "scale(1,1)", "-webkit-transform": "scale(1,1)", "-moz-transform": "scale(1,1)", "-ms-transform": "scale(1,1)", "-o-transform": "scale(1,1)", filter: "none"}; var flipCSS_anim = 0; var willBeFacingRight = true; if(currentPosition > targetPosition) { flipCSS = {transform: "scale(-1,1)", "-webkit-transform": "scale(-1,1)", "-moz-transform": "scale(-1,1)", "-ms-transform": "scale(-1,1)", "-o-transform": "scale(-1,1)", filter: "fliph"}; flipCSS_anim = 180; flipNeeded = facingRight; willBeFacingRight = false; } facingRight = willBeFacingRight; resizedWindow = false; if (animate) { $("#taxi").stop(true, false); currentTarget = item; if (flipNeeded) { $("#taxi").animate({"text-indent": flipCSS_anim}, { duration: FLIP_DURATION, easing: EASING, complete: function(){ $("#taxi").css(flipCSS); }, step: function(now,fx) { $(this).css({transform: "rotateY( "+now+"deg )", "-webkit-transform": "rotateY( "+now+"deg )", "-moz-transform": "rotateY( "+now+"deg )", "-ms-transform": "rotateY( "+now+"deg )", "-o-transform": "rotateY( "+now+"deg )", filter: "none"}); } }); } else { $("#taxi").css(flipCSS); } $("#taxi").animate({left: targetPosition+"px"}, { duration: DURATION, easing: EASING, complete: function() { currentTarget = null; } }); } else { $("#taxi").css(flipCSS); $("#taxi").css({left: targetPosition+"px"}); } }; var returnToBase = function() { gotoMenuItem(base, true); returnTimeout = null; } $(".menu-item").mouseover(function() { var obj = $(this); while(obj[0] != undefined && obj[0].tagName.toLowerCase() != "div") { obj = obj.parent(); } if(!obj.hasClass("menu-item")) return; if(returnTimeout != null) { clearTimeout(returnTimeout); returnTimeout = null; } gotoMenuItem(obj); }).mouseout(function() { if(!$(this).hasClass("menu-item")) return; if(returnTimeout == null) { returnTimeout = setTimeout(returnToBase, DELAY); } }); animate = false; returnToBase(); animate = true; } };
}()); /* if(!window.console){
//Dummy console... window.console = { log: function(){ } };
}*/
$(function(){
EPFL.onLoad();
});
var oldRunOnloadHook = runOnloadHook; runOnloadHook = function(){
EPFL.init(); if (oldRunOnloadHook) oldRunOnloadHook();};