Team:EPF-Lausanne/template/script.js

From 2013.igem.org

(Difference between revisions)
(Created page with "//Generate private namespace window.EPFL = (function(){ //Little hack to stop MediaWiki from replacing ampersands... var amp = unescape("%26"); return { init: function(...")
Line 14: Line 14:
     },
     },
     onLoad: function() {
     onLoad: function() {
-
        
+
       $(".main-item").on
     },
     },
     setupTOC: function(){
     setupTOC: function(){
Line 50: Line 50:
        
        
       $("#pageTitle").text(title);
       $("#pageTitle").text(title);
 +
    },
 +
    startTaxi: function() {
 +
      var taxi = $("#taxi");
 +
      var menu = $($(".menu")[0]);
 +
     
 +
      var DURATION = 400;
 +
      var EASING = "swing";
 +
      var DELAY = 800;
 +
      var OFFSET = 15;
 +
     
 +
      var base = $(menu.find(".main-item")[0]);
 +
      var current = $(menu.find(".main-item")[0]);
 +
     
 +
      current.addClass("menu-item-selected");
 +
     
 +
      var returnTimeout = null;
 +
      var gotoMenuItem = function(item) {
 +
        if (current == item) return;
 +
        current.removeClass("menu-item-selected");
 +
        current = item;
 +
        current.addClass("menu-item-selected");
 +
        var menuPosition = 0;//menu.position().left;
 +
        var currentPosition = $("#taxi").position().left
 +
        var menuItemPosition = item.position().left;
 +
       
 +
        var targetPosition = menuItemPosition - menuPosition + OFFSET;
 +
       
 +
        $("#taxi").stop(true, false);
 +
        $("#taxi").animate({left: targetPosition+"px"}, {
 +
          duration: DURATION,
 +
          easing: EASING
 +
        });
 +
       
 +
        if(currentPosition-menuPosition <= targetPosition) {
 +
          $("#taxi").css({transform: "scale(1,1)", filter: ""});
 +
        } else {
 +
          $("#taxi").css({transform: "scale(-1,1)", filter: "fliph"});
 +
        }
 +
      };
 +
      var returnToBase = function() {
 +
        gotoMenuItem(base);
 +
        returnTimeout = null;
 +
      }
 +
     
 +
      $(".menu-item").mouseover(function() {
 +
        if(!$(this).hasClass("main-item")) return;
 +
       
 +
        if(returnTimeout != null) {
 +
          clearTimeout(returnTimeout);
 +
          returnTimeout = null;
 +
        }
 +
       
 +
        gotoMenuItem($(this));
 +
      }).mouseout(function() {
 +
        if(!$(this).hasClass("main-item")) return;
 +
       
 +
        if(returnTimeout == null) {
 +
          returnTimeout = setTimeout(returnToBase, DELAY);
 +
        }
 +
      });
     }
     }
   };
   };
Line 60: Line 120:
   };
   };
}
}
-
 
-
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: "https://static.igem.org/mediawiki/2012/6/61/Team-EPF-Lausanne_Template_Doodle_Placeholder.png" },
 
-
    { height: 27, src: "https://static.igem.org/mediawiki/2012/9/9b/Team-EPF-Lausanne_Doodle_Switch.png" },
 
-
    { height: 131, src: "https://static.igem.org/mediawiki/2012/3/3b/Team-EPF-Lausanne_Doodle_Graph.png" },
 
-
    { height: 87, src: "https://static.igem.org/mediawiki/2012/f/f6/Team-EPF-Lausanne_Doodle_Circuit.png" },
 
-
    { height: 138, src: "https://static.igem.org/mediawiki/2012/f/f4/Team-EPF-Lausanne_Doodle_Centrifuge.png" },
 
-
    { height: 76, src: "https://static.igem.org/mediawiki/2012/4/49/Team-EPF-Lausanne_Doodle_Agarose.png" },
 
-
 
 
-
 
 
-
    { height: 156, src: "https://static.igem.org/mediawiki/2012/7/7f/LED.png" },
 
-
    { height: 175, src: "https://static.igem.org/mediawiki/2012/a/a2/Pipette.png" },
 
-
    { height: 141, src: "https://static.igem.org/mediawiki/2012/d/d2/Team_epf_Resistance.png" },
 
-
    { height: 71, src: "https://static.igem.org/mediawiki/2012/6/6f/Team_epf_Restrictionenzyme.png" },
 
-
    { height: 128, src: "https://static.igem.org/mediawiki/2012/8/86/Team_epf_Wellplate.png" },
 
-
    { height: 98, src: "https://static.igem.org/mediawiki/2012/8/8a/Team_epf_Plasmid.png" }
 
-
 
 
-
  ];
 
-
 
 
-
  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 = $("<div>")
 
-
          .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;
 
-
    }
 
-
  };
 
-
})();
 
$(function(){
$(function(){

Revision as of 16:58, 28 August 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();
     $('p')
       .filter(function(){
         return $.trim($(this).text()) ===  && $.trim($(this).html()).length < 10;
       })
       .remove();
   },
   onLoad: function() {
     $(".main-item").on
   },
   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 = $($(".menu")[0]);
     
     var DURATION = 400;
     var EASING = "swing";
     var DELAY = 800;
     var OFFSET = 15;
     
     var base = $(menu.find(".main-item")[0]);
     var current = $(menu.find(".main-item")[0]);
     
     current.addClass("menu-item-selected");
     
     var returnTimeout = null;
     var gotoMenuItem = function(item) {
       if (current == item) return;
       current.removeClass("menu-item-selected");
       current = item;
       current.addClass("menu-item-selected");
       var menuPosition = 0;//menu.position().left;
       var currentPosition = $("#taxi").position().left
       var menuItemPosition = item.position().left;
       
       var targetPosition = menuItemPosition - menuPosition + OFFSET;
       
       $("#taxi").stop(true, false);
       $("#taxi").animate({left: targetPosition+"px"}, {
         duration: DURATION,
         easing: EASING
       });
       
       if(currentPosition-menuPosition <= targetPosition) {
         $("#taxi").css({transform: "scale(1,1)", filter: ""});
       } else {
         $("#taxi").css({transform: "scale(-1,1)", filter: "fliph"});
       }
     };
     var returnToBase = function() {
       gotoMenuItem(base);
       returnTimeout = null;
     }
     
     $(".menu-item").mouseover(function() {
       if(!$(this).hasClass("main-item")) return;
       
       if(returnTimeout != null) {
         clearTimeout(returnTimeout);
         returnTimeout = null;
       }
       
       gotoMenuItem($(this));
     }).mouseout(function() {
       if(!$(this).hasClass("main-item")) return;
       
       if(returnTimeout == null) {
         returnTimeout = setTimeout(returnToBase, DELAY);
       }
     });
   }
 };

}());

if(!window.console){

 //Dummy console...
 window.console = {
   log: function(){ }
 };

}

$(function(){

 EPFL.onLoad();

});

var oldRunOnloadHook = runOnloadHook; runOnloadHook = function(){

 EPFL.init();
 if (oldRunOnloadHook) oldRunOnloadHook();

};