Skip to content

Commit

Permalink
Sort article, product categories #521
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriaWei committed Nov 15, 2024
1 parent 78e68e1 commit a6490c4
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 27 deletions.
5 changes: 5 additions & 0 deletions Database/Update/4.2/MsSql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE dbo.ArticleType ADD DisplayOrder int NULL
GO

ALTER TABLE dbo.ProductCategory ADD DisplayOrder int NULL
GO
9 changes: 8 additions & 1 deletion src/ZKEACMS.Article/Controllers/ArticleTypeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override IActionResult Edit(ArticleType entity)
[DefaultAuthorize(Policy = PermissionKeys.ViewArticleType)]
public JsonResult GetArticleTypeTree()
{
var allNodes = Service.Get().ToList();
var allNodes = Service.Get().OrderBy(m => m.DisplayOrder ?? m.ID).ToList();
var node = new Tree<ArticleType>().Source(allNodes).ToNode(m => m.ID.ToString(), m => m.Title, m => m.ParentID.ToString(), "0");
return Json(node);
}
Expand All @@ -72,5 +72,12 @@ public IActionResult Select(int? selected)
ViewBag.Selected = selected;
return View();
}

[HttpPost, DefaultAuthorize(Policy = PermissionKeys.ManageArticleType)]
public JsonResult Move(int id, int parentId, int position)
{
Service.Move(id, parentId, position);
return Json(true);
}
}
}
21 changes: 18 additions & 3 deletions src/ZKEACMS.Article/Service/ArticleTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using ZKEACMS.Common.Models;

namespace ZKEACMS.Article.Service
{
Expand All @@ -22,12 +23,12 @@ public ArticleTypeService(IApplicationContext applicationContext, IArticleServic
{
_articleService = articleService;
_localize = localize;
}
}

public override ErrorOr<ArticleType> Add(ArticleType item)
{
item.ParentID = item.ParentID ?? 0;
if (item.Url.IsNotNullAndWhiteSpace()&& GetByUrl(item.Url) != null)
if (item.Url.IsNotNullAndWhiteSpace() && GetByUrl(item.Url) != null)
{
return new Error("Url", _localize.Get("URL already exists"));
}
Expand Down Expand Up @@ -58,10 +59,24 @@ public override void Remove(ArticleType item)
GetChildren(item.ID).Each(m =>
{
Remove(m);
});
});
}
base.Remove(item);
}

public void Move(int id, int parentId, int position)
{
var articleType = Get(id);
articleType.ParentID = parentId;

var siblings = Get().Where(m => m.ParentID == articleType.ParentID && m.ID != articleType.ID).OrderBy(m => m.DisplayOrder ?? m.ID).ToList();
siblings.Insert(position, articleType);

for (int i = 0; i < siblings.Count; i++)
{
siblings[i].DisplayOrder = i + 1;
}
UpdateRange(siblings.ToArray());
}
}
}
13 changes: 11 additions & 2 deletions src/ZKEACMS.Article/Views/ArticleType/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
@{
Script.Reqiured("jsTree").AtFoot();
Style.Reqiured("jsTree").AtHead();
var articleTree = Html.Tree<ArticleType>().Source("GetArticleTypeTree", "ArticleType", new {module = "admin"});
var articleTree = Html.Tree<ArticleType>().Source("GetArticleTypeTree", "ArticleType", new {module = "admin"})
.CheckCallBack("checkCallBack")
.On(Events.MoveNode, "moveNode");
if (Authorizer.Authorize(ZKEACMS.Article.PermissionKeys.ManageArticleType))
{
articleTree
.AddPlugin(Plugins.ContextMenu)
.AddPlugin(Plugins.DragAndDrop)
.AddContextMenuItem(new ContextmenuItem {Label = L("New").Text, Action = "Create", Icon = "glyphicon glyphicon-plus"})
.AddContextMenuItem(new ContextmenuItem {Label = L("Edit").Text, Action = "Edit", Icon = "glyphicon glyphicon-edit"});
}
Expand Down Expand Up @@ -41,6 +44,12 @@
var id = node.reference.attr("id");
window.location.href = '@Url.Action("Edit", "ArticleType", new { module = "admin"})' + '/' + id;
}
function checkCallBack(operation, node, node_parent, node_position, more) {
return true;
}
function moveNode(node, parent) {
$.post("@Url.Action("Move")", { id: parent.node.id, parentId: parent.parent, position: parent.position }, function () {
}, "json");
}
</script>
9 changes: 8 additions & 1 deletion src/ZKEACMS.Product/Controllers/ProductCategoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override IActionResult Delete(int id)
[DefaultAuthorize(Policy = PermissionKeys.ViewProductCategory)]
public JsonResult GetProductCategoryTree()
{
var pages = Service.Get().ToList();
var pages = Service.Get().OrderBy(m => m.DisplayOrder ?? m.ID).ToList();
var node = new Tree<ProductCategory>().Source(pages).ToNode(m => m.ID.ToString(), m => m.Title, m => m.ParentID.ToString(), "0");
return Json(node);
}
Expand All @@ -74,5 +74,12 @@ public IActionResult Select(int? selected)
ViewBag.Selected = selected;
return View();
}

[HttpPost, DefaultAuthorize(Policy = PermissionKeys.ManageProductCategory)]
public JsonResult Move(int id, int parentId, int position)
{
Service.Move(id, parentId, position);
return Json(true);
}
}
}
14 changes: 14 additions & 0 deletions src/ZKEACMS.Product/Service/ProductCategoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,19 @@ public override void Remove(ProductCategory item)
base.Remove(item);
});
}

public void Move(int id, int parentId, int position)
{
var productCategory = Get(id);
productCategory.ParentID = parentId;
var siblings = Get().Where(m => m.ParentID == productCategory.ParentID && m.ID != productCategory.ID).OrderBy(m => m.DisplayOrder ?? m.ID).ToList();
siblings.Insert(position, productCategory);

for (int i = 0; i < siblings.Count; i++)
{
siblings[i].DisplayOrder = i + 1;
}
UpdateRange(siblings.ToArray());
}
}
}
13 changes: 11 additions & 2 deletions src/ZKEACMS.Product/Views/ProductCategory/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
@{
Script.Reqiured("jsTree").AtFoot();
Style.Reqiured("jsTree").AtHead();
var categoryTree = Html.Tree<ProductCategory>().Source("GetProductCategoryTree", "ProductCategory");
var categoryTree = Html.Tree<ProductCategory>().Source("GetProductCategoryTree", "ProductCategory")
.CheckCallBack("checkCallBack")
.On(Events.MoveNode, "moveNode");
if (Authorizer.Authorize(ZKEACMS.Product.PermissionKeys.ManageProductCategory))
{
categoryTree
.AddPlugin(Plugins.ContextMenu)
.AddPlugin(Plugins.DragAndDrop)
.AddContextMenuItem(new ContextmenuItem {Label = L("New").Text, Action = "Create", Icon = "glyphicon glyphicon-plus"})
.AddContextMenuItem(new ContextmenuItem {Label = L("Edit").Text, Action = "Edit", Icon = "glyphicon glyphicon-edit"});
}
Expand Down Expand Up @@ -41,6 +44,12 @@
var id = node.reference.attr("id");
window.location.href = '@Url.Action("Edit")' + '/' + id;
}
function checkCallBack(operation, node, node_parent, node_position, more) {
return true;
}
function moveNode(node, parent) {
$.post("@Url.Action("Move")", { id: parent.node.id, parentId: parent.parent, position: parent.position }, function () {
}, "json");
}
</script>
2 changes: 1 addition & 1 deletion src/ZKEACMS.WebHost/Views/Navigation/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
return true;
}
function moveNode(node, parent) {
$.post("@Url.Action("MoveNav")", { id: parent.node.id, parentId: parent.parent, position: parent.position + 1, oldPosition: parent.old_position + 1 }, function () {
$.post("@Url.Action("MoveNav")", { id: parent.node.id, parentId: parent.parent, position: parent.position }, function () {
}, "json");
}
Expand Down
2 changes: 2 additions & 0 deletions src/ZKEACMS/Article/Models/ArticleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ArticleType : EditorEntity
public string SEOTitle { get; set; }
public string SEOKeyWord { get; set; }
public string SEODescription { get; set; }
public int? DisplayOrder { get; set; }
}
class ArtycleTypeMetaData : ViewMetaData<ArticleType>
{
Expand All @@ -30,6 +31,7 @@ protected override void ViewConfigure()
{
ViewConfig(m => m.ID).AsHidden();
ViewConfig(m => m.ParentID).AsHidden();
ViewConfig(m => m.DisplayOrder).AsHidden();
ViewConfig(m => m.Title).AsTextBox().Order(1).MaxLength(200).Required();
ViewConfig(m => m.Url).AsTextBox().Order(2).MaxLength(100).UrlPart().RandomText().Required();
ViewConfig(m => m.Status).AsDropDownList().DataSource(DicKeys.RecordStatus, SourceType.Dictionary);
Expand Down
1 change: 1 addition & 0 deletions src/ZKEACMS/Article/Service/IArticleTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface IArticleTypeService : IService<ArticleType>
{
IEnumerable<ArticleType> GetChildren(long id);
ArticleType GetByUrl(string url);
void Move(int id, int parentId, int position);
}
}
2 changes: 1 addition & 1 deletion src/ZKEACMS/Common/Service/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace ZKEACMS.Common.Service
{
public interface INavigationService : IService<NavigationEntity>
{
void Move(string id, string parentId, int position, int oldPosition);
void Move(string id, string parentId, int position);
}
}
20 changes: 6 additions & 14 deletions src/ZKEACMS/Common/Service/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,18 @@ public override void Remove(Expression<Func<NavigationEntity, bool>> filter)
base.Remove(filter);
}

public void Move(string id, string parentId, int position, int oldPosition)
public void Move(string id, string parentId, int position)
{
var nav = Get(id);
nav.ParentId = parentId;
nav.DisplayOrder = position;
var siblings = Get().Where(m => m.ParentId == nav.ParentId && m.ID != nav.ID).OrderBy(m => m.DisplayOrder).ToList();
siblings.Insert(position, nav);

IEnumerable<NavigationEntity> navs = CurrentDbSet.AsTracking().Where(m => m.ParentId == nav.ParentId && m.ID != nav.ID).OrderBy(m => m.DisplayOrder);

int order = 1;
for (int i = 0; i < navs.Count(); i++)
for (int i = 0; i < siblings.Count; i++)
{
var eleNav = navs.ElementAt(i);
if (i == position - 1)
{
order++;
}
eleNav.DisplayOrder = order;
order++;
siblings[i].DisplayOrder = i + 1;
}
Update(nav);
UpdateRange(siblings.ToArray());
}

private void Santize(NavigationEntity item)
Expand Down
4 changes: 2 additions & 2 deletions src/ZKEACMS/Controllers/NavigationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public JsonResult GetSelectNavTree()
}

[HttpPost, DefaultAuthorize(Policy = PermissionKeys.ManageNavigation)]
public JsonResult MoveNav(string id, string parentId, int position, int oldPosition)
public JsonResult MoveNav(string id, string parentId, int position)
{
Service.Move(id, parentId, position, oldPosition);
Service.Move(id, parentId, position);
return Json(true);
}
public IActionResult Select(string selected)
Expand Down
1 change: 1 addition & 0 deletions src/ZKEACMS/Product/Models/ProductCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ProductCategory : EditorEntity
public string SEOTitle { get; set; }
public string SEOKeyWord { get; set; }
public string SEODescription { get; set; }
public int? DisplayOrder { get; set; }
}
class ProductCategoryMetaData : ViewMetaData<ProductCategory>
{
Expand Down
1 change: 1 addition & 0 deletions src/ZKEACMS/Product/Service/IProductCategoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ namespace ZKEACMS.Product.Service
public interface IProductCategoryService : IService<ProductCategory>
{
ProductCategory GetByUrl(string url);
void Move(int id, int parentId, int position);
}
}

0 comments on commit a6490c4

Please sign in to comment.