From ee3cbf08a254dfb7111caf7d65b2b6ce68cb6663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Kottal?= Date: Tue, 23 Oct 2018 22:43:46 +0200 Subject: [PATCH] #3215 Adds ability to create template from doctype editor (#3366) ### Prerequisites - [x] I have [created an issue](https://github.com/umbraco/Umbraco-CMS/issues) for the proposed changes in this PR, the link is: #3215 - [x] I have added steps to test this contribution in the description below ### Description I added a button for creating a new template in the template view of the doctype editor. The button uses the template resource to fetch a template scaffold, and then save a new template. After saving, the template gets selected as allowed, and if there is no default template selected, the new template will also be the default. https://i.imgur.com/KQbmjGH.gifv --- .../views/templates/templates.controller.js | 45 +++++++++++++++---- .../views/templates/templates.html | 5 +++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.controller.js b/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.controller.js index fe1b0f0e7e70..3ce795b01e87 100644 --- a/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.controller.js @@ -9,7 +9,7 @@ (function() { 'use strict'; - function TemplatesController($scope, entityResource, contentTypeHelper, $routeParams) { + function TemplatesController($scope, entityResource, contentTypeHelper, templateResource, $routeParams) { /* ----------- SCOPE VARIABLES ----------- */ @@ -21,24 +21,53 @@ /* ---------- INIT ---------- */ - init(); + init(function () { - function init() { + // update placeholder template information on new doc types + if (!$routeParams.notemplate && $scope.model.id === 0) { + vm.updateTemplatePlaceholder = true; + vm.availableTemplates = contentTypeHelper.insertTemplatePlaceholder(vm.availableTemplates); + } + }); + + function init(callback) { entityResource.getAll("Template").then(function(templates){ vm.availableTemplates = templates; - // update placeholder template information on new doc types - if (!$routeParams.notemplate && $scope.model.id === 0) { - vm.updateTemplatePlaceholder = true; - vm.availableTemplates = contentTypeHelper.insertTemplatePlaceholder(vm.availableTemplates); - } + callback(); }); } + vm.createTemplate = function () { + templateResource.getScaffold(-1).then(function (template) { + + template.alias = $scope.model.alias; + template.name = $scope.model.name; + + templateResource.save(template).then(function (savedTemplate) { + + init(function () { + + var newTemplate = vm.availableTemplates.filter(function (t) { return t.id === savedTemplate.id }); + if (newTemplate.length > 0) { + $scope.model.allowedTemplates.push(newTemplate[0]); + + if ($scope.model.defaultTemplate === null) { + $scope.model.defaultTemplate = newTemplate[0]; + } + } + + }); + + }); + + }); + } + } angular.module("umbraco").controller("Umbraco.Editors.DocumentType.TemplatesController", TemplatesController); diff --git a/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.html b/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.html index a1385102e44d..c02bb78de7fd 100644 --- a/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.html +++ b/src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.html @@ -16,6 +16,11 @@
alias="model.alias" update-placeholder="vm.updateTemplatePlaceholder"> + +