penguin/AstroJS

Javascript framework for my blog

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

raw ยท 2656 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" );

	
	var acquirelib = function(insertSnippet, snippetWrap, createContext, override)
	{
		var temp, i, j
		
		, handler = function ()
		{
			// Input fields
			var input_text = Dand.wrap('input', null, "v_snippet_input_single", null, new IKey("type", "text"));
			
			if (this._stage)
			{
				input_text.value = this._text;
			}
			
			// Popup MessageBox
			new MessageBox(
				"Acquire library" + ( this._stage ? " (Edit)" : "" )
				, Dand.wrape([ Dand.wrapc( "v_instruction flsf", "Module" ) , input_text ])
				, "OK", "Cancel"
				, visualizer.bind({ text:input_text, stage: this._stage })
			).show();
		}
		
		, visualizer = function (submitted, override)
		{
			var src = override ? override.value : this.text.value
			, stage = this.stage;
			
			if (submitted && src)
			{
				// Shared clause
				
				if (!stage)
				{
					if (!src) return;
					
					
					// Visualize component
					temp = Dand.wrap(
						'span'
						, null
						, "flsf"
						, "AcquireLib: " + src
						, [
							new DataKey( "value", src )
							, new IKey(
								"style", "background-color: #444; color: white; padding: 0.2em 0.5em;"
							)
						]
					);
					insertSnippet(j = snippetWrap("AcquireLib", temp, false, 'span'), Boolean(override));
				}
				else
				{
					IDOMElement(stage).setAttribute( new DataKey("value", src) );
					
					stage.removeChild(stage.firstChild);
					stage.appendChild(Dand.textNode("AcquireLib: " + src));
					
					// set temp back to stage
					temp = stage;
				}
				
				i = {_text: src, _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)
	{
		var element = IDOMElement(stage);
		
		return "[acquirelib]" + element.getDAttribute("value") + "[/acquirelib]";
	};

	__static_method( acquirelib, "compile", compile );

	ns[ NS_EXPORT ]( EX_CLASS, "AcquireLib", acquirelib );
})();