Template:Team:Berkeley/script

From 2013.igem.org

(Difference between revisions)
 
Line 1: Line 1:
-
/*
+
/* idTabs ~ Sean Catchpole - Version 2.2 - MIT/GPL */  
-
Activatables -- Make sets of elements active/inactive through anchors.
+
(function(){
-
Copyright (c) 2009 Andreas Blixt
+
var dep = {"jQuery":"http://code.jquery.com/jquery-latest.min.js"};
-
MIT license
+
var init = function(){ 
-
 
+
-
Permission is hereby granted, free of charge, to any person obtaining a copy
+
/* Options (in any order):
-
of this software and associated documentation files (the "Software"), to deal
+
-
in the Software without restriction, including without limitation the rights
+
start (number|string)
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+
    Index number of default tab. ex: $(...).idTabs(0)
-
copies of the Software, and to permit persons to whom the Software is
+
    String of id of default tab. ex: $(...).idTabs("tab1")
-
furnished to do so, subject to the following conditions:
+
    default: class "selected" or index 0
-
 
+
    Passing null will force it to not select a default tab
-
The above copyright notice and this permission notice shall be included in
+
-
all copies or substantial portions of the Software.
+
change (boolean)
-
 
+
    True - Url will change. ex: $(...).idTabs(true)
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+
    False - Url will not change. ex: $(...).idTabs(false)
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+
    default: false
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+
click (function)
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+
    Function will be called when a tab is clicked. ex: $(...).idTabs(foo)  
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+
    If the function returns true, idTabs will show/hide content (as usual).  
-
THE SOFTWARE.
+
    If the function returns false, idTabs will not take any action.  
-
*/
+
    The function is passed four variables:
-
 
+
      The id of the element to be shown
-
/*
+
      an array of all id's that can be shown
-
!!! Usage notes !!!
+
      the element containing the tabs
-
 
+
      and the current settings
-
This code stores a cache of all anchor elements (<a>), and due to this fact, the
+
-
code must not be executed before all anchor elements have been loaded into the
+
selected (string)
-
DOM, to avoid any unexpected behavior. Currently, there is no support for
+
    Class to use for selected. ex: $(...).idTabs(".current")  
-
handling anchors added to the DOM after this file has been included.
+
    default: ".selected"
-
 
+
-
It is recommended that this file is included before the </body> tag to ensure
+
event (string)
-
that all anchors are cached. Note that the code that calls the activatables()
+
    Event to trigger idTabs on. ex: $(...).idTabs("!mouseover")
-
function needs to be placed after the inclusion of this file.
+
    default: "!click"
-
*/
+
    To bind multiple event, call idTabs multiple times
-
 
+
      ex: $(...).idTabs("!click").idTabs("!focus")  
-
// Wrapped in a function so as to not pollute the global scope.
+
-
var activatables = (function () {
+
*/
-
// The CSS classes to use for active/inactive elements.
+
(function($){
-
var activeClass = 'active';
+
-
var inactiveClass = 'inactive';
+
  $.fn.idTabs = function(){
-
 
+
    //Loop Arguments matching options
-
var anchors = {}, activates = {};
+
    var s = {};  
-
var regex = /#([A-Za-z][A-Za-z0-9:._-]*)$/;
+
    for(var i=0; i<arguments.length; ++i) {  
-
 
+
      var a=arguments[i];  
-
// Find all anchors (<a href="#something">.)
+
      switch(a.constructor){
-
var temp = document.getElementsByTagName('a');
+
        case Object: $.extend(s,a); break;
-
for (var i = 0; i < temp.length; i++) {
+
        case Boolean: s.change = a; break;  
-
var a = temp[i];
+
        case Number: s.start = a; break;  
-
 
+
        case Function: s.click = a; break;  
-
// Make sure the anchor isn't linking to another page.
+
        case String:
-
if ((a.pathname != location.pathname &&
+
          if(a.charAt(0)=='.') s.selected = a;  
-
'/' + a.pathname != location.pathname) ||
+
          else if(a.charAt(0)=='!') s.event = a;  
-
a.search != location.search) continue;
+
          else s.start = a;  
-
 
+
        break;  
-
// Make sure the anchor has a hash part.
+
      }
-
var match = regex.exec(a.href);
+
    }
-
if (!match) continue;
+
-
var id = match[1];
+
    if(typeof s['return'] == "function") //backwards compatible
-
 
+
      s.change = s['return'];  
-
// Add the anchor to a lookup table.
+
   
-
if (id in anchors)
+
    return this.each(function(){ $.idTabs(this,s); }); //Chainable
-
anchors[id].push(a);
+
  }  
-
else
+
-
anchors[id] = [a];
+
  $.idTabs = function(tabs,options) {  
-
}
+
    //Settings
-
 
+
    var meta = ($.metadata)?$(tabs).metadata():{};  
-
// Adds/removes the active/inactive CSS classes depending on whether the
+
    var s = $.extend({},$.idTabs.settings,meta,options);  
-
// element is active or not.
+
-
function setClass(elem, active) {
+
    //Play nice
-
var classes = elem.className.split(/\s+/);
+
    if(s.selected.charAt(0)=='.') s.selected=s.selected.substr(1);  
-
var cls = active ? activeClass : inactiveClass, found = false;
+
    if(s.event.charAt(0)=='!') s.event=s.event.substr(1);  
-
for (var i = 0; i < classes.length; i++) {
+
    if(s.start==null) s.start=-1; //no tab selected
-
if (classes[i] == activeClass || classes[i] == inactiveClass) {
+
   
-
if (!found) {
+
    //Setup Tabs
-
classes[i] = cls;
+
    var showId = function(){
-
found = true;
+
      if($(this).is('.'+s.selected))
-
} else {
+
        return s.change; //return if already selected
-
delete classes[i--];
+
      var id = "#"+this.href.split('#')[1];  
-
}
+
      var aList = []; //save tabs
-
}
+
      var idList = []; //save possible elements
-
}
+
      $("a",tabs).each(function(){  
-
 
+
        if(this.href.match(/#/)) {  
-
if (!found) classes.push(cls);
+
          aList.push(this);  
-
elem.className = classes.join(' ');
+
          idList.push("#"+this.href.split('#')[1]);  
-
}
+
        }  
-
 
+
      });  
-
// Functions for managing the hash.
+
      if(s.click && !s.click.apply(this,[id,idList,tabs,s])) return s.change;  
-
function getParams() {
+
      //Clear tabs, and hide all
-
var hash = location.hash || '#';
+
      for(i in aList) $(aList[i]).removeClass(s.selected);  
-
var parts = hash.substring(1).split('&');
+
      for(i in idList) $(idList[i]).hide();  
-
 
+
      //Select clicked tab and show content
-
var params = {};
+
      $(this).addClass(s.selected);
-
for (var i = 0; i < parts.length; i++) {
+
      $(id).show();
-
var nv = parts[i].split('=');
+
      return s.change; //Option for changing url
-
if (!nv[0]) continue;
+
    }
-
params[nv[0]] = nv[1] || null;
+
-
}
+
    //Bind idTabs
-
+
    var list = $("a[href*='#']",tabs).unbind(s.event,showId).bind(s.event,showId);  
-
return params;
+
    list.each(function(){ $("#"+this.href.split('#')[1]).hide(); });  
-
}
+
-
 
+
    //Select default tab
-
function setParams(params) {
+
    var test=false;  
-
var parts = [];
+
    if((test=list.filter('.'+s.selected)).length); //Select tab with selected class
-
for (var name in params) {
+
    else if(typeof s.start == "number" &&(test=list.eq(s.start)).length); //Select num tab
-
// One of the following two lines of code must be commented out. Use the
+
    else if(typeof s.start == "string" //Select tab linking to id
-
// first to keep empty values in the hash query string; use the second
+
        &&(test=list.filter("[href*='#"+s.start+"']")).length);
-
// to remove them.
+
    if(test) { test.removeClass(s.selected); test.trigger(s.event); } //Select tab
-
//parts.push(params[name] ? name + '=' + params[name] : name);
+
-
if (params[name]) parts.push(name + '=' + params[name]);
+
    return s; //return current settings (be creative)
-
}
+
  }
-
 
+
-
location.hash = knownHash = '#' + parts.join('&');
+
  //Defaults
-
}
+
  $.idTabs.settings = {  
-
 
+
    start:0,
-
// Looks for changes to the hash.
+
    change:false,
-
var knownHash = location.hash;
+
    click:null,
-
function pollHash() {
+
    selected:".selected",
-
var hash = location.hash;
+
    event:"!click"
-
if (hash != knownHash) {
+
  };
-
var params = getParams();
+
-
for (var name in params) {
+
  //Version
-
if (!(name in activates)) continue;
+
  $.idTabs.version = "2.2";
-
activates[name](params[name]);
+
-
}
+
  //Auto-run
-
knownHash = hash;
+
  $(function(){ $(".idTabs").idTabs(); });
-
}
+
-
}
+
})(jQuery);  
-
setInterval(pollHash, 250);
+
-
 
+
-
function getParam(name) {
+
-
var params = getParams();
+
} //init
-
return params[name];
+
-
}
+
// Check Dependencies
-
 
+
var check = function(o,s){
-
function setParam(name, value) {
+
  s = s.split('.');  
-
var params = getParams();
+
  while(o && s.length) o = o[s.shift()];  
-
params[name] = value;
+
  return o;  
-
setParams(params);
+
}  
-
}
+
-
 
+
// Add Script
-
// If the hash is currently set to something that looks like a single id,
+
var head = document.getElementsByTagName("head")[0];  
-
// automatically activate any elements with that id.
+
var add = function(url){  
-
var initialId = null;
+
  var s = document.createElement("script");  
-
var match = regex.exec(knownHash);
+
  s.type = "text/javascript"; s.src = url;
-
if (match) {
+
  head.appendChild(s);  
-
initialId = match[1];
+
}  
-
}
+
-
 
+
// Save Self
-
// Takes an array of either element IDs or a hash with the element ID as the key
+
var s = document.getElementsByTagName('script');  
-
// and an array of sub-element IDs as the value.
+
var src = s[s.length-1].src;  
-
// When activating these sub-elements, all parent elements will also be
+
-
// activated in the process.
+
// Load Dependencies
-
function makeActivatable(paramName, activatables) {
+
var ok=true;  
-
var all = {}, first = initialId;
+
for(d in dep) {  
-
 
+
  if(check(this,d)) continue;  
-
// Activates all elements for a specific id (and inactivates the others.)
+
  ok=false;
-
function activate(id) {
+
  add(dep[d]);  
-
if (!(id in all)) return false;
+
} if(ok) return init();  
-
 
+
-
for (var cur in all) {
+
// Reload Self
-
if (cur == id) continue;
+
add(src);  
-
for (var i = 0; i < all[cur].length; i++) {
+
-
setClass(all[cur][i], false);
+
-
}
+
-
}
+
-
 
+
-
for (var i = 0; i < all[id].length; i++) {
+
-
setClass(all[id][i], true);
+
-
}
+
-
 
+
-
setParam(paramName, id);
+
-
 
+
-
return true;
+
-
}
+
-
 
+
-
activates[paramName] = activate;
+
-
 
+
-
function attach(item, basePath) {
+
-
if (item instanceof Array) {
+
-
for (var i = 0; i < item.length; i++) {
+
-
attach(item[i], basePath);
+
-
}
+
-
} else if (typeof item == 'object') {
+
-
for (var p in item) {
+
-
var path = attach(p, basePath);
+
-
attach(item[p], path);
+
-
}
+
-
} else if (typeof item == 'string') {
+
-
var path = basePath ? basePath.slice(0) : [];
+
-
var e = document.getElementById(item);
+
-
if (!e) throw 'Could not find "' + item + '".'
+
-
path.push(e);
+
-
 
+
-
if (!first) first = item;
+
-
 
+
-
// Store the elements in a lookup table.
+
-
all[item] = path;
+
-
 
+
-
// Attach a function that will activate the appropriate element
+
-
// to all anchors.
+
-
if (item in anchors) {
+
-
// Create a function that will call the 'activate' function with
+
-
// the proper parameters. It will be used as the event callback.
+
-
var func = (function (id) {
+
-
return function (e) {
+
-
activate(id);
+
-
 
+
-
if (!e) e = window.event;
+
-
if (e.preventDefault) e.preventDefault();
+
-
e.returnValue = false;
+
-
return false;
+
-
};
+
-
})(item);
+
-
 
+
-
for (var i = 0; i < anchors[item].length; i++) {
+
-
var a = anchors[item][i];
+
-
 
+
-
if (a.addEventListener) {
+
-
a.addEventListener('click', func, false);
+
-
} else if (a.attachEvent) {
+
-
a.attachEvent('onclick', func);
+
-
} else {
+
-
throw 'Unsupported event model.';
+
-
}
+
-
 
+
-
all[item].push(a);
+
-
}
+
-
}
+
-
 
+
-
return path;
+
-
} else {
+
-
throw 'Unexpected type.';
+
-
}
+
-
 
+
-
return basePath;
+
-
}
+
-
 
+
-
attach(activatables);
+
-
 
+
-
// Activate an element.
+
-
if (first) activate(getParam(paramName)) || activate(first);
+
-
}
+
-
 
+
-
return makeActivatable;
+
})();
})();

Latest revision as of 03:34, 21 October 2013

/* idTabs ~ Sean Catchpole - Version 2.2 - MIT/GPL */ (function(){ var dep = {"jQuery":"http://code.jquery.com/jquery-latest.min.js"}; var init = function(){

/* Options (in any order):

start (number|string) 
   Index number of default tab. ex: $(...).idTabs(0) 
   String of id of default tab. ex: $(...).idTabs("tab1") 
   default: class "selected" or index 0 
   Passing null will force it to not select a default tab 

change (boolean) 
   True - Url will change. ex: $(...).idTabs(true) 
   False - Url will not change. ex: $(...).idTabs(false) 
   default: false 

click (function) 
   Function will be called when a tab is clicked. ex: $(...).idTabs(foo) 
   If the function returns true, idTabs will show/hide content (as usual). 
   If the function returns false, idTabs will not take any action. 
   The function is passed four variables: 
     The id of the element to be shown 
     an array of all id's that can be shown 
     the element containing the tabs 
     and the current settings 

selected (string) 
   Class to use for selected. ex: $(...).idTabs(".current") 
   default: ".selected" 

event (string) 
   Event to trigger idTabs on. ex: $(...).idTabs("!mouseover") 
   default: "!click" 
   To bind multiple event, call idTabs multiple times 
     ex: $(...).idTabs("!click").idTabs("!focus") 

  • /

(function($){

 $.fn.idTabs = function(){ 
   //Loop Arguments matching options 
   var s = {}; 
   for(var i=0; i<arguments.length; ++i) { 
     var a=arguments[i]; 
     switch(a.constructor){ 
       case Object: $.extend(s,a); break; 
       case Boolean: s.change = a; break; 
       case Number: s.start = a; break; 
       case Function: s.click = a; break; 
       case String: 
         if(a.charAt(0)=='.') s.selected = a; 
         else if(a.charAt(0)=='!') s.event = a; 
         else s.start = a; 
       break; 
     } 
   } 

   if(typeof s['return'] == "function") //backwards compatible 
     s.change = s['return']; 
    
   return this.each(function(){ $.idTabs(this,s); }); //Chainable 
 } 

 $.idTabs = function(tabs,options) { 
   //Settings 
   var meta = ($.metadata)?$(tabs).metadata():{}; 
   var s = $.extend({},$.idTabs.settings,meta,options); 

   //Play nice 
   if(s.selected.charAt(0)=='.') s.selected=s.selected.substr(1); 
   if(s.event.charAt(0)=='!') s.event=s.event.substr(1); 
   if(s.start==null) s.start=-1; //no tab selected 
    
   //Setup Tabs 
   var showId = function(){ 
     if($(this).is('.'+s.selected)) 
       return s.change; //return if already selected 
     var id = "#"+this.href.split('#')[1]; 
     var aList = []; //save tabs 
     var idList = []; //save possible elements 
     $("a",tabs).each(function(){ 
       if(this.href.match(/#/)) { 
         aList.push(this); 
         idList.push("#"+this.href.split('#')[1]); 
       } 
     }); 
     if(s.click && !s.click.apply(this,[id,idList,tabs,s])) return s.change; 
     //Clear tabs, and hide all 
     for(i in aList) $(aList[i]).removeClass(s.selected); 
     for(i in idList) $(idList[i]).hide(); 
     //Select clicked tab and show content 
     $(this).addClass(s.selected); 
     $(id).show(); 
     return s.change; //Option for changing url 
   } 

   //Bind idTabs 
   var list = $("a[href*='#']",tabs).unbind(s.event,showId).bind(s.event,showId); 
   list.each(function(){ $("#"+this.href.split('#')[1]).hide(); }); 

   //Select default tab 
   var test=false; 
   if((test=list.filter('.'+s.selected)).length); //Select tab with selected class 
   else if(typeof s.start == "number" &&(test=list.eq(s.start)).length); //Select num tab 
   else if(typeof s.start == "string" //Select tab linking to id 
        &&(test=list.filter("[href*='#"+s.start+"']")).length); 
   if(test) { test.removeClass(s.selected); test.trigger(s.event); } //Select tab 

   return s; //return current settings (be creative) 
 } 

 //Defaults 
 $.idTabs.settings = { 
   start:0, 
   change:false, 
   click:null, 
   selected:".selected", 
   event:"!click" 
 }; 

 //Version 
 $.idTabs.version = "2.2"; 

 //Auto-run 
 $(function(){ $(".idTabs").idTabs(); }); 

})(jQuery);


} //init

// Check Dependencies var check = function(o,s){

 s = s.split('.'); 
 while(o && s.length) o = o[s.shift()]; 
 return o; 

}

// Add Script var head = document.getElementsByTagName("head")[0]; var add = function(url){

 var s = document.createElement("script"); 
 s.type = "text/javascript"; s.src = url; 
 head.appendChild(s); 

}

// Save Self var s = document.getElementsByTagName('script'); var src = s[s.length-1].src;

// Load Dependencies var ok=true; for(d in dep) {

 if(check(this,d)) continue; 
 ok=false; 
 add(dep[d]); 

} if(ok) return init();

// Reload Self add(src);

})();