From e651d5917a69ec874c9242026203d169eca6c9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Tue, 22 Jan 2019 14:38:10 +0100 Subject: [PATCH] Finds and set the active ContentApp when running init, fixing the issue of retuning to a node after editing the DocType in infinity editing. Fixes #4185 --- .../components/content/edit.controller.js | 63 +++++--- .../src/views/media/edit.html | 2 +- .../src/views/media/media.edit.controller.js | 139 ++++++++++++------ .../src/views/mediatypes/edit.controller.js | 5 +- 4 files changed, 143 insertions(+), 66 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/components/content/edit.controller.js b/src/Umbraco.Web.UI.Client/src/common/directives/components/content/edit.controller.js index 89c8b541d1..ecaf8b3ad3 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/components/content/edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/components/content/edit.controller.js @@ -26,20 +26,43 @@ $scope.allowOpen = true; $scope.app = null; - function init(content) { - if (!$scope.app) { - // set first app to active + function init() { + + var content = $scope.content; + + // we need to check wether an app is present in the current data, if not we will present the default app. + var isAppPresent = false; + + // on first init, we dont have any apps. but if we are re-initializing, we do, but ... + if ($scope.app) { + + // lets check if it still exists as part of our apps array. (if not we have made a change to our docType, even just a re-save of the docType it will turn into new Apps.) + _.forEach(content.apps, function(app) { + if (app === $scope.app) { + isAppPresent = true; + } + }); + + // if we did reload our DocType, but still have the same app we will try to find it by the alias. + if (isAppPresent === false) { + _.forEach(content.apps, function(app) { + if (app.alias === $scope.app.alias) { + isAppPresent = true; + app.active = true; + $scope.appChanged(app); + } + }); + } + + } + + // if we still dont have a app, lets show the first one: + if (isAppPresent === false) { content.apps[0].active = true; - $scope.app = content.apps[0]; + $scope.appChanged(content.apps[0]); } - - if (infiniteMode) { - createInfiniteModeButtons(content); - } else { - createButtons(content); - } - - editorState.set($scope.content); + + editorState.set(content); //We fetch all ancestors of the node to generate the footer breadcrumb navigation if (!$scope.page.isNew) { @@ -129,7 +152,7 @@ "/content/content/edit/" + data.parentId; } - init($scope.content); + init(); syncTreeNode($scope.content, $scope.content.path, true); @@ -337,7 +360,7 @@ showNotifications: args.showNotifications }).then(function (data) { //success - init($scope.content); + init(); syncTreeNode($scope.content, data.path); eventsService.emit("content.saved", { content: $scope.content, action: args.action }); @@ -439,7 +462,7 @@ $scope.content = data; - init($scope.content); + init(); resetLastListPageNumber($scope.content); @@ -478,7 +501,7 @@ .then(function (data) { formHelper.resetForm({ scope: $scope }); contentEditingHelper.reBindChangedProperties($scope.content, data); - init($scope.content); + init(); syncTreeNode($scope.content, data.path); $scope.page.buttonGroupState = "success"; eventsService.emit("content.unpublished", { content: $scope.content }); @@ -915,8 +938,14 @@ * @param {any} app */ $scope.appChanged = function (app) { + $scope.app = app; - createButtons($scope.content); + + if (infiniteMode) { + createInfiniteModeButtons($scope.content); + } else { + createButtons($scope.content); + } }; // methods for infinite editing diff --git a/src/Umbraco.Web.UI.Client/src/views/media/edit.html b/src/Umbraco.Web.UI.Client/src/views/media/edit.html index be1219f052..35f9a6f7ff 100644 --- a/src/Umbraco.Web.UI.Client/src/views/media/edit.html +++ b/src/Umbraco.Web.UI.Client/src/views/media/edit.html @@ -14,7 +14,7 @@ hide-description="true" hide-alias="true" navigation="content.apps" - on-select-navigation-item="appChanged(app)"> + on-select-navigation-item="appChanged(item)"> diff --git a/src/Umbraco.Web.UI.Client/src/views/media/media.edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/media/media.edit.controller.js index 19932887fd..af7184c82f 100644 --- a/src/Umbraco.Web.UI.Client/src/views/media/media.edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/media/media.edit.controller.js @@ -6,7 +6,10 @@ * @description * The controller for the media editor */ -function mediaEditController($scope, $routeParams, $q, appState, mediaResource, entityResource, navigationService, notificationsService, angularHelper, serverValidationManager, contentEditingHelper, fileManager, formHelper, editorState, umbRequestHelper, $http, eventsService) { +function mediaEditController($scope, $routeParams, $q, appState, mediaResource, + entityResource, navigationService, notificationsService, angularHelper, + serverValidationManager, contentEditingHelper, fileManager, formHelper, + editorState, umbRequestHelper, $http, eventsService) { var evts = []; var nodeId = null; @@ -41,7 +44,90 @@ function mediaEditController($scope, $routeParams, $q, appState, mediaResource, $scope.page.listViewPath = null; $scope.page.saveButtonState = "init"; $scope.page.submitButtonLabel = "Save"; + $scope.app = null; + if (create) { + + $scope.page.loading = true; + + mediaResource.getScaffold(nodeId, $routeParams.doctype) + .then(function (data) { + $scope.content = data; + + init(); + + $scope.page.loading = false; + + }); + } + else { + $scope.page.loading = true; + loadMedia() + .then(function(){ + $scope.page.loading = false; + }); + } + + function init() { + + var content = $scope.content; + + // we need to check wether an app is present in the current data, if not we will present the default app. + var isAppPresent = false; + + // on first init, we dont have any apps. but if we are re-initializing, we do, but ... + if ($scope.app) { + + // lets check if it still exists as part of our apps array. (if not we have made a change to our docType, even just a re-save of the docType it will turn into new Apps.) + _.forEach(content.apps, function(app) { + if (app === $scope.app) { + isAppPresent = true; + } + }); + + // if we did reload our DocType, but still have the same app we will try to find it by the alias. + if (isAppPresent === false) { + _.forEach(content.apps, function(app) { + if (app.alias === $scope.app.alias) { + isAppPresent = true; + app.active = true; + $scope.appChanged(app); + } + }); + } + + } + + // if we still dont have a app, lets show the first one: + if (isAppPresent === false) { + content.apps[0].active = true; + $scope.appChanged(content.apps[0]); + } + + + editorState.set($scope.content); + + bindEvents(); + + } + + function bindEvents() { + //bindEvents can be called more than once and we don't want to have multiple bound events + for (var e in evts) { + eventsService.unsubscribe(evts[e]); + } + + evts.push(eventsService.on("editors.mediaType.saved", function(name, args) { + // if this media item uses the updated media type we need to reload the media item + if(args && args.mediaType && args.mediaType.key === $scope.content.contentType.key) { + $scope.page.loading = true; + loadMedia().then(function() { + $scope.page.loading = false; + }); + } + })); + } + /** Syncs the content item to it's tree node - this occurs on first load and after saving */ function syncTreeNode(content, path, initialLoad) { @@ -68,45 +154,6 @@ function mediaEditController($scope, $routeParams, $q, appState, mediaResource, }); } } - - if (create) { - - $scope.page.loading = true; - - mediaResource.getScaffold(nodeId, $routeParams.doctype) - .then(function (data) { - $scope.content = data; - - editorState.set($scope.content); - - init(); - - $scope.page.loading = false; - - }); - } - else { - $scope.page.loading = true; - loadMedia() - .then(function(){ - $scope.page.loading = false; - }); - } - - function init() { - - if (!$scope.app) { - // set first app to active - $scope.content.apps[0].active = true; - $scope.app = $scope.content.apps[0]; - } - - // setup infinite mode - if(infiniteMode) { - $scope.page.submitButtonLabel = "Save and Close"; - } - - } $scope.save = function () { @@ -212,14 +259,12 @@ function mediaEditController($scope, $routeParams, $q, appState, mediaResource, $scope.appChanged = function (app) { $scope.app = app; - } - - evts.push(eventsService.on("editors.mediaType.saved", function(name, args) { - // if this media item uses the updated media type we need to reload the media item - if(args && args.mediaType && args.mediaType.key === $scope.content.contentType.key) { - loadMedia(); + + // setup infinite mode + if(infiniteMode) { + $scope.page.submitButtonLabel = "Save and Close"; } - })); + } //ensure to unregister from all events! $scope.$on('$destroy', function () { diff --git a/src/Umbraco.Web.UI.Client/src/views/mediatypes/edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/mediatypes/edit.controller.js index c8aba53cf5..0740f7866f 100644 --- a/src/Umbraco.Web.UI.Client/src/views/mediatypes/edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/mediatypes/edit.controller.js @@ -9,7 +9,10 @@ (function () { "use strict"; - function MediaTypesEditController($scope, $routeParams, mediaTypeResource, dataTypeResource, editorState, contentEditingHelper, formHelper, navigationService, iconHelper, contentTypeHelper, notificationsService, $filter, $q, localizationService, overlayHelper, eventsService) { + function MediaTypesEditController($scope, $routeParams, mediaTypeResource, + dataTypeResource, editorState, contentEditingHelper, formHelper, + navigationService, iconHelper, contentTypeHelper, notificationsService, + $filter, $q, localizationService, overlayHelper, eventsService) { var vm = this; var evts = [];