penguin/AstroJS

Javascript framework for my blog

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

raw ยท 3720 bytes

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

	/** @type {typeof System.utils.IKey} */
	var IKey                              = __import( "System.utils.IKey" );
	/** @type {typeof System.utils.DataKey} */
	var DataKey                           = __import( "System.utils.DataKey" );
	/** @type {function(...?): Dandelion.IDOMElement} */
	var IDOMElement                       = __import( "Dandelion.IDOMElement" );
	/** @type {typeof Dandelion} */
	var Dand                              = __import( "Dandelion" );
	/** @type {typeof 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 unescapeStr = ns[ NS_INVOKE ]( "unescapeStr" );
	var compileProp = ns[ NS_INVOKE ]( "compileProp" );

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

	/** @type {_AstConf_.AstroEdit} */
	var config;

	var ArticleContent = function ( insertSnippet, snippetWrap, createContext, override )
	{
		config = Config.get( "AstroEdit" );
		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" ) );

			if ( this._stage )
			{
				v_snippetInput.value = this._id;
			}

			// Popup MessageBox
			new MessageBox(
				"ArticleContent"
				, Dand.wrape([
					Dand.wrapc( "v_instruction", "ArticleContent id:" )
					, v_snippetInput
				])
				, "OK", "Cancel"
				, visualizer.bind({
					id: v_snippetInput
					, stage: this._stage
				})
			).show();
		}

		, visualizer = function ( submitted, override )
		{

			var id, stage = this && this.stage;

			if( override )
			{
				id = unescapeStr( override.value );
			}
			else
			{
				id = this.id.value;
			}

			var ACInfo = [ "ArticleContent[ ", Dand.wrap( "span", null, "info", id ), " ]" ];
			if ( submitted && id )
			{
				// Visualize component
				if (!stage)
				{
					temp =  Dand.wrapne( "span"
						, ACInfo
						, new DataKey( "value", id )
					);

					insertSnippet(
						j = snippetWrap(
							"ArticleContent"
							, temp
							, false, "span"
						)
						, !!override
					);
				}
				else
				{
					var stg = IDOMElement( stage );

					stg.clear();
					stg.loot( Dand.wrape( ACInfo ) );
					stg.setAttribute( new DataKey( "value", id ) );
				}

				i = {
					_id: id
					, _stage: temp
				};

				// Get title of this article
				postData(
					config.paths.get_article
					, { article_id: id },
					/**
					 * @param {!_AstJson_.AJaxGetArticle} e
					 * @return {void}
					 */
					function( e )
					{
						ACInfo[1].firstChild.textContent = e.entry.title;
					}
					, function()
					{
						ACInfo[1].firstChild.textContent = "ERROR";
					} );


				// 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;

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

	__static_method( ArticleContent, "compile", compile );

	ns[ NS_EXPORT ]( EX_CLASS, "ArticleContent", ArticleContent );

})();