/src/Framework/N2/Edit/TreeSorter.cs

https://github.com/lundbeck/n2cms
C# | 148 lines | 131 code | 16 blank | 1 comment | 41 complexity | da61ed7023e2ad99267ac382b4c4122f MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using N2.Collections;
  5. using N2.Engine;
  6. using N2.Persistence;
  7. using N2.Web;
  8. namespace N2.Edit
  9. {
  10. [Service(typeof(ITreeSorter))]
  11. public class TreeSorter : ITreeSorter
  12. {
  13. IPersister persister;
  14. IEditManager editManager;
  15. IWebContext webContext;
  16. public TreeSorter(IPersister persister, IEditManager editManager, IWebContext webContext)
  17. {
  18. this.persister = persister;
  19. this.editManager = editManager;
  20. this.webContext = webContext;
  21. }
  22. #region ITreeSorter Members
  23. public void MoveUp(ContentItem item)
  24. {
  25. if (item.Parent != null)
  26. {
  27. ItemFilter filter = editManager.GetEditorFilter(webContext.User);
  28. IList<ContentItem> siblings = item.Parent.Children;
  29. IList<ContentItem> filtered = new ItemList(siblings, filter);
  30. int index = filtered.IndexOf(item);
  31. if (index > 0)
  32. {
  33. MoveTo(item, NodePosition.Before, filtered[index - 1]);
  34. }
  35. }
  36. }
  37. public void MoveDown(ContentItem item)
  38. {
  39. if (item.Parent != null)
  40. {
  41. ItemFilter filter = editManager.GetEditorFilter(webContext.User);
  42. IList<ContentItem> siblings = item.Parent.Children;
  43. IList<ContentItem> filtered = new ItemList(siblings, filter);
  44. int index = filtered.IndexOf(item);
  45. if (index + 1 < filtered.Count)
  46. {
  47. MoveTo(item, NodePosition.After, filtered[index + 1]);
  48. }
  49. }
  50. }
  51. public void MoveTo(ContentItem item, ContentItem parent)
  52. {
  53. if (item.Parent == parent)
  54. {
  55. // move it last
  56. item.AddTo(null);
  57. item.AddTo(parent);
  58. }
  59. else if (item.Parent == null || !parent.Children.Contains(item))
  60. item.AddTo(parent);
  61. using (var tx = persister.Repository.BeginTransaction())
  62. {
  63. foreach (ContentItem updatedItem in Utility.UpdateSortOrder(parent.Children))
  64. {
  65. persister.Repository.SaveOrUpdate(updatedItem);
  66. }
  67. tx.Commit();
  68. }
  69. }
  70. public void MoveTo(ContentItem item, ContentItem parent, int index)
  71. {
  72. if (item.Parent != parent || !parent.Children.Contains(item))
  73. item.AddTo(parent);
  74. else if (parent.Children.Contains(item) && parent.Children.Last() != item)
  75. {
  76. item.AddTo(null);
  77. item.AddTo(parent);
  78. }
  79. IList<ContentItem> siblings = parent.Children;
  80. Utility.MoveToIndex(siblings, item, index);
  81. using (var tx = persister.Repository.BeginTransaction())
  82. {
  83. persister.Repository.SaveOrUpdate(item);
  84. foreach (ContentItem updatedItem in Utility.UpdateSortOrder(siblings))
  85. {
  86. persister.Repository.SaveOrUpdate(updatedItem);
  87. }
  88. tx.Commit();
  89. }
  90. }
  91. public void MoveTo(ContentItem item, int index)
  92. {
  93. IList<ContentItem> siblings = item.Parent.Children;
  94. Utility.MoveToIndex(siblings, item, index);
  95. using (var tx = persister.Repository.BeginTransaction())
  96. {
  97. foreach (ContentItem updatedItem in Utility.UpdateSortOrder(siblings))
  98. {
  99. persister.Repository.SaveOrUpdate(updatedItem);
  100. }
  101. tx.Commit();
  102. }
  103. }
  104. public void MoveTo(ContentItem item, NodePosition position, ContentItem relativeTo)
  105. {
  106. if (relativeTo == null) throw new ArgumentNullException("item");
  107. if (relativeTo == null) throw new ArgumentNullException("relativeTo");
  108. if (relativeTo.Parent == null) throw new ArgumentException("The supplied item '" + relativeTo + "' has no parent to add to.", "relativeTo");
  109. if (item.Parent == null
  110. || item.Parent != relativeTo.Parent
  111. || !item.Parent.Children.Contains(item))
  112. item.AddTo(relativeTo.Parent);
  113. IList<ContentItem> siblings = item.Parent.Children;
  114. int itemIndex = siblings.IndexOf(item);
  115. int relativeToIndex = siblings.IndexOf(relativeTo);
  116. if(itemIndex < 0)
  117. {
  118. if(position == NodePosition.Before)
  119. siblings.Insert(relativeToIndex, item);
  120. else
  121. siblings.Insert(relativeToIndex + 1, item);
  122. }
  123. else if(itemIndex < relativeToIndex && position == NodePosition.Before)
  124. MoveTo(item, relativeToIndex - 1);
  125. else if (itemIndex > relativeToIndex && position == NodePosition.After)
  126. MoveTo(item, relativeToIndex + 1);
  127. else
  128. MoveTo(item, relativeToIndex);
  129. }
  130. #endregion
  131. }
  132. }