Links (functions)

var
Links          = {}
Links.Activate = LinksActivate
Links.Complete = LinksComplete

Activate

Links.Activate( links, pathname, top_level_links, root_links )

function LinksActivate( links, href, top_level_links )
{
	var n = links.length;

    if ( !top_level_links ) top_level_links = [];
	
	for ( var i=0; i < n; i++ )
	{
		var link = links[i];

        if ( link.href )
        {
            if ( link.href == href )
            {
                link.className += ("" == link.className) ? "active" : " active";
            }

            //  Top-level link = "/dashboard/"
            //  Menu link      = "/dashbaord/projects/"
            //  Page link      = "/dashboard/projects/current_page/"
            //
            //  If href       = '/dashboard/some/directory/',
            //  and link.href = '/dashboard/some/',
            //
            //  then it should have the class 'subactive' added,
            //  indicating that the link is a parent directory of the current page
            //  unless it is a 'top_level' link.
            //

            var is_prefix    = LinksActivate.IsPrefix( href, link.href );
            var is_tll       = top_level_links.includes( link.href );

            if ( is_prefix && !is_tll )
            {
                link.className += ("" == link.className) ? "subactive" : " subactive";
            }
        }
	}
}

LinksActivate.IsPrefix
=
function( string, prefix )
{
    return (0 === string.indexOf( prefix ));
}

LinksActivate.IsRootHref
=
function( href )
{
    return ((location.protocol + '//') == href);
}

Complete

Links.Complete( links, tuple )

function LinksComplete( links, tuple )
{
	var n = links.length;
	
	for ( var i=0; i < n; i++ )
	{
		var link = links[i];

        if ( link.href )
        {
    		link.href = Replace( link.href, tuple, true );
        }
		link.innerHTML = Replace( link.innerHTML, tuple );
	}
}