Hooking the widget movement arrows back up.

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2011-03-17 13:03:59 -07:00
parent 7251fd9140
commit 8972dcf45b
3 changed files with 29 additions and 38 deletions

View File

@@ -69,27 +69,17 @@ namespace Orchard.Widgets.Controllers {
}
[HttpPost, ActionName("Index")]
public ActionResult IndexWidgetPOST(int? id, string returnUrl) {
const string moveDownString = "submit.MoveDown.";
const string moveUpString = "submit.MoveUp.";
public ActionResult IndexWidgetPOST(int? moveUp, int? moveDown, string returnUrl) {
if (!Services.Authorizer.Authorize(Permissions.ManageWidgets, T(NotAuthorizedManageWidgetsLabel)))
return new HttpUnauthorizedResult();
try {
string moveDownAction = HttpContext.Request.Form.AllKeys.FirstOrDefault(key => key.StartsWith(moveDownString));
if (moveDownAction != null) {
moveDownAction = moveDownAction.Substring(moveDownString.Length, moveDownAction.IndexOf(".", moveDownString.Length) - moveDownString.Length);
_widgetsService.MoveWidgetDown(int.Parse(moveDownAction));
}
else {
string moveUpAction = HttpContext.Request.Form.AllKeys.FirstOrDefault(key => key.StartsWith(moveUpString));
if (moveUpAction != null) {
moveUpAction = moveUpAction.Substring(moveUpString.Length, moveUpAction.IndexOf(".", moveUpString.Length) - moveUpString.Length);
_widgetsService.MoveWidgetUp(int.Parse(moveUpAction));
}
}
} catch (Exception exception) {
if (moveUp.HasValue)
_widgetsService.MoveWidgetUp(moveUp.Value);
if (moveDown.HasValue)
_widgetsService.MoveWidgetDown(moveDown.Value);
}
catch (Exception exception) {
this.Error(exception, T("Moving widget failed: {0}", exception.Message), Logger, Services.Notifier);
}
@@ -140,7 +130,7 @@ namespace Orchard.Widgets.Controllers {
if (widgetPart == null)
return HttpNotFound();
int widgetPosition = _widgetsService.GetWidgets(layerId).Count() + 1;
int widgetPosition = _widgetsService.GetWidgets().Where(widget => widget.Zone == widgetPart.Zone).Count() + 1;
widgetPart.Position = widgetPosition.ToString();
widgetPart.Zone = zone;
widgetPart.LayerPart = _widgetsService.GetLayer(layerId);

View File

@@ -127,10 +127,20 @@ namespace Orchard.Widgets.Services {
_contentManager.Remove(GetWidget(widgetId).ContentItem);
}
private static int ParsePosition(WidgetPart widgetPart) {
int value;
if (!int.TryParse(widgetPart.Record.Position, out value))
return 0;
return value;
}
public bool MoveWidgetUp(int widgetId) {
return MoveWidgetUp(GetWidget(widgetId));
}
public bool MoveWidgetUp(WidgetPart widgetPart) {
int currentPosition = ParsePosition(widgetPart);
WidgetPart widgetBefore = GetWidgets(widgetPart.LayerPart.Id)
WidgetPart widgetBefore = GetWidgets()
.Where(widget => widget.Zone == widgetPart.Zone)
.OrderByDescending(widget => widget.Position, new UI.FlatPositionComparer())
.FirstOrDefault(widget => ParsePosition(widget) < currentPosition);
@@ -144,21 +154,13 @@ namespace Orchard.Widgets.Services {
return false;
}
private static int ParsePosition(WidgetPart widgetPart) {
int value;
if (!int.TryParse(widgetPart.Record.Position, out value))
return 0;
return value;
public bool MoveWidgetDown(int widgetId) {
return MoveWidgetDown(GetWidget(widgetId));
}
public bool MoveWidgetUp(int widgetId) {
return MoveWidgetUp(GetWidget(widgetId));
}
public bool MoveWidgetDown(WidgetPart widgetPart) {
int currentPosition = ParsePosition(widgetPart);
WidgetPart widgetAfter = GetWidgets(widgetPart.LayerPart.Id)
WidgetPart widgetAfter = GetWidgets()
.Where(widget => widget.Zone == widgetPart.Zone)
.OrderBy(widget => widget.Position, new UI.FlatPositionComparer())
.FirstOrDefault(widget => ParsePosition(widget) > currentPosition);
@@ -172,9 +174,6 @@ namespace Orchard.Widgets.Services {
return false;
}
public bool MoveWidgetDown(int widgetId) {
return MoveWidgetDown(GetWidget(widgetId));
}
private static void SwitchWidgetPositions(WidgetPart sourceWidget, WidgetPart targetWidget) {
string tempPosition = sourceWidget.Record.Position;

View File

@@ -33,14 +33,15 @@
<div id="widgets-placement">
<div id="widgets-layers" class="widgets-container detail-view switchable">
<div id="widgets-zones">
@using (Html.BeginFormAntiForgeryPost()) {
<ol>
@foreach (string zone in zones) {
int count = widgets.Where(w => w.Zone == zone).Count();
MvcHtmlString classA = null;
MvcHtmlString classAttr = null;
if (count == 0) {
classA = new MvcHtmlString(" class=\"widgets-none\"");
classAttr = new MvcHtmlString(" class=\"widgets-none\"");
}
<li@(classA)>
<li@(classAttr)>
<h2>@zone</h2>
<div class="widgets-actions">@Html.ActionLink(T("Add").Text, "ChooseWidget", new { layerId = Model.CurrentLayer.Id, zone, returnUrl }, new { @class = "button" })</div>
<ul class="widgets-zone-widgets">
@@ -49,10 +50,10 @@
foreach (WidgetPart widget in widgets.Where(w => w.Zone == zone).OrderBy(w => w.Position, new Orchard.UI.FlatPositionComparer())) {
<li>
@if (i > 0) {
<input class="widgets-mover" type="image" name="submit.MoveUp.@widget.Id" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/moveup.gif")" alt="Move up" value="@widget.Id" />
<input class="widgets-mover" type="image" name="moveUp" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/moveup.gif")" alt="Move up" value="@widget.Id" />
}
@if (++i < count) {
<input class="widgets-mover" type="image" name="submit.MoveDown.@widget.Id" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/movedown.gif")" alt="Move down" value="@widget.Id" />
<input class="widgets-mover" type="image" name="moveDown" src="@Url.Content("~/modules/orchard.widgets/Content/Admin/images/movedown.gif")" alt="Move down" value="@widget.Id" />
}
@Html.ActionLink(HasText(widget.Title) ? widget.Title : widget.TypeDefinition.DisplayName, "EditWidget", new { @widget.Id })
</li>
@@ -62,6 +63,7 @@
</li>
}
</ol>
}
</div>
</div>
</div>