penguin/AstroJS

Javascript framework for my blog

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

raw ยท 4674 bytes

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

	/** @type {System.Debug} */
	var debug                            = __import( "System.Debug" );
	/** @type {System.utils.IKey} */
	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, nObj )
	{
		if( !config ) throw new Error( "config is not defined" );
		// TODO: Make a trigger for downloading from server
		var stage = Dand.id( id );
		if( !stage )
		{
			debug.Info( "[SiteFile] id not found: " + id );
			return;
		}
		this.id = id;
		this.hash = hash;
		this.type = IDOMElement( stage ).getDAttribute( "type" );

		if(!( this.type == "default" || this.type == null )) return this;
		if( stage.getAttribute( "noauto" ) != null ) return this;

		var applyStructure = function( obj )
		{
			return _applyStructure( JSON.parse( obj ).file );
		};

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

			switch( finfo.type )
			{
				case "image":
					var node = Dand.tag( "img", false, stage )[0];

					switch( IDOMElement(stage).getDAttribute('size') )
					{
						case "small":
							node.src = finfo.thumbs.small || ( config.path.image.small + hash + '.jpg' );
							break;
						case "medium":
							node.src = finfo.thumbs.medium || ( config.path.image.medium + hash + '.jpg' );
							break;
						case "original":
							node.src = config.path.image.original + hash + '.jpg';
							break;
						default: // large
							node.src = finfo.thumbs.large || ( config.path.image.large + hash + '.jpg' );
					}

					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);
			}

		};

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

		if( nObj )
		{
			_applyStructure( nObj );
		}
		else
		{
			this.loadInfo( applyStructure, loadFailed );
		}
	};

	SiteFile.prototype.loadInfo = function( success, failed )
	{
		getData( config.path.info + this.hash, success, failed );
	};

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

	Bootstrap.regInit( init );

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