diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/entity.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/entity.resource.js index fdcc4946327f..e3ab51ad5800 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/entity.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/entity.resource.js @@ -514,10 +514,9 @@ function entityResource($q, $http, umbRequestHelper) { * */ getPagedChildren: function (parentId, type, options) { - var defaults = { - pageSize: 1, - pageNumber: 100, + pageSize: 100, + pageNumber: 1, filter: '', orderDirection: "Ascending", orderBy: "SortOrder", diff --git a/src/Umbraco.Web.UI.Client/src/views/common/infiniteeditors/mediapicker/mediapicker.controller.js b/src/Umbraco.Web.UI.Client/src/views/common/infiniteeditors/mediapicker/mediapicker.controller.js index 08ecf390fe49..cf7e7289a1ad 100644 --- a/src/Umbraco.Web.UI.Client/src/views/common/infiniteeditors/mediapicker/mediapicker.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/common/infiniteeditors/mediapicker/mediapicker.controller.js @@ -130,17 +130,13 @@ angular.module("umbraco") } } - function onInit() { - - localizationService.localizeMany(["defaultdialogs_selectMedia", "mediaPicker_tabClipboard"]) .then(function (localizationResult) { setTitle(localizationResult); setNavigation(localizationResult); }); - userService.getCurrentUser().then(function (userData) { userStartNodes = userData.startMediaIds; @@ -148,10 +144,10 @@ angular.module("umbraco") entityResource.getById($scope.startNodeId, "media") .then(function (ent) { $scope.startNodeId = ent.id; - run(); + return run(); }); } else { - run(); + return run(); } }); } @@ -160,10 +156,11 @@ angular.module("umbraco") //default root item if (!$scope.target) { if ($scope.lastOpenedNode && $scope.lastOpenedNode !== -1) { - entityResource.getById($scope.lastOpenedNode, "media") - .then(ensureWithinStartNode, gotoStartNode); + return entityResource.getById($scope.lastOpenedNode, "media") + .then(ensureWithinStartNode) + .catch(function () { return gotoStartNode(); }); } else { - gotoStartNode(); + return gotoStartNode(); } } else { // if a target is specified, go look it up - generally this target will just contain ids not the actual full @@ -176,11 +173,11 @@ angular.module("umbraco") // ID of a UDI or legacy int ID still could be null/undefinied here // As user may dragged in an image that has not been saved to media section yet if (id) { - entityResource.getById(id, "Media") + return entityResource.getById(id, "Media") .then(function (node) { $scope.target = node; - // Moving directly to existing node's folder - gotoFolder({ id: node.parentId }).then(function () { + // Move directly to existing node's folder, then open details + return gotoFolder({ id: node.parentId }).then(function () { selectMedia(node); $scope.target.url = mediaHelper.resolveFileFromEntity(node); $scope.target.thumbnail = mediaHelper.resolveFileFromEntity(node, true); @@ -195,6 +192,7 @@ angular.module("umbraco") // No ID set - then this is going to be a tmpimg that has not been uploaded // User editing this will want to be changing the ALT text openDetailsDialog(); + return; } } } @@ -257,22 +255,27 @@ angular.module("umbraco") folder = { id: -1, name: "Media", icon: "icon-folder" }; } - if (folder.id > 0) { - entityResource.getAncestors(folder.id, "media", null, { dataTypeKey: dataTypeKey }) - .then(function (anc) { - $scope.path = _.filter(anc, - function (f) { - return f.path.indexOf($scope.startNodeId) !== -1; - }); - folder.path = $scope.path[0].path; - performGotoFolder(folder); - }); - } else { - $scope.path = []; - performGotoFolder(folder); - } + var setPathPromise = (folder.id > 0) + ? entityResource.getAncestors(folder.id, "media", null, { dataTypeKey: dataTypeKey }) + .then(function (anc) { + $scope.path = _.filter(anc, function (f) { + return f.path.indexOf($scope.startNodeId) !== -1; + }); + folder.path = $scope.path[0].path; + }) + : Promise.resolve().then(function () { + $scope.path = []; + }); - return getChildren(folder.id); + // Chain: resolve path → set current folder → reset paging → fetch page + return setPathPromise.then(function () { + performGotoFolder(folder); + // Reset pagination to the first page on folder change + vm.searchOptions.pageNumber = 1; + vm.searchOptions.totalItems = 0; + vm.searchOptions.totalPages = 0; + return getChildren(folder.id); + }); } function performGotoFolder(folder) { @@ -388,11 +391,9 @@ angular.module("umbraco") // also make sure the node is not trashed if (nodePath.indexOf($scope.startNodeId.toString()) !== -1 && node.trashed === false) { - gotoFolder({ id: $scope.lastOpenedNode || $scope.startNodeId, name: "Media", icon: "icon-folder", path: node.path }); - return true; + return gotoFolder({ id: $scope.lastOpenedNode || $scope.startNodeId, name: "Media", icon: "icon-folder", path: node.path }); } else { - gotoFolder({ id: $scope.startNodeId, name: "Media", icon: "icon-folder" }); - return false; + return gotoFolder({ id: $scope.startNodeId, name: "Media", icon: "icon-folder" }); } } @@ -408,7 +409,7 @@ angular.module("umbraco") } function gotoStartNode() { - gotoFolder({ id: $scope.startNodeId, name: "Media", icon: "icon-folder" }); + return gotoFolder({ id: $scope.startNodeId, name: "Media", icon: "icon-folder" }); } function openDetailsDialog() { @@ -484,8 +485,15 @@ angular.module("umbraco") function changePagination(pageNumber) { vm.loading = true; vm.searchOptions.pageNumber = pageNumber; - searchMedia(); - }; + + if (vm.searchOptions.filter) { + // search pagination (already present) + searchMedia(); + } else { + // pagination in folder view + getChildren($scope.currentFolder.id); + } + } function searchMedia() { vm.loading = true; @@ -559,22 +567,38 @@ angular.module("umbraco") function getChildren(id) { vm.loading = true; - return entityResource.getChildren(id, "Media", vm.searchOptions).then(function (data) { + return entityResource + .getPagedChildren(id, "Media", vm.searchOptions) + .then(handlePagedChildren) + .finally(function () { vm.loading = false; }); + } - var allowedTypes = dialogOptions.filter ? dialogOptions.filter.split(",") : null; + function handlePagedChildren(data) { + var items = transformItems(data && data.items ? data.items : [], getAllowedTypes()); + $scope.images = items; + syncPagination(vm.searchOptions, data); + vm.searchOptions.filter = ""; // reset filter to ensure folder navigation always starts unfiltered + preSelectMedia(); + } - for (var i = 0; i < data.length; i++) { - setDefaultData(data[i]); - data[i].filtered = allowedTypes && allowedTypes.indexOf(data[i].metaData.ContentTypeAlias) < 0; - } + function getAllowedTypes() { + return dialogOptions.filter ? dialogOptions.filter.split(",") : null; + } - vm.searchOptions.filter = ""; - $scope.images = data ? data : []; + function transformItems(items, allowedTypes) { + for (var i = 0; i < items.length; i++) { + setDefaultData(items[i]); + items[i].filtered = !!(allowedTypes && allowedTypes.indexOf(items[i].metaData.ContentTypeAlias) < 0); + } + return items; + } - // set already selected medias to selected - preSelectMedia(); - vm.loading = false; - }); + function syncPagination(opts, data) { + var d = data || {}; + opts.pageNumber = d.pageNumber; + opts.pageSize = d.pageSize; + opts.totalItems = d.totalItems; + opts.totalPages = d.totalPages; } function setDefaultData(item) {