penguin/AstroJS

Javascript framework for my blog

botanjs/src/Astro/Blog/Components/Entry/Block.js

raw ยท 2305 bytes

(function(){
	var ns = __namespace( "Astro.Blog.Components.Entry" );

	/** @type {System.Cycle} */
	var Cycle                            = __import( "System.Cycle" );
	/** @type {System.utils.Perf} */
	var Perf                             = __import( "System.utils.Perf" );
	/** @type {Dandelion} */
	var Dand                             = __import( "Dandelion" );
	/** @type {Astro.Bootstrap} */
	var Bootstrap                        = __import( "Astro.Bootstrap" );

	/** @type {Astro.Blog.Config} */
	var Conf                                      = __import( "Astro.Blog.Config" );
	/** @type {Astro.Blog.Components.SiteFile} */
	var SiteFile                                  = __import( "Astro.Blog.Components.SiteFile" );

	/** @type {_AstConf_.SiteFile} */
	var config = null;

	var Block  = function ( element )
	{
		if( !config ) throw new Error( "Config is not defined" );
		var slide = Dand.glass( "slide", false, element )[0];
		var delayCont = function( firstTime )
		{
			Cycle.delay(
				function()
				{
					slide.getAttribute( "on" )
						? slide.removeAttribute( "on" )
						: slide.setAttribute( "on", 1 )
						;
					delayCont( false );
				}
				, firstTime
					? ( 500 + Math.random()*4500 )
					: ( 5500 + Math.random()*4500 )
			);
		};

		delayCont( true );

		var banner = Dand.glass( "banner", false, element )[0];
		var bId = banner.getAttribute( "id" );

		var placeBackground = function( obj )
		{
			/** @type {_AstJson_.SiteFile} */
			var finfo = JSON.parse( obj );

			/** @type {_AstJson_.SiteFile} */
			var file = finfo.files[0];

			var path = config.path.image.medium + file.hash + ".jpg";
			banner.style.backgroundImage = "url(" + path  + ")"; 
		};

		var placeError = function( obj )
		{
			throw new Error( obj );
		};

		for( var i in config.files )
		{
			var f = config.files[i];
			var id = f[0];
			if( id == bId )
			{
				new SiteFile( id, f[1] ).loadInfo( placeBackground, placeError );
			}
		}
	};

	var init = function()
	{
		config = Conf.get( "SiteFile" );

		var stage = Dand.glass( "block-entries" )[0];
		var everyEntries = Dand.glass( "block", false, stage );
		for( var i = 0; i < everyEntries.length; i ++ )
		{
			new Block( everyEntries[ i ] );
		}
	};

	Bootstrap.regInit( init );

	ns[ NS_EXPORT ]( EX_CLASS, "Block", Block );
})();