📄 #scaffold.cs#
字号:
/**
* Project: emergetk: stateful web framework for the masses
* File name: Scaffold.cs
* Description: Inspired by RubyOnRails scaffold. Generates viewable and editable templates for data models. View templates are customizable.
*
* @author Ben Joldersma, All-In-One Creations, Ltd. http://all-in-one-creations.net, Copyright (C) 2006.
*
* @see The GNU Public License (GPL)
*/
/*
* 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
*/
using System;
using System.Collections;
using System.Collections.Generic;
using EmergeTk.Model;
using System.Reflection;
using EmergeTk.Scaffold;
using EmergeTk.Widgets.Html;
namespace EmergeTk
{
/// <summary>
/// Summary description for Scaffold.
/// </summary>
public class Scaffold<T> : Widget, IDataSourced
where T : AbstractRecord, new()
{
private Widget rootScaffold;
private Repeater<T> grid;
private Pane customGridTemplate;
private Hashtable customGridTemplates;
private ModelForm<T> addPane;
private Button showAddButton;
private IRecordList<T> dataSource;
private Model.ColumnInfo[] fieldInfos;
private string model;
/// <summary>
/// Property Model (Model)
/// </summary>
public string Model
{
get
{
return this.model;
}
set
{
model = value;
}
}
private string friendlyModelName;
/// <summary>
/// Property FriendlyModelName (string)
/// </summary>
public string FriendlyModelName
{
get
{
if( friendlyModelName == null )
{
friendlyModelName = model;
if( friendlyModelName.IndexOf(".") > -1 )
{
friendlyModelName = friendlyModelName.Split('.')[1];
}
}
return this.friendlyModelName;
}
set
{
this.friendlyModelName = value;
}
}
public IRecordList<T> DataSource
{
get { return dataSource; }
set { dataSource = value; }
}
private Type buttonType = typeof(Button);
private string button;
/// <summary>
/// Property ButtonType (string)
/// </summary>
public string Button
{
get
{
return this.button;
}
set
{
this.button = value;
this.buttonType = DiscoverType(button);
}
}
private bool showNewButton = true;
public bool ShowNewButton
{
get { return showNewButton; }
set { showNewButton = value; }
}
private string order = "ROWID";
/// <summary>
/// Property Order (string)
/// </summary>
public string Order
{
get
{
return this.order;
}
set
{
this.order = value;
}
}
public Scaffold()
{
this.ClientClass = "PlaceHolder";
rootScaffold = this;
}
public override void Init()
{
//todo: support order.
if (dataSource == null) dataSource = DataProvider.RequestProvider<T>().Load<T>();
fieldInfos = ColumnInfoManager.RequestColumns(dataSource,typeof(T));
Pane gridPane = customGridTemplate != null ? customGridTemplate : buildDefaultGridPane();
insertEditAndDeleteButtons( gridPane );
insertChildScaffolds( gridPane );
gridPane.Id = "_";
grid = RootContext.CreateWidget<Repeater<T>>();
grid.id = "grid";
grid.Add( gridPane );
grid.DataSource = dataSource;
showAddButton = RootContext.CreateUnkownWidget(buttonType) as Button;
showAddButton.id = "add";
showAddButton.Label = "New " + FriendlyModelName;
showAddButton.OnClick += new OnClickHandler(add_OnClick);
Add( grid );
if( ShowNewButton ) Add(showAddButton);
}
private Pane buildDefaultGridPane()
{
Pane gridPane = RootContext.CreateWidget<Pane>();
gridPane.ClassName = "simplePane";
foreach( Model.ColumnInfo fi in fieldInfos )
{
Widget c = null;
if (fi.DataType == DataType.RecordList)
{
c = new PlaceHolder();
}
else
{
c = RootContext.CreateWidget<Label>();
c.SetAttribute("Text", "<strong>" + fi.Name + ":</strong> {" + fi.Name + "}");
}
c.Id = fi.Name;
gridPane.Add( c );
}
PlaceHolder editPh = RootContext.CreateWidget<PlaceHolder>();
editPh.Id = "EditPlaceHolder";
PlaceHolder deletePh = RootContext.CreateWidget<PlaceHolder>();
deletePh.Id = "DeletePlaceHolder";
gridPane.Add( editPh );
gridPane.Add( deletePh );
return gridPane;
}
Dictionary<string, Type> childButtonsToTypes = null;
private void insertChildScaffolds( Pane pane )
{
foreach( Model.ColumnInfo fi in fieldInfos )
{
if( fi.DataType == DataType.RecordList )
{
Widget c = pane.Find(fi.Name);
if( c != null && c.Widgets == null )
{
Button b = RootContext.CreateUnkownWidget(buttonType) as Button;
b.Id = "Show" + fi.Name;
b.Label = string.Format( "Show {0}",fi.Name);
b.Arg = fi.Name;
b.OnClick += new OnClickHandler(ChildRecords_onClick);
c.Add(b);
if (childButtonsToTypes == null)
childButtonsToTypes = new Dictionary<string, Type>();
childButtonsToTypes[b.Arg] = fi.Type;
}
}
}
}
private void ChildRecords_onClick(Widget sender, string e)
{
MethodInfo mi = this.GetType().GetMethod(
"createChildScaffold", BindingFlags.NonPublic | BindingFlags.Instance, null,
new Type[] { typeof(Widget), typeof(string) }, null);
mi.MakeGenericMethod(childButtonsToTypes[(sender as Button).Arg].GetGenericArguments()[0]).Invoke(this, new object[] { sender, e });
}
private void createChildScaffold<C>(Widget sender, string e)
where C : AbstractRecord, new()
{
Button b = sender as Button;
RecordList<C> list = b.Record[b.Arg] as RecordList<C>;
Widget childwidget = null;
if (b.StateBag.ContainsKey("Childwidget"))
childwidget = b.StateBag["Childwidget"] as Widget;
if( b.Label.StartsWith("Show") )
{
b.Label = b.Label.Replace("Show", "Hide");
if (childwidget != null)
{
childwidget.Visible = true;
return;
}
if (availableRecordLists != null &&
availableRecordLists.ContainsKey(b.Arg))
{
SelectList<C> sl = root.CreateWidget<SelectList<C>>();
sl.DataSource = (IRecordList<C>)root.RecordLists[availableRecordLists[b.Arg].Source];
sl.LabelFormat = availableRecordLists[b.Arg].Format;
sl.Mode = SelectionMode.Multiple;
sl.Init();
sl.OnChanged += new EventHandler<SelectItemEventArgs>(sl_OnChanged);
sl.DataBind();
sl.Record = b.Record;
sl.StateBag["RecordList"] = list;
b.Parent.Add(sl);
b.StateBag["Childwidget"] = sl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -