diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index f4f634d974..ec886d788d 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -735,6 +735,26 @@ namespace Umbraco.Core.Services return empty.Union(files.Except(empty)); } + public void CreatePartialViewFolder(string folderPath) + { + var uow = _fileUowProvider.GetUnitOfWork(); + using (var repository = RepositoryFactory.CreatePartialViewRepository(uow)) + { + ((PartialViewRepository)repository).AddFolder(folderPath); + uow.Commit(); + } + } + + public void CreatePartialViewMacroFolder(string folderPath) + { + var uow = _fileUowProvider.GetUnitOfWork(); + using (var repository = RepositoryFactory.CreatePartialViewMacroRepository(uow)) + { + ((PartialViewMacroRepository)repository).AddFolder(folderPath); + uow.Commit(); + } + } + public void DeletePartialViewFolder(string folderPath) { using (var uow = _fileUowProvider.GetUnitOfWork()) diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 453d199bfe..e8f1065219 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -11,6 +11,8 @@ namespace Umbraco.Core.Services public interface IFileService : IService { IEnumerable GetPartialViewSnippetNames(params string[] filterNames); + void CreatePartialViewFolder(string folderPath); + void CreatePartialViewMacroFolder(string folderPath); void DeletePartialViewFolder(string folderPath); void DeletePartialViewMacroFolder(string folderPath); IPartialView GetPartialView(string path); diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/codefile.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/codefile.resource.js index e83bb4f14e..44454f21e3 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/codefile.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/codefile.resource.js @@ -205,6 +205,38 @@ function codefileResource($q, $http, umbDataFormatter, umbRequestHelper) { "codeFileApiBaseUrl", "GetScaffold?type=" + type + "&id=" + id + "&snippetName=" + snippetName)), "Failed to get scaffold for" + type); + }, + + /** + * @ngdoc method + * @name umbraco.resources.codefileResource#createContainer + * @methodOf umbraco.resources.codefileResource + * + * @description + * Creates a container/folder + * + * ##usage + *
+         * codefileResource.createContainer("partialViews", "folder%2ffolder", "folder")
+         *    .then(function(data) {
+         *        alert('its here!');
+         *    });
+         * 
+ * + * @param {string} File type: (scripts, partialViews, partialViewMacros). + * @param {string} Parent Id: url encoded path + * @param {string} Container name + * @returns {Promise} resourcePromise object. + * + */ + + createContainer: function(type, parentId, name) { + return umbRequestHelper.resourcePromise( + $http.post(umbRequestHelper.getApiUrl( + "codeFileApiBaseUrl", + "PostCreateContainer", + { type: type, parentId: parentId, name: encodeURIComponent(name) })), + 'Failed to create a folder under parent id ' + parentId); } }; diff --git a/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.controller.js b/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.controller.js index 3b3fb0f9f5..6132ba3f89 100644 --- a/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.controller.js @@ -1,14 +1,17 @@ (function () { "use strict"; - function PartialViewMacrosCreateController($scope, codefileResource, $location, navigationService) { + function PartialViewMacrosCreateController($scope, codefileResource, $location, navigationService, formHelper, localizationService, appState) { var vm = this; var node = $scope.dialogOptions.currentNode; + var localizeCreateFolder = localizationService.localize("defaultdialog_createFolder"); vm.snippets = []; vm.showSnippets = false; vm.creatingFolder = false; + vm.createFolderError = ""; + vm.folderName = ""; vm.createPartialViewMacro = createPartialViewMacro; vm.showCreateFolder = showCreateFolder; @@ -39,8 +42,38 @@ vm.creatingFolder = true; } - function createFolder() { + function createFolder(form) { + if (formHelper.submitForm({scope: $scope, formCtrl: form, statusMessage: localizeCreateFolder})) { + codefileResource.createContainer("partialViewMacros", node.id, vm.folderName).then(function (saved) { + + navigationService.hideMenu(); + + navigationService.syncTree({ + tree: "partialViewMacros", + path: saved.path, + forceReload: true, + activate: true + }); + + formHelper.resetForm({ + scope: $scope + }); + + var section = appState.getSectionState("currentSection"); + + }, function(err) { + + vm.createFolderError = err; + + //show any notifications + if (angular.isArray(err.data.notifications)) { + for (var i = 0; i < err.data.notifications.length; i++) { + notificationsService.showNotification(err.data.notifications[i]); + } + } + }); + } } function showCreateFromSnippet() { diff --git a/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.html b/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.html index aa8796ae7a..0c5a74c4b0 100644 --- a/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.html +++ b/src/Umbraco.Web.UI.Client/src/views/partialviewmacros/create.html @@ -47,12 +47,12 @@
+ ng-submit="vm.createFolder(createFolderForm)" + val-form-manager> -
-
{{error.errorMsg}}
-

{{error.data.message}}

+
+
{{vm.createFolderError.errorMsg}}
+

{{vm.createFolderError.data.message}}

diff --git a/src/Umbraco.Web.UI.Client/src/views/partialviews/create.controller.js b/src/Umbraco.Web.UI.Client/src/views/partialviews/create.controller.js index 5aeaa28935..95d224b890 100644 --- a/src/Umbraco.Web.UI.Client/src/views/partialviews/create.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/partialviews/create.controller.js @@ -1,14 +1,17 @@ (function () { "use strict"; - function PartialViewsCreateController($scope, codefileResource, $location, navigationService) { + function PartialViewsCreateController($scope, codefileResource, $location, navigationService, formHelper, localizationService, appState) { var vm = this; var node = $scope.dialogOptions.currentNode; + var localizeCreateFolder = localizationService.localize("defaultdialog_createFolder"); vm.snippets = []; vm.showSnippets = false; vm.creatingFolder = false; + vm.createFolderError = ""; + vm.folderName = ""; vm.createPartialView = createPartialView; vm.showCreateFolder = showCreateFolder; @@ -39,8 +42,38 @@ vm.creatingFolder = true; } - function createFolder() { + function createFolder(form) { + if (formHelper.submitForm({scope: $scope, formCtrl: form, statusMessage: localizeCreateFolder})) { + codefileResource.createContainer("partialViews", node.id, vm.folderName).then(function(saved) { + + navigationService.hideMenu(); + + navigationService.syncTree({ + tree: "partialViews", + path: saved.path, + forceReload: true, + activate: true + }); + + formHelper.resetForm({ + scope: $scope + }); + + var section = appState.getSectionState("currentSection"); + + }, function(err) { + + vm.createFolderError = err; + + //show any notifications + if (angular.isArray(err.data.notifications)) { + for (var i = 0; i < err.data.notifications.length; i++) { + notificationsService.showNotification(err.data.notifications[i]); + } + } + }); + } } function showCreateFromSnippet() { diff --git a/src/Umbraco.Web.UI.Client/src/views/partialviews/create.html b/src/Umbraco.Web.UI.Client/src/views/partialviews/create.html index fc9bf98c4f..7aae1f871a 100644 --- a/src/Umbraco.Web.UI.Client/src/views/partialviews/create.html +++ b/src/Umbraco.Web.UI.Client/src/views/partialviews/create.html @@ -47,12 +47,12 @@
+ ng-submit="vm.createFolder(createFolderForm)" + val-form-manager> -
-
{{error.errorMsg}}
-

{{error.data.message}}

+
+
{{vm.createFolderError.errorMsg}}
+

{{vm.createFolderError.data.message}}

diff --git a/src/Umbraco.Web.UI.Client/src/views/scripts/create.controller.js b/src/Umbraco.Web.UI.Client/src/views/scripts/create.controller.js index 1729693d83..bb3aedeb4a 100644 --- a/src/Umbraco.Web.UI.Client/src/views/scripts/create.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/scripts/create.controller.js @@ -1,13 +1,15 @@ (function () { "use strict"; - function ScriptsCreateController($scope, $location, navigationService) { + function ScriptsCreateController($scope, $location, navigationService, formHelper, codefileResource, localizationService, appState) { var vm = this; var node = $scope.dialogOptions.currentNode; + var localizeCreateFolder = localizationService.localize("defaultdialog_createFolder"); vm.creatingFolder = false; vm.folderName = ""; + vm.createFolderError = ""; vm.fileExtension = ""; vm.createFile = createFile; @@ -23,8 +25,40 @@ vm.creatingFolder = true; } - function createFolder() { - + function createFolder(form) { + + if (formHelper.submitForm({scope: $scope, formCtrl: form, statusMessage: localizeCreateFolder})) { + + codefileResource.createContainer("scripts", node.id, vm.folderName).then(function (saved) { + + navigationService.hideMenu(); + + navigationService.syncTree({ + tree: "scripts", + path: saved.path, + forceReload: true, + activate: true + }); + + formHelper.resetForm({ + scope: $scope + }); + + var section = appState.getSectionState("currentSection"); + + }, function(err) { + + vm.createFolderError = err; + + //show any notifications + if (angular.isArray(err.data.notifications)) { + for (var i = 0; i < err.data.notifications.length; i++) { + notificationsService.showNotification(err.data.notifications[i]); + } + } + }); + } + } } diff --git a/src/Umbraco.Web.UI.Client/src/views/scripts/create.html b/src/Umbraco.Web.UI.Client/src/views/scripts/create.html index d57ead3b15..22b238606d 100644 --- a/src/Umbraco.Web.UI.Client/src/views/scripts/create.html +++ b/src/Umbraco.Web.UI.Client/src/views/scripts/create.html @@ -22,12 +22,12 @@
-
-
{{error.errorMsg}}
-

{{error.data.message}}

+
+
{{vm.createFolderError.errorMsg}}
+

{{vm.createFolderError.data.message}}

diff --git a/src/Umbraco.Web/Editors/CodeFileController.cs b/src/Umbraco.Web/Editors/CodeFileController.cs index bcb6630c0a..6775f84a61 100644 --- a/src/Umbraco.Web/Editors/CodeFileController.cs +++ b/src/Umbraco.Web/Editors/CodeFileController.cs @@ -56,6 +56,61 @@ namespace Umbraco.Web.Editors } } + /// + /// Used to create a container/folder in 'partialViews', 'partialViewMacros' or 'scripts' + /// + /// 'partialViews', 'partialViewMacros' or 'scripts' + /// The virtual path of the parent. + /// The name of the container/folder + /// + [HttpPost] + public CodeFileDisplay PostCreateContainer(string type, string parentId, string name) + { + if (string.IsNullOrWhiteSpace(type) || string.IsNullOrWhiteSpace(name)) + { + throw new HttpResponseException(HttpStatusCode.BadRequest); + } + + // if the parentId is root (-1) then we just need an empty string as we are + // creating the path below and we don't wan't -1 in the path + if (parentId == Core.Constants.System.Root.ToInvariantString()) + { + parentId = string.Empty; + } + + name = System.Web.HttpUtility.UrlDecode(name); + + if (parentId.IsNullOrWhiteSpace() == false) + { + parentId = System.Web.HttpUtility.UrlDecode(parentId); + name = parentId.EnsureEndsWith("/") + name; + } + + var virtualPath = string.Empty; + switch (type) + { + case Core.Constants.Trees.PartialViews: + virtualPath = NormalizeVirtualPath(name, SystemDirectories.PartialViews); + Services.FileService.CreatePartialViewFolder(virtualPath); + break; + case Core.Constants.Trees.PartialViewMacros: + virtualPath = NormalizeVirtualPath(name, SystemDirectories.MacroPartials); + Services.FileService.CreatePartialViewMacroFolder(virtualPath); + break; + case Core.Constants.Trees.Scripts: + virtualPath = NormalizeVirtualPath(name, SystemDirectories.Scripts); + Services.FileService.CreateScriptFolder(virtualPath); + break; + + } + + return new CodeFileDisplay + { + VirtualPath = virtualPath, + Path = Url.GetTreePathFromFilePath(virtualPath) + }; + } + /// /// Used to get a specific file from disk via the FileService /// @@ -70,7 +125,6 @@ namespace Umbraco.Web.Editors } virtualPath = System.Web.HttpUtility.UrlDecode(virtualPath); - switch (type) {