penguin/AstroJS

Javascript framework for my blog

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

raw ยท 8340 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 {System.Cycle.Trigger} */
	var Trigger                           = __import( "System.Cycle.Trigger" );
	/** @type {Astro.utils.Date} */
	var XDate                             = __import( "Astro.utils.Date" );

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

	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"))
				, input_preferred = Dand.wrapna( "input", new IKey( "type", "checkbox" ) )
				;

			if ( this._stage )
			{
				v_snippetInput.value = this._content;
				input_preferred.checked = ( this._preferred == "on" );
			}

			// Popup MessageBox
			new MessageBox(
				"Insert site file"
				, Dand.wrape([
					v_snippetInput
					, Dand.wrape( Dand.wrapne( "label", [ input_preferred, "Preferred" ] ) )
				])
				, "OK", "Cancel"
				, visualizer.bind({
					code: v_snippetInput
					, preferred: input_preferred
					, stage: this._stage
				})
			).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, o;

			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 detectImageChange = function() { return _image.complete; };
					var setImageSizeProp = function()
					{
						_stage.setAttribute( new DataKey( "dimensions", _image.naturalWidth + "x" + _image.naturalHeight ) );
					};

					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;
							case "original":
								_image.setAttribute( "src", config.path.image.original + _hash + ".jpg" );
								break;
						}

						Trigger.register( detectImageChange, setImageSizeProp, 100 );
					};

					Trigger.register( detectImageChange, setImageSizeProp, 100 );
					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))
							, o = Dand.wrapna("input", keys.concat(  new IKey("id", sid = "size_" + Perf.uuid) ))
							, Dand.wrapne("label", "original", 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"}));
					IDOMElement(o).addEventListener("Change", selectionChanged.bind({size: "original"}));

					var hasValue = _stage.getDAttribute( "size" );
					if( hasValue )
					{
						switch( hasValue )
						{
							case "small": s.checked = 1; break;
							case "medium": m.checked = 1; break;
							case "large": l.checked = 1; break;
							case "original": o.checked = 1; break;
						}

						selectionChanged.bind({ size: hasValue })();
					}

					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, _obj, stage = this.stage;

			if( override )
			{
				hash = override.value;
				preferred = override.preferred ? "on" : "";
			}
			else
			{
				hash = this.code.value;
				preferred = this.preferred.checked ? "on" : "";
			}

			_obj = { file: hash };

			if ( submitted && hash )
			{
				// Visualize component
				if (!stage)
				{
					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", ( override && override.size ) || "large" )
							, new IKey( "style", "max-height: 150px;" )
							, new DataKey( "preferred", preferred )
						]
					);

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

					insertSnippet(
						j = snippetWrap( "SiteFile", temp )
						, !!override
					);
				}
				else
				{
					IDOMElement( stage ).setAttribute([
						new DataKey( "value", hash )
						, new DataKey( "preferred", preferred )
					]);
					getData( config.path.info + hash, __applyData.bind({stage: temp, hash: hash}), loadFailed );
				}

				i = {
					_content: hash
					, _preferred: preferred || "off"
					, _stage: temp
				};

				// Set context menu
				createContext( i, j, handler );

			}
		};

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

	};

	var compile = function ( stage )
	{
		stage = IDOMElement( stage );
		var options = "";
		var opt;
		if( opt = stage.getDAttribute( "collection" ) )
		{
			options += " collection=\"1\"";
		}
		if( opt = stage.getDAttribute( "size" ) )
		{
			options += " size=\"" + opt + "\"";
		}
		if( opt = stage.getDAttribute( "dimensions" ) )
		{
			options += " dimensions=\"" + opt + "\"";
		}
		if( opt = stage.getDAttribute( "preferred" ) )
		{
			options += " preferred=\"" + opt + "\"";
		}

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

	__static_method( sitefile, "compile", compile );

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

})();