/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
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, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace Everest.Forms.ExtForm{
public class ExtColumnLayoutGenerator : ExtformGenerator
{
const string DefaultFormType = "Everest.FormPanel";
public ExtColumnLayoutGenerator(ISchemaProvider provider) : base(provider) { }
const string formLayout = @"
new {4}({{
{7}
labelAlign: 'left',
frame: true,
url:'{2}',
containerType:'form',
loadUrl:'{3}',
title: '{0}',
bodyStyle: 'padding:5px 5px 0',
fileUpload: {5},
items: [{1}],
childForms:[{6}]
}})
";
public override string Generate(string schemaName, NameValueCollection config)
{
ISchema schema = SchemaProvider.GetSchema(schemaName,config);
NameValueCollection gridConfig = new NameValueCollection(schema.GridAttributes);
if (config != null)
{
foreach (var key in config.AllKeys)
{
gridConfig[key] = config[key];
}
}
bool fileUpload;
IColumn[] detailsColumns = schema.Columns.Where(c => !string.IsNullOrEmpty(c.DataIndexInDetail)).ToArray();
string columns = GetItemsJSON(schema, detailsColumns, config, out fileUpload);
string attributes = GetAttributes(config);
return string.Format(formLayout, schema.Name, columns, schema.SubmitUrl, schema.DetailsUrl,
string.IsNullOrEmpty(schema.FormType) ? DefaultFormType : schema.FormType, fileUpload.ToString().ToLower(), GeneratorChildSchemas(schema, this.SchemaProvider, config), attributes);
}
private string GetItemsJSON(ISchema schema, IColumn[] columns, NameValueCollection config, out bool fileUpload)
{
fileUpload = false;
StringBuilder stringBuilder = new StringBuilder();
IColumn[] orderedColumn = columns.OrderBy<IColumn, int>((IColumn column) => column.Order).ToArray<IColumn>();
IColumn[] normalControls = orderedColumn.Where<IColumn>((IColumn column) => column.Control != null && column.Control.EntireRow == false && column.EntireRow == false).Select<IColumn, IColumn>((IColumn column) => column).ToArray<IColumn>();
IColumn[] requiredSingleRow = orderedColumn.Where<IColumn>((IColumn column) => column.Control != null && column.Control.EntireRow == true || column.EntireRow == true).Select<IColumn, IColumn>((IColumn column) => column).ToArray<IColumn>();
if (normalControls.Length > 0)
{
stringBuilder.Append(@"{
layout: 'column',items:[");
stringBuilder.Append(GenerateAlternateColumns(normalControls, 0, out fileUpload));
stringBuilder.Append(GenerateAlternateColumns(normalControls, 1, out fileUpload));
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append(@"]}");
}
if (requiredSingleRow.Length > 0)
{
if (normalControls.Length > 0)
{
stringBuilder.Append(",");
}
foreach (IColumn column in requiredSingleRow)
{
stringBuilder.AppendFormat(@"{0},", column.Control.Render(column));
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
}
return stringBuilder.ToString();
}
private string GenerateAlternateColumns(IColumn[] columns, int startIndex, out bool fileUpload)
{
fileUpload = false;
if (startIndex > columns.Length)
return "";
StringBuilder stringBuilder = new StringBuilder(@"{
columnWidth: .5,
layout: 'form',
items: [");
int i = startIndex;
while (i < columns.Length)
{
fileUpload = fileUpload || columns[i].Control.FileUpload;
stringBuilder.AppendFormat("{0},", columns[i].Control.Render(columns[i]));
i += 2;
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append("]},");
return stringBuilder.ToString();
}
}
}
|