:- ensure_loaded(common).
cvs_id('$Id: basicpage.pl,v 1.37 2005/04/14 00:01:29 sandro Exp $').
/*
  rename to: "view.pl", "tabs.pl", ...?
  
  Display a page with multiple top-level views of the focus, on tabs.

  Tabs are just views, but we have some more data about them, via
  tab/5.
  

================================================================
== STACKING 
 
We imagine all the views to be in a stack.  They start out in the same
order as their tabs (alpha, for now), but when you click on one it
moves to the top.   The topmost view that applies is used.

To maintain this, we have a query parameter which is the names of the
views which have put on the top (topmost first).

BUG: right now we add every tab to the stacking list; we should leave
out the ones that have never been clicked on.
  

*/

write_basic_page(Q0, ReqId) :-
	debug(basicpage, 'PS: ~q', [Q]),
	memberchk(focus=Focus, Q0),
	memberchk(stacking(Stacking), Q0),
	Q=[sources(_) | Q0],
	% Yes, we "generate" all the tabs, even the ones not
	% shown, but heavy use of the +foo (late, nested eval)
	% in the returned HTML tree means we don't actually
	% do much work until serialization time.
	%     (turn on ts/1 to confirm this...)
	findall(tab(SortOrder, Name, Content),
		(   %debug(view, 'page view of ~q?', [Focus]),
		    tab(SortOrder, Q, Focus, Name, Content)
		    %ts(used_by(html_view(Name, page, Focus, Q, 'X'))),
		    %debug(view, '... yes, Name=~q', [Name])
		),
		UnsortedTabs),
	sort(UnsortedTabs, Tabs),
	current_tab_name(Tabs, Stacking, CurrentTabName, CurrentTabContent),
	write_page(ReqId, basicBody(Q, Focus, Tabs, CurrentTabName, CurrentTabContent), Focus).
write_basic_page(A, B) :-
	throw(failed(write_basic_page(A, B))).
	
basicBody(Q, Focus, Tabs, CurrentTabName, CurrentTabContent, H) :-
	H = [ div(class=harvestReport, +harvest_status(Q, Focus)),
	      div(class=body,
		  div(class=body2,
		      +para(Q, Focus),
		      \tabs(Q, Tabs, CurrentTabName),
		      div(class='underTab', CurrentTabContent)
		     )),
	      +sources(Q)
	    ].

sources(Q, H) :-
	memberchk(sources(Sources), Q),
	length(Sources, Count),
	QTemp = [sources(_) | Q],
	(   Count = 1
	->  Plural = ''
	;   Plural = 's'
	),
	H = div(class=sources,
		p(int(Count), ' Source', Plural, ':  ', +thing_list(QTemp, Sources))
	       ).

% current_tab_name(+Tabs, +Stacking, -Top
current_tab_name(Tabs, [Name|_], Name, Content) :-
	  member(tab(_,Name,Content), Tabs).
current_tab_name(Tabs, [_|Names], Top, Content) :-
	current_tab_name(Tabs, Names, Top, Content).
% if none of the names given in Stacking are present
% in Tabs, just use the first Tab.
current_tab_name([tab(_, Name, Content)|_], [], Name, Content).

current_tab_name(_, _, none, p('no suitable views')).
	
% options(PageState) -->
% 	[form(class='semWalkerOptions',
% 	      method='get',
% 	      action='/basic',
% 	      /*  <span class='semWalkerLoggedIn'>Logged in as:</span><br/><a class='semWalkerUserName' href='#'>Sandro Hawke</a><br/>
% 		  <a href='#'>Logout</a> */
% 	      a(class='ln', href='#', 'Configure'),
% 	      br([]),
% 	      a(class='ln', href='/help', 'Help'),
% 	      br([])
% 	     )].

options(_PageState) -->
	[span(class='semWalkerOptions',
	   %   a(class='ln', href= \self_uri(configure, PageState, []), 'Configure'),
	   %   br([]),
	      a(class='ln', href='help', 'Help'),
	      br([])
	     )].
/*
focusForm(Focus) -->
	[form(class='semWalkerFocusForm', method='get', action='basic',
	      name='semWalkerFocusForm',
	      p(class='semWalkerFocusFormTag',
		'Enter search words or a subject identifier (URI, URI-Ref, Prefix, or QName):',
		br([]),
		input(name='focus', size='72', type='text', value=Focus),
		input(type='submit', value='Go'),
		input(type='button', value='Clear',
		      onclick='document.semWalkerFocusForm.focus.value='''''),
	        br([]),
		'Privacy Notice: Text entered here may be recorded and republished.'
	       ))].
*/

/*
title(_Q) -->
	{ rdf_global_id(self:me, _Me), site_title(Title) },

%	[h2(class='semWalkerTitle', Title), +para(_Q, _Me)].
	[div(a(class='semWalkerTitle', href='.', Title))].
*/

restack(PageState, NewTop) -->
	{
	 member(stacking(OldStacking), PageState),
	 ( select(NewTop, OldStacking, Rest)
	 ; Rest=OldStacking ),
	 Stacking=[NewTop | Rest]
	},
	self_uri(basic, PageState, [stacking=Stacking]).
	 
tabs(PageState, Tabs, CurrentTabName) -->
	{ tab_lis(PageState, Tabs, CurrentTabName, Tab_lis) },
	[div(id=header, span(id=tabIntro, 'View as:'),
	     ul(Tab_lis))].

tab_lis(PageState, [tab(_,Name,_)|Tabs], Name,
	[li(id=current, a(href= \restack(PageState, Name), Name)) | LIs]) :-
	tab_lis(PageState, Tabs, Name, LIs).
tab_lis(PageState, [tab(_,Name,_)|Tabs], CurName,
	[li(a(href= \restack(PageState, Name), Name)) | LIs]) :-
	tab_lis(PageState, Tabs, CurName, LIs).
tab_lis(_, [], _, []).

% should look like:
%	     ul(li(id=current, a(href='#', 'Folded Graph View')) %,
%		li(            a(href='#', 'Generic Graph')),
%		li(            a(href='#', 'Person'))
%	       ))].
		
