penguin/AstroJS

Javascript framework for my blog

botanjs/src/Astro/Blog/Components/Notification.js

raw ยท 6133 bytes

(function(){
	var ns = __namespace( "Astro.Blog.Components.Notification" );

	/** @type {System.Debug} */
	var debug                                   = __import( "System.Debug" );
	/** @type {System.Cycle} */
	var Cycle                                   = __import( "System.Cycle" );
	/** @type {Dandelion.IDOMElement} */
	var IDOMElement                             = __import( "Dandelion.IDOMElement" );
	/** @type {System.utils.IKey} */
	var IKey                                    = __import( "System.utils.IKey" );
	/** @type {System.utils.DataKey} */
	var DataKey                                 = __import( "System.utils.DataKey" );
	/** @type {System.utils.EventKey} */
	var EventKey                                = __import( "System.utils.EventKey" );
	/** @type {Dandelion} */
	var Dand                                    = __import( "Dandelion" );
	/** @type {Components.MessageBox} */
	var MessageBox                              = __import( "Components.MessageBox" );
	/** @type {Components.Mouse.ContextMenu} */
	var ContextMenu                             = __import( "Components.Mouse.ContextMenu" );
	/** @type {Astro.Bootstrap} */
	var Bootstrap                               = __import( "Astro.Bootstrap" );
	/** @type {Astro.Blog.Config} */
	var Config                                  = __import( "Astro.Blog.Config" );
	/** @type {Astro.utils.Date} */
	var XDate                                   = __import( "Astro.utils.Date" );

	var postData                                = __import( "System.Net.postData" );
	var smstamp                                 = __import( "Astro.utils.Date.smstamp" );

	var init = function ()
	{
		var stage = Dand.id( "notifications" );
		var bodyStyle = Dand.id( "nt_body" ).style;

		var conf = Config.get( "Notification" );
		var base_path = Config.get( "BasePath" );

		var processor = base_path + conf.paths.get_mesg;
		var contextMenu;

		var itemsMenu;

		var nError = function( e ) {
			// TODO: Do something on error
			debug.Info( e );
		};

		var followLink = function(e)
		{
			window.location = base_path + this.link;
		};

		var toggleFollow = function(e)
		{
			var _action = "enable";
			if( this.item.getAttribute( "active" ) )
			{
				_action = "disable";
				if(
					0 < IDOMElement( this.count ).getDAttribute( "count" )
					&& !confirm( "All followed notifications of this type will be unfollowed. Continue?" )
				) return;
			}

			postData(
				processor
				, {
					"action": _action
					, "tid": IDOMElement( this.item ).getDAttribute( "tid" )
					, "cid": 0
				}
				, toggleSuccess.bind(this)
				, nError
			);
		};

		var toggleSuccess = function(e)
		{
			if( this.item.getAttribute( "active" ) )
			{
				this.item.removeAttribute( "active" );
				this.count.innerHTML = "N/A";
			}
			else
			{
				this.item.setAttribute( "active", 1 );
				this.count.innerHTML = "0";
			}

		};

		var showSettings = function( e )
		{
			// Create list <ul> <li> ... </ul>
			var ul = Dand.wrap("ul", "nt_imenu", "nt_smenu");

			for( var i in e.data )
			{
				var li
					, li_s = [
							// On/off toggle
							Dand.wrap( "span", null, "nt_switch" )
							, Dand.wrap( "span", null, "nt_tname", e.data[i].name )
						]
					, keys = [ new DataKey( "tid", i ) ]
				;

				if( e.data[i].hasOwnProperty("count") )
				{
					// type count
					li_s[2] = Dand.wrap(
						"span"
						, null
						, "nt_tcount"
						, e.data[i].count + ""
						, new DataKey( "count", e.data[i].count )
					);
					// Active
					keys[ keys.length ] = new IKey( "active", 1 );
				}
				else
				{
					li_s[2] = Dand.wrap( "span", null, "nt_tcount", "N/A" );
				}

				// Create li element
				li = Dand.wrapne( "li", li_s, keys );
				ul.appendChild( li );

				IDOMElement( li ).addEventListener(
					"Click"
					, toggleFollow.bind({ item: li, count: li_s[2] })
				);

			}

			var msgBox = new MessageBox( "Notification Settings", ul, "Close" ).show();
		};

		var popupSettings = function()
		{
			postData( processor, { action: "getSettings" }, showSettings, nError );
		};

		var readNotification = function(e)
		{
			postData( processor, { action: "read", id: this.id }, followLink.bind( this ), nError );
		};
		// functions
		var createContextMenu = function(e)
		{
			var items = [], menuShow = false;

			for( var n in e.data )
			{
				// Create items
				items[items.length] = new IKey(
					e.data[n].message 
					, new EventKey(
						"m_" + e.data[n].id
						, readNotification.bind( e.data[n] )
					)
				);
			}

			items[ items.length ] = new EventKey( "Settings", popupSettings );

			contextMenu = new ContextMenu(
				stage
				, items
				, "LMB"
				, nt_body
				// showMenu/hideMenu overrides style settings
				, {
					"class": "nt_container"
					, "showMenu": function(stage, event)
					{
						if( stage.className == "nt_date" ) return;
						if( menuShow ) return;
						bodyStyle.marginLeft = "1em";
						bodyStyle.marginRight = "-1em";
						bodyStyle.opacity = 0;
						bodyStyle.display = "block";
						Cycle.next(function()
						{
							bodyStyle.marginLeft
							= bodyStyle.marginRight = 0;

							bodyStyle.opacity = 1;
							menuShow = true;
						});
					}
					, "hideMenu": function(stage)
					{
						if( stage.className == "nt_date" ) return;
						if( !menuShow ) return;
						bodyStyle.marginLeft = "1em";
						bodyStyle.marginRight = "-1em";
						bodyStyle.opacity = 0;

						Cycle.delay(function()
						{
							bodyStyle.display = "none";
							// cubic 200, we set delay to 200
							menuShow = false;
						}, 200);
					}
					// Prevent default subnode chaining
					, "chainHide": function( stage ) { }
					, "chainShow": function( stage ) { }
				}
			);

			for( n in e.data )
			{
				var li = contextMenu.getItemByKey( "m_" + e.data[n].id ).stage;
				li.appendChild(
					Dand.wrapc(
						"nt_date"
						, smstamp( new Date( Number( e.data[n].date ) * 1000 ) )
					)
				);
			}

			contextMenu.getItemByKey("Settings").stage.className = "nt_icon_settings";
		};

		postData( processor, { action: "get" }, createContextMenu );
	};

	Bootstrap.regInit( init );

})();