penguin/AstroJS

Javascript framework for my blog

botanjs/src/Astro/Starfall/Layout/PureColumn.js

raw ยท 1848 bytes

(function(){
	var ns = __namespace( "Astro.Starfall.Layout.PureColumn" );

	/** @type {Astro.Bootstrap} */
	var Bootstrap                    = __import( "Astro.Bootstrap" );
	/** @type {System.Cycle} */
	var Cycle                        = __import( "System.Cycle" );
	/** @type {Astro.Blog.Config} */
	var config                       = __import( "Astro.Blog.Config" );
	/** @type {System.Debug} */
	var debug                        = __import( "System.Debug" );

	/** @type {MessageEvent} */
	var allowedOrigin;

	var catchMessage = function( e )
	{
		debug.Info( "Origin: " + e.origin );
		var allowedOrigins = config.get( "AllowedOrigins" );
		var j = e.origin;

		for ( var i in allowedOrigins )
		{
			if ( j === allowedOrigins[i] )
			{
				// Stop listener
				debug.Info( "  captured" );
				window.removeEventListener( "message", catchMessage, false );
				allowedOrigin = e;

				// post the content height
				postContentHeight();
				return;
			}
		}
		debug.Info( "  refused. Continue listening..." );
	}
	
	var postContentHeight = function ()
	{
		if ( allowedOrigin.source && allowedOrigin.origin )
		{
			// per second trigger for body height
			var pHeight;
			allowedOrigin.source.postMessage(
				pHeight = document.body.clientHeight + ""
				, allowedOrigin.origin
			);

			var heightUpdate = function()
			{
				if( pHeight != document.body.clientHeight )
				{
					allowedOrigin.source.postMessage(
						pHeight = document.body.clientHeight + ""
						, allowedOrigin.origin
					);
				}
			}

			Cycle.perma( "bodyHeightMonitor", heightUpdate, 500 );
		}
	};

	var init = function ()
	{
		if ( window.self !== window.top )
		{
			debug.Info( "iframe detected, listening ..." );
			// Wait for parent frame's message
			window.addEventListener( "message", catchMessage, false );
		}
	};

	Bootstrap.regInit( init );
})();