Skip to content

Commit

Permalink
Content app for dictionary items
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemooij9 authored and nathanwoulfe committed Oct 18, 2021
1 parent e04efe6 commit 1d2272f
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 169 deletions.
32 changes: 32 additions & 0 deletions src/Umbraco.Core/ContentApps/DictionaryContentAppFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Models.Membership;

namespace Umbraco.Cms.Core.ContentApps
{
internal class DictionaryContentAppFactory : IContentAppFactory
{
private const int Weight = -100;

private ContentApp _dictionaryApp;

public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups)
{
switch (source)
{
case IDictionaryItem _:
return _dictionaryApp ??= new ContentApp
{
Alias = "dictionaryContent",
Name = "Content",
Icon = "icon-document",
View = "views/dictionary/views/content/content.html",
Weight = Weight
};
default:
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ internal static void AddAllCoreCollectionBuilders(this IUmbracoBuilder builder)
.Append<ContentTypeDesignContentAppFactory>()
.Append<ContentTypeListViewContentAppFactory>()
.Append<ContentTypePermissionsContentAppFactory>()
.Append<ContentTypeTemplatesContentAppFactory>();
.Append<ContentTypeTemplatesContentAppFactory>()
.Append<DictionaryContentAppFactory>();

// all built-in finders in the correct order,
// devs can then modify this list on application startup
Expand Down
6 changes: 5 additions & 1 deletion src/Umbraco.Core/Manifest/ManifestContentAppFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -70,6 +70,10 @@ public ContentApp GetContentAppFor(object o,IEnumerable<IReadOnlyUserGroup> user
partA = "contentType";
partB = contentType.Alias;
break;
case IDictionaryItem _:
partA = "dictionary";
partB = "*"; //Not really a different type for dictionary items
break;

default:
return null;
Expand Down
9 changes: 8 additions & 1 deletion src/Umbraco.Core/Models/ContentEditing/DictionaryDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

Expand All @@ -17,6 +17,7 @@ public DictionaryDisplay()
{
Notifications = new List<BackOfficeNotification>();
Translations = new List<DictionaryTranslationDisplay>();
ContentApps = new List<ContentApp>();
}

/// <inheritdoc />
Expand All @@ -37,5 +38,11 @@ public DictionaryDisplay()
/// </summary>
[DataMember(Name = "translations")]
public List<DictionaryTranslationDisplay> Translations { get; private set; }

/// <summary>
/// Apps for the dictionary item
/// </summary>
[DataMember(Name = "apps")]
public List<ContentApp> ContentApps { get; private set; }
}
}
4 changes: 2 additions & 2 deletions src/Umbraco.Core/Models/Mapping/CommonMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.ContentApps;
Expand Down Expand Up @@ -47,7 +47,7 @@ public ContentTypeBasic GetContentType(IContentBase source, MapperContext contex
return contentTypeBasic;
}

public IEnumerable<ContentApp> GetContentApps(IUmbracoEntity source)
public IEnumerable<ContentApp> GetContentApps(IEntity source)
{
var apps = _contentAppDefinitions.GetContentAppsFor(source).ToArray();

Expand Down
7 changes: 5 additions & 2 deletions src/Umbraco.Core/Models/Mapping/DictionaryMapDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Mapping;
Expand All @@ -14,10 +14,12 @@ namespace Umbraco.Cms.Core.Models.Mapping
public class DictionaryMapDefinition : IMapDefinition
{
private readonly ILocalizationService _localizationService;
private readonly CommonMapper _commonMapper;

public DictionaryMapDefinition(ILocalizationService localizationService)
public DictionaryMapDefinition(ILocalizationService localizationService, CommonMapper commonMapper)
{
_localizationService = localizationService;
_commonMapper = commonMapper;
}

public void DefineMaps(IUmbracoMapper mapper)
Expand All @@ -44,6 +46,7 @@ private void Map(IDictionaryItem source, DictionaryDisplay target, MapperContext
target.Name = source.ItemKey;
target.ParentId = source.ParentId ?? Guid.Empty;
target.Udi = Udi.Create(Constants.UdiEntityType.DictionaryItem, source.Key);
target.ContentApps.AddRange(_commonMapper.GetContentApps(source));

// build up the path to make it possible to set active item in tree
// TODO: check if there is a better way
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* @ngdoc controller
* @name Umbraco.Editors.Dictionary.EditController
* @function
Expand All @@ -7,128 +7,131 @@
* The controller for editing dictionary items
*/
function DictionaryEditController($scope, $routeParams, $location, dictionaryResource, navigationService, appState, editorState, contentEditingHelper, formHelper, notificationsService, localizationService) {

var vm = this;

//setup scope vars
vm.nameDirty = false;
vm.header = {};
vm.header.editorfor = "template_insertDictionaryItem";
vm.header.setPageTitle = true;

vm.page = {};
vm.page.loading = false;
vm.page.nameLocked = false;
vm.page.menu = {};
vm.page.menu.currentSection = appState.getSectionState("currentSection");
vm.page.menu.currentNode = null;
vm.description = "";
vm.showBackButton = true;
vm.maxlength = 1000;

vm.save = saveDictionary;
vm.back = back;
vm.change = change;

function loadDictionary() {

vm.page.loading = true;

//we are editing so get the content item from the server
dictionaryResource.getById($routeParams.id)
.then(function (data) {
bindDictionary(data);
vm.page.loading = false;
});
}

function createTranslationProperty(translation) {
return {
alias: translation.isoCode,
label: translation.displayName,
hideLabel : false
}
var vm = this;

//setup scope vars
vm.nameDirty = false;
vm.header = {};
vm.header.editorfor = "template_insertDictionaryItem";
vm.header.setPageTitle = true;

vm.page = {};
vm.page.navigation = {};
vm.page.loading = false;
vm.page.nameLocked = false;
vm.page.menu = {};
vm.page.menu.currentSection = appState.getSectionState("currentSection");
vm.page.menu.currentNode = null;
vm.description = "";
vm.showBackButton = true;
vm.maxlength = 1000;

vm.save = saveDictionary;
vm.back = back;
vm.change = change;

function loadDictionary() {

vm.page.loading = true;

//we are editing so get the content item from the server
dictionaryResource.getById($routeParams.id)
.then(function (data) {
bindDictionary(data);
vm.page.navigation = data.apps;
data.apps[0].active = true;
vm.page.loading = false;
});
}

function createTranslationProperty(translation) {
return {
alias: translation.isoCode,
label: translation.displayName,
hideLabel: false
}
}

function bindDictionary(data) {
localizationService.localize("dictionaryItem_description").then(function (value) {
vm.description = value.replace("%0%", data.name);
});
function bindDictionary(data) {
localizationService.localize("dictionaryItem_description").then(function (value) {
vm.description = value.replace("%0%", data.name);
});

// create data for umb-property displaying
for (var i = 0; i < data.translations.length; i++) {
data.translations[i].property = createTranslationProperty(data.translations[i]);
change(data.translations[i]);
}
// create data for umb-property displaying
for (var i = 0; i < data.translations.length; i++) {
data.translations[i].property = createTranslationProperty(data.translations[i]);
change(data.translations[i]);
}

contentEditingHelper.handleSuccessfulSave({
scope: $scope,
savedContent: data
});
contentEditingHelper.handleSuccessfulSave({
scope: $scope,
savedContent: data
});

// set content
vm.content = data;
// set content
vm.content = data;

//share state
editorState.set(vm.content);
//share state
editorState.set(vm.content);

navigationService.syncTree({ tree: "dictionary", path: data.path, forceReload: true }).then(function (syncArgs) {
vm.page.menu.currentNode = syncArgs.node;
});
}
navigationService.syncTree({ tree: "dictionary", path: data.path, forceReload: true }).then(function (syncArgs) {
vm.page.menu.currentNode = syncArgs.node;
});
}

function onInit() {
loadDictionary();
}
function onInit() {
loadDictionary();
}

function saveDictionary() {
if (formHelper.submitForm({ scope: $scope, statusMessage: "Saving..." })) {

function saveDictionary() {
if (formHelper.submitForm({ scope: $scope, statusMessage: "Saving..." })) {
vm.page.saveButtonState = "busy";

vm.page.saveButtonState = "busy";
dictionaryResource.save(vm.content, vm.nameDirty)
.then(function (data) {

dictionaryResource.save(vm.content, vm.nameDirty)
.then(function (data) {
formHelper.resetForm({ scope: $scope });

formHelper.resetForm({ scope: $scope });
bindDictionary(data);

bindDictionary(data);
vm.page.saveButtonState = "success";
},
function (err) {

vm.page.saveButtonState = "success";
},
function (err) {
formHelper.resetForm({ scope: $scope, hasErrors: true });

formHelper.resetForm({ scope: $scope, hasErrors: true });
contentEditingHelper.handleSaveError({
err: err
});

contentEditingHelper.handleSaveError({
err: err
});

notificationsService.error(err.data.message);
notificationsService.error(err.data.message);

vm.page.saveButtonState = "error";
});
}
}

function back() {
$location.path(vm.page.menu.currentSection + "/dictionary/list");
vm.page.saveButtonState = "error";
});
}
}

function change(translation) {
if (translation.translation) {
var charsCount = translation.translation.length;
translation.nearMaxLimit = charsCount > Math.max(vm.maxlength * .8, vm.maxlength - 50);
}
function back() {
$location.path(vm.page.menu.currentSection + "/dictionary/list");
}

function change(translation) {
if (translation.translation) {
var charsCount = translation.translation.length;
translation.nearMaxLimit = charsCount > Math.max(vm.maxlength * .8, vm.maxlength - 50);
}
}

$scope.$watch("vm.content.name", function (newVal, oldVal) {
//when the value changes, we need to set the name dirty
if (newVal && (newVal !== oldVal) && typeof(oldVal) !== "undefined") {
vm.nameDirty = true;
}
});
$scope.$watch("vm.content.name", function (newVal, oldVal) {
//when the value changes, we need to set the name dirty
if (newVal && (newVal !== oldVal) && typeof (oldVal) !== "undefined") {
vm.nameDirty = true;
}
});

onInit();
onInit();
}

angular.module("umbraco").controller("Umbraco.Editors.Dictionary.EditController", DictionaryEditController);
Loading

0 comments on commit 1d2272f

Please sign in to comment.