Team:CityU HK/lablog/project1.js

From 2014.igem.org

(Difference between revisions)
 
Line 1: Line 1:
-
jQuery.easing['jswing'] = jQuery.easing['swing'];
+
(function( window, $, undefined ) {
 +
 +
/*
 +
* smartresize: debounced resize event for jQuery
 +
*
 +
* latest version and complete README available on Github:
 +
* https://github.com/louisremi/jquery.smartresize.js
 +
*
 +
* Copyright 2011 @louis_remi
 +
* Licensed under the MIT license.
 +
*/
-
jQuery.extend( jQuery.easing,
+
var $event = $.event, resizeTimeout;
-
{
+
 
-
def: 'easeOutQuad',
+
$event.special.smartresize = {
-
swing: function (x, t, b, c, d) {
+
setup: function() {
-
//alert(jQuery.easing.default);
+
$(this).bind( "resize", $event.special.smartresize.handler );
-
return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
+
},
-
},
+
teardown: function() {
-
easeInQuad: function (x, t, b, c, d) {
+
$(this).unbind( "resize", $event.special.smartresize.handler );
-
return c*(t/=d)*t + b;
+
},
-
},
+
handler: function( event, execAsap ) {
-
easeOutQuad: function (x, t, b, c, d) {
+
// Save the context
-
return -c *(t/=d)*(t-2) + b;
+
var context = this,
-
},
+
args = arguments;
-
easeInOutQuad: function (x, t, b, c, d) {
+
 
-
if ((t/=d/2) < 1) return c/2*t*t + b;
+
// set correct event type
-
return -c/2 * ((--t)*(t-2) - 1) + b;
+
event.type = "smartresize";
-
},
+
 
-
easeInCubic: function (x, t, b, c, d) {
+
if ( resizeTimeout ) { clearTimeout( resizeTimeout ); }
-
return c*(t/=d)*t*t + b;
+
resizeTimeout = setTimeout(function() {
-
},
+
jQuery.event.handle.apply( context, args );
-
easeOutCubic: function (x, t, b, c, d) {
+
}, execAsap === "execAsap"? 0 : 100 );
-
return c*((t=t/d-1)*t*t + 1) + b;
+
}
-
},
+
};
-
easeInOutCubic: function (x, t, b, c, d) {
+
 
-
if ((t/=d/2) < 1) return c/2*t*t*t + b;
+
$.fn.smartresize = function( fn ) {
-
return c/2*((t-=2)*t*t + 2) + b;
+
return fn ? this.bind( "smartresize", fn ) : this.trigger( "smartresize", ["execAsap"] );
-
},
+
};
-
easeInQuart: function (x, t, b, c, d) {
+
-
return c*(t/=d)*t*t*t + b;
+
$.Accordion = function( options, element ) {
-
},
+
-
easeOutQuart: function (x, t, b, c, d) {
+
this.$el = $( element );
-
return -c * ((t=t/d-1)*t*t*t - 1) + b;
+
// list items
-
},
+
this.$items = this.$el.children('ul').children('li');
-
easeInOutQuart: function (x, t, b, c, d) {
+
// total number of items
-
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
+
this.itemsCount = this.$items.length;
-
return -c/2 * ((t-=2)*t*t*t - 2) + b;
+
-
},
+
// initialize accordion
-
easeInQuint: function (x, t, b, c, d) {
+
this._init( options );
-
return c*(t/=d)*t*t*t*t + b;
+
-
},
+
};
-
easeOutQuint: function (x, t, b, c, d) {
+
-
return c*((t=t/d-1)*t*t*t*t + 1) + b;
+
$.Accordion.defaults = {
-
},
+
// index of opened item. -1 means all are closed by default.
-
easeInOutQuint: function (x, t, b, c, d) {
+
open : -1,
-
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
+
// if set to true, only one item can be opened. Once one item is opened, any other that is opened will be closed first
-
return c/2*((t-=2)*t*t*t*t + 2) + b;
+
oneOpenedItem : false,
-
},
+
// speed of the open / close item animation
-
easeInSine: function (x, t, b, c, d) {
+
speed : 600,
-
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
+
// easing of the open / close item animation
-
},
+
easing : 'easeInOutExpo',
-
easeOutSine: function (x, t, b, c, d) {
+
// speed of the scroll to action animation
-
return c * Math.sin(t/d * (Math.PI/2)) + b;
+
scrollSpeed : 900,
-
},
+
// easing of the scroll to action animation
-
easeInOutSine: function (x, t, b, c, d) {
+
scrollEasing : 'easeInOutExpo'
-
return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
+
    };
-
},
+
-
easeInExpo: function (x, t, b, c, d) {
+
$.Accordion.prototype = {
-
return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
+
_init : function( options ) {
-
},
+
-
easeOutExpo: function (x, t, b, c, d) {
+
this.options = $.extend( true, {}, $.Accordion.defaults, options );
-
return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
+
-
},
+
// validate options
-
easeInOutExpo: function (x, t, b, c, d) {
+
this._validate();
-
if (t==0) return b;
+
-
if (t==d) return b+c;
+
// current is the index of the opened item
-
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
+
this.current = this.options.open;
-
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
+
-
},
+
// hide the contents so we can fade it in afterwards
-
easeInCirc: function (x, t, b, c, d) {
+
this.$items.find('div.st-content').hide();
-
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
+
-
},
+
// save original height and top of each item
-
easeOutCirc: function (x, t, b, c, d) {
+
this._saveDimValues();
-
return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
+
-
},
+
// if we want a default opened item...
-
easeInOutCirc: function (x, t, b, c, d) {
+
if( this.current != -1 )
-
if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
+
this._toggleItem( this.$items.eq( this.current ) );
-
return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
+
-
},
+
// initialize the events
-
easeInElastic: function (x, t, b, c, d) {
+
this._initEvents();
-
var s=1.70158;var p=0;var a=c;
+
-
if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
+
},
-
if (a < Math.abs(c)) { a=c; var s=p/4; }
+
_saveDimValues : function() {
-
else var s = p/(2*Math.PI) * Math.asin (c/a);
+
-
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
+
this.$items.each( function() {
-
},
+
-
easeOutElastic: function (x, t, b, c, d) {
+
var $item = $(this);
-
var s=1.70158;var p=0;var a=c;
+
-
if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
+
$item.data({
-
if (a < Math.abs(c)) { a=c; var s=p/4; }
+
originalHeight : $item.find('a:first').height(),
-
else var s = p/(2*Math.PI) * Math.asin (c/a);
+
offsetTop : $item.offset().top
-
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
+
});
-
},
+
-
easeInOutElastic: function (x, t, b, c, d) {
+
});
-
var s=1.70158;var p=0;var a=c;
+
-
if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
+
},
-
if (a < Math.abs(c)) { a=c; var s=p/4; }
+
// validate options
-
else var s = p/(2*Math.PI) * Math.asin (c/a);
+
_validate : function() {
-
if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
+
-
return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
+
// open must be between -1 and total number of items, otherwise we set it to -1
-
},
+
if( this.options.open < -1 || this.options.open > this.itemsCount - 1 )
-
easeInBack: function (x, t, b, c, d, s) {
+
this.options.open = -1;
-
if (s == undefined) s = 1.70158;
+
-
return c*(t/=d)*t*((s+1)*t - s) + b;
+
},
-
},
+
_initEvents : function() {
-
easeOutBack: function (x, t, b, c, d, s) {
+
-
if (s == undefined) s = 1.70158;
+
var instance = this;
-
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
+
-
},
+
// open / close item
-
easeInOutBack: function (x, t, b, c, d, s) {
+
this.$items.find('a:first').bind('click.accordion', function( event ) {
-
if (s == undefined) s = 1.70158;  
+
-
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
+
var $item = $(this).parent();
-
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
+
-
},
+
// close any opened item if oneOpenedItem is true
-
easeInBounce: function (x, t, b, c, d) {
+
if( instance.options.oneOpenedItem && instance._isOpened() && instance.current!== $item.index() ) {
-
return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
+
-
},
+
instance._toggleItem( instance.$items.eq( instance.current ) );
-
easeOutBounce: function (x, t, b, c, d) {
+
-
if ((t/=d) < (1/2.75)) {
+
}
-
return c*(7.5625*t*t) + b;
+
-
} else if (t < (2/2.75)) {
+
// open / close item
-
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
+
instance._toggleItem( $item );
-
} else if (t < (2.5/2.75)) {
+
-
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
+
return false;
-
} else {
+
-
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
+
});
 +
 +
$(window).bind('smartresize.accordion', function( event ) {
 +
 +
// reset orinal item values
 +
instance._saveDimValues();
 +
 +
// reset the content's height of any item that is currently opened
 +
instance.$el.find('li.st-open').each( function() {
 +
 +
var $this = $(this);
 +
$this.css( 'height', $this.data( 'originalHeight' ) + $this.find('div.st-content').outerHeight( true ) );
 +
 +
});
 +
 +
// scroll to current
 +
if( instance._isOpened() )
 +
instance._scroll();
 +
 +
});
 +
 +
},
 +
// checks if there is any opened item
 +
_isOpened : function() {
 +
 +
return ( this.$el.find('li.st-open').length > 0 );
 +
 +
},
 +
// open / close item
 +
_toggleItem : function( $item ) {
 +
 +
var $content = $item.find('div.st-content');
 +
 +
( $item.hasClass( 'st-open' ) )  
 +
 +
? ( this.current = -1, $content.stop(true, true).fadeOut( this.options.speed ), $item.removeClass( 'st-open' ).stop().animate({
 +
height : $item.data( 'originalHeight' )
 +
}, this.options.speed, this.options.easing ) )
 +
 +
: ( this.current = $item.index(), $content.stop(true, true).fadeIn( this.options.speed ), $item.addClass( 'st-open' ).stop().animate({
 +
height : $item.data( 'originalHeight' ) + $content.outerHeight( true )
 +
}, this.options.speed, this.options.easing ), this._scroll( this ) )
 +
 +
},
 +
// scrolls to current item or last opened item if current is -1
 +
_scroll : function( instance ) {
 +
 +
var instance = instance || this, current;
 +
 +
( instance.current !== -1 ) ? current = instance.current : current = instance.$el.find('li.st-open:last').index();
 +
 +
$('html, body').stop().animate({
 +
scrollTop : ( instance.options.oneOpenedItem ) ? instance.$items.eq( current ).data( 'offsetTop' ) : instance.$items.eq( current ).offset().top
 +
}, instance.options.scrollSpeed, instance.options.scrollEasing );
 +
 +
}
 +
};
 +
 +
var logError = function( message ) {
 +
 +
if ( this.console ) {
 +
 +
console.error( message );
 +
 +
}
 +
 +
};
 +
 +
$.fn.accordion = function( options ) {
 +
 +
if ( typeof options === 'string' ) {
 +
 +
var args = Array.prototype.slice.call( arguments, 1 );
 +
 
 +
this.each(function() {
 +
 +
var instance = $.data( this, 'accordion' );
 +
 +
if ( !instance ) {
 +
logError( "cannot call methods on accordion prior to initialization; " +
 +
"attempted to call method '" + options + "'" );
 +
return;
 +
}
 +
 +
if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
 +
logError( "no such method '" + options + "' for accordion instance" );
 +
return;
 +
}
 +
 +
instance[ options ].apply( instance, args );
 +
 +
});
 +
 +
}
 +
else {
 +
 +
this.each(function() {
 +
var instance = $.data( this, 'accordion' );
 +
if ( !instance ) {
 +
$.data( this, 'accordion', new $.Accordion( options, this ) );
 +
}
 +
});
 +
}
}
-
},
+
-
easeInOutBounce: function (x, t, b, c, d) {
+
return this;
-
if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
+
-
return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
+
};
-
}
+
-
});
+
})( window, jQuery );

Latest revision as of 14:20, 10 September 2014

(function( window, $, undefined ) {

/* * smartresize: debounced resize event for jQuery * * latest version and complete README available on Github: * https://github.com/louisremi/jquery.smartresize.js * * Copyright 2011 @louis_remi * Licensed under the MIT license. */

var $event = $.event, resizeTimeout;

$event.special.smartresize = { setup: function() { $(this).bind( "resize", $event.special.smartresize.handler ); }, teardown: function() { $(this).unbind( "resize", $event.special.smartresize.handler ); }, handler: function( event, execAsap ) { // Save the context var context = this, args = arguments;

// set correct event type event.type = "smartresize";

if ( resizeTimeout ) { clearTimeout( resizeTimeout ); } resizeTimeout = setTimeout(function() { jQuery.event.handle.apply( context, args ); }, execAsap === "execAsap"? 0 : 100 ); } };

$.fn.smartresize = function( fn ) { return fn ? this.bind( "smartresize", fn ) : this.trigger( "smartresize", ["execAsap"] ); };

$.Accordion = function( options, element ) {

this.$el = $( element ); // list items this.$items = this.$el.children('ul').children('li'); // total number of items this.itemsCount = this.$items.length;

// initialize accordion this._init( options );

};

$.Accordion.defaults = { // index of opened item. -1 means all are closed by default. open : -1, // if set to true, only one item can be opened. Once one item is opened, any other that is opened will be closed first oneOpenedItem : false, // speed of the open / close item animation speed : 600, // easing of the open / close item animation easing : 'easeInOutExpo', // speed of the scroll to action animation scrollSpeed : 900, // easing of the scroll to action animation scrollEasing : 'easeInOutExpo'

   };

$.Accordion.prototype = { _init : function( options ) {

this.options = $.extend( true, {}, $.Accordion.defaults, options );

// validate options this._validate();

// current is the index of the opened item this.current = this.options.open;

// hide the contents so we can fade it in afterwards this.$items.find('div.st-content').hide();

// save original height and top of each item this._saveDimValues();

// if we want a default opened item... if( this.current != -1 ) this._toggleItem( this.$items.eq( this.current ) );

// initialize the events this._initEvents();

}, _saveDimValues : function() {

this.$items.each( function() {

var $item = $(this);

$item.data({ originalHeight : $item.find('a:first').height(), offsetTop : $item.offset().top });

});

}, // validate options _validate : function() {

// open must be between -1 and total number of items, otherwise we set it to -1 if( this.options.open < -1 || this.options.open > this.itemsCount - 1 ) this.options.open = -1;

}, _initEvents : function() {

var instance = this;

// open / close item this.$items.find('a:first').bind('click.accordion', function( event ) {

var $item = $(this).parent();

// close any opened item if oneOpenedItem is true if( instance.options.oneOpenedItem && instance._isOpened() && instance.current!== $item.index() ) {

instance._toggleItem( instance.$items.eq( instance.current ) );

}

// open / close item instance._toggleItem( $item );

return false;

});

$(window).bind('smartresize.accordion', function( event ) {

// reset orinal item values instance._saveDimValues();

// reset the content's height of any item that is currently opened instance.$el.find('li.st-open').each( function() {

var $this = $(this); $this.css( 'height', $this.data( 'originalHeight' ) + $this.find('div.st-content').outerHeight( true ) );

});

// scroll to current if( instance._isOpened() ) instance._scroll();

});

}, // checks if there is any opened item _isOpened : function() {

return ( this.$el.find('li.st-open').length > 0 );

}, // open / close item _toggleItem : function( $item ) {

var $content = $item.find('div.st-content');

( $item.hasClass( 'st-open' ) )

? ( this.current = -1, $content.stop(true, true).fadeOut( this.options.speed ), $item.removeClass( 'st-open' ).stop().animate({ height : $item.data( 'originalHeight' ) }, this.options.speed, this.options.easing ) )

: ( this.current = $item.index(), $content.stop(true, true).fadeIn( this.options.speed ), $item.addClass( 'st-open' ).stop().animate({ height : $item.data( 'originalHeight' ) + $content.outerHeight( true ) }, this.options.speed, this.options.easing ), this._scroll( this ) )

}, // scrolls to current item or last opened item if current is -1 _scroll : function( instance ) {

var instance = instance || this, current;

( instance.current !== -1 ) ? current = instance.current : current = instance.$el.find('li.st-open:last').index();

$('html, body').stop().animate({ scrollTop : ( instance.options.oneOpenedItem ) ? instance.$items.eq( current ).data( 'offsetTop' ) : instance.$items.eq( current ).offset().top }, instance.options.scrollSpeed, instance.options.scrollEasing );

} };

var logError = function( message ) {

if ( this.console ) {

console.error( message );

}

};

$.fn.accordion = function( options ) {

if ( typeof options === 'string' ) {

var args = Array.prototype.slice.call( arguments, 1 );

this.each(function() {

var instance = $.data( this, 'accordion' );

if ( !instance ) { logError( "cannot call methods on accordion prior to initialization; " + "attempted to call method '" + options + "'" ); return; }

if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) { logError( "no such method '" + options + "' for accordion instance" ); return; }

instance[ options ].apply( instance, args );

});

} else {

this.each(function() { var instance = $.data( this, 'accordion' ); if ( !instance ) { $.data( this, 'accordion', new $.Accordion( options, this ) ); } });

}

return this;

};

})( window, jQuery );