/*
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;
using Everest.Library.Json;
using Everest.Library.ExtensionMethod;
namespace Everest.Forms.ExtForm{
public abstract class ExtformGenerator : IFormGenerator
{
static List<string> NotStringValueList = new List<string>();
static ExtformGenerator()
{
NotStringValueList.Add("renderer");
NotStringValueList.Add("sortable");
NotStringValueList.Add("fixed");
NotStringValueList.Add("hidden");
NotStringValueList.Add("hideable");
NotStringValueList.Add("listeners");
NotStringValueList.Add("menuDisabled");
NotStringValueList.Add("resizable");
NotStringValueList.Add("sortable");
NotStringValueList.Add("width");
NotStringValueList.Add("tbarItems");
NotStringValueList.Add("enableTopbar");
NotStringValueList.Add("addable");
NotStringValueList.Add("stateful");
NotStringValueList.Add("stateEvents");
NotStringValueList.Add("pageSize");
}
public ExtformGenerator(ISchemaProvider schemaProvider)
{
this.SchemaProvider = schemaProvider;
}
#region ISchemaFormGenerator Members
public abstract string Generate(string schemaName, NameValueCollection config);
#endregion
protected string GetAttributes(NameValueCollection config)
{
StringBuilder attributes = new StringBuilder();
if (config != null && config.Count > 0)
{
foreach (string name in config.Keys)
{
if (!string.IsNullOrEmpty(name))
{
attributes.AppendFormat("{0},", GetConfigValue(name, config[name]));
}
}
}
return attributes.ToString();
}
protected string GetConfigValue(string configName, string configValue)
{
if (NotStringValueList.Contains(configName))
{
return string.Format("{0}:{1}", configName, configValue);
}
else
return string.Format("{0}:'{1}'", configName, JSONHelper.QuoteString(configValue));
}
#region IFormGenerator Members
public ISchemaProvider SchemaProvider
{
get;
set;
}
#endregion
const string ChildSchameContainer = @"{0}";
protected string GeneratorChildSchemas(ISchema schema, ISchemaProvider provider, NameValueCollection config)
{
StringBuilder stringBuilder = new StringBuilder();
if (schema.ReferenceForms != null && schema.ReferenceForms.Count() != 0)
{
StringBuilder itemBuilder = new StringBuilder();
IGeneratorFactory factory = new ExtGeneratorFactory(provider);
foreach (IReferenceForm childForm in schema.ReferenceForms)
{
NameValueCollection nameValue = new NameValueCollection(config);
nameValue.Add("dataIndex", childForm.DataIndex);
nameValue.Add("isChildForm", "true");
if (childForm is SchemaReference)
{
SchemaReference schemaRef = (SchemaReference)childForm;
itemBuilder.AppendFormat("{0},", factory.GetGenerator(schemaRef.FormType).Generate(schemaRef.SchemaName, nameValue));
}
else if (childForm is GeneratorReference)
{
GeneratorReference generatorRef = (GeneratorReference)childForm;
Type generatorType = Type.GetType(generatorRef.Type);
IFormGenerator generator = (IFormGenerator)Activator.CreateInstance(generatorType);
itemBuilder.AppendFormat("{0},", generator.Generate(string.Empty, nameValue));
}
}
stringBuilder.AppendFormat(ChildSchameContainer, itemBuilder.RemoveLastSpecifiedChar(',').ToString());
}
return stringBuilder.ToString();
}
}
}
|