Added ability to have predefined templates for Partial View Macros

This commit is contained in:
Sebastiaan Janssen
2013-08-26 17:28:15 +02:00
parent ee4312b7f2
commit d17b2cf884
8 changed files with 161 additions and 112 deletions

View File

@@ -2132,6 +2132,7 @@
</Content>
<Content Include="Umbraco\Scripting\templates\cshtml\MultinodeTree-picker.cshtml" />
<Content Include="Umbraco\Scripting\templates\cshtml\TwitterFeed.cshtml" />
<Content Include="Umbraco\PartialViews\Templates\Login.cshtml" />
<None Include="Umbraco_client\CodeMirror\js\mode\coffeescript\LICENSE" />
<None Include="Umbraco_client\CodeMirror\js\mode\pascal\LICENSE" />
<None Include="Umbraco_client\CodeMirror\js\mode\perl\LICENSE" />

View File

@@ -0,0 +1 @@
@using Umbraco.Web.Macros.PartialViewMacroPage

View File

@@ -0,0 +1,3 @@
@using Umbraco.Web.Macros.PartialViewMacroPage
@* Simple login template *@

View File

@@ -11,12 +11,10 @@ Filename (without .cshtml):
<div>
<asp:TextBox ID="FileName" runat="server" CssClass="bigInput"></asp:TextBox>
</div>
<%--<div>
<div>
Choose a template:<br />
<asp:ListBox ID="PartialViewTemplate" runat="server" Rows="1" SelectionMode="Single">
<asp:ListItem Value="clean.xslt">Clean</asp:ListItem>
</asp:ListBox>
</div>--%>
<asp:ListBox ID="PartialViewTemplate" runat="server" Width="350" CssClass="bigInput" Rows="1" SelectionMode="Single" />
</div>
<div>
<asp:CheckBox ID="CreateMacroCheckBox" runat="server" Checked="true" Text="Create Macro"></asp:CheckBox>

View File

@@ -1,25 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.BasePages;
using Umbraco.Core;
using Umbraco.Core.IO;
using umbraco.presentation.create;
using UmbracoSettings = Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Web.UI.Umbraco.Create
{
public partial class PartialViewMacro : System.Web.UI.UserControl
public partial class PartialViewMacro : UserControl
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
DataBind();
LoadTemplates(PartialViewTemplate);
}
private static void LoadTemplates(ListControl list)
{
var path = IOHelper.MapPath(SystemDirectories.Umbraco + "/partialviews/templates/");
list.Items.Clear();
// always add the option of an empty one
list.Items.Add(new ListItem("Empty template", "EmptyTemplate.cshtml"));
if (System.IO.Directory.Exists(path))
{
const string extension = ".cshtml";
//Already adding Empty Template as the first item, so don't add it again
foreach (var fileInfo in new System.IO.DirectoryInfo(path).GetFiles("*" + extension).Where(f => f.Name != "EmptyTemplate.cshtml"))
{
var filename = System.IO.Path.GetFileName(fileInfo.FullName);
var liText = filename.Replace(extension, "").SplitPascalCasing().ToFirstUpperInvariant();
list.Items.Add(new ListItem(liText, filename));
}
}
}
protected void SubmitButton_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
@@ -30,11 +52,11 @@ namespace Umbraco.Web.UI.Umbraco.Create
var createMacroVal = 0;
if (CreateMacroCheckBox.Checked)
createMacroVal = 1;
var returnUrl = dialogHandler_temp.Create(
Request.GetItemAsString("nodeType"),
createMacroVal, //apparently we need to pass this value to 'ParentID'... of course! :P then we'll extract it in PartialViewTasks to create it.
FileName.Text);
string returnUrl = dialogHandler_temp.Create(Request.GetItemAsString("nodeType"),
createMacroVal, //apparently we need to pass this value to 'ParentID'... of course! :P then we'll extract it in PartialViewTasks to create it.
PartialViewTemplate.SelectedValue + "|||" + FileName.Text);
BasePage.Current.ClientTools
.ChangeContentFrameUrl(returnUrl)

View File

@@ -30,6 +30,15 @@ namespace Umbraco.Web.UI.Umbraco.Create {
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox FileName;
/// <summary>
/// PartialViewTemplate control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.ListBox PartialViewTemplate;
/// <summary>
/// CreateMacroCheckBox control.
/// </summary>

View File

@@ -1872,9 +1872,13 @@
<Content Include="umbraco.presentation\umbraco\dialogs\importDocumenttype.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\insertMacro.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\insertTable.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\notifications.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\notifications.aspx">
<SubType>ASPXCodeBehind</SubType>
</Content>
<Content Include="umbraco.presentation\umbraco\developer\Packages\installer.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\protectPage.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\protectPage.aspx">
<SubType>ASPXCodeBehind</SubType>
</Content>
<Content Include="umbraco.presentation\umbraco\dialogs\RegexWs.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\rollBack.aspx" />
<Content Include="umbraco.presentation\umbraco\dialogs\sendToTranslation.aspx" />
@@ -1943,11 +1947,15 @@
<Content Include="umbraco.presentation\umbraco\developer\Xslt\xsltChooseExtension.aspx" />
<Content Include="umbraco.presentation\umbraco\developer\Xslt\xsltInsertValueOf.aspx" />
<Content Include="umbraco.presentation\umbraco\js\language.aspx" />
<Content Include="umbraco.presentation\umbraco\members\EditMember.aspx" />
<Content Include="umbraco.presentation\umbraco\members\EditMember.aspx">
<SubType>ASPXCodeBehind</SubType>
</Content>
<Content Include="umbraco.presentation\umbraco\members\EditMemberGroup.aspx">
<SubType>Form</SubType>
</Content>
<Content Include="umbraco.presentation\umbraco\members\EditMemberType.aspx" />
<Content Include="umbraco.presentation\umbraco\members\EditMemberType.aspx">
<SubType>ASPXCodeBehind</SubType>
</Content>
<Content Include="umbraco.presentation\umbraco\schemas\umbraco.xsd">
<SubType>Designer</SubType>
</Content>

View File

@@ -9,116 +9,123 @@ using Umbraco.Core;
namespace umbraco
{
/// <summary>
/// The UI 'tasks' for the create dialog and delete processes
/// </summary>
[UmbracoWillObsolete("http://issues.umbraco.org/issue/U4-1373", "This will one day be removed when we overhaul the create process")]
public class PartialViewTasks : interfaces.ITaskReturnUrl
{
private string _alias;
private int _parentId;
private int _typeId;
private int _userId;
/// <summary>
/// The UI 'tasks' for the create dialog and delete processes
/// </summary>
[UmbracoWillObsolete("http://issues.umbraco.org/issue/U4-1373", "This will one day be removed when we overhaul the create process")]
public class PartialViewTasks : interfaces.ITaskReturnUrl
{
private string _alias;
private int _parentId;
private int _typeId;
private int _userId;
protected virtual string EditViewFile
{
get { return "Settings/Views/EditView.aspx"; }
}
protected virtual string EditViewFile
{
get { return "Settings/Views/EditView.aspx"; }
}
protected string BasePath
{
get { return SystemDirectories.MvcViews + "/" + ParentFolderName.EnsureEndsWith('/'); }
}
protected string BasePath
{
get { return SystemDirectories.MvcViews + "/" + ParentFolderName.EnsureEndsWith('/'); }
}
protected virtual string ParentFolderName
{
get { return "Partials"; }
}
protected virtual string ParentFolderName
{
get { return "Partials"; }
}
public int UserId
{
set { _userId = value; }
}
public int TypeID
{
set { _typeId = value; }
get { return _typeId; }
}
public int UserId
{
set { _userId = value; }
}
public int TypeID
{
set { _typeId = value; }
get { return _typeId; }
}
public string Alias
{
set { _alias = value; }
get { return _alias; }
}
public string Alias
{
set { _alias = value; }
get { return _alias; }
}
public int ParentID
{
set { _parentId = value; }
get { return _parentId; }
}
public int ParentID
{
set { _parentId = value; }
get { return _parentId; }
}
public bool Save()
{
var fileName = _alias + ".cshtml";
var fullFilePath = IOHelper.MapPath(BasePath + fileName);
//return the link to edit the file if it already exists
if (File.Exists(fullFilePath))
{
_returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName));
return true;
}
public bool Save()
{
var pipesIndex = _alias.IndexOf("|||", System.StringComparison.Ordinal);
var template = _alias.Substring(0, pipesIndex).Trim();
var fileName = _alias.Substring(pipesIndex + 3, _alias.Length - pipesIndex - 3) + ".cshtml";
//create the file
using (var sw = File.CreateText(fullFilePath))
{
WriteTemplateHeader(sw);
}
var fullFilePath = IOHelper.MapPath(BasePath + fileName);
// Create macro?
if (ParentID == 1)
{
//return the link to edit the file if it already exists
if (File.Exists(fullFilePath))
{
_returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName));
return true;
}
//create the file
using (var sw = File.CreateText(fullFilePath))
{
using (var templateFile = File.OpenText(IOHelper.MapPath(SystemDirectories.Umbraco + "/partialviews/templates/" + template)))
{
var templateContent = templateFile.ReadToEnd();
sw.Write(templateContent);
}
}
// Create macro?
if (ParentID == 1)
{
var name = fileName
.Substring(0, (fileName.LastIndexOf('.') + 1)).Trim('.')
.SplitPascalCasing().ToFirstUpperInvariant();
var m = cms.businesslogic.macro.Macro.MakeNew(name);
m.ScriptingFile = BasePath + fileName;
}
_returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName));
return true;
}
var m = cms.businesslogic.macro.Macro.MakeNew(name);
m.ScriptingFile = BasePath + fileName;
}
protected virtual void WriteTemplateHeader(StreamWriter sw)
{
//write out the template header
sw.Write("@inherits ");
sw.Write(typeof(UmbracoViewPage<>).FullName.TrimEnd("`1"));
sw.Write("<dynamic>");
}
_returnUrl = string.Format(EditViewFile + "?file={0}", HttpUtility.UrlEncode(ParentFolderName.EnsureEndsWith('/') + fileName));
return true;
}
public bool Delete()
{
var path = IOHelper.MapPath(BasePath + _alias.TrimStart('/'));
protected virtual void WriteTemplateHeader(StreamWriter sw)
{
//write out the template header
sw.Write("@inherits ");
sw.Write(typeof(UmbracoViewPage<>).FullName.TrimEnd("`1"));
sw.Write("<dynamic>");
}
if (File.Exists(path))
File.Delete(path);
else if (Directory.Exists(path))
Directory.Delete(path, true);
public bool Delete()
{
var path = IOHelper.MapPath(BasePath + _alias.TrimStart('/'));
LogHelper.Info<PartialViewTasks>(string.Format("{0} Deleted by user {1}", _alias, UmbracoEnsuredPage.CurrentUser.Id));
if (File.Exists(path))
File.Delete(path);
else if (Directory.Exists(path))
Directory.Delete(path, true);
return true;
}
LogHelper.Info<PartialViewTasks>(string.Format("{0} Deleted by user {1}", _alias, UmbracoEnsuredPage.CurrentUser.Id));
#region ITaskReturnUrl Members
private string _returnUrl = "";
public string ReturnUrl
{
get { return _returnUrl; }
}
return true;
}
#endregion
}
#region ITaskReturnUrl Members
private string _returnUrl = "";
public string ReturnUrl
{
get { return _returnUrl; }
}
#endregion
}
}