diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index 6360ad8988..2105f7af2c 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -744,40 +744,9 @@ namespace Umbraco.Core.Services if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs(partialView, true, partialView.Alias, -1), this)) return Attempt.Fail(); - string partialViewHeader; - switch (partialViewType) - { - case PartialViewType.PartialView: - partialViewHeader = PartialViewHeader; - break; - case PartialViewType.PartialViewMacro: - partialViewHeader = PartialViewMacroHeader; - break; - default: - throw new ArgumentOutOfRangeException("partialViewType"); - } - if (snippetName.IsNullOrWhiteSpace() == false) { - //create the file - var snippetPathAttempt = TryGetSnippetPath(snippetName); - if (snippetPathAttempt.Success == false) - { - throw new InvalidOperationException("Could not load snippet with name " + snippetName); - } - - using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) - { - var snippetContent = snippetFile.ReadToEnd().Trim(); - - //strip the @inherits if it's there - snippetContent = StripPartialViewHeader(snippetContent); - - var content = string.Format("{0}{1}{2}", - partialViewHeader, - Environment.NewLine, snippetContent); - partialView.Content = content; - } + partialView.Content = GetPartialViewMacroSnippetContent(snippetName, partialViewType); } var uow = _fileUowProvider.GetUnitOfWork(); @@ -914,6 +883,55 @@ namespace Umbraco.Core.Services } } + public string GetPartialViewSnippetContent(string snippetName) + { + return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialView); + } + + public string GetPartialViewMacroSnippetContent(string snippetName) + { + return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro); + } + + private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) + { + if (snippetName.IsNullOrWhiteSpace()) + throw new ArgumentNullException("snippetName"); + + string partialViewHeader; + switch (partialViewType) + { + case PartialViewType.PartialView: + partialViewHeader = PartialViewHeader; + break; + case PartialViewType.PartialViewMacro: + partialViewHeader = PartialViewMacroHeader; + break; + default: + throw new ArgumentOutOfRangeException("partialViewType"); + } + + // Try and get the snippet path + var snippetPathAttempt = TryGetSnippetPath(snippetName); + if (snippetPathAttempt.Success == false) + { + throw new InvalidOperationException("Could not load snippet with name " + snippetName); + } + + using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) + { + var snippetContent = snippetFile.ReadToEnd().Trim(); + + //strip the @inherits if it's there + snippetContent = StripPartialViewHeader(snippetContent); + + var content = string.Format("{0}{1}{2}", + partialViewHeader, + Environment.NewLine, snippetContent); + return content; + } + } + public void SetPartialViewMacroFileContent(string filepath, Stream content) { using (var repository = GetPartialViewRepository(PartialViewType.PartialViewMacro, UowProvider.GetUnitOfWork())) diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 5115a25087..453d199bfe 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -331,6 +331,13 @@ namespace Umbraco.Core.Services /// The content of the macro partial view. Stream GetPartialViewMacroFileContentStream(string filepath); + /// + /// Gets the content of a macro partial view snippet as a string + /// + /// The name of the snippet + /// + string GetPartialViewMacroSnippetContent(string snippetName); + /// /// Sets the content of a macro partial view. /// @@ -352,6 +359,13 @@ namespace Umbraco.Core.Services /// The content of the partial view. Stream GetPartialViewFileContentStream(string filepath); + /// + /// Gets the content of a partial view snippet as a string. + /// + /// The name of the snippet + /// The content of the partial view. + string GetPartialViewSnippetContent(string snippetName); + /// /// Sets the content of a partial view. /// diff --git a/src/Umbraco.Web/Editors/CodeFileController.cs b/src/Umbraco.Web/Editors/CodeFileController.cs index 4f1be8563a..f8d6692e3c 100644 --- a/src/Umbraco.Web/Editors/CodeFileController.cs +++ b/src/Umbraco.Web/Editors/CodeFileController.cs @@ -33,11 +33,13 @@ namespace Umbraco.Web.Editors { case Core.Constants.Trees.PartialViews: var view = new PartialView(display.VirtualPath); + view.Content = display.Content; var result = Services.FileService.CreatePartialView(view, display.Snippet, Security.CurrentUser.Id); return result.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(result.Exception.Message); case Core.Constants.Trees.PartialViewMacros: var viewMacro = new PartialView(display.VirtualPath); + viewMacro.Content = display.Content; var resultMacro = Services.FileService.CreatePartialViewMacro(viewMacro, display.Snippet, Security.CurrentUser.Id); return resultMacro.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(resultMacro.Exception.Message); @@ -136,6 +138,46 @@ namespace Umbraco.Web.Editors return snippets.Select(snippet => new SnippetDisplay() {Name = snippet.SplitPascalCasing().ToFirstUpperInvariant(), FileName = snippet}); } + /// + /// Used to scaffold the json object for the editors for 'scripts', 'partialViews', 'partialViewMacros' + /// + /// This is a string but will be 'scripts' 'partialViews', 'partialViewMacros' + /// + /// + public CodeFileDisplay GetScaffold(string type, string snippetName = null) + { + if (string.IsNullOrWhiteSpace(type)) + { + throw new HttpResponseException(HttpStatusCode.BadRequest); + } + + CodeFileDisplay codeFileDisplay; + + switch (type) + { + case Core.Constants.Trees.PartialViews: + codeFileDisplay = Mapper.Map(new PartialView(string.Empty)); + if (snippetName.IsNullOrWhiteSpace() == false) + codeFileDisplay.Content = Services.FileService.GetPartialViewSnippetContent(snippetName); + break; + case Core.Constants.Trees.PartialViewMacros: + codeFileDisplay = Mapper.Map(new PartialView(string.Empty)); + if (snippetName.IsNullOrWhiteSpace() == false) + codeFileDisplay.Content = Services.FileService.GetPartialViewMacroSnippetContent(snippetName); + break; + case Core.Constants.Trees.Scripts: + codeFileDisplay = Mapper.Map(new Script(string.Empty)); + break; + default: + throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unsupported editortype")); + } + + codeFileDisplay.FileType = type; + codeFileDisplay.VirtualPath = "-1"; + + return codeFileDisplay; + } + /// /// Used to delete a specific file from disk via the FileService ///