/*
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/.
*/
///get source at http://pietschsoft.com/post/2008/08/Custom-Themes-in-ASPNET-MVC-Updated-for-Preview-5.aspx
///
using System;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
namespace Everest.Library.Mvc.View{
public class WebFormEmbeddedThemeViewEngine : System.Web.Mvc.WebFormViewEngine
{
private static readonly string[] _emptyLocations;
public WebFormEmbeddedThemeViewEngine()
{
base.ViewLocationFormats = new string[] {
"~/Views/{2}/{1}/{0}.aspx",
"~/Views/{2}/{1}/{0}.ascx",
"~/Views/{2}/Shared/{0}.aspx",
"~/Views/{2}/Shared/{0}.ascx",
"~/App_Resource/{3}/{3}.Views.{2}.{1}.{0}.aspx",
"~/App_Resource/{3}/{3}.Views.{2}.{1}.{0}.ascx",
"~/App_Resource/{3}/{3}.Views.{2}.Shared.{0}.aspx",
"~/App_Resource/{3}/{3}.Views.{2}.Shared.{0}.ascx"
};
base.MasterLocationFormats = new string[] {
"~/Views/{2}/{1}/{0}.master",
"~/Views/{2}/Shared/{0}.master",
"~/App_Resource/{3}/{3}.Views.{2}.{1}.{0}.master",
"~/App_Resource/{3}/{3}.Views.{2}.Shared.{0}.master"
};
base.PartialViewLocationFormats = new string[] {
"~/Views/{2}/{1}/{0}.aspx",
"~/Views/{2}/{1}/{0}.ascx",
"~/Views/{2}/Shared/{0}.aspx",
"~/Views/{2}/Shared/{0}.ascx",
"~/App_Resource/{3}/{3}.Views.{2}.{1}.{0}.aspx",
"~/App_Resource/{3}/{3}.Views.{2}.{1}.{0}.ascx",
"~/App_Resource/{3}/{3}.Views.{2}.Shared.{0}.aspx",
"~/App_Resource/{3}/{3}.Views.{2}.Shared.{0}.ascx"
};
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (string.IsNullOrEmpty(viewName))
{
throw new ArgumentException("Value is required.", "viewName");
}
string assemblyName = null;
if (controllerContext.Controller is EverestControllerBase)
{
assemblyName = ((EverestControllerBase)controllerContext.Controller).AssemblyName;
}
string themeName = this.GetThemeToUse(controllerContext);
string[] searchedViewLocations;
string[] searchedMasterLocations;
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
string viewPath = this.GetPath(this.ViewLocationFormats, viewName, controllerName, themeName, assemblyName, out searchedViewLocations);
string masterPath = this.GetPath(this.MasterLocationFormats, masterName, controllerName, themeName, assemblyName, out searchedMasterLocations);
if (!(string.IsNullOrEmpty(viewPath)) && (!(masterPath == string.Empty) || string.IsNullOrEmpty(masterName)))
{
return new ViewEngineResult(this.CreateView(controllerContext, viewPath, masterPath),this);
}
return new ViewEngineResult(searchedViewLocations.Union<string>(searchedMasterLocations));
}
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (string.IsNullOrEmpty(partialViewName))
{
throw new ArgumentException("Value is required.", partialViewName);
}
string assemblyName = null;
if (controllerContext.Controller is EverestControllerBase)
{
assemblyName = ((EverestControllerBase)controllerContext.Controller).AssemblyName;
}
string themeName = this.GetThemeToUse(controllerContext);
string[] searchedLocations;
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
string partialPath = this.GetPath(this.PartialViewLocationFormats, partialViewName, controllerName, themeName, assemblyName, out searchedLocations);
if (string.IsNullOrEmpty(partialPath))
{
return new ViewEngineResult(searchedLocations);
}
return new ViewEngineResult(this.CreatePartialView(controllerContext, partialPath),this);
}
private string GetThemeToUse(ControllerContext controllerContext)
{
string themeName = controllerContext.HttpContext.Items["themeName"] as string;
if (themeName == null) themeName = "Default";
return themeName;
}
private string GetPath(string[] locations, string name, string controllerName, string themeName, string assemblyFile, out string[] searchedLocations)
{
searchedLocations = _emptyLocations;
if (string.IsNullOrEmpty(name))
{
return string.Empty;
}
string path = null;
searchedLocations = new string[locations.Length];
for (int i = 0; i < locations.Length; i++)
{
path = string.Format(CultureInfo.InvariantCulture, locations[i], new object[] { name, controllerName, themeName, assemblyFile });
if (this.VirtualPathProvider.FileExists(path))
{
searchedLocations = new string[0];
return path;
}
searchedLocations[i] = path;
}
return null;
}
}
}
|