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 1471abd8d3..a5d960a9ec 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 @@ -20,14 +20,14 @@ function macroResource($q, $http, umbRequestHelper) { * @param {int} macroId The macro id to get parameters for * */ - getMacroParameters: function (macroId) { + getMacroParameters: function(macroId) { return umbRequestHelper.resourcePromise( - $http.get( - umbRequestHelper.getApiUrl( - "macroRenderingApiBaseUrl", - "GetMacroParameters", - [{ macroId: macroId }])), - 'Failed to retrieve macro parameters for macro with id ' + macroId); + $http.get( + umbRequestHelper.getApiUrl( + "macroRenderingApiBaseUrl", + "GetMacroParameters", + [{ macroId: macroId }])), + 'Failed to retrieve macro parameters for macro with id ' + macroId); }, /** @@ -43,13 +43,14 @@ function macroResource($q, $http, umbRequestHelper) { * @param {Array} macroParamDictionary A dictionary of macro parameters * */ - getMacroResultAsHtmlForEditor: function (macroAlias, pageId, macroParamDictionary) { + getMacroResultAsHtmlForEditor: function(macroAlias, pageId, macroParamDictionary) { return umbRequestHelper.resourcePromise( $http.post( umbRequestHelper.getApiUrl( "macroRenderingApiBaseUrl", - "GetMacroResultAsHtmlForEditor"), { + "GetMacroResultAsHtmlForEditor"), + { macroAlias: macroAlias, pageId: pageId, macroParams: macroParamDictionary @@ -68,10 +69,11 @@ function macroResource($q, $http, umbRequestHelper) { $http.post( umbRequestHelper.getApiUrl( "macroRenderingApiBaseUrl", - "CreatePartialViewMacroWithFile"), { - virtualPath: virtualPath, - filename: filename - } + "CreatePartialViewMacroWithFile"), + { + virtualPath: virtualPath, + filename: filename + } ), 'Failed to create macro "' + filename + '"' ); @@ -96,7 +98,7 @@ function macroResource($q, $http, umbRequestHelper) { ); }, - getParameterEditors: function () { + getParameterEditors: function() { return umbRequestHelper.resourcePromise( $http.get(umbRequestHelper.getApiUrl("macroApiBaseUrl", "GetParameterEditors"), "Failed to get parameter editors") @@ -105,10 +107,16 @@ function macroResource($q, $http, umbRequestHelper) { getById: function(id) { return umbRequestHelper.resourcePromise( - $http.get(umbRequestHelper.getApiUrl("macroApiBaseUrl", "GetById", { "id" : id}), "Failed to get macro") - ); + $http.get(umbRequestHelper.getApiUrl("macroApiBaseUrl", "GetById", { "id": id }), "Failed to get macro") + ); + }, + + saveMacro: function(macro) { + return umbRequestHelper.resourcePromise( + $http.post(umbRequestHelper.getApiUrl("macroApiBaseUrl", "Save"), macro) + ); } - }; +}; } angular.module('umbraco.resources').factory('macroResource', macroResource); diff --git a/src/Umbraco.Web.UI.Client/src/views/macros/macros.edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/macros/macros.edit.controller.js index 91af9b0f09..978025ceb4 100644 --- a/src/Umbraco.Web.UI.Client/src/views/macros/macros.edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/macros/macros.edit.controller.js @@ -6,7 +6,7 @@ * @description * The controller for editing macros. */ -function MacrosEditController($scope, $q, $routeParams, macroResource, editorState, navigationService, dateHelper, userService, entityResource, formHelper, contentEditingHelper, localizationService, angularHelper) { +function MacrosEditController($scope, $q, $routeParams, macroResource, editorState, navigationService, formHelper, contentEditingHelper, localizationService, angularHelper) { var vm = this; @@ -25,24 +25,23 @@ function MacrosEditController($scope, $q, $routeParams, macroResource, editorSta vm.toggle = toggleValue; - function saveMacro() { - vm.page.saveButtonState = "busy"; + function saveMacro() { if (formHelper.submitForm({ scope: $scope, statusMessage: "Saving..." })) { - console.log(vm.macro); - //relationTypeResource.save(vm.relationType).then(function (data) { - // formHelper.resetForm({ scope: $scope, notifications: data.notifications }); - // bindRelationType(data); - // vm.page.saveButtonState = "success"; - //}, function (error) { - // contentEditingHelper.handleSaveError({ - // redirectOnFailure: false, - // err: error - // }); + vm.page.saveButtonState = "busy"; + + macroResource.saveMacro(vm.macro).then(function (data) { + formHelper.resetForm({ scope: $scope, notifications: data.notifications }); + bindMacro(data); + vm.page.saveButtonState = "success"; + }, function (error) { + contentEditingHelper.handleSaveError({ + redirectOnFailure: false, + err: error + }); - // notificationsService.error(error.data.message); - // vm.page.saveButtonState = "error"; - //}); + vm.page.saveButtonState = "error"; + }); } } @@ -95,6 +94,15 @@ function MacrosEditController($scope, $q, $routeParams, macroResource, editorSta return deferred.promise; } + function bindMacro(data) { + vm.macro = data; + editorState.set(vm.macro); + + navigationService.syncTree({ tree: "macros", path: vm.macro.path, forceReload: true }).then(function (syncArgs) { + vm.page.menu.currentNode = syncArgs.node; + }); + } + function init() { vm.page.loading = true; @@ -116,13 +124,8 @@ function MacrosEditController($scope, $q, $routeParams, macroResource, editorSta vm.parameterEditors = values[key]; } - if (keys[i] === 'macro') { - vm.macro = values[key]; - editorState.set(vm.macro); - - navigationService.syncTree({ tree: "macros", path: vm.macro.path, forceReload: true }).then(function (syncArgs) { - vm.page.menu.currentNode = syncArgs.node; - }); + if (keys[i] === 'macro') { + bindMacro(values[key]); } } diff --git a/src/Umbraco.Web/Editors/MacrosController.cs b/src/Umbraco.Web/Editors/MacrosController.cs index f8214b77a8..933de89635 100644 --- a/src/Umbraco.Web/Editors/MacrosController.cs +++ b/src/Umbraco.Web/Editors/MacrosController.cs @@ -15,6 +15,7 @@ using Umbraco.Web.Composing; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Mvc; + using Umbraco.Web.UI; using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; @@ -48,18 +49,18 @@ if (this.Services.MacroService.GetByAlias(alias) != null) { - return this.ReturnErrorResponse("Macro with this name already exists"); + return this.ReturnErrorResponse("Macro with this alias already exists"); } try { var macro = new Macro - { - Alias = alias, - Name = name, - MacroSource = string.Empty, - MacroType = MacroTypes.PartialView - }; + { + Alias = alias, + Name = name, + MacroSource = string.Empty, + MacroType = MacroTypes.PartialView + }; this.Services.MacroService.Save(macro, this.Security.CurrentUser.Id); @@ -72,7 +73,7 @@ } [HttpGet] - public HttpResponseMessage GetById(int id) + public HttpResponseMessage GetById(int id) { var macro = this.Services.MacroService.GetById(id); @@ -82,27 +83,31 @@ } var macroDisplay = new MacroDisplay - { - Alias = macro.Alias, Id = macro.Id, Key = macro.Key, Name = macro.Name, - CacheByPage = macro.CacheByPage, CacheByUser = macro.CacheByMember, - CachePeriod = macro.CacheDuration, - View = macro.MacroSource, - RenderInEditor = !macro.DontRender, - UseInEditor = macro.UseInEditor, - Path = $"-1,{macro.Id}" - }; + { + Alias = macro.Alias, + Id = macro.Id, + Key = macro.Key, + Name = macro.Name, + CacheByPage = macro.CacheByPage, + CacheByUser = macro.CacheByMember, + CachePeriod = macro.CacheDuration, + View = macro.MacroSource, + RenderInEditor = !macro.DontRender, + UseInEditor = macro.UseInEditor, + Path = $"-1,{macro.Id}" + }; var parameters = new List(); foreach (var param in macro.Properties.Values.OrderBy(x => x.SortOrder)) { parameters.Add(new MacroParameterDisplay - { - Editor = param.EditorAlias, - Key = param.Alias, - Label = param.Name, - Id = param.Id - }); + { + Editor = param.EditorAlias, + Key = param.Alias, + Label = param.Name, + Id = param.Id + }); } macroDisplay.Parameters = parameters; @@ -110,6 +115,57 @@ return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); } + [HttpPost] + public HttpResponseMessage Save(MacroDisplay macroDisplay) + { + if (macroDisplay == null) + { + return this.ReturnErrorResponse($"No macro data found in request"); + } + + var macro = this.Services.MacroService.GetById(int.Parse(macroDisplay.Id.ToString())); + + if (macro == null) + { + return this.ReturnErrorResponse($"Macro with id {macroDisplay.Id} does not exist"); + } + + if (macroDisplay.Alias != macro.Alias) + { + var macroByAlias = this.Services.MacroService.GetByAlias(macroDisplay.Alias); + + if (macroByAlias != null) + { + return this.ReturnErrorResponse("Macro with this alias already exists"); + } + } + + macro.Alias = macroDisplay.Alias; + macro.Name = macroDisplay.Name; + macro.CacheByMember = macroDisplay.CacheByUser; + macro.CacheByPage = macroDisplay.CacheByPage; + macro.CacheDuration = macroDisplay.CachePeriod; + macro.DontRender = !macroDisplay.RenderInEditor; + macro.UseInEditor = macroDisplay.UseInEditor; + macro.MacroSource = macroDisplay.View; + macro.MacroType = MacroTypes.PartialView; + + try + { + this.Services.MacroService.Save(macro, this.Security.CurrentUser.Id); + + macroDisplay.Notifications.Clear(); + + macroDisplay.Notifications.Add(new Models.ContentEditing.Notification("Success", "Macro saved", SpeechBubbleIcon.Success)); + + return this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay); + } + catch (Exception exception) + { + return this.ReturnErrorResponse("Error creating macro", true, exception); + } + } + /// /// Gets a list of available macro partials /// @@ -245,7 +301,7 @@ { var files = new List(); var dirInfo = new DirectoryInfo(path); - + foreach (var dir in dirInfo.GetDirectories()) { files.AddRange(this.FindPartialViewFilesInFolder(orgPath, path + "/" + dir.Name, prefixVirtualPath));