diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/macro.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/macro.resource.js index a5d960a9ec..73a1651b5e 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/macro.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/macro.resource.js @@ -115,6 +115,12 @@ function macroResource($q, $http, umbRequestHelper) { return umbRequestHelper.resourcePromise( $http.post(umbRequestHelper.getApiUrl("macroApiBaseUrl", "Save"), macro) ); + }, + + deleteById: function(id) { + return umbRequestHelper.resourcePromise( + $http.post(umbRequestHelper.getApiUrl("macroApiBaseUrl", "deleteById", { "id": id })) + ); } }; } diff --git a/src/Umbraco.Web.UI.Client/src/views/macros/delete.html b/src/Umbraco.Web.UI.Client/src/views/macros/delete.html new file mode 100644 index 0000000000..2d097e110b --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/macros/delete.html @@ -0,0 +1,10 @@ +
+
+ +

+ Are you sure you want to delete {{vm.name}} ? +

+ + +
+
diff --git a/src/Umbraco.Web.UI.Client/src/views/macros/macros.delete.controller.js b/src/Umbraco.Web.UI.Client/src/views/macros/macros.delete.controller.js new file mode 100644 index 0000000000..1aadef9f31 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/macros/macros.delete.controller.js @@ -0,0 +1,32 @@ +/** + * @ngdoc controller + * @name Umbraco.Editors.Macros.DeleteController + * @function + * + * @description + * The controller for deleting macro items + */ +function MacrosDeleteController($scope, $location, macroResource, navigationService, treeService) { + var vm = this; + + vm.name = $scope.currentNode.name; + function performDelete() { + $scope.currentNode.loading = true; + macroResource.deleteById($scope.currentNode.id).then(function () { + $scope.currentNode.loading = false; + + treeService.removeNode($scope.currentNode); + + navigationService.hideMenu(); + }); + } + + function cancel() { + navigationService.hideDialog(); + } + + vm.performDelete = performDelete; + vm.cancel = cancel; +} + +angular.module("umbraco").controller("Umbraco.Editors.Macros.DeleteController", MacrosDeleteController); diff --git a/src/Umbraco.Web/Editors/MacroRenderingController.cs b/src/Umbraco.Web/Editors/MacroRenderingController.cs index 08ff4ca44b..0b4a78e603 100644 --- a/src/Umbraco.Web/Editors/MacroRenderingController.cs +++ b/src/Umbraco.Web/Editors/MacroRenderingController.cs @@ -15,6 +15,7 @@ using Umbraco.Web.Macros; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Services; namespace Umbraco.Web.Editors { @@ -29,11 +30,14 @@ namespace Umbraco.Web.Editors [PluginController("UmbracoApi")] public class MacroRenderingController : UmbracoAuthorizedJsonController, IRequiresSessionState { + private readonly IMacroService _macroService; + private readonly IContentService _contentService; private readonly IVariationContextAccessor _variationContextAccessor; - - public MacroRenderingController(IVariationContextAccessor variationContextAccessor) + public MacroRenderingController(IVariationContextAccessor variationContextAccessor, IMacroService macroService, IContentService contentService) { _variationContextAccessor = variationContextAccessor; + _macroService = macroService; + _contentService = contentService; } /// @@ -41,12 +45,12 @@ namespace Umbraco.Web.Editors /// /// /// - /// Note that ALL logged in users have access to this method because editors will need to isnert macros into rte (content/media/members) and it's used for + /// Note that ALL logged in users have access to this method because editors will need to insert macros into rte (content/media/members) and it's used for /// inserting into templates/views/etc... it doesn't expose any sensitive data. /// public IEnumerable GetMacroParameters(int macroId) { - var macro = Services.MacroService.GetById(macroId); + var macro = _macroService.GetById(macroId); if (macro == null) { throw new HttpResponseException(HttpStatusCode.NotFound); @@ -97,13 +101,13 @@ namespace Umbraco.Web.Editors { // note - here we should be using the cache, provided that the preview content is in the cache... - var doc = Services.ContentService.GetById(pageId); + var doc = _contentService.GetById(pageId); if (doc == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } - var m = Services.MacroService.GetByAlias(macroAlias); + var m = _macroService.GetByAlias(macroAlias); if (m == null) throw new HttpResponseException(HttpStatusCode.NotFound); var macro = new MacroModel(m); @@ -166,7 +170,7 @@ namespace Umbraco.Web.Editors MacroSource = model.VirtualPath.EnsureStartsWith("~") }; - Services.MacroService.Save(macro); // may throw + _macroService.Save(macro); // may throw return new HttpResponseMessage(HttpStatusCode.OK); } diff --git a/src/Umbraco.Web/Editors/MacrosController.cs b/src/Umbraco.Web/Editors/MacrosController.cs index 5ff2f7bf34..84cd1911c3 100644 --- a/src/Umbraco.Web/Editors/MacrosController.cs +++ b/src/Umbraco.Web/Editors/MacrosController.cs @@ -1,4 +1,6 @@ -namespace Umbraco.Web.Editors +using Umbraco.Core.Services; + +namespace Umbraco.Web.Editors { using System; using System.Collections.Generic; @@ -28,6 +30,13 @@ [UmbracoTreeAuthorize(Constants.Trees.Macros)] public class MacrosController : BackOfficeNotificationsController { + private readonly IMacroService _macroService; + + public MacrosController(IMacroService macroService) + { + _macroService = macroService; + } + /// /// Creates a new macro /// @@ -47,7 +56,7 @@ var alias = name.ToSafeAlias(); - if (this.Services.MacroService.GetByAlias(alias) != null) + if (_macroService.GetByAlias(alias) != null) { return this.ReturnErrorResponse("Macro with this alias already exists"); } @@ -62,7 +71,7 @@ MacroType = MacroTypes.PartialView }; - this.Services.MacroService.Save(macro, this.Security.CurrentUser.Id); + _macroService.Save(macro, this.Security.CurrentUser.Id); return this.Request.CreateResponse(HttpStatusCode.OK, macro.Id); } @@ -75,7 +84,7 @@ [HttpGet] public HttpResponseMessage GetById(int id) { - var macro = this.Services.MacroService.GetById(id); + var macro = _macroService.GetById(id); if (macro == null) { @@ -115,6 +124,22 @@ return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); } + + [HttpPost] + public HttpResponseMessage DeleteById(int id) + { + var macro = _macroService.GetById(id); + + if (macro == null) + { + return this.ReturnErrorResponse($"Macro with id {id} does not exist"); + } + + _macroService.Delete(macro); + + return Request.CreateResponse(HttpStatusCode.OK); + } + [HttpPost] public HttpResponseMessage Save(MacroDisplay macroDisplay) { @@ -123,7 +148,7 @@ return this.ReturnErrorResponse($"No macro data found in request"); } - var macro = this.Services.MacroService.GetById(int.Parse(macroDisplay.Id.ToString())); + var macro = _macroService.GetById(int.Parse(macroDisplay.Id.ToString())); if (macro == null) { @@ -132,7 +157,7 @@ if (macroDisplay.Alias != macro.Alias) { - var macroByAlias = this.Services.MacroService.GetByAlias(macroDisplay.Alias); + var macroByAlias = _macroService.GetByAlias(macroDisplay.Alias); if (macroByAlias != null) { @@ -153,7 +178,7 @@ try { - this.Services.MacroService.Save(macro, this.Security.CurrentUser.Id); + _macroService.Save(macro, this.Security.CurrentUser.Id); macroDisplay.Notifications.Clear(); diff --git a/src/Umbraco.Web/Trees/MacrosTreeController.cs b/src/Umbraco.Web/Trees/MacrosTreeController.cs index cdbe4bfcf7..0300dbd6c6 100644 --- a/src/Umbraco.Web/Trees/MacrosTreeController.cs +++ b/src/Umbraco.Web/Trees/MacrosTreeController.cs @@ -54,7 +54,7 @@ namespace Umbraco.Web.Trees { //Create the normal create action menu.Items.Add(Services.TextService); - + //refresh action menu.Items.Add(new RefreshNode(Services.TextService, true)); @@ -65,16 +65,7 @@ namespace Umbraco.Web.Trees if (macro == null) return new MenuItemCollection(); //add delete option for all macros - menu.Items.Add(Services.TextService, opensDialog: true) - //Since we haven't implemented anything for macros in angular, this needs to be converted to - //use the legacy format - .ConvertLegacyMenuItem(new EntitySlim - { - Id = macro.Id, - Level = 1, - ParentId = -1, - Name = macro.Name - }, "macros", queryStrings.GetValue("application")); + menu.Items.Add(Services.TextService, opensDialog: true); return menu; }