Difference between revisions of "Template:UCAS-China/igemUCAS.js"

m
m
Line 3: Line 3:
 
  * Copyright UCAS iGEM team
 
  * Copyright UCAS iGEM team
 
  */
 
  */
(function(c,a){var b=c.iU=a(c);b()})(window,function(d){var f=d.document,k=(function(){var o="",p=1,q=function(r,s){o=o.concat(p," ",r,": ",s,"\n");p++},n=function(r){var s={string:function(t){if((!t)||typeof t!="string"){q(r,"parameter should be of .")}return s},number:function(t){if((!t)||typeof t!="number"){q(r,"parameter should be of number type.")}return s},keyUnused:function(u,t){if(u[t]){q(r,"key "+t+" has been used.")}return s},assert:function(u,t){if(!u){q(r,t)}return s}};return s};n.displayErrors=function(){if(o.length==0){alert("No errors, Congrats!")}else{alert(o)}};return n})(),e={SIDE:0.1,SEGW:24,INF:2000000},j={merge:function(o,p){for(var n in p){if(o[n]){if(typeof o[n]=="object"&&typeof p[n]=="object"){o[n]=j.merge(o[n],p[n])}else{o[n]=p[n]}}else{o[n]=p[n]}}return o},getUid:(function(){var n=-1;return function(){n++;return(function(o){return"_"+o+"_"})(n)}})(),realWidth:function(n){return n*d.innerWidth/e.SEGW}};var b={general:{"font-family":"Georgia","font-size":"25px","text-align":"justify","color":"white"},span:{},a:{"text-decoration":"none","mouseover":function(){},"mouseleave":function(){}},b:{"color":"yellow"}};function l(o,p,n){this.elem=(typeof o=="string")?f.createElement(o):o;this.elem.id=p||j.getUid();this.elem.style.visibility="hidden";this.span=n;this.styles={};this.iU_base.appendChild(this.elem);this.state="wait"}l.prototype={constructor:l,adaptWidth:function(){var o=j.realWidth(this.span);if(o==this.elem.clientWidth){return this.elem.clientHeight}this.css({"display":"block","width":o+"px"});var n=this.elem.clientHeight;if(n!=0&&this.state=="wait"){this.state="unused"}return n},updataSchema:function(){this.css(b.general);this.css(b[(this.elem.tagName||"").toLowerCase()]);for(var n in this.elem.childNodes){l.prototype.css.call(this.elem.childNodes[n],b[(this.elem.childNodes[n].tagName||"").toLowerCase()])}},css:function(q){if(!q){return}var p=this.elem||this;if(this.styles){for(var s in this.styles){p.style[s]=this.styles[s]}}for(var s in q){var o="",r=false;for(var n=0;n<s.length;n++){if(s[n]=="-"){r=true}else{o+=r?s[n].toUpperCase():s[n];r=false}}if(typeof q[s]=="function"){p.addEventListener(o,q[s])}else{p.style[o]=q[s]}if(this.styles){this.styles[o]=q[s]}}},render:function(n){this.adaptWidth();this.css({"position":"absolute","left":(n+this.left)+"px","top":this.top+"px","visibility":"visible"})},toString:function(){return"["+this.elem.tagName.toLowerCase()+"] "+this.elem.innerHTML.substr(0,50)+"\n"}};var m={upgradeText:function(){for(var o=0,n=this.elem.childNodes.length;o<n;o++){var p=this.elem.childNodes[o];if(!p.attributes){var q=f.createElement("span");q.innerHTML=p.textContent;this.elem.replaceChild(q,p)}}}};function c(n){this.span=n.span;this.padding=n.padding;this.spacing=n.spacing;this.marginCross=n.marginCross;this.marginTop=n.marginTop;this.height=d.innerHeight-n.marginTop-n.marginBottom;this.page=0;this.frontier=0;this.reserve=0;this.usedW=0;this.comps=[];this.buffer=[];this.isVisible=true}c.prototype={constructor:c,operate:function(){var p=false;while(!p&&this.buffer.length>0){switch(this.buffer[0].state){case"dispose":this.buffer.shift();break;case"wait":this.buffer[0].adaptWidth();if(this.buffer[0].state=="wait"){p=true;break}case"unused":var n=this.buffer.shift(),o=n.adaptWidth();if(this.page==0){this.page=1}else{if(this.usedW==0||this.usedW+n.span>this.span){if(this.frontier+o>this.height){this.page+=1;this.frontier=0}else{this.frontier+=this.spacing}this.reserve=this.frontier;this.usedW=0}}n.left=j.realWidth(this.span*(this.page-1)+this.usedW+this.page*this.padding);n.top=this.reserve+this.marginTop;n.state="used";this.usedW+=n.span;this.frontier=Math.max(this.frontier,this.reserve+o);this.comps.push(n)}}},getSpan:function(o){var n=o?o.value:this.span;n=(n<0)?(n+this.span):Math.min(n,this.span);return n},script:function(o){var n=new l(o,o["name"],this.getSpan(o["span"]));m.upgradeText.call(n);n.updataSchema();n.adaptWidth();this.buffer.push(n)},image:function(o){var n=new l(o,o["name"],this.getSpan(o["span"]));n.updataSchema();n.adaptWidth();this.buffer.push(n)},title:function(o){var n=new l(o,o["name"],this.getSpan(o["span"]));n.updataSchema();n.adaptWidth();this.buffer.push(n)},hide:function(){if(this.isVisible){for(var o=0,n=this.comps.length;o<n;o++){this.comps[o].elem.style["visibility"]="hidden"}}},getBreadth:function(){return this.page*this.span+(this.page+1)*this.marginCross},render:function(n){this.isVisible=true;this.operate();for(var p=0,o=this.comps.length;p<o;p++){this.comps[p].render(n)}}};function a(n){this.flows=[]}a.prototype={constructor:a,render:function(s){var w=0,u=[];for(var r=0,v=this.flows.length;r<v;r++){var o=this.flows[r],t=w-s,n=o.getBreadth(),p=d.innerWidth;if(t<-n||t>p){o.hide()}else{u.push([o,t])}w+=n}for(var q in u){u[q][0].render(-u[q][1])}return w}};function i(o){this.boot=o;o.id="UACSboot";o.style["display"]="none";this.logo=o.children[0];this.sponsors=[];
+
 
for(var n=1;n<o.children.length;n++){this.sponsors.push(o.children[n])}this.headings=[]}i.prototype={constructor:i,render:function(r){r=Math.max(r,d.innerWidth*e.SIDE);var q=((r/d.innerWidth)-e.SIDE)/(1-e.SIDE);l.prototype.css.call(this.boot,{"display":"block","position":"absolute","width":r+"px","height":d.innerHeight+"px","background-color":"black","visibility":"visible"});if(q>0){l.prototype.css.call(this.logo,{"display":"block","position":"absolute","top":Math.floor(d.innerHeight*0.2)+"px","left":Math.floor(d.innerWidth*0.3)+"px","width":Math.floor(d.innerWidth*0.4)+"px","opacity":q})}else{l.prototype.css.call(this.logo,{"display":"none"})}var n=0;for(var o=0;o<this.headings.length;o++){l.prototype.css.call(this.headings[o],{"position":"inherit","width":Math.floor(d.innerWidth*e.SIDE)+"px","visibility":"visible"});var p=this.headings[o].clientHeight;l.prototype.css.call(this.sponsors[o],{"position":"absolute","top":n+"px","left":"0","visibility":"visible","opacity":1-q});n+=p}n=0;for(var o=0;o<this.sponsors.length;o++){l.prototype.css.call(this.sponsors[o].children[0],{"position":"inherit","width":Math.floor(d.innerWidth*e.SIDE)+"px","visibility":"visible"});n+=this.sponsors[o].children[0].clientHeight;l.prototype.css.call(this.sponsors[o],{"position":"absolute","top":(d.innerHeight-n)+"px","left":"0","visibility":"visible","opacity":1-q})}}};var g={initRoot:function(p){var n=f.getElementById("igemUCAS");if(!n){return null}n.style.visibility="hidden";n.style["z-index"]="-100";if(p){var o=f.getElementsByTagName("body")[0];l.prototype.css.call(o,{"overflow-x":"hidden","overflow-y":"hidden","padding":"0","margin":"0","background-color":"black"})}k("fn.initRoot").assert(n,"");return n},initWebGL:function(o){var n=f.createElement("canvas");n.id="bgCanvas";n.width=d.innerWidth;n.height=d.innerHeight;o.parentNode.appendChild(n);var p=n.getContext("webgl")||n.getContext("experimental-webgl");if(p){l.prototype.css.call(n,{"position":"absolute","top":"0","left":"0","z-index":"-90","overflow":"hidden","visibility":"visible"})}return p},initBase:function(n){var o=f.createElement("div");o.id="UCASigem";o.style.position="absolute";n.parentNode.appendChild(o);return o},root_not_found:function(){},gl_not_supported:function(){},defaultConfig:{overflow:true,webgl:true}};function h(){r=j.merge(r,d.schema||{});var u=g.initRoot((d.configs||g.defaultConfig).overflow);if(!u){return g.root_not_found()}var y=g.initBase(u);l.prototype.iU_base=y;var n=new i(u.children[0]);u.parentNode.appendChild(n.boot);var t=(d.configs||g.defaultConfig).webgl||g.initWebGL(u);if(!t){g.gl_not_supported()}var r=j.merge(g.defaultSchema,d.schema||{});c.prototype.schema=r;var x=new a(d.innerHeight);while(u.children.length>0){var v=u.children[0];if((v.tagName||"").toLowerCase()=="div"){var w=v["spacing"]?v["spacing"].value.split(/\t/):"",o=new c({span:v["span"]?v["span"].value:8,padding:v["padding"]?v["padding"].value:1,spacing:parseInt(w[0])||40,marginTop:parseInt(w[1])||100,marginBottom:w[2]?parseInt(w[2]):(parseInt(w[1])||100)});x.flows.push(o);while(v.children.length>0){var q=v.children[0];switch(((q||{}).tagName||"").toLowerCase()){case"p":o.script(q);break;case"b":o.title(q);break;case"img":o.image(q);break;default:v.removeChild(q);break}}}u.removeChild(v)}u.parentNode.removeChild(u);var s=d.innerWidth;var p=function(z){z=z||d.event;s-=z.wheelDelta||z.detail;s=Math.min(s,d.innerWidth);n.render(s);x.render(s)};d.onmousewheel=f.onmousewheel=p;p({detail:0})}h.displayErrors=k.displayErrors;return h});
+
( function( global, factory ) { var iU = global.iU = factory(global); iU(); } ) (window, function( window ) {
 +
var  
 +
document = window.document,
 +
inspect = (function(){
 +
var
 +
strErr = "",
 +
nextseq = 1,
 +
error = function( location, desc ){
 +
strErr = strErr.concat(nextseq, " ", location, ": ", desc, '\n');
 +
nextseq ++;
 +
},
 +
garrison = function( name ){
 +
var f = {
 +
string: function( s ){
 +
if( (!s) || typeof s != 'string' )
 +
error(name, "parameter should be of .");
 +
return f;
 +
},
 +
number: function( s ){
 +
if( (!s) || typeof s != 'number' )
 +
error(name, "parameter should be of number type.");
 +
return f;
 +
},
 +
keyUnused: function(obj, key){
 +
if( obj[key] )
 +
error(name, "key "+key+" has been used.");
 +
return f;
 +
},
 +
assert: function( val, desc ){
 +
if( !val )
 +
error(name, desc );
 +
return f;
 +
}
 +
};
 +
return f;
 +
};
 +
 
 +
garrison.displayErrors = function(){
 +
if(strErr.length == 0 )
 +
alert("No errors, Congrats!")
 +
else
 +
alert( strErr );
 +
};
 +
return garrison;
 +
})(),
 +
 
 +
 
 +
consts = {
 +
SIDE: 0.1,
 +
SEGW: 24,
 +
INF: 2000000
 +
},
 +
 
 +
utils = {
 +
merge: function( t, c){
 +
for(var i in c){
 +
if( t[i] ){
 +
if( typeof t[i] == 'object' && typeof c[i] == 'object')
 +
t[i] = utils.merge(t[i],c[i]);
 +
else
 +
t[i] = c[i];
 +
}
 +
else
 +
t[i] = c[i];
 +
}
 +
return t;
 +
},
 +
getUid: (function(){
 +
var now = -1;
 +
return function(){
 +
now ++;
 +
return (function(n){
 +
return '_'+n+'_';
 +
})(now);
 +
};
 +
})(),
 +
realWidth: function( span ){
 +
return span * window.innerWidth / consts.SEGW;
 +
}
 +
};
 +
 
 +
var
 +
schema = {
 +
general: {
 +
"font-family": "Georgia",
 +
"font-size": "25px",
 +
"text-align":   "justify",
 +
"color": "white"
 +
},
 +
span: {
 +
 
 +
},
 +
a: {
 +
"text-decoration": "none",
 +
"mouseover": function(){
 +
 +
},
 +
"mouseleave": function(){
 +
 
 +
}
 +
},
 +
b: {
 +
"color": "yellow"
 +
}
 +
}
 +
 
 +
function Component( node, id, span ){
 +
this.elem = (typeof node == "string") ? document.createElement(node) : node;
 +
this.elem.id = id || utils.getUid();
 +
this.elem.style.visibility = "hidden";
 +
 
 +
this.span = span;
 +
this.styles = {};
 +
 
 +
this.iU_base.appendChild(this.elem);
 +
this.state = "wait";
 +
}
 +
Component.prototype = {
 +
constructor: Component,
 +
adaptWidth: function(){
 +
var
 +
width = utils.realWidth(this.span);
 +
 
 +
if( width == this.elem.clientWidth )
 +
return this.elem.clientHeight;
 +
 
 +
this.css({
 +
"display": "block",
 +
"width": width+"px"
 +
});
 +
 
 +
var
 +
h = this.elem.clientHeight;
 +
if( h != 0 && this.state == "wait" )
 +
this.state = "unused";
 +
 +
return h;
 +
},
 +
updataSchema: function(){
 +
this.css( schema.general );
 +
this.css( schema[ (this.elem.tagName||"").toLowerCase() ] );
 +
for(var i in this.elem.childNodes)
 +
Component.prototype.css.call( this.elem.childNodes[i], schema[ (this.elem.childNodes[i].tagName||"").toLowerCase() ] );
 +
},
 +
css: function( styles ){
 +
if( !styles )
 +
return ;
 +
 
 +
var
 +
elem = this.elem || this;//maybe a component or DOM Element
 +
if(this.styles){
 +
for(var prop in this.styles)
 +
elem.style[prop] = this.styles[ prop ];
 +
}
 +
 
 +
for(var prop in styles){
 +
var  
 +
key = "",
 +
change = false;
 +
 
 +
for(var j = 0; j < prop.length; j ++){
 +
if( prop[j] == '-' )
 +
change = true;
 +
else{
 +
key += change ? prop[j].toUpperCase() : prop[j];
 +
change = false;
 +
}
 +
}
 +
 
 +
if( typeof styles[ prop ] == "function" )
 +
elem.addEventListener(key, styles[prop]);
 +
else
 +
elem.style[ key ] = styles[ prop ];
 +
 
 +
if(this.styles)
 +
this.styles[ key ] = styles[ prop ];
 +
}
 +
},
 +
render: function( offsetX ){
 +
this.adaptWidth();
 +
 
 +
this.css({
 +
"position": "absolute",
 +
"left": (offsetX+this.left) +"px",
 +
"top": this.top +"px",
 +
"visibility": "visible"
 +
});
 +
},
 +
toString: function(){
 +
return "["+this.elem.tagName.toLowerCase() +"] "+ this.elem.innerHTML.substr(0,50)+"\n";
 +
}
 +
};
 +
 
 +
var
 +
Supp = {
 +
upgradeText: function(){
 +
for(var i = 0, i_len = this.elem.childNodes.length; i < i_len; i ++){
 +
var
 +
node = this.elem.childNodes[i];
 +
if( !node.attributes ){
 +
var
 +
child = document.createElement("span");
 +
child.innerHTML = node.textContent;
 +
this.elem.replaceChild(child, node);
 +
}
 +
}
 +
}
 +
};
 +
 
 +
function Flow( configs ){
 +
this.span = configs.span;
 +
this.padding = configs.padding;
 +
 
 +
this.spacing = configs.spacing;
 +
this.marginCross= configs.marginCross;
 +
this.marginTop = configs.marginTop;
 +
this.height = window.innerHeight - configs.marginTop - configs.marginBottom;
 +
 
 +
this.page = 0;
 +
this.frontier = 0;
 +
this.reserve = 0;
 +
this.usedW = 0;
 +
 
 +
this.comps = [];
 +
this.buffer = [];
 +
this.isVisible = true;
 +
}
 +
Flow.prototype = {
 +
constructor: Flow,
 +
operate: function(){
 +
var
 +
wait = false;
 +
while( !wait && this.buffer.length > 0 ){
 +
switch( this.buffer[0].state ){
 +
case "dispose": this.buffer.shift(); break;
 +
 
 +
case "wait":
 +
this.buffer[0].adaptWidth();
 +
if( this.buffer[0].state == "wait" ){
 +
        wait = true;
 +
        break;
 +
}
 +
case "unused":  
 +
var
 +
comp = this.buffer.shift(),
 +
h = comp.adaptWidth();
 +
 
 +
if( this.page == 0 )
 +
this.page = 1;
 +
else if( this.usedW == 0 || this.usedW + comp.span > this.span ){
 +
if( this.frontier + h > this.height ){
 +
 +
this.page += 1;
 +
this.frontier = 0;
 +
}
 +
else
 +
this.frontier+= this.spacing;
 +
 
 +
this.reserve = this.frontier;
 +
this.usedW   = 0;
 +
}
 +
 
 +
comp.left = utils.realWidth( this.span * (this.page-1) + this.usedW + this.page*this.padding );
 +
comp.top = this.reserve + this.marginTop;
 +
comp.state = "used";
 +
 
 +
this.usedW   += comp.span;
 +
this.frontier = Math.max( this.frontier, this.reserve+h );
 +
 
 +
this.comps.push( comp );
 +
}
 +
}
 +
},
 +
getSpan: function( s ){
 +
var
 +
span = s ? s.value : this.span;
 +
span = (span < 0) ? (span + this.span) : Math.min(span, this.span);
 +
return span;
 +
},
 +
 
 +
script: function( node ){
 +
var
 +
t = new Component( node, node["name"], this.getSpan( node["span"] ));
 +
Supp.upgradeText.call(t);
 +
t.updataSchema();
 +
t.adaptWidth();
 +
 
 +
this.buffer.push( t );
 +
},
 +
image: function( node ){
 +
var
 +
i = new Component( node, node["name"], this.getSpan( node["span"] ));
 +
i.updataSchema();
 +
i.adaptWidth();
 +
 
 +
this.buffer.push( i );
 +
},
 +
title: function( node ){
 +
var
 +
t = new Component( node, node["name"], this.getSpan( node["span"] ));
 +
t.updataSchema();
 +
t.adaptWidth();
 +
 
 +
this.buffer.push( t );
 +
},
 +
 
 +
hide: function(){
 +
if( this.isVisible ){
 +
for(var i = 0, i_len = this.comps.length; i < i_len; i ++)
 +
this.comps[ i ].elem.style["visibility"] = "hidden";
 +
}
 +
},
 +
getBreadth: function(){
 +
return this.page * this.span + (this.page+1) * this.marginCross;
 +
},
 +
render: function( offsetX ){
 +
this.isVisible = true;
 +
this.operate();
 +
 
 +
for(var i = 0, i_len = this.comps.length; i < i_len; i ++)
 +
this.comps[ i ].render( offsetX );
 +
}
 +
};
 +
 
 +
function Scroll( height ){
 +
this.flows = [];
 +
}
 +
Scroll.prototype = {
 +
constructor: Scroll,
 +
render: function( position ){
 +
var  
 +
accum = 0,
 +
d = [];
 +
 
 +
for(var i = 0, i_len = this.flows.length; i < i_len; i ++){
 +
var
 +
flow = this.flows[i],
 +
offsetX = accum - position,
 +
breadth = flow.getBreadth(),
 +
width = window.innerWidth ;
 +
 
 +
if( offsetX < -breadth || offsetX > width )
 +
flow.hide();
 +
else
 +
d.push( [flow, offsetX] );
 +
 
 +
accum += breadth;
 +
}
 +
for(var j in d)
 +
d[j][0].render( -d[j][1] );
 +
 
 +
return accum;
 +
}
 +
};
 +
 
 +
function SideBar( iU_boot ){
 +
this.boot = iU_boot;
 +
iU_boot.id = "UACSboot";
 +
iU_boot.style["display"] = "none";
 +
 
 +
this.logo = iU_boot.children[0];
 +
 
 +
this.sponsors = [];
 +
for(var i = 1; i < iU_boot.children.length; i ++)
 +
this.sponsors.push( iU_boot.children[i] );
 +
 
 +
this.headings = [];
 +
}
 +
SideBar.prototype = {
 +
constructor: SideBar,
 +
render: function( width ){
 +
width = Math.max(width, window.innerWidth*consts.SIDE);
 +
var
 +
ratio = ((width / window.innerWidth) - consts.SIDE ) / ( 1 - consts.SIDE );
 +
 
 +
Component.prototype.css.call(this.boot, {
 +
"display": "block",
 +
"position": "absolute",
 +
"width": width + "px",
 +
"height": window.innerHeight + "px",
 +
"background-color": "black",
 +
"visibility": "visible"
 +
});
 +
if(ratio > 0){
 +
Component.prototype.css.call(this.logo, {
 +
"display": "block",
 +
"position": "absolute",
 +
"top": Math.floor(window.innerHeight*0.2) + "px",
 +
"left": Math.floor(window.innerWidth *0.3) + "px",
 +
"width": Math.floor(window.innerWidth *0.4) + "px",
 +
"opacity": ratio
 +
});
 +
}
 +
else
 +
Component.prototype.css.call(this.logo, {
 +
"display": "none"
 +
});
 +
 
 +
var
 +
accum = 0;
 +
for(var i = 0; i < this.headings.length; i ++){
 +
Component.prototype.css.call(this.headings[i], {
 +
"position": "inherit",
 +
"width": Math.floor(window.innerWidth *consts.SIDE) + "px",
 +
"visibility":"visible"
 +
});
 +
var
 +
h = this.headings[i].clientHeight;
 +
Component.prototype.css.call(this.sponsors[i], {
 +
"position": "absolute",
 +
"top": accum + "px",
 +
"left": "0",
 +
"visibility":"visible",
 +
"opacity": 1.0-ratio
 +
});
 +
accum += h;
 +
}
 +
 
 +
accum = 0;
 +
for(var i = 0; i < this.sponsors.length; i ++){
 +
Component.prototype.css.call(this.sponsors[i].children[0], {
 +
"position": "inherit",
 +
"width": Math.floor(window.innerWidth *consts.SIDE) + "px",
 +
"visibility":"visible"
 +
});
 +
accum += this.sponsors[i].children[0].clientHeight;
 +
Component.prototype.css.call(this.sponsors[i], {
 +
"position": "absolute",
 +
"top": (window.innerHeight - accum) + "px",
 +
"left": "0",
 +
"visibility":"visible",
 +
"opacity": 1.0-ratio
 +
});
 +
}
 +
 
 +
 
 +
 +
}
 +
}
 +
 
 +
var
 +
fn = {
 +
initRoot: function( overflow ){
 +
var  
 +
iU_root = document.getElementById("igemUCAS");
 +
 
 +
if( !iU_root )
 +
return null;
 +
 
 +
iU_root.style.visibility = "hidden";
 +
iU_root.style["z-index"] = "-100";
 +
 
 +
if( overflow ){
 +
var
 +
webpage = document.getElementsByTagName("body")[0];
 +
Component.prototype.css.call(webpage,{
 +
"overflow-x": "hidden",
 +
"overflow-y": "hidden",
 +
"padding" : "0",
 +
"margin" : "0",
 +
"background-color": "black"
 +
});
 +
}
 +
 
 +
inspect("fn.initRoot").assert(iU_root, "");
 +
return iU_root;
 +
},
 +
initWebGL: function( iU_root ){
 +
var
 +
canvas    = document.createElement("canvas");
 +
 
 +
canvas.id = "bgCanvas";
 +
canvas.width = window.innerWidth;
 +
canvas.height = window.innerHeight;
 +
iU_root.parentNode.appendChild(canvas);
 +
 +
var
 +
gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
 +
 
 +
if(gl)
 +
Component.prototype.css.call(canvas, {
 +
"position":"absolute",
 +
"top":"0",
 +
"left":"0",
 +
"z-index":"-90",
 +
"overflow":"hidden",
 +
"visibility":"visible"
 +
});
 +
 +
return gl;
 +
},
 +
initBase: function( iU_root ){
 +
var
 +
iU_base = document.createElement("div");
 +
iU_base.id = "UCASigem";
 +
iU_base.style.position   = "absolute";
 +
 
 +
iU_root.parentNode.appendChild(iU_base);
 +
return iU_base;
 +
},
 +
 
 +
root_not_found: function(){
 +
 
 +
},
 +
gl_not_supported: function(){
 +
 
 +
},
 +
defaultConfig: {
 +
overflow: true,
 +
webgl: true
 +
}
 +
}
 +
 
 +
function igemUCAS(){
 +
schema = utils.merge( schema, window.schema||{} );
 +
 
 +
var
 +
iU_root = fn.initRoot( (window.configs||fn.defaultConfig).overflow );
 +
if( !iU_root )
 +
return fn.root_not_found();
 +
 
 +
var
 +
iU_base = fn.initBase( iU_root );
 +
Component.prototype.iU_base = iU_base;
 +
 
 +
var
 +
sidebar = new SideBar( iU_root.children[0] );
 +
iU_root.parentNode.appendChild( sidebar.boot );
 +
 
 +
var
 +
gl = (window.configs||fn.defaultConfig).webgl || fn.initWebGL( iU_root );
 +
if( !gl )
 +
fn.gl_not_supported();
 +
 
 +
var
 +
schema = utils.merge( fn.defaultSchema, window.schema||{} );  
 +
Flow.prototype.schema = schema;
 +
 
 +
var
 +
scroll = new Scroll( window.innerHeight );
 +
while( iU_root.children.length > 0 ){
 +
var
 +
page = iU_root.children[0];
 +
 
 +
if( (page.tagName||"").toLowerCase() == "div" ){
 +
var
 +
spacing = page["spacing"] ? page["spacing"].value.split(/\t/): "",
 +
flow = new Flow({
 +
span: page["span"] ? page["span"].value : 8,
 +
padding: page["padding"] ? page["padding"].value : 1,
 +
spacing: parseInt(spacing[0]) || 40,  
 +
marginTop: parseInt(spacing[1]) || 100,
 +
marginBottom: spacing[2] ? parseInt(spacing[2]) : (parseInt(spacing[1]) || 100)
 +
});
 +
 
 +
scroll.flows.push(flow);
 +
 
 +
while( page.children.length > 0 ){
 +
var
 +
node = page.children[0];
 +
switch( ((node||{}).tagName||"").toLowerCase() ){
 +
case "p": flow.script(node); break;
 +
case "b": flow.title(node); break;
 +
case "img": flow.image(node); break;
 +
default: page.removeChild(node); break;
 +
}
 +
}
 +
}
 +
iU_root.removeChild( page );
 +
}
 +
iU_root.parentNode.removeChild(iU_root);
 +
 
 +
var
 +
offset = window.innerWidth;
 +
 
 +
var scrollHandler = function( ev ){
 +
ev = ev || window.event;
 +
offset -= ev.wheelDelta || ev.detail;
 +
offset  = Math.min(offset, window.innerWidth);
 +
sidebar.render( offset );
 +
scroll .render( offset );
 +
};
 +
 
 +
//document.addEventListener("DOMMouseScroll", scrollHandler , false);
 +
window.onmousewheel = document.onmousewheel = scrollHandler;
 +
scrollHandler( {detail: 0} );
 +
}
 +
igemUCAS.displayErrors = inspect.displayErrors;
 +
 
 +
return igemUCAS;
 +
});

Revision as of 12:40, 9 October 2018

/*!

* igemUCAS JavaScript Library v0.18.16
* Copyright UCAS iGEM team
*/

( function( global, factory ) { var iU = global.iU = factory(global); iU(); } ) (window, function( window ) { var document = window.document, inspect = (function(){ var strErr = "", nextseq = 1, error = function( location, desc ){ strErr = strErr.concat(nextseq, " ", location, ": ", desc, '\n'); nextseq ++; }, garrison = function( name ){ var f = { string: function( s ){ if( (!s) || typeof s != 'string' ) error(name, "parameter should be of ."); return f; }, number: function( s ){ if( (!s) || typeof s != 'number' ) error(name, "parameter should be of number type."); return f; }, keyUnused: function(obj, key){ if( obj[key] ) error(name, "key "+key+" has been used."); return f; }, assert: function( val, desc ){ if( !val ) error(name, desc ); return f; } }; return f; };

garrison.displayErrors = function(){ if(strErr.length == 0 ) alert("No errors, Congrats!") else alert( strErr ); }; return garrison; })(),


consts = { SIDE: 0.1, SEGW: 24, INF: 2000000 },

utils = { merge: function( t, c){ for(var i in c){ if( t[i] ){ if( typeof t[i] == 'object' && typeof c[i] == 'object') t[i] = utils.merge(t[i],c[i]); else t[i] = c[i]; } else t[i] = c[i]; } return t; }, getUid: (function(){ var now = -1; return function(){ now ++; return (function(n){ return '_'+n+'_'; })(now); }; })(), realWidth: function( span ){ return span * window.innerWidth / consts.SEGW; } };

var schema = { general: { "font-family": "Georgia", "font-size": "25px", "text-align": "justify", "color": "white" }, span: {

}, a: { "text-decoration": "none", "mouseover": function(){

}, "mouseleave": function(){

} }, b: { "color": "yellow" } }

function Component( node, id, span ){ this.elem = (typeof node == "string") ? document.createElement(node) : node; this.elem.id = id || utils.getUid(); this.elem.style.visibility = "hidden";

this.span = span; this.styles = {};

this.iU_base.appendChild(this.elem); this.state = "wait"; } Component.prototype = { constructor: Component, adaptWidth: function(){ var width = utils.realWidth(this.span);

if( width == this.elem.clientWidth ) return this.elem.clientHeight;

this.css({ "display": "block", "width": width+"px" });

var h = this.elem.clientHeight; if( h != 0 && this.state == "wait" ) this.state = "unused";

return h; }, updataSchema: function(){ this.css( schema.general ); this.css( schema[ (this.elem.tagName||"").toLowerCase() ] ); for(var i in this.elem.childNodes) Component.prototype.css.call( this.elem.childNodes[i], schema[ (this.elem.childNodes[i].tagName||"").toLowerCase() ] ); }, css: function( styles ){ if( !styles ) return ;

var elem = this.elem || this;//maybe a component or DOM Element if(this.styles){ for(var prop in this.styles) elem.style[prop] = this.styles[ prop ]; }

for(var prop in styles){ var key = "", change = false;

for(var j = 0; j < prop.length; j ++){ if( prop[j] == '-' ) change = true; else{ key += change ? prop[j].toUpperCase() : prop[j]; change = false; } }

if( typeof styles[ prop ] == "function" ) elem.addEventListener(key, styles[prop]); else elem.style[ key ] = styles[ prop ];

if(this.styles) this.styles[ key ] = styles[ prop ]; } }, render: function( offsetX ){ this.adaptWidth();

this.css({ "position": "absolute", "left": (offsetX+this.left) +"px", "top": this.top +"px", "visibility": "visible" }); }, toString: function(){ return "["+this.elem.tagName.toLowerCase() +"] "+ this.elem.innerHTML.substr(0,50)+"\n"; } };

var Supp = { upgradeText: function(){ for(var i = 0, i_len = this.elem.childNodes.length; i < i_len; i ++){ var node = this.elem.childNodes[i]; if( !node.attributes ){ var child = document.createElement("span"); child.innerHTML = node.textContent; this.elem.replaceChild(child, node); } } } };

function Flow( configs ){ this.span = configs.span; this.padding = configs.padding;

this.spacing = configs.spacing; this.marginCross= configs.marginCross; this.marginTop = configs.marginTop; this.height = window.innerHeight - configs.marginTop - configs.marginBottom;

this.page = 0; this.frontier = 0; this.reserve = 0; this.usedW = 0;

this.comps = []; this.buffer = []; this.isVisible = true; } Flow.prototype = { constructor: Flow, operate: function(){ var wait = false; while( !wait && this.buffer.length > 0 ){ switch( this.buffer[0].state ){ case "dispose": this.buffer.shift(); break;

case "wait": this.buffer[0].adaptWidth(); if( this.buffer[0].state == "wait" ){ wait = true; break; } case "unused": var comp = this.buffer.shift(), h = comp.adaptWidth();

if( this.page == 0 ) this.page = 1; else if( this.usedW == 0 || this.usedW + comp.span > this.span ){ if( this.frontier + h > this.height ){

this.page += 1; this.frontier = 0; } else this.frontier+= this.spacing;

this.reserve = this.frontier; this.usedW = 0; }

comp.left = utils.realWidth( this.span * (this.page-1) + this.usedW + this.page*this.padding ); comp.top = this.reserve + this.marginTop; comp.state = "used";

this.usedW += comp.span; this.frontier = Math.max( this.frontier, this.reserve+h );

this.comps.push( comp ); } } }, getSpan: function( s ){ var span = s ? s.value : this.span; span = (span < 0) ? (span + this.span) : Math.min(span, this.span); return span; },

script: function( node ){ var t = new Component( node, node["name"], this.getSpan( node["span"] )); Supp.upgradeText.call(t); t.updataSchema(); t.adaptWidth();

this.buffer.push( t ); }, image: function( node ){ var i = new Component( node, node["name"], this.getSpan( node["span"] )); i.updataSchema(); i.adaptWidth();

this.buffer.push( i ); }, title: function( node ){ var t = new Component( node, node["name"], this.getSpan( node["span"] )); t.updataSchema(); t.adaptWidth();

this.buffer.push( t ); },

hide: function(){ if( this.isVisible ){ for(var i = 0, i_len = this.comps.length; i < i_len; i ++) this.comps[ i ].elem.style["visibility"] = "hidden"; } }, getBreadth: function(){ return this.page * this.span + (this.page+1) * this.marginCross; }, render: function( offsetX ){ this.isVisible = true; this.operate();

for(var i = 0, i_len = this.comps.length; i < i_len; i ++) this.comps[ i ].render( offsetX ); } };

function Scroll( height ){ this.flows = []; } Scroll.prototype = { constructor: Scroll, render: function( position ){ var accum = 0, d = [];

for(var i = 0, i_len = this.flows.length; i < i_len; i ++){ var flow = this.flows[i], offsetX = accum - position, breadth = flow.getBreadth(), width = window.innerWidth ;

if( offsetX < -breadth || offsetX > width ) flow.hide(); else d.push( [flow, offsetX] );

accum += breadth; } for(var j in d) d[j][0].render( -d[j][1] );

return accum; } };

function SideBar( iU_boot ){ this.boot = iU_boot; iU_boot.id = "UACSboot"; iU_boot.style["display"] = "none";

this.logo = iU_boot.children[0];

this.sponsors = []; for(var i = 1; i < iU_boot.children.length; i ++) this.sponsors.push( iU_boot.children[i] );

this.headings = []; } SideBar.prototype = { constructor: SideBar, render: function( width ){ width = Math.max(width, window.innerWidth*consts.SIDE); var ratio = ((width / window.innerWidth) - consts.SIDE ) / ( 1 - consts.SIDE );

Component.prototype.css.call(this.boot, { "display": "block", "position": "absolute", "width": width + "px", "height": window.innerHeight + "px", "background-color": "black", "visibility": "visible" }); if(ratio > 0){ Component.prototype.css.call(this.logo, { "display": "block", "position": "absolute", "top": Math.floor(window.innerHeight*0.2) + "px", "left": Math.floor(window.innerWidth *0.3) + "px", "width": Math.floor(window.innerWidth *0.4) + "px", "opacity": ratio }); } else Component.prototype.css.call(this.logo, { "display": "none" });

var accum = 0; for(var i = 0; i < this.headings.length; i ++){ Component.prototype.css.call(this.headings[i], { "position": "inherit", "width": Math.floor(window.innerWidth *consts.SIDE) + "px", "visibility":"visible" }); var h = this.headings[i].clientHeight; Component.prototype.css.call(this.sponsors[i], { "position": "absolute", "top": accum + "px", "left": "0", "visibility":"visible", "opacity": 1.0-ratio }); accum += h; }

accum = 0; for(var i = 0; i < this.sponsors.length; i ++){ Component.prototype.css.call(this.sponsors[i].children[0], { "position": "inherit", "width": Math.floor(window.innerWidth *consts.SIDE) + "px", "visibility":"visible" }); accum += this.sponsors[i].children[0].clientHeight; Component.prototype.css.call(this.sponsors[i], { "position": "absolute", "top": (window.innerHeight - accum) + "px", "left": "0", "visibility":"visible", "opacity": 1.0-ratio }); }


} }

var fn = { initRoot: function( overflow ){ var iU_root = document.getElementById("igemUCAS");

if( !iU_root ) return null;

iU_root.style.visibility = "hidden"; iU_root.style["z-index"] = "-100";

if( overflow ){ var webpage = document.getElementsByTagName("body")[0]; Component.prototype.css.call(webpage,{ "overflow-x": "hidden", "overflow-y": "hidden", "padding" : "0", "margin" : "0", "background-color": "black" }); }

inspect("fn.initRoot").assert(iU_root, ""); return iU_root; }, initWebGL: function( iU_root ){ var canvas = document.createElement("canvas");

canvas.id = "bgCanvas"; canvas.width = window.innerWidth; canvas.height = window.innerHeight; iU_root.parentNode.appendChild(canvas);

var gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');

if(gl) Component.prototype.css.call(canvas, { "position":"absolute", "top":"0", "left":"0", "z-index":"-90", "overflow":"hidden", "visibility":"visible" });

return gl; }, initBase: function( iU_root ){ var iU_base = document.createElement("div"); iU_base.id = "UCASigem"; iU_base.style.position = "absolute";

iU_root.parentNode.appendChild(iU_base); return iU_base; },

root_not_found: function(){

}, gl_not_supported: function(){

}, defaultConfig: { overflow: true, webgl: true } }

function igemUCAS(){ schema = utils.merge( schema, window.schema||{} );

var iU_root = fn.initRoot( (window.configs||fn.defaultConfig).overflow ); if( !iU_root ) return fn.root_not_found();

var iU_base = fn.initBase( iU_root ); Component.prototype.iU_base = iU_base;

var sidebar = new SideBar( iU_root.children[0] ); iU_root.parentNode.appendChild( sidebar.boot );

var gl = (window.configs||fn.defaultConfig).webgl || fn.initWebGL( iU_root ); if( !gl ) fn.gl_not_supported();

var schema = utils.merge( fn.defaultSchema, window.schema||{} ); Flow.prototype.schema = schema;

var scroll = new Scroll( window.innerHeight ); while( iU_root.children.length > 0 ){ var page = iU_root.children[0];

if( (page.tagName||"").toLowerCase() == "div" ){ var spacing = page["spacing"] ? page["spacing"].value.split(/\t/): "", flow = new Flow({ span: page["span"] ? page["span"].value : 8, padding: page["padding"] ? page["padding"].value : 1, spacing: parseInt(spacing[0]) || 40, marginTop: parseInt(spacing[1]) || 100, marginBottom: spacing[2] ? parseInt(spacing[2]) : (parseInt(spacing[1]) || 100) });

scroll.flows.push(flow);

while( page.children.length > 0 ){ var node = page.children[0]; switch( ((node||{}).tagName||"").toLowerCase() ){ case "p": flow.script(node); break; case "b": flow.title(node); break; case "img": flow.image(node); break; default: page.removeChild(node); break; } } } iU_root.removeChild( page ); } iU_root.parentNode.removeChild(iU_root);

var offset = window.innerWidth;

var scrollHandler = function( ev ){ ev = ev || window.event; offset -= ev.wheelDelta || ev.detail; offset = Math.min(offset, window.innerWidth); sidebar.render( offset ); scroll .render( offset ); };

//document.addEventListener("DOMMouseScroll", scrollHandler , false); window.onmousewheel = document.onmousewheel = scrollHandler; scrollHandler( {detail: 0} ); } igemUCAS.displayErrors = inspect.displayErrors;

return igemUCAS; });