/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  webspltut05.webspl: An example from the WebSPL Tutorial
 */

load "wsf";
load "wsf_display";
load "wsf_menu";

load "file";
load "encode_js";

object Editpage WsfComponent
{
	var filename;

	method get_html()
	{
		var data = file_read(filename);

		return
		<>
		<div style="background-color: #aaaaaa; width:86%; height:80%;
			position:absolute; left:5%; top:10%; padding:2%"
			id="$id">
		<h3>Edit page `${xml::filename =~ s/\.html$//R}':</h3>
		<form ${add_action(cgi.url)}>
		<input type="hidden" name="sid" value="${xml::sid}">
		<textarea style="width:100%; height:80%;"
			name="data">${xml::data}</textarea>
		<br/>
		<input type="submit" value="Save">
		<input type="button" value="Show"
			onClick="window.open('${js::filename}', '');">
		<input type="submit" name="remove" onClick="return confirm(
			'${js::"Do you really want to remove page $[
			]`${filename =~ s/\.html$//R}'?"}');"
			value="Remove Page">
		</form>
		</div>
		</>;
	}

	method main()
	{
		task_co_return();
		if (declared cgi.param.remove) {
			file_delete(filename);
			create_menu();
			page.root.child_set("content", new WsfComponent());
		} else {
			file_write(filename, cgi.param.data);
			dirty = 1;
		}
	}

	method init(_filename)
	{
		filename = _filename;
		return *WsfComponent.init();
	}
}

object Newpage WsfComponent
{
	method get_html()
	{
		return
		<>
		<div style="background-color: #aaaaaa; width:30%;
			position:absolute; left:30%; top:20%; padding:10px"
			id="$id">
		<h3>Create a new page:</h3>
		<form ${add_action(cgi.url)}>
		<input type="hidden" name="sid" value="${xml::sid}">
		<b>Filename:</b><br />
		<input style="width:100%;" name="filename" /><p />
		<b>Page Title:</b><br />
		<input style="width:100%;" name="pagetitle" /><p />
		<input type="submit" value="Create">
		</form>
		</div>
		</>;
	}

	method main()
	{
		task_co_return();

		var pagetitle = xml::cgi.param.pagetitle;
		var filename  = xml::cgi.param.filename;

		filename =~ s/^(.*\/|)\.*//;
		filename =~ s/(?<!\.html)$/.html/;

		var html = <<EOT:
			:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
			:<html>
			:<head>
			:  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
			:  <title>${xml::pagetitle}</title>
			:<head>
			:<body>
			:  <h1>${xml::pagetitle}</h1>
			:</body>
			:</html>
		EOT;

		file_write(filename, html);

		create_menu();
		page.root.child_set("content", new Editpage(filename));
	}
}

function create_menu()
{
	var menu;

	menu["Pages"] = ({
		var pages;
		var ls = file_list(".");
		foreach i (ls)
			if ( ls[i] =~ /^(?P<filename>(?P<title>.*)\.html)$/ ) {
				import $$;
				pages[title] = function() {
					page.root.child_set("content",
						new Editpage(filename));
				};
			}
		return pages;
	});

	menu["New Page"] = function() {
		page.root.child_set("content", new Newpage());
	};

	page.root.child_set("menu", new WsfMenu(menu));
}

// hide login logic because it is covered in a different tutorial section
#file-as-code webspltut06.spl

var page = new WsfDocument();
page.root = new WsfComponent();
create_menu();
page.main();

