Data Storage (session and local)

var
DataStorage         = {}
DataStorage.Local   = {}
DataStorage.Session = {}

Exists

DataStorage.Exists()

DataStorage.Exists
=
function()
{
	return typeof( Storage ) !== "undefined";
}

Set item (local)

DataStorage.Local.SetItem( key, value )

DataStorage.Local.SetItem
=
function( key, value )
{
	var success = false;

	if ( typeof( Storage ) !== "undefined" )
	{
		if ( value || ("" == value.trim()) )
		{
			window.localStorage.setItem( key, value );
		
			success = (window.localStorage.key == value);
		}
	}
	return success;
}

Get item (local)

DataStorage.GetItem( key )

DataStorage.Local.GetItem
=
function( key )
{
	var value = null;

	if ( DataStorage.Exists() )
	{
		value = window.localStorage.getItem( key );
	}
	return value;
}

Remove item (local)

DataStorage.RemoveItem( key )

DataStorage.Local.RemoveItem
=
function( key )
{
	var success = false;

	if ( typeof( Storage ) !== "undefined" )
	{
		window.localStorage.removeItem( key );
		
		success = (window.localStorage.key == null);
	}
	return success;
}

Has item (local)

DataStorage.Local.HasItem( key )

DataStorage.Local.HasItem
=
function( key )
{
	var success = false;

	if ( typeof( Storage ) !== "undefined" )
	{
		success = window.localStorage.hasOwnProperty( key );
	}
	return success;
}

Set item (session)

DataStorage.Session.SetItem( key, value )

DataStorage.Session.SetItem
=
function( key, value )
{
	var success = false;

	if ( typeof( Storage ) !== "undefined" )
	{
		if ( value || ("" == value.trim()) )
		{
			window.sessionStorage.setItem( key, value );
		
			success = (window.sessionStorage.key == value);
		}
	}
	return success;
}

Get item (session)

DataStorage.Session.GetItem( key )

DataStorage.Session.GetItem
=
function( key )
{
	var value = null;

	if ( DataStorage.Exists() )
	{
		value = window.sessionStorage.getItem( key );
	}
	return value;
}

Remove item (session)

DataStorage.Session.RemoveItem( key )

DataStorage.Session.RemoveItem
=
function( key )
{
	var success = false;

	if ( typeof( Storage ) !== "undefined" )
	{
		window.sessionStorage.removeItem( key );
		
		success = (window.sessionStorage.key == null);
	}
	return success;
}

Has item (session)

DataStorage.Session.HasItem( key )

DataStorage.Session.HasItem
=
function( key )
{
	var success = false;

	if ( typeof( Storage ) !== "undefined" )
	{
		success = window.sessionStorage.hasOwnProperty( key );
	}
	return success;
}

DataStorage Session Clear

DataStorage.Session.Clear()

DataStorage.Session.Clear
=
function()
{
    window.sessionStorage.clear();
}

Has item (session)

DataStorage.SetSelectFilter( event )

DataStorage.SetSelectFilter
=
function( event )
{
    var select = event.target;
    var name   = select.name;
    var value  = select.options[select.selectedIndex].value;

    DataStorage.Local.SetUserItem( name, value );
}

DataStorage Local Hash Key

DataStorage.Local.HashKey( name, include_path )

DataStorage.Local.HashKey
=
function( name, include_path )
{
    var path = include_path ? location.pathname : "/";
    var hash = DataStorage.Session.GetItem( "user_hash" );

    if ( !hash )
    {
        console.log( "Trying: Session.user_hash" );
        hash = Session.user_hash;
    }

    if ( hash && name && path )
    {
        return hash + "|" + name + "|" + path;
    }
    else
    {
        return null;
    }
}

DataStorage Local Set User Item

DataStorage.Local.SetUserItem( name, value, include_path = false )

DataStorage.Local.SetUserItem
=
function( name, value, include_path = false )
{
    var key = DataStorage.Local.HashKey( name, include_path );

    if ( ! key )
    {
        //alert( "Error: DataStorage.Local.SetUserItem is being called before user_hash is set - please fix your code." );
    }
    else
    {
        return DataStorage.Local.SetItem( key, value );
    }
}

DataStorage Local Get User Item

DataStorage.Local.GetUserItem( name, include_path = false )

DataStorage.Local.GetUserItem
=
function( name, include_path = false )
{
    var key = DataStorage.Local.HashKey( name, include_path );

    if ( ! key )
    {
        //alert( "Error: DataStorage.Local.GetUserItem is being called before user_hash is set - please fix your code." );

        return null;
    }
    else
    {
        return DataStorage.Local.GetItem( key );
    }
}

DataStorage Local Remove User Item

DataStorage.Local.RemoveUserItem( name, include_path = false )

DataStorage.Local.RemoveUserItem
=
function( name, include_path = false )
{
    var key = DataStorage.Local.HashKey( name, include_path );

    if ( ! key )
    {
        //alert( "Error: DataStorage.Local.RemoveUserItem is being called before user_hash is set - please fix your code." );
    }
    else
    {
        return DataStorage.Local.RemoveItem( key );
    }
}

DataStorage Local Clear

DataStorage.Local.Clear()

DataStorage.Local.Clear
=
function()
{
    window.localStorage.clear();
}