Skip to content

Commit

Permalink
umbraco#3215 Adds ability to create template from doctype editor (umb…
Browse files Browse the repository at this point in the history
…raco#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: umbraco#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

<!-- Thanks for contributing to Umbraco CMS! -->
  • Loading branch information
skttl authored and nul800sebastiaan committed Oct 23, 2018
1 parent 3e14da5 commit ee3cbf0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(function() {
'use strict';

function TemplatesController($scope, entityResource, contentTypeHelper, $routeParams) {
function TemplatesController($scope, entityResource, contentTypeHelper, templateResource, $routeParams) {

/* ----------- SCOPE VARIABLES ----------- */

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ <h5><localize key="contentTypeEditor_allowedTemplatesHeading" /></h5>
alias="model.alias"
update-placeholder="vm.updateTemplatePlaceholder">
</umb-grid-selector>

<button type="button" ng-click="vm.createTemplate()" class="btn btn-info" ng-show="model.allowedTemplates.length == 0">
<i class="icon icon-add"></i>
<localize key="shortcuts_addTemplate">Add template</localize>
</button>
</div>

</div>

0 comments on commit ee3cbf0

Please sign in to comment.