penguin/AstroJS

Javascript framework for my blog

botanjs/src/Astro/Blog/Components/Album.js

raw ยท 2514 bytes

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

	/** @type {System.Cycle} */
	var Cycle                            = __import( "System.Cycle" );
	/** @type {typeof System.utils.IKey} */
	var IKey                             = __import( "System.utils.IKey" );
	/** @type {typeof System.utils.DataKey} */
	var DataKey                          = __import( "System.utils.DataKey" );
	/** @type {typeof Dandelion} */
	var Dand                             = __import( "Dandelion" );
	/** @type {function(...?): Dandelion.IDOMElement} */
	var IDOMElement                      = __import( "Dandelion.IDOMElement" );
	/** @type {Astro.Bootstrap} */
	var Bootstrap                        = __import( "Astro.Bootstrap" );
	/** @type {System.utils.Perf} **/
	var Perf                             = __import( "System.utils.Perf" );
	/** @type {Astro.Blog.Config} */
	var Conf                             = __import( "Astro.Blog.Config" );

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

	var SiteFile = ns[ NS_INVOKE ]( "SiteFile" );
	var BLANK_IMG = __import( "Dandelion.StaticRes.BLANK_IMG" );

	var Album = function ( id, hash )
	{
		if( !config ) throw new Error( "config is not defined" );
		var stage = Dand.id( id, true );

		var sf = new SiteFile( id, hash );
		if( sf.type != "album" ) return;

		var ostage = stage.element;

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

			var files = finfo.files;
			var l = files.length;

			// XXX: Not traditional!
			// Maximum of 4 for listing
			if( ostage.parentNode.className == "h_img" && 4 < l )
			{
				l = 4;
			}

			for( var i = 0; i < l; i ++ )
			{
				/** @type {_AstJson_.SiteFile} */
				var file = files[i];
				var uuid = Perf.uuid;

				ostage.appendChild( Dand.wrapne(
					"div"
					, Dand.wrapna( "img", [ new IKey( "src", BLANK_IMG ) ] )
					, [ new IKey( "id", uuid ), new DataKey( "size", "medium" ) ]
				) );

				Cycle.next( function()
				{
					new SiteFile( this.id, this.info.hash, this.info );
				}.bind({ id: uuid, info: file }) );
			}
		};

		var loadFailed = function( obj )
		{
			console.log( obj );
		};

		sf.loadInfo( applyStructure, loadFailed );
	};

	var init = function()
	{
		config = Conf.get( "SiteFile" );
		if( config )
		{
			var SFOs = Conf.get( "SFObjects" );
			for( var i in SFOs )
			{
				var f = SFOs[i];
				new Album( f[0], f[1] );
			}
		}
	};

	Bootstrap.regInit( init );

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