penguin/AstroJS

Javascript framework for my blog

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

raw ยท 4179 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 {function(...?): Dandelion.IDOMElement} */
	var IDOMElement                       = __import( "Dandelion.IDOMElement" );
	/** @type {typeof 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 unescapeStr = ns[ NS_INVOKE ]( "unescapeStr" );
	var compileProp = ns[ NS_INVOKE ]( "compileProp" );

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

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

	var ArticleLink = 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" ) )
				, input_title = Dand.wrap( "input", null, "v_snippet_input_single", null, new IKey( "type", "text" ) )
				;

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

			// Popup MessageBox
			new MessageBox(
				"Link to article"
				, Dand.wrape([
					Dand.wrapc( "v_instruction", "Article id:" )
					, v_snippetInput
					, Dand.wrapc( "v_instruction", "Title override:" )
					, input_title
				])
				, "OK", "Cancel"
				, visualizer.bind({
					id: v_snippetInput
					, title: input_title
					, stage: this._stage
				})
			).show();
		}

		, visualizer = function ( submitted, override )
		{

			var id, title, stage = this.stage;

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

			if ( submitted && id )
			{
				// Visualize component
				if (!stage)
				{
					temp =  Dand.wrapne( "span"
						, title || "ArticleLink[" + id + "]"
						, [
							new DataKey( "value", id )
							, new DataKey( "title", title )
						]
					);

					insertSnippet(
						j = snippetWrap( "ArticleLink", temp, false, "span" )
						, !!override
					);
				}
				else
				{
					stage.firstChild.textContent = title || "ArticleLink[" + id + "]";

					IDOMElement( stage ).setAttribute([
						new DataKey( "value", id )
						, new DataKey( "title", title )
					]);
				}

				// Get the default title if not defined
				if( !title )
				{
					postData(
						config.paths.get_article
						, { article_id: id }

						/** @param {_AstJson_.AJaxGetArticle} */
						, function( e )
						{
							j.firstChild.textContent = e.entry.title;
						}
						, function()
						{
							j.firstChild.textContent = "ArticleLink[ ERROR ]";
						} );
				}

				i = {
					_id: id
					, _title: title
					, _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( "title" ) )
		{
			options += " title=\"" + escapeStr( opt ) + "\"";
		}

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

	__static_method( ArticleLink, "compile", compile );

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

})();