/*
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.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.Mvc;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Library.ExtensionMethod;
using InteSoft.Web.ExternalResourceLoader;
using Everest.Library.Web;
namespace Everest.CmsServices.MvcHelper{
public class CmsViewPage : ViewPage, ICmsView
{
const string IncludeStylesheet = "\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />";
const string IncludeJavascript = "\r\n<script type=\"text/javascript\" src=\"{0}\"></script>";
public CmsViewPage()
{
}
/// <summary>
/// Gets or sets the CMS theme.
/// </summary>
/// <value>The CMS theme.</value>
public string CmsTheme { get; set; }
bool enableTheming = true;
/// <summary>
/// Gets or sets a Boolean value indicating whether themes apply to the control that is derived from the <see cref="T:System.Web.UI.TemplateControl"/> class.
/// </summary>
/// <value></value>
/// <returns>true to use themes; otherwise, false. The default is true.
/// </returns>
public override bool EnableTheming
{
get
{
return enableTheming;
}
set
{
enableTheming = value;
}
}
bool enableJavascript = true;
/// <summary>
/// Gets or sets a value indicating whether [enable javascript].
/// </summary>
/// <value><c>true</c> if [enable javascript]; otherwise, <c>false</c>.</value>
public virtual bool EnableJavascript
{
get
{
return enableJavascript;
}
set
{
enableJavascript = value;
}
}
#region ICmsView Members
/// <summary>
/// Gets or sets the application.
/// </summary>
/// <value>The application.</value>
public aspnet_Applications Cms_Application
{
get
{
return this.ViewContext.HttpContext.GetCmsContext().Cms_Application;
}
}
/// <summary>
/// Gets or sets the CMS_ page.
/// </summary>
/// <value>The CMS_ page.</value>
public Cms_Page Cms_Page
{
get
{
return CmsContext.Cms_Page;
}
}
/// <summary>
/// Gets or sets the data context.
/// </summary>
/// <value>The data context.</value>
public IEverestCmsDataContext DataContext
{
get
{
return CmsContext.DataContext;
}
}
public ViewPage ViewPage
{
get { return this; }
}
public CmsContext CmsContext
{
get { return this.ViewContext.HttpContext.GetCmsContext(); }
}
public PageHelper PageHelper
{
get { return this.CmsContext.PageHelper; }
}
public ViewDataDictionary PrivateViewData
{
get { return this.ViewData; }
}
public ContentService ContentService
{
get { return this.CmsContext.ContentService; }
}
public FolderService FolderService
{
get
{
return this.CmsContext.FolderService;
}
}
/// <summary>
/// Gets the search service.
/// </summary>
/// <value>The search service.</value>
public Search.SearchService SearchService
{
get { return CmsContext.SearchService; }
}
#endregion
#region Include files
private List<string> javscripts = new List<string>();
public List<string> JavascriptFiles
{
get
{
return javscripts;
}
}
private List<string> stylesheets = new List<string>();
public List<string> StyleSheetFiles
{
get
{
return stylesheets;
}
}
public bool IsIncludeFiles
{
get
{
return javscripts.Count > 0 || stylesheets.Count > 0;
}
}
List<ThemeRulesInterpretor> themeRulesInterpretors = new List<ThemeRulesInterpretor>();
public List<ThemeRulesInterpretor> ThemeRulesInterpretors
{
get
{
return themeRulesInterpretors;
}
}
#endregion
#region Render
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.DataBind();
}
/// <summary>
/// Initializes the <see cref="T:System.Web.UI.HtmlTextWriter"/> object and calls on the child controls of the <see cref="T:System.Web.UI.Page"/> to render.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> that receives the page content.</param>
protected override void Render(HtmlTextWriter writer)
{
var theme = CmsContext.Cms_Application.Theme;
if (!StringExtensions.IsNullOrEmptyTrim(CmsTheme))
{
theme = this.CmsTheme;
}
//Theme
if (!StringExtensions.IsNullOrEmptyTrim(theme) && this.EnableTheming)
{
string themePath = TemplateFileManager.GetThemePath(theme);
var themeRulesInterpretor = new ThemeRulesInterpretor(themePath, Url);
ThemeRulesInterpretors.Add(themeRulesInterpretor);
if (Cms_Application.IsRelease)
{
this.StyleSheetFiles.Add(Url.Action("GetTheme", "Script", new { Application = Cms_Application.ApplicationName, Theme = theme }));
}
else
{
var stylesheets = TemplateFileManager.GetThemeStylesheets(theme);
if (stylesheets != null)
{
foreach (var path in stylesheets)
{
if (!themeRulesInterpretor.Files.Contains(path, StringComparer.InvariantCultureIgnoreCase))
{
this.StyleSheetFiles.Add(Url.ResolveUrl(UrlConvertor.AbsolutePathToRelativeUrl(path)));
}
}
}
}
}
//Javascript
string javascriptApp = Cms_Application.Javascript;
if (!string.IsNullOrEmpty(javascriptApp) && this.EnableJavascript)
{
var javascripts = TemplateFileManager.GetJavascriptFiles(javascriptApp);
if (javascripts != null && javascripts.Count() > 0)
{
//Javascripts
if (Cms_Application.IsRelease)
{
this.JavascriptFiles.Add(Url.Action("GetJavascript", "Script", new { application = javascriptApp }));
}
else
{
if (javascripts != null)
{
foreach (var path in javascripts)
{
this.JavascriptFiles.Add(Url.ResolveUrl(UrlConvertor.AbsolutePathToRelativeUrl(path)));
}
}
}
}
}
//Design Mode
if (CmsContext.IsDesignMode)
{
this.StyleSheetFiles.Add(Url.WebResourceUrl(Profile.Theme + "Theme"));
if (Cms_Application.IsRelease)
{
this.StyleSheetFiles.Add(Url.WebResourceUrl("styles"));
this.JavascriptFiles.Add(Url.WebResourceUrl("extjs"));
this.JavascriptFiles.Add(Url.WebResourceUrl("visualDesignJs"));
}
else
{
this.StyleSheetFiles.Add(ResolveUrl("~/Kooboojs/ext/3.0/resources/css/ext-all.css"));
this.JavascriptFiles.Add(ResolveUrl("~/Kooboojs/ext/3.0/ext-base-debug.js"));
this.JavascriptFiles.Add(ResolveUrl("~/Kooboojs/ext/3.0/ext-all-debug.js"));
this.JavascriptFiles.Add(ResolveUrl(string.Format("~/Kooboojs/VisualDesign.js?d={0}", DateTime.Now.Day)));
}
}
IncludeFiles(writer);
base.Render(writer);
}
private void IncludeFiles(HtmlTextWriter writer)
{
if (this.Header != null)
{
if (StyleSheetFiles != null)
{
foreach (var styleFile in StyleSheetFiles)
{
HtmlLink css = new HtmlLink();
css.Href = styleFile;
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
this.Header.Controls.Add(css);
}
}
foreach (var themeRulesInterpretor in ThemeRulesInterpretors)
{
if (themeRulesInterpretor != null && !StringExtensions.IsNullOrEmptyTrim(themeRulesInterpretor.RepleacedText))
{
Literal control = new Literal();
control.Text = themeRulesInterpretor.RepleacedText;
this.Header.Controls.Add(control);
}
}
if (JavascriptFiles != null)
{
foreach (var jsFile in JavascriptFiles)
{
Literal control = new Literal();
control.Text = string.Format(IncludeJavascript, jsFile);
this.Header.Controls.Add(control);
}
}
}
else
{
if (IsIncludeFiles)
{
writer.WriteLine(@"<!--Using themed css files requires a header control on the page. (e.g. <head runat=""server"" />). -->");
}
//
if (StyleSheetFiles != null)
{
foreach (var styleFile in StyleSheetFiles)
{
writer.Write(IncludeStylesheet, styleFile);
}
}
foreach (var themeRulesInterpretor in ThemeRulesInterpretors)
{
if (themeRulesInterpretor != null && !StringExtensions.IsNullOrEmptyTrim(themeRulesInterpretor.RepleacedText))
{
writer.Write(themeRulesInterpretor.RepleacedText);
}
}
if (JavascriptFiles != null)
{
foreach (var jsFile in JavascriptFiles)
{
writer.Write(IncludeJavascript, jsFile);
}
}
if (IsIncludeFiles)
{
writer.WriteLine(@"<!--/Using themed css files requires a header control on the page. (e.g. <head runat=""server"" />). -->");
}
//
}
}
#endregion
#region Override the RenderView to MVC 1.0
public override void RenderView(ViewContext viewContext)
{
// Not use SwitchWriter, it will cause the Response.Clear() unavailable.
//
this.ViewContext = viewContext;
this.InitHelpers();
this.ID = Guid.NewGuid().ToString();
this.ProcessRequest(HttpContext.Current);
}
#endregion
}
}
|