pp/1/Default.aspx.cs 3.2 KB (3307b); Sat, 04 Jun 2011 17:42
´╗┐using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GCats_Default : System.Web.UI.Page
{
	private List<string> Links = new List<string> { "IByC", "CByI", "Matrix", "Cats", "Items" };

	protected void Page_Load(object sender, EventArgs e)
	{
		SaveOp();
		var s = Request.QueryString["s"] ?? "Matrix";
		Show(s);
		LinksCtl.InnerHtml = Join(Links, "<a href='?s={0}' class='{1}'>{0}</a>", x => x == s ? "s" : "n");
	}

	protected void OnNewItem(object sender, EventArgs e)
	{
		ItemMgr.NewItem(NewItemCtl.Text);
		Show("Items");
	}

	protected void OnNewCat(object sender, EventArgs e)
	{
		ItemMgr.NewCat(NewCatCtl.Text);
		Show("Cats");
	}

	protected void Show(string what)
	{
		phCats.Visible = phCByI.Visible = phIByC.Visible = phItems.Visible = phMatrix.Visible = false;
		switch (what)
		{
			case "IByC":
				phIByC.Visible = true;
				ItemsByCtl.InnerHtml = Concat(ItemMgr.AllItems.OrderBy(x=>x.Name).Select(x=>Group(x.Name,
					x.Categories.Select(y=>y.Name))));
				break;
			case "CByI":
				phCByI.Visible = true;
				CatsByCtl.InnerHtml = Concat(ItemMgr.AllCategories.OrderBy(x => x.Name).Select(x => Group(x.Name,
					x.Items.Select(y => y.Name))));
				break;
			case "Matrix":
				phMatrix.Visible = true;
				MtxCats.InnerHtml = Join(ItemMgr.AllCategories.Select(x => x.Name), "<th>{0}</th>");
				MtxItms.InnerHtml = Join(ItemMgr.AllItems.Select(x => Item(x)), "<tr>{0}</tr>"); ;
				break;
			case "Cats":
				phCats.Visible = true;
				CatsCtl.DataSource = ItemMgr.AllCategories;
				CatsCtl.DataBind();
				break;
			case "Items":
				phItems.Visible = true;
				ItemsCtl.DataSource = ItemMgr.AllItems;
				ItemsCtl.DataBind();
				break;
			default:
				break;
		}
	}

	private void SaveOp()
	{
		var o = Request.QueryString["o"];
		if (o == "y" || o == "n")
		{
			var i = int.Parse(Request.QueryString["i"]);
			var c = int.Parse(Request.QueryString["c"]);
			if (o == "y")
				ItemMgr.Add(i, c);
			else
				ItemMgr.Remove(i, c);
		}
	}

	private string Row(Item i, bool matched, Category c)
	{
		return string.Format("<a href='?s=Matrix&i={0}&c={1}&o={4}' class='lnk-{3}'>{2}</a>"
			, i.Id, c.Id, " ", matched ? "y" : "n", matched ? "n" : "y");
	}

	private string Item(Item i)
	{
		var cats = ItemMgr.AllCategories;
		var cols = Join(cats.Select(x => Row(i, x.Items.Contains(i), x)), "<td>{0}</td>");
		return string.Format("<td>{0}</td>{1}", i.Name, cols);
	}

	private string Group(string name, IEnumerable<string> items)
	{
		return string.Format("<tr><td>{0}</td><td>{1}</td></tr>", name,
			string.Join(", ", items.OrderBy(x => x).ToArray()));
	}

	private string Concat(IEnumerable<string> items)
	{
		return string.Join(Environment.NewLine, items.ToArray());
	}

	private string Join(IEnumerable<string> items, string format)
	{
		return string.Join(Environment.NewLine, items.Select(x => string.Format(format, x)).ToArray());
	}

	private string Join(IEnumerable<string> items, string format, Func<string, string> arg2Of)
	{
		return string.Join(Environment.NewLine, items.Select(x => string.Format(format, x, arg2Of(x))).ToArray());
	}
}