penguin/AstroJS

Javascript framework for my blog

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

raw ยท 4056 bytes

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

	/** @type {System.utils.EventKey} */
	var IKey                             = __import( "System.utils.IKey" );
	/** @type {Dandelion} */
	var Dand                             = __import( "Dandelion" );
	/** @type {Dandelion.IDOMElement} */
	var IDOMElement                      = __import( "Dandelion.IDOMElement" );
	/** @type {Astro.Bootstrap} */
	var Bootstrap                        = __import( "Astro.Bootstrap" );
	/** @type {Astro.Blog.Config} */
	var Conf                             = __import( "Astro.Blog.Config" );

	var getData                          = __import( "System.Net.getData" );
	var getSMStamp                       = __import( "Astro.utils.Date.smstamp" );

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

	var SiteFile  = function ( id, hash )
	{
		if( !config ) throw new Error( "config is not defined" );
		// TODO: Make a trigger for downloading from server
		var stage = Dand.id( id )

			, applyStructure = function( obj )
			{
				// remove loading bubbles
				while( stage.hasChildNodes() ) stage.removeChild(stage.firstChild);

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

				switch( finfo.type ) {

					case "image":
						var node = Dand.wrap('img');
						var k = new IKey( "src", null );

						switch( IDOMElement(stage).getDAttribute('size') )
						{
							case "small":
								k.keyValue = config.path.image.small + hash + '.jpg';
								break;
							case "medium":
								k.keyValue = config.path.image.medium + hash + '.jpg';
								break;
							default: // large
								k.keyValue = config.path.image.large + hash + '.jpg';
						}
						IDOMElement( node ).setAttribute( k );

						stage.appendChild(Dand.wrapne(
							'a', node
							, [
								new IKey( 'href', config.f_host + finfo.src_location )
								, new IKey( 'title', finfo.name )
								, new IKey( 'target', '_blank' )
							]
						));

						break;

					case "audio":
						// TODO
						break;

					default:
						// create a form to post hash string to php
						var	hash_field = Dand.wrapna(
								'input', [ new IKey( 'type', 'hidden' ), new IKey( 'name', 'hash' ) ]
							)
							, name_field = Dand.wrapna(
								'input', [ new IKey( 'type', 'hidden' ), new IKey( 'name', 'name' ) ]
							)
							, link = Dand.wrapne(
								'a', 'download', new IKey( 'href', config.path.download + finfo.name )
							)

							, form = Dand.wrap(
								'form', null, 'sf_regular'
								, [ name_field, hash_field ]
								, [
									new IKey('target', '_blank')
									, new IKey('action', config.path.download + finfo.name)
									, new IKey('method', 'POST')
								]
							)
						;

						hash_field.value = hash;
						name_field.value = finfo.name;

						IDOMElement(link).addEventListener(
							'Click'
							, function(e) {
								form.submit();
								e.preventDefault();
								return false;
							}
						);

						// file name
						form.appendChild( Dand.wrapne( 'span', 'File: ' + finfo.name) );

						// download, submit button
						form.appendChild( Dand.wrapne( 'sup', [ Dand.textNode(" ["), link, Dand.textNode("]") ] ) );

						// hash
						form.appendChild( Dand.wrapne( 'sup', Dand.wrape( 'MD5: ' + hash ) ) );
						// date
						form.appendChild( Dand.wrapne( 'sup', Dand.wrape( 'Date: ' + getSMStamp( new Date( finfo.date_created ) ) ) ) );


						stage.appendChild(form);

				}

			}
			, loadFailed = function( obj )
			{
				while( stage.hasChildNodes() ) stage.removeChild( stage.firstChild );
				stage.appendChild( Dand.wrapc( "sf_failed", Dand.textNode( "Error: Failed to get resources info" ) ) );
			}
			;

		getData( config.path.info + hash, applyStructure, loadFailed );
	};

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

	Bootstrap.regInit( init );

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