mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 10:54:50 +08:00
Fixing bug on move widget functionality.
--HG-- branch : dev
This commit is contained in:
@@ -211,6 +211,54 @@ namespace Orchard.Tests.Modules.Widgets.Services {
|
||||
Assert.That(zones.FirstOrDefault(zone => zone == ThemeZoneName2), Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MoveWidgetTest() {
|
||||
const string layerName = "Test layer 1";
|
||||
const string layerDescription = "Test layer 1";
|
||||
const string widgetTitle1 = "Test widget 1";
|
||||
const string widgetTitle2 = "Test widget 2";
|
||||
const string widgetTitle3 = "Test widget 3";
|
||||
const string zone1 = "Zone1";
|
||||
const string zone2 = "Zone2";
|
||||
const string position1 = "1";
|
||||
const string position2 = "3";
|
||||
const string position3 = "4";
|
||||
|
||||
LayerPart layerPart = _widgetService.CreateLayer(layerName, layerDescription, "");
|
||||
|
||||
// same zone widgets
|
||||
WidgetPart widgetPart1 = _widgetService.CreateWidget(layerPart.Id, "HtmlWidget", widgetTitle1, position1, zone1);
|
||||
WidgetPart widgetPart2 = _widgetService.CreateWidget(layerPart.Id, "HtmlWidget", widgetTitle2, position2, zone1);
|
||||
|
||||
// different zone widget
|
||||
WidgetPart widgetPart3 = _widgetService.CreateWidget(layerPart.Id, "HtmlWidget", widgetTitle3, position3, zone2);
|
||||
_contentManager.Flush();
|
||||
|
||||
// test 1 - moving first widget up will have no effect
|
||||
Assert.That(_widgetService.MoveWidgetUp(widgetPart1.Id), Is.False);
|
||||
|
||||
// test 2 - moving first widget down will be successfull
|
||||
Assert.That(_widgetService.MoveWidgetDown(widgetPart1.Id), Is.True);
|
||||
|
||||
widgetPart1 = _widgetService.GetWidget(widgetPart1.Id);
|
||||
Assert.That(widgetPart1.Position, Is.EqualTo(position2), "First widget moved to second widget position");
|
||||
|
||||
widgetPart2 = _widgetService.GetWidget(widgetPart2.Id);
|
||||
Assert.That(widgetPart2.Position, Is.EqualTo(position1), "Second widget moved to first widget position");
|
||||
|
||||
// test 3 - moving last widget down will have no effect even though there is a widget in another zone with a higher position
|
||||
Assert.That(_widgetService.MoveWidgetDown(widgetPart1.Id), Is.False);
|
||||
|
||||
widgetPart1 = _widgetService.GetWidget(widgetPart1.Id);
|
||||
Assert.That(widgetPart1.Position, Is.EqualTo(position2), "Widget remained in the same position");
|
||||
|
||||
widgetPart2 = _widgetService.GetWidget(widgetPart2.Id);
|
||||
Assert.That(widgetPart2.Position, Is.EqualTo(position1), "Widget remained in the same position");
|
||||
|
||||
widgetPart3 = _widgetService.GetWidget(widgetPart3.Id);
|
||||
Assert.That(widgetPart3.Position, Is.EqualTo(position3), "Widget remained in the same position");
|
||||
}
|
||||
|
||||
public class StubLayerPartHandler : ContentHandler {
|
||||
public StubLayerPartHandler(IRepository<LayerPartRecord> layersRepository) {
|
||||
Filters.Add(new ActivatingFilter<LayerPart>("Layer"));
|
||||
|
@@ -22,7 +22,7 @@ namespace Orchard.Widgets.Services {
|
||||
void UpdateLayer(int layerId, string name, string description, string layerRule);
|
||||
void DeleteLayer(int layerId);
|
||||
|
||||
void MoveWidgetUp(int widgetId);
|
||||
void MoveWidgetDown(int widgetId);
|
||||
bool MoveWidgetUp(int widgetId);
|
||||
bool MoveWidgetDown(int widgetId);
|
||||
}
|
||||
}
|
@@ -110,26 +110,43 @@ namespace Orchard.Widgets.Services {
|
||||
_contentManager.Remove(GetWidget(widgetId).ContentItem);
|
||||
}
|
||||
|
||||
public void MoveWidgetUp(int widgetId) {
|
||||
public bool MoveWidgetUp(int widgetId) {
|
||||
WidgetPart widgetPart = GetWidget(widgetId);
|
||||
|
||||
int currentPosition = int.Parse(widgetPart.Record.Position);
|
||||
if (currentPosition > 0) {
|
||||
WidgetPart widgetBefore = GetWidgets(widgetPart.LayerPart.Id).FirstOrDefault(widgetPartBefore => widgetPartBefore.Record.Position == (currentPosition - 1).ToString());
|
||||
WidgetPart widgetBefore = GetWidgets(widgetPart.LayerPart.Id)
|
||||
.Where(widget => widget.Zone == widgetPart.Zone)
|
||||
.OrderByDescending(widget => widget.Position, new UI.FlatPositionComparer())
|
||||
.FirstOrDefault(widget => int.Parse(widget.Record.Position) < currentPosition);
|
||||
if (widgetBefore != null) {
|
||||
string tempPosition = widgetBefore.Record.Position;
|
||||
widgetBefore.Record.Position = currentPosition.ToString();
|
||||
widgetPart.Record.Position = (currentPosition - 1).ToString();
|
||||
widgetPart.Record.Position = tempPosition;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void MoveWidgetDown(int widgetId) {
|
||||
public bool MoveWidgetDown(int widgetId) {
|
||||
WidgetPart widgetPart = GetWidget(widgetId);
|
||||
|
||||
int currentPosition = int.Parse(widgetPart.Record.Position);
|
||||
if (currentPosition < GetWidgets(widgetPart.LayerPart.Id).Count()) {
|
||||
WidgetPart widgetAfter = GetWidgets(widgetPart.LayerPart.Id).FirstOrDefault(widgetPartAfter => widgetPartAfter.Record.Position == (currentPosition + 1).ToString());
|
||||
|
||||
WidgetPart widgetAfter = GetWidgets(widgetPart.LayerPart.Id)
|
||||
.Where(widget => widget.Zone == widgetPart.Zone)
|
||||
.OrderBy(widget => widget.Position, new UI.FlatPositionComparer())
|
||||
.FirstOrDefault(widget => int.Parse(widget.Record.Position) > currentPosition);
|
||||
if (widgetAfter != null) {
|
||||
string tempPosition = widgetAfter.Record.Position;
|
||||
widgetAfter.Record.Position = currentPosition.ToString();
|
||||
widgetPart.Record.Position = (currentPosition + 1).ToString();
|
||||
}
|
||||
widgetPart.Record.Position = tempPosition;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -49,14 +49,21 @@
|
||||
<li>
|
||||
<div class="widgets-zone">@zone</div>
|
||||
<ul>
|
||||
@{
|
||||
int count = Model.CurrentLayerWidgets.Where(widgetPart => widgetPart.Zone == zone).Count() - 1;
|
||||
int i = 0;
|
||||
}
|
||||
@foreach (WidgetPart widget in Model.CurrentLayerWidgets.Where(widgetPart => widgetPart.Zone == zone).OrderBy(widgetPart => widgetPart.Position, new Orchard.UI.FlatPositionComparer())) {
|
||||
<li class="widgets-zoneWidget">
|
||||
@if (widget.Position != "1") {
|
||||
@if (i > 0) {
|
||||
<input type="image" name="submit.MoveUp.@widget.Id" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/moveup.gif")" alt="Move up" value="@widget.Id" />
|
||||
}
|
||||
@if (int.Parse(widget.Position) < Model.CurrentLayerWidgets.Where(widgetPart => widgetPart.Zone == zone).Count()) {
|
||||
@if (i < count) {
|
||||
<input type="image" name="submit.MoveDown.@widget.Id" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/movedown.gif")" alt="Move down" value="@widget.Id" />
|
||||
}
|
||||
@{
|
||||
i++;
|
||||
}
|
||||
@Html.ActionLink(@widget.Title, "EditWidget", new { @widget.Id })
|
||||
</li>
|
||||
}
|
||||
|
Reference in New Issue
Block a user