diff --git a/src/Umbraco.Web.UI.Client/src/views/content/content.sort.controller.js b/src/Umbraco.Web.UI.Client/src/views/content/content.sort.controller.js index 94cba6b2e8..0f4d73cbdb 100644 --- a/src/Umbraco.Web.UI.Client/src/views/content/content.sort.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/content/content.sort.controller.js @@ -4,7 +4,7 @@ function ContentSortController($scope, $filter, contentResource, navigationService) { var vm = this; - var parentId = $scope.currentNode.parentId; + var parentId = $scope.currentNode.parentId ? $scope.currentNode.parentId : "-1"; var id = $scope.currentNode.id; vm.loading = false; diff --git a/src/Umbraco.Web.UI.Client/src/views/media/media.sort.controller.js b/src/Umbraco.Web.UI.Client/src/views/media/media.sort.controller.js new file mode 100644 index 0000000000..7b2598c9b4 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/media/media.sort.controller.js @@ -0,0 +1,82 @@ +(function () { + "use strict"; + + function MediaSortController($scope, $filter, mediaResource, navigationService) { + + var vm = this; + var parentId = $scope.currentNode.parentId ? $scope.currentNode.parentId : "-1"; + var id = $scope.currentNode.id; + + vm.loading = false; + vm.children = []; + vm.saveButtonState = "init"; + vm.sortOrder = {}; + vm.sortableOptions = { + distance: 10, + tolerance: "pointer", + opacity: 0.7, + scroll: true, + cursor: "move", + helper: fixSortableHelper, + update: function() { + // clear the sort order when drag and drop is used + vm.sortOrder.column = ""; + vm.sortOrder.reverse = false; + } + }; + + vm.save = save; + vm.sort = sort; + + function onInit() { + vm.loading = true; + mediaResource.getChildren(id) + .then(function(data){ + vm.children = data.items; + vm.loading = false; + }); + } + + function save() { + vm.saveButtonState = "busy"; + + var args = { + parentId: parentId, + sortedIds: _.map(vm.children, function(child){ return child.id; }) + }; + + mediaResource.sort(args) + .then(function(){ + navigationService.syncTree({ tree: "media", path: $scope.currentNode.path, forceReload: true, activate: false }); + vm.saveButtonState = "success"; + }).catch(function(error) { + vm.error = error; + vm.saveButtonState = "error"; + }); + } + + function fixSortableHelper(e, ui) { + // keep the correct width of each table cell when sorting + ui.children().each(function () { + $(this).width($(this).width()); + }); + return ui; + } + + function sort(column) { + // reverse if it is already ordered by that column + if(vm.sortOrder.column === column) { + vm.sortOrder.reverse = !vm.sortOrder.reverse + } else { + vm.sortOrder.column = column; + vm.sortOrder.reverse = false; + } + vm.children = $filter('orderBy')(vm.children, vm.sortOrder.column, vm.sortOrder.reverse); + } + + onInit(); + + } + + angular.module("umbraco").controller("Umbraco.Editors.Media.SortController", MediaSortController); +})(); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/media/sort.html b/src/Umbraco.Web.UI.Client/src/views/media/sort.html new file mode 100644 index 0000000000..14bf92ec42 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/views/media/sort.html @@ -0,0 +1,88 @@ +