From 3402f36de319c02efa42f6afe64c23a7fd40eea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Fri, 22 Nov 2019 09:02:28 +0100 Subject: [PATCH] check if template tree exists, to avoid error if its not present. --- .../components/tree/umbtree.directive.js | 21 +++++++++++++++++++ .../src/common/services/navigation.service.js | 16 ++++++++++++++ .../views/documenttypes/edit.controller.js | 15 ++++++++----- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtree.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtree.directive.js index 2bd93a4b27..3d743c7e9a 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtree.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtree.directive.js @@ -65,6 +65,7 @@ function umbTreeDirective($q, $rootScope, treeService, notificationsService, use vm.reloadNode = reloadNode; vm.syncTree = syncTree; vm.loadChildren = loadChildren; + vm.hasTree = hasTree; //wire up the exposed api object for hosting controllers if ($scope.api) { @@ -72,6 +73,7 @@ function umbTreeDirective($q, $rootScope, treeService, notificationsService, use $scope.api.load = vm.load; $scope.api.reloadNode = vm.reloadNode; $scope.api.syncTree = vm.syncTree; + $scope.api.hasTree = vm.hasTree; } //flag to track the last loaded section when the tree 'un-loads'. We use this to determine if we should @@ -203,6 +205,25 @@ function umbTreeDirective($q, $rootScope, treeService, notificationsService, use }); } + //given a tree alias, this will search the current section tree for the specified tree alias and set the current active tree to it's root node + function hasTree(treeAlias) { + + if (!$scope.tree) { + throw "Err in umbtree.directive.loadActiveTree, $scope.tree is null"; + } + + if (!treeAlias) { + return false; + } + + var treeRoots = getTreeRootNodes(); + var foundTree = _.find(treeRoots, function (node) { + return node.metaData.treeAlias.toUpperCase() === treeAlias.toUpperCase(); + }); + + return foundTree !== undefined; + } + //given a tree alias, this will search the current section tree for the specified tree alias and set the current active tree to it's root node function loadActiveTree(treeAlias) { diff --git a/src/Umbraco.Web.UI.Client/src/common/services/navigation.service.js b/src/Umbraco.Web.UI.Client/src/common/services/navigation.service.js index ce70e9f543..8d1caab850 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/navigation.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/navigation.service.js @@ -338,6 +338,22 @@ function navigationService($routeParams, $location, $q, $injector, eventsService }); }, + /** + * @ngdoc method + * @name umbraco.services.navigationService#hasTree + * @methodOf umbraco.services.navigationService + * + * @description + * Checks if a tree with the given alias exists. + * + * @param {String} treeAlias the tree alias to check + */ + hasTree: function (treeAlias) { + return navReadyPromise.promise.then(function () { + return mainTreeApi.hasTree(treeAlias); + }); + }, + /** Internal method that should ONLY be used by the legacy API wrapper, the legacy API used to have to set an active tree and then sync, the new API does this in one method by using syncTree diff --git a/src/Umbraco.Web.UI.Client/src/views/documenttypes/edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/documenttypes/edit.controller.js index 48e0e6e46b..9d73aa8838 100644 --- a/src/Umbraco.Web.UI.Client/src/views/documenttypes/edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/documenttypes/edit.controller.js @@ -519,11 +519,16 @@ })); evts.push(eventsService.on("editors.documentType.saved", function(name, args) { - if(args.documentType.allowedTemplates.length > 0){ - navigationService.syncTree({ tree: "templates", path: [], forceReload: true }) - .then(function (syncArgs) { - navigationService.reloadNode(syncArgs.node) - }); + if(args.documentType.allowedTemplates.length > 0) { + navigationService.hasTree("templates").then(function (treeExists) { + if (treeExists) { + navigationService.syncTree({ tree: "templates", path: [], forceReload: true }) + .then(function (syncArgs) { + navigationService.reloadNode(syncArgs.node) + } + ); + } + }); } }));