Skip to content

Commit f50cfe8

Browse files
committed
So far so good. Kan relatere til documents og media fra resp. og doctypes og mediatypes fra resp.
1 parent dece99d commit f50cfe8

File tree

3 files changed

+96
-26
lines changed

3 files changed

+96
-26
lines changed

Umbraco.RelationEditor.Web/App_Plugins/RelationEditor/relationeditor.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function () {
2-
function EditRelationsController($scope, dialogService, relationsResources, assetsService, navigationService) {
2+
function EditRelationsController($scope, dialogService, relationsResources, assetsService, navigationService, eventsService) {
33
var dialog;
44

55
function relationPicked(index, data) {
@@ -34,13 +34,18 @@
3434
$scope.data = {};
3535

3636
$scope.pickRelation = function (index) {
37+
var selected = eventsService.on("dialogs.treePickerController.select", function (evt, args) {
38+
relationPicked(index, args.node);
39+
args.node.filtered = true;
40+
dialogService.close(dialog);
41+
});
42+
3743
dialogService.close(dialog);
3844
dialog = dialogService.treePicker({
39-
treeAlias: "content",
40-
section: "content",
41-
customTreeParams: "test=tast",
42-
callback: function (data) {
43-
relationPicked(index, data);
45+
treeAlias: $scope.resourceSets[index].ChildType.TreeType,
46+
section: $scope.resourceSets[index].ChildType.Section,
47+
closeCallback: function () {
48+
selected();
4449
}
4550
});
4651
};
@@ -64,7 +69,7 @@
6469

6570
assetsService.loadCss(Umbraco.Sys.ServerVariables.umbracoSettings.appPluginsPath + "/RelationEditor/relationeditor.css");
6671

67-
var promise = relationsResources.getById($scope.currentNode.id);
72+
var promise = relationsResources.getById($scope.currentNode.section, $scope.currentNode.nodeType, $scope.currentNode.id);
6873
promise.then(function (data) {
6974
$scope.data = data;
7075
$scope.resourceSets = data.Sets;
@@ -74,13 +79,13 @@
7479

7580
function RelationsResources($q, $http, umbDataFormatter, umbRequestHelper) {
7681
return {
77-
getById: function(id) {
82+
getById: function(section, treeType, id) {
7883
return umbRequestHelper.resourcePromise(
7984
$http.get(
8085
Umbraco.Sys.ServerVariables.umbracoSettings.umbracoPath + "/relationseditor/relations/getrelations", {
8186
params: {
82-
from: "Document",
83-
to: "Document",
87+
section: section,
88+
treeType: treeType || "",
8489
parentId: id
8590
}
8691
}),
@@ -104,6 +109,7 @@
104109
"RelationEditor.RelationResources",
105110
"assetsService",
106111
"navigationService",
112+
"eventsService",
107113
EditRelationsController]);
108114

109115
/*

Umbraco.RelationEditor/RelationsController.cs

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Transactions;
77
using System.Web.Http;
88
using System.Web.Http.Controllers;
9+
using umbraco;
910
using Umbraco.Core.Models;
1011
using Umbraco.Core.Services;
1112
using Umbraco.Web;
@@ -20,11 +21,39 @@ public class RelationsController : UmbracoApiController
2021
{
2122
private readonly IRelationService relationService;
2223
private readonly IContentService contentService;
24+
private readonly IMediaService mediaService;
25+
private readonly IContentTypeService contentTypeService;
26+
27+
private readonly Dictionary<UmbracoObjectTypes, UmbracoObjectTypes[]> allowedRelations = new Dictionary<UmbracoObjectTypes, UmbracoObjectTypes[]>
28+
{
29+
{UmbracoObjectTypes.DocumentType, new[] {UmbracoObjectTypes.DocumentType, UmbracoObjectTypes.MediaType}},
30+
{UmbracoObjectTypes.MediaType, new[] {UmbracoObjectTypes.DocumentType, UmbracoObjectTypes.MediaType}},
31+
{UmbracoObjectTypes.Document, new[] {UmbracoObjectTypes.Document, UmbracoObjectTypes.Media}},
32+
{UmbracoObjectTypes.Media, new[] {UmbracoObjectTypes.Document, UmbracoObjectTypes.Media}},
33+
};
34+
35+
private readonly Dictionary<TreeNodeType, UmbracoObjectTypes> treeNodeObjectTypes = new Dictionary<TreeNodeType, UmbracoObjectTypes>
36+
{
37+
{ new TreeNodeType("content", null), UmbracoObjectTypes.Document },
38+
{ new TreeNodeType("media", null), UmbracoObjectTypes.Media },
39+
{ new TreeNodeType("settings", "nodeTypes"), UmbracoObjectTypes.DocumentType },
40+
{ new TreeNodeType("settings", "mediaTypes"), UmbracoObjectTypes.MediaType }
41+
};
42+
43+
private readonly Dictionary<Guid, TreeNodeType> objectTypeTreeTypes = new Dictionary<Guid, TreeNodeType>
44+
{
45+
{ UmbracoObjectTypes.Document.GetGuid(), new TreeNodeType("content", null) },
46+
{ UmbracoObjectTypes.Media.GetGuid(), new TreeNodeType("media", null) },
47+
{ UmbracoObjectTypes.DocumentType.GetGuid(), new TreeNodeType("settings", "nodeTypes") },
48+
{ UmbracoObjectTypes.MediaType.GetGuid(), new TreeNodeType("settings", "mediaTypes") }
49+
};
2350

2451
public RelationsController()
2552
{
2653
relationService = ApplicationContext.Services.RelationService;
2754
contentService = ApplicationContext.Services.ContentService;
55+
mediaService = ApplicationContext.Services.MediaService;
56+
contentTypeService = ApplicationContext.Services.ContentTypeService;
2857
}
2958

3059
public string[] GetObjectTypes()
@@ -33,39 +62,35 @@ public string[] GetObjectTypes()
3362
}
3463

3564
public ContentRelationsDto GetRelations(
36-
string from,
37-
string to,
65+
string section,
66+
string treeType,
3867
int parentId
3968
)
4069
{
41-
UmbracoObjectTypes fromType;
42-
UmbracoObjectTypes toType;
70+
var treeNodeType = new TreeNodeType(section, treeType);
71+
var fromType = UmbracoObjectTypes.Unknown;
4372

4473
if (
45-
!Enum.TryParse(from, out fromType) ||
46-
!Enum.TryParse(to, out toType) ||
47-
fromType == UmbracoObjectTypes.Unknown ||
48-
toType == UmbracoObjectTypes.Unknown
74+
!treeNodeObjectTypes.TryGetValue(treeNodeType, out fromType)
75+
|| fromType == UmbracoObjectTypes.Unknown
4976
)
5077
throw new Exception("Cannot get relation types for unknown object type");
5178

52-
if (fromType != UmbracoObjectTypes.Document || toType != UmbracoObjectTypes.Document)
53-
throw new Exception("Haven't implemented anything but document relations yet");
54-
5579
var allRelations = relationService.GetByParentOrChildId(parentId);
5680
var relationSets = relationService.GetAllRelationTypes()
57-
.Where(rt => rt.ParentObjectType == fromType.GetGuid() && rt.ChildObjectType == toType.GetGuid())
81+
.Where(rt => rt.ParentObjectType == fromType.GetGuid() && allowedRelations[fromType].Any(ar => ar.GetGuid() == rt.ChildObjectType))
5882
.Select(rt => new RelationSetDto
5983
{
6084
RelationTypeId = rt.Id,
85+
ChildType = objectTypeTreeTypes[rt.ChildObjectType],
6186
Alias = rt.Alias,
6287
Name = rt.Name,
6388
Relations = allRelations
6489
.Where(r => r.RelationTypeId == rt.Id)
6590
.Select(r => new RelationDto
6691
{
6792
ChildId = r.ChildId,
68-
ChildName = contentService.GetById(r.ChildId).Name,
93+
ChildName = GetChildName(rt.ChildObjectType, r.ChildId),
6994
State = RelationStateEnum.Unmodified
7095
}).ToList()
7196
}).ToList();
@@ -100,6 +125,22 @@ public void SaveRelations(ContentRelationsDto contentRelations)
100125
}
101126
}
102127
}
128+
129+
private string GetChildName(Guid childObjectType, int childId)
130+
{
131+
switch (UmbracoObjectTypesExtensions.GetUmbracoObjectType(childObjectType))
132+
{
133+
case UmbracoObjectTypes.Document:
134+
return contentService.GetById(childId).Name;
135+
case UmbracoObjectTypes.Media:
136+
return mediaService.GetById(childId).Name;
137+
case UmbracoObjectTypes.DocumentType:
138+
return contentTypeService.GetContentType(childId).Name;
139+
case UmbracoObjectTypes.MediaType:
140+
return contentTypeService.GetMediaType(childId).Name;
141+
}
142+
throw new Exception("Unknown child type");
143+
}
103144
}
104145

105146
public class ContentRelationsDto
@@ -111,6 +152,7 @@ public class ContentRelationsDto
111152
public class RelationSetDto
112153
{
113154
public int RelationTypeId { get; set; }
155+
public TreeNodeType ChildType { get; set; }
114156
public string Alias { get; set; }
115157
public string Name { get; set; }
116158
public IList<RelationDto> Relations { get; set; }

Umbraco.RelationEditor/Startup.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Web;
34
using System.Web.Mvc;
45
using System.Web.Routing;
@@ -10,6 +11,15 @@ namespace Umbraco.RelationEditor
1011
{
1112
public class Startup : ApplicationEventHandler
1213
{
14+
private readonly TreeNodeType[] treeNodeTypes =
15+
{
16+
new TreeNodeType("content", null),
17+
new TreeNodeType("media", null),
18+
new TreeNodeType("settings", "nodeTypes"),
19+
new TreeNodeType("settings", "mediaTypes")
20+
};
21+
//private readonly string[] types = {}
22+
1323
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
1424
{
1525
base.ApplicationStarted(umbracoApplication, applicationContext);
@@ -27,15 +37,27 @@ private void TreeControllerBaseOnMenuRendering(TreeControllerBase sender, MenuRe
2737
{
2838
var context = new HttpContextWrapper(HttpContext.Current);
2939
var urlHelper = new UrlHelper(new RequestContext(context, new RouteData()));
30-
31-
if (sender.TreeAlias == "content")
40+
var treeNodeType = new TreeNodeType(sender.TreeAlias ?? eventArgs.QueryStrings.Get("section"), eventArgs.QueryStrings.Get("treeType"));
41+
if (treeNodeTypes.Contains(treeNodeType) && Convert.ToInt32(eventArgs.NodeId) > 0)
3242
{
3343
var menuItem = eventArgs.Menu.Items.Add<EditRelationsAction>("Edit relations");
3444
menuItem.LaunchDialogView(
35-
urlHelper.Content("~/App_Plugins/RelationEditor/editrelations.html"),
45+
urlHelper.Content("~/App_Plugins/RelationEditor/editrelations.html"),
3646
"Edit relations"
3747
);
3848
}
3949
}
4050
}
51+
52+
public struct TreeNodeType
53+
{
54+
public string Section;
55+
public string TreeType;
56+
57+
public TreeNodeType(string section, string treeType)
58+
{
59+
Section = section;
60+
TreeType = treeType;
61+
}
62+
}
4163
}

0 commit comments

Comments
 (0)