penguin/AstroJS

Javascript framework for my blog

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

raw ยท 3396 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 video = function (insertSnippet, snippetWrap, createContext, override)
	{
		var temp, i, j
		
		, getVimeoThumbnail = function (vtag)
		{
			getData("http://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/" + vtag.getAttribute("data-value"), setThumbnail.bind(vtag), noThumb.bind(vtag));
		}
	
		, noThumb = function ()
		{
			
		}
		
		, setThumbnail = function (str)
		{
			this.style.background = "black url(" + JSON.parse(str)["thumbnail_url"] + ") center center no-repeat";
			
		}
		
		, handler = function ()
		{
			// Input fields
			var input_url = Dand.wrap('input', null, "v_snippet_input_single", null, new IKey("type", "text"));
			
			// Popup MessageBox
			new MessageBox("Insert video snippet"
				, Dand.wrape([ Dand.wrapc("v_instruction flsf", "Paste a vimeo/Youtube link below:"), input_url ])
				, "OK", "Cancel", visualizer.bind(input_url)
			).show();
		}
	
		, visualizer = function (submitted, override)
		{
			var t, v;
			
			if (override)
			{
				v = override.value;
				i = override.type;
			}
			else
			{
				t = this.value;
				// Match youtube links
				v = t.match(/\/\/(www\.)?youtube\.com\/watch\?.*?v=([^\&\?\/\#]+)/) || t.match(/\/\/(www\.)?youtube\.com\/embed\/([^\&\?\/]+)/)
						|| t.match(/\/\/(www\.)?youtube\.com\/v\/([^\&\?\/]+)/) || t.match(/\/\/(www\.)?youtu\.be\/([^\&\?\/]+)/);
				if (v)
				{
					i = "youtube";
					v = v[2];
				}
				else
				{
					// match vimeo links
					v = t.match(/\/\/(www\.)?vimeo.com\/(\d+)($|\/)/);
					if (v)
					{
						i = "vimeo";
						v = v[2];
					}
					else
					{
						// None matched, do nothing
						return;
					}
				}
			}
			
			if (submitted)
			{
				
				// Visualize component
				temp =  Dand.wrapc('v_box', Dand.wrapc('v_description', "Video(url): " + t)
				, [
					new DataKey("value", v)
					, new DataKey("type", i)
					, new IKey("style"
						, "width: 640px; height: 390px;"
						+ ( (i[0] == "v") ? "" : ("background: black url(http://img.youtube.com/vi/" + v + "/hqdefault.jpg) no-repeat center center;") )
					)
				  ]
				);
				
				if (i[0] == "v")
				{
					getVimeoThumbnail(temp);
				}
				
				insertSnippet(j = snippetWrap("Video", 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)
	{
		// [video type=\"youtube\"]" + v[2] + "[/video]
		var element = IDOMElement(stage)
		, type = element.getDAttribute("type");
		
		return "[video type=\"" + type + "\"]" + element.getDAttribute("value") + "[/video]";
	};

	__static_method( video, "compile", compile );

	ns[ NS_EXPORT ]( EX_CLASS, "Video", video );
})();