create and load nested media

This commit is contained in:
2020-05-29 15:46:03 +02:00
parent 91234102fb
commit ca6d2957ea
6 changed files with 113 additions and 20 deletions
+51 -10
View File
@@ -1,4 +1,5 @@
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
using System.Collections.Generic;
using System.Linq;
@@ -37,35 +38,74 @@ namespace zero.Core.Api
/// <inheritdoc />
public async Task<IList<TreeItem>> GetAllAsTree(string parentId = null)
public async Task<IList<TreeItem>> GetAllAsTree(string parentId = null, string activeId = null)
{
List<TreeItem> result = new List<TreeItem>();
List<TreeItem> items = new List<TreeItem>();
string[] openIds = new string[0] { };
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
{
IList<MediaFolder> items = await session.Query<MediaFolder>()
IList<MediaFolder> folders = await session.Query<MediaFolder>()
.Scope(Scope)
.WhereIf(x => x.ParentId == parentId, !parentId.IsNullOrEmpty(), x => x.ParentId == null)
.OrderByDescending(x => x.Name)
.ToListAsync();
foreach (MediaFolder folder in items)
// get hierarchy so we know if we should set the folder to open
if (!activeId.IsNullOrEmpty())
{
result.Add(new TreeItem()
MediaFolder_ByHierarchy.Result result = await session.Query<MediaFolder_ByHierarchy.Result, MediaFolder_ByHierarchy>()
.ProjectInto<MediaFolder_ByHierarchy.Result>()
.Include<MediaFolder_ByHierarchy.Result, MediaFolder>(x => x.Path.Select(p => p.Id))
.Scope(Scope)
.FirstOrDefaultAsync(x => x.Id == activeId);
if (result != null)
{
openIds = result.Path.Select(x => x.Id).ToArray();
}
}
// get children for all folders
string[] folderIds = folders.Select(x => x.Id).ToArray();
IList<MediaFolders_WithChildren.Result> children = await session.Query<MediaFolders_WithChildren.Result, MediaFolders_WithChildren>()
.ProjectInto<MediaFolders_WithChildren.Result>()
.Scope(Scope)
.Where(x => x.Id.In(folderIds))
.ToListAsync();
foreach (MediaFolder folder in folders)
{
int childCount = children.Count(x => x.Id == folder.Id);
items.Add(new TreeItem()
{
Id = folder.Id,
Name = folder.Name,
HasChildren = true,
HasChildren = childCount > 0,
ChildCount = childCount,
ParentId = folder.ParentId,
Sort = folder.Sort,
Icon = "fth-folder"
Icon = "fth-folder",
IsOpen = openIds.Contains(folder.Id),
IsInactive = !folder.IsActive,
HasActions = true,
Modifier = !folder.IsActive ? new TreeItemModifier()
{
Icon = "fth-minus-circle color-red",
Name = "Inactive"
} : null
});
}
}
if (parentId.IsNullOrEmpty())
{
result.Add(new TreeItem()
items.Add(new TreeItem()
{
Id = "recyclebin",
ParentId = null,
@@ -76,7 +116,7 @@ namespace zero.Core.Api
});
}
return result;
return items;
}
@@ -104,6 +144,7 @@ namespace zero.Core.Api
/// <inheritdoc />
public async Task<EntityResult<MediaFolder>> Save(MediaFolder model)
{
model.IsActive = true;
return await SaveModel(model, new MediaFolderValidator());
}
@@ -136,7 +177,7 @@ namespace zero.Core.Api
/// <summary>
/// Get all folders with the specified parent or on root for tree output
/// </summary>
Task<IList<TreeItem>> GetAllAsTree(string parentId = null);
Task<IList<TreeItem>> GetAllAsTree(string parentId = null, string activeId = null);
/// <summary>
/// Creates or updates a folder
@@ -0,0 +1,48 @@
using Raven.Client.Documents.Indexes;
using System;
using System.Linq;
using zero.Core.Entities;
namespace zero.Core.Database.Indexes
{
public class MediaFolders_WithChildren : AbstractIndexCreationTask<MediaFolder, MediaFolders_WithChildren.Result>
{
public class Result : IZeroIdEntity, IAppAwareEntity, IZeroDbConventions
{
public string Id { get; set; }
public string ParentId { get; set; }
public string AppId { get; set; }
public string Name { get; set; }
public string[] ChildrenIds { get; set; }
}
public MediaFolders_WithChildren()
{
Map = items => items.Where(x => x.ParentId != null).Select(item => new Result
{
Id = item.Id,
ParentId = item.ParentId,
Name = item.Name,
AppId = item.AppId,
ChildrenIds = new string[] { }
});
Reduce = results => results.GroupBy(x => new { x.ParentId, x.AppId }).Select(group => new Result()
{
Id = group.Key.ParentId,
ParentId = group.Key.ParentId,
Name = String.Empty,
AppId = group.Key.AppId,
ChildrenIds = group.Select(x => x.Id).ToArray()
});
StoreAllFields(FieldStorage.Yes);
//Index(x => x.ChannelId, FieldIndexing.Exact);
}
}
}
-2
View File
@@ -37,8 +37,6 @@
onLoad(form)
{
console.info(JSON.parse(JSON.stringify(this.model)));
form.load(!this.model.id ? MediaFolderApi.getEmpty() : MediaFolderApi.getById(this.model.id)).then(response =>
{
this.disabled = !response.canEdit;
+5 -4
View File
@@ -138,6 +138,7 @@
watch: {
'$route'()
{
this.cache = {};
this.getItems();
}
},
@@ -177,7 +178,7 @@
return Promise.resolve(this.cache[key]);
}
return MediaFolderApi.getAllAsTree(parent).then(response =>
return MediaFolderApi.getAllAsTree(parent, this.id).then(response =>
{
response.forEach(item =>
{
@@ -211,10 +212,10 @@
{
setTimeout(() =>
{
this.cache = {};
this.$refs.tree.refresh();
this.getItems();
}, 1000);
this.$router.push({ name: 'mediafolder', params: { id: item.model.id } });
//this.getItems();
}, 500);
}, () => { });
},
+7 -2
View File
@@ -17,9 +17,14 @@ export default {
},
// get all folder with a certain parent (can be empty) as tree
getAllAsTree(parent)
getAllAsTree(parent, active)
{
return Axios.get(base + 'getAllAsTree', { params: { parent } }).then(res => Promise.resolve(res.data));
return Axios.get(base + 'getAllAsTree', {
params: {
parent: parent,
active: active
}
}).then(res => Promise.resolve(res.data));
},
// save a media folder
@@ -41,9 +41,9 @@ namespace zero.Web.Controllers
/// <summary>
/// Get all folders with a specific parent as tree
/// </summary>
public async Task<IActionResult> GetAllAsTree([FromQuery] string parent = null)
public async Task<IActionResult> GetAllAsTree([FromQuery] string parent = null, [FromQuery] string active = null)
{
return Json(await Api.GetAllAsTree(parent));
return Json(await Api.GetAllAsTree(parent, active));
}