pageType handler
This commit is contained in:
@@ -8,7 +8,6 @@ using System.Threading.Tasks;
|
||||
using zero.Core.Attributes;
|
||||
using zero.Core.Database;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Entities.Messages;
|
||||
using zero.Core.Extensions;
|
||||
using zero.Core.Utils;
|
||||
|
||||
@@ -175,15 +174,6 @@ namespace zero.Core.Api
|
||||
|
||||
meta?.Invoke(session.Advanced.GetMetadataFor(model));
|
||||
|
||||
await Backoffice.Messages.Publish(new EntitySavedMessage<T>()
|
||||
{
|
||||
Id = model.Id,
|
||||
IsCreate = isCreate,
|
||||
IsDelete = false,
|
||||
Model = model,
|
||||
Session = session
|
||||
});
|
||||
|
||||
await session.SaveChangesAsync();
|
||||
|
||||
return EntityResult<T>.Success(model);
|
||||
|
||||
+21
-13
@@ -54,8 +54,6 @@ namespace zero.Core.Api
|
||||
model.PageTypeAlias = type.Alias;
|
||||
model.ParentId = parentId; // TODO validate if type is allowed and if parentid is allowed
|
||||
|
||||
Handler.Get<IPageCreationHandler>()?.OnCreate(model);
|
||||
|
||||
return Task.FromResult(model);
|
||||
}
|
||||
catch
|
||||
@@ -92,26 +90,36 @@ namespace zero.Core.Api
|
||||
public async Task<IList<PageType>> GetAllowedPageTypes(string parentId = null)
|
||||
{
|
||||
IEnumerable<PageType> types = Options.Pages.GetAllItems();
|
||||
List<IPage> parents = new();
|
||||
|
||||
if (parentId.IsNullOrEmpty())
|
||||
using IAsyncDocumentSession session = Store.OpenAsyncSession();
|
||||
|
||||
if (!parentId.IsNullOrEmpty())
|
||||
{
|
||||
return types.Where(x => x.AllowAsRoot || x.OnlyAtRoot).ToList();
|
||||
Pages_ByHierarchy.Result result = await session.Query<Pages_ByHierarchy.Result, Pages_ByHierarchy>()
|
||||
.ProjectInto<Pages_ByHierarchy.Result>()
|
||||
.Include<Pages_ByHierarchy.Result, IPage>(x => x.Id)
|
||||
.Include<Pages_ByHierarchy.Result, IPage>(x => x.Path.Select(p => p.Id))
|
||||
.FirstOrDefaultAsync(x => x.Id == parentId);
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
List<string> ids = result.Path.Select(x => x.Id).ToList();
|
||||
ids.Add(result.Id);
|
||||
parents = (await session.LoadAsync<IPage>(ids)).Select(x => x.Value).Reverse().ToList();
|
||||
}
|
||||
}
|
||||
|
||||
Page page = await GetById<Page>(parentId);
|
||||
PageType pageType = page != null ? types.FirstOrDefault(x => x.Alias == page.PageTypeAlias) : null;
|
||||
IPageTypeHandler handler = Handler.Get<IPageTypeHandler>();
|
||||
|
||||
if (pageType == null)
|
||||
// if there is no registered handler we just allow all page types
|
||||
if (handler == null)
|
||||
{
|
||||
return new List<PageType>();
|
||||
return types.ToList();
|
||||
}
|
||||
|
||||
if (pageType.AllowAllChildrenTypes)
|
||||
{
|
||||
return types.Where(x => !x.OnlyAtRoot).ToList();
|
||||
}
|
||||
|
||||
return types.Where(x => !x.OnlyAtRoot && pageType.AllowedChildrenTypes.Contains(x.Alias)).ToList();
|
||||
return handler.GetAllowedPageTypes(Backoffice.Context.Application, types, parents)?.ToList() ?? new();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
using Raven.Client.Documents.Session;
|
||||
using zero.Core.Messages;
|
||||
|
||||
namespace zero.Core.Entities.Messages
|
||||
{
|
||||
public abstract class EntityMessageBase<T> : IMessage
|
||||
{
|
||||
public IAsyncDocumentSession Session { get; set; }
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public T Model { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
namespace zero.Core.Entities.Messages
|
||||
{
|
||||
public class EntityCreatedMessage : EntityMessageBase<IZeroEntity>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class EntityUpdatedMessage : EntityMessageBase<IZeroEntity>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class EntityDeletedMessage : EntityMessageBase<IZeroEntity>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class EntitySavedMessage<T> : EntityMessageBase<T>
|
||||
{
|
||||
public bool IsCreate { get; set; }
|
||||
|
||||
public bool IsDelete { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace zero.Core.Entities
|
||||
{
|
||||
@@ -42,26 +41,6 @@ namespace zero.Core.Entities
|
||||
/// </summary>
|
||||
public string Icon { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether this page type can be used as a website entry point
|
||||
/// </summary>
|
||||
public bool AllowAsRoot { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// This page type is only allowed at the root level (ignoring other properties)
|
||||
/// </summary>
|
||||
public bool OnlyAtRoot { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether all page types can be created as children of this type
|
||||
/// </summary>
|
||||
public bool AllowAllChildrenTypes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Page types which are allowed as children
|
||||
/// </summary>
|
||||
public List<string> AllowedChildrenTypes { get; set; } = new List<string>();
|
||||
|
||||
|
||||
public PageType(Type type)
|
||||
{
|
||||
@@ -75,11 +54,7 @@ namespace zero.Core.Entities
|
||||
Alias = model.Alias,
|
||||
Name = model.Name,
|
||||
Description = model.Description,
|
||||
Icon = model.Icon,
|
||||
AllowAllChildrenTypes = model.AllowAllChildrenTypes,
|
||||
AllowedChildrenTypes = model.AllowedChildrenTypes,
|
||||
AllowAsRoot = model.AllowAsRoot,
|
||||
OnlyAtRoot = model.OnlyAtRoot
|
||||
Icon = model.Icon
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Entities;
|
||||
|
||||
namespace zero.Core.Handlers
|
||||
{
|
||||
public interface IPageCreationHandler : IHandler
|
||||
{
|
||||
void OnCreate(IPage page);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Raven.Client.Documents.Session;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Core.Entities;
|
||||
|
||||
namespace zero.Core.Handlers
|
||||
{
|
||||
public interface IPageTypeHandler : IHandler
|
||||
{
|
||||
IEnumerable<PageType> GetAllowedPageTypes(IApplication application, IEnumerable<PageType> registeredTypes, IEnumerable<IPage> parents);
|
||||
}
|
||||
}
|
||||
@@ -18,34 +18,26 @@ namespace zero.Core.Options
|
||||
}
|
||||
|
||||
|
||||
public void Add<T>(string alias, string name, string description, string icon, bool allowAsRoot = false, bool allowAllChildrenTypes = false, List<string> allowedChildrenTypes = null, bool onlyAtRoot = false) where T : Page, new()
|
||||
public void Add<T>(string alias, string name, string description, string icon) where T : Page, new()
|
||||
{
|
||||
Items.Add(new PageType(typeof(T))
|
||||
{
|
||||
Alias = alias,
|
||||
Name = name,
|
||||
Description = description,
|
||||
Icon = icon,
|
||||
AllowAsRoot = allowAsRoot,
|
||||
AllowAllChildrenTypes = allowAllChildrenTypes,
|
||||
AllowedChildrenTypes = allowedChildrenTypes ?? new List<string>(),
|
||||
OnlyAtRoot = onlyAtRoot
|
||||
Icon = icon
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void Add(Type type, string alias, string name, string description, string icon, bool allowAsRoot = false, bool allowAllChildrenTypes = false, List<string> allowedChildrenTypes = null, bool onlyAtRoot = false)
|
||||
public void Add(Type type, string alias, string name, string description, string icon)
|
||||
{
|
||||
Items.Add(new PageType(type)
|
||||
{
|
||||
Alias = alias,
|
||||
Name = name,
|
||||
Description = description,
|
||||
Icon = icon,
|
||||
AllowAsRoot = allowAsRoot,
|
||||
AllowAllChildrenTypes = allowAllChildrenTypes,
|
||||
AllowedChildrenTypes = allowedChildrenTypes ?? new List<string>(),
|
||||
OnlyAtRoot = onlyAtRoot
|
||||
Icon = icon
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
<template>
|
||||
<div v-if="!loading" class="page-create">
|
||||
<div class="page-create">
|
||||
<h2 class="ui-headline" v-localize="'@page.create.title'"></h2>
|
||||
<div v-if="pageTypes.length && config.parent" class="page-create-parent">
|
||||
<span v-localize="'@page.create.parent'"></span>: <strong>{{config.parent.name}}</strong>
|
||||
</div>
|
||||
<div class="page-create-items">
|
||||
<button type="button" v-for="item in pageTypes" class="page-create-item" @click="onSelect(item)">
|
||||
<i class="page-create-item-icon" :class="item.icon"></i>
|
||||
<span class="page-create-item-text">
|
||||
{{item.name | localize}}
|
||||
<span v-if="item.description" v-localize="item.description"></span>
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
<ui-message type="error" v-if="!pageTypes.length" text="@page.create.nonavailable" />
|
||||
<div class="app-confirm-buttons">
|
||||
<ui-button type="light" :label="config.closeLabel" :disabled="loading" @click="config.close"></ui-button>
|
||||
<div v-if="!loading">
|
||||
<div v-if="pageTypes.length && config.parent" class="page-create-parent">
|
||||
<span v-localize="'@page.create.parent'"></span>: <strong>{{config.parent.name}}</strong>
|
||||
</div>
|
||||
<div class="page-create-items">
|
||||
<button type="button" v-for="item in pageTypes" class="page-create-item" @click="onSelect(item)">
|
||||
<i class="page-create-item-icon" :class="item.icon"></i>
|
||||
<span class="page-create-item-text">
|
||||
{{item.name | localize}}
|
||||
<span v-if="item.description" v-localize="item.description"></span>
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
<ui-message type="error" v-if="!pageTypes.length" text="@page.create.nonavailable" />
|
||||
<div class="app-confirm-buttons">
|
||||
<ui-button type="light" :label="config.closeLabel" @click="config.close"></ui-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -52,6 +54,8 @@
|
||||
|
||||
mounted()
|
||||
{
|
||||
this.loading = true;
|
||||
|
||||
PagesApi.getAllowedPageTypes(this.model.parentId).then(response =>
|
||||
{
|
||||
this.pageTypes = response;
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace zero.Web.Defaults
|
||||
zero.Permissions.AddCollection<SettingsPermissions>();
|
||||
zero.Permissions.AddCollection<SpacePermissions>();
|
||||
|
||||
zero.Pages.Add<PageFolder>(Constants.Pages.FolderAlias, "@page.folder.name", "@page.folder.description", "fth-folder", true, true);
|
||||
zero.Pages.Add<PageFolder>(Constants.Pages.FolderAlias, "@page.folder.name", "@page.folder.description", "fth-folder");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user