penguin/AstroJS

Javascript framework for my blog

botanjs/src/Astro/Blog/AstroEdit/Visualizer/Snippet/SiteFile.js

raw ยท 5816 bytes

(function ()
{
	var ns = __namespace( "Astro.Blog.AstroEdit.Visualizer.Snippet" );

	/** @type {System.utils.IKey} */
	var IKey                              = __import( "System.utils.IKey" );
	/** @type {System.utils.DataKey} */
	var DataKey                           = __import( "System.utils.DataKey" );
	/** @type {Dandelion.IDOMElement} */
	var IDOMElement                       = __import( "Dandelion.IDOMElement" );
	/** @type {Dandelion} */
	var Dand                              = __import( "Dandelion" );
	/** @type {Components.MessageBox} */
	var MessageBox                        = __import( "Components.MessageBox" );
	/** @type {Astro.Blog.Config} */
	var Config                            = __import( "Astro.Blog.Config" );
	/** @type {System.utils.Perf} */
	var Perf                              = __import( "System.utils.Perf" );
	/** @type {Astro.utils.Date} */
	var XDate                             = __import( "Astro.utils.Date" );


	var escapeStr = ns[ NS_INVOKE ]( "escapeStr" );

	var getData = __import( "System.Net.getData" );

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

	var sitefile = function ( insertSnippet, snippetWrap, createContext, override )
	{
		config = Config.get( "SiteFile" );
		if( !config ) throw new Error( "config is not defined" );

		var temp, i, j

		, handler = function ()
		{
			// Input fields
			var v_snippetInput = Dand.wrap("input", null, "v_snippet_input_single", null, new IKey("type", "text"));

			// Popup MessageBox
			new MessageBox("Insert site file", v_snippetInput, "OK", "Cancel", visualizer.bind({code: v_snippetInput})).show();
		}

		, __applyData = function (e)
		{
			if( typeof( e ) == "string" )
				e = JSON.parse(e);

			var _stage = IDOMElement( this.stage );

			var isAlbum = false;
			// Check if this is an album
			if( isAlbum = ( "files" in e ) )
			{
				e.file = e.files[0];
				_stage.setAttribute( new DataKey( "collection", 1 ) );
			}

			var s, m, l;

			var finfo = e.file;
			var content = this.stage.firstChild;
			var desc = this.stage.lastChild;
			var _hash = finfo.hash;

			switch ( finfo.type )
			{
				case "image":

					this.stage.removeChild(content);
					// Default size is large
					var _image = Dand.wrapna( "img", new IKey("src", config.path.image.large + _hash + ".jpg") );
					var keys = [ new IKey( "type", "radio" ), new IKey( "name", "size_grp" + Perf.uuid ) ];
					var sid;
					var selectionChanged = function ()
						{
							_stage.setAttribute( new DataKey( "size", this.size ) );
							//// Handles the size selection
							switch(this.size)
							{
								case "small":
									_image.setAttribute( "src", config.path.image.small + _hash + ".jpg" );
									break;
								case "medium":
									_image.setAttribute( "src", config.path.image.medium + _hash + ".jpg" );
									break;
								case "large":
									_image.setAttribute( "src", config.path.image.large + _hash + ".jpg" );
									break;
							}
						};

					this.stage.insertBefore( _image, desc );

					desc.removeChild( desc.firstChild );
					desc.appendChild(
						Dand.wrape([
							Dand.textNode( ( isAlbum ? "[Album] " : "" ) + "Size: ")
							, s = Dand.wrapna("input", keys.concat( new IKey("id", sid = "size_" + Perf.uuid) ))
							, Dand.wrapne("label", "small", new IKey("for", sid))
							, m = Dand.wrapna("input", keys.concat( new IKey("id", sid = "size_" + Perf.uuid) ))
							, Dand.wrapne("label", "medium", new IKey("for", sid))
							, l = Dand.wrapna("input", keys.concat( new IKey("checked", "1"), new IKey("id", sid = "size_" + Perf.uuid) ))
							, Dand.wrapne("label", "large (default)", new IKey("for", sid))
					   ])
					);

					// ad handlers to handles size change event
					IDOMElement(s).addEventListener("Change", selectionChanged.bind({size: "small"}));
					IDOMElement(m).addEventListener("Change", selectionChanged.bind({size: "medium"}));
					IDOMElement(l).addEventListener("Change", selectionChanged.bind({size: "large"}));
					break;
				case "audio":
					// TODO
					break;
				default:
					content.firstChild.nodeValue = "Regular file: " + finfo.name;
					content.style.paddingBottom = "2em";
					content.appendChild( Dand.wrap( "br" ) );
					content.appendChild( Dand.textNode( "Date: " + XDate.pretty( new Date( finfo.date_created ), true ) ) );
			}

		}

		, loadFailed = function (e) { }

		, visualizer = function ( submitted, override )
		{

			var hash = override ? override.value : this.code.value
				, _obj = {file: hash};

			if ( submitted && hash )
			{
				// Visualize component

				temp =  Dand.wrapc("v_box"
					, [
						Dand.wrape("Getting information from Server ...")
						, Dand.wrapc("v_description", "Site file (hash): " + hash)
					]
					, [
						new DataKey( "value", hash )
						, new DataKey( "size", "large" )
						, new IKey( "style", "max-height: 150px;" )
					]
				);

				// Get data from site library
				getData( config.path.info + hash, __applyData.bind({stage: temp, hash: hash}), loadFailed );

				insertSnippet(j = snippetWrap("SiteFile", temp), Boolean(override));

				// Set context menu
				createContext(null, j);
			}
		};

		if (override)
		{
			visualizer(true, override);
			override = false;
		}
		else
		{
			return handler;
		}
		return true;

	};

	var compile = function ( stage )
	{
		stage = IDOMElement( stage );
		// [html][/html]
		var options = "";
		var opt;
		if( opt = stage.getDAttribute( "collection" ) )
		{
			options += " collection=\"1\"";
		}
		if( opt = stage.getDAttribute( "size" ) )
		{
			options += " size=\"" + opt + "\"";
		}

		return "[sitefile" + options + "]"
			+ escapeStr( stage.getDAttribute( "value" ) )
			+ "[/sitefile]";
	};

	__static_method( sitefile, "compile", compile );

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

})();