Recovering form location when replying with invalid value

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-11-04 21:11:38 -08:00
parent 133b9d65c3
commit aedadd1d94
4 changed files with 49 additions and 14 deletions

View File

@@ -55,7 +55,7 @@ namespace Orchard.Comments.Controllers {
}
// is it a response to another comment ?
if(commentPart.RepliedOn.HasValue && (commentsPart == null || !commentsPart.ThreadedComments)) {
if(commentPart.RepliedOn.HasValue && commentsPart != null && commentsPart.ThreadedComments) {
var replied = Services.ContentManager.Get(commentPart.RepliedOn.Value);
if(replied != null) {
var repliedPart = replied.As<CommentPart>();
@@ -116,6 +116,10 @@ namespace Orchard.Comments.Controllers {
}
TempData["Comments.InvalidCommentEditorShape"] = editorShape;
var commentPart = comment.As<CommentPart>();
if(commentPart.RepliedOn.HasValue) {
TempData["Comments.RepliedOn"] = commentPart.RepliedOn.Value;
}
}
return this.RedirectLocal(returnUrl, "~/");

View File

@@ -2,7 +2,7 @@
@foreach (var comment in Model.CommentShapes) {
<li>
@{
comment.Id = "comment-" + comment.ContentPart.Id;
comment.Id = "comment-" + comment.ContentItem.Id;
var tag = Tag(comment, "article"); // using Tag so the comment can be altered by custom modules
tag.AddCssClass("comment");
}

View File

@@ -27,6 +27,7 @@ else if (WorkContext.CurrentUser == null && !AuthorizedFor(Permissions.AddCommen
}
else {
@Html.ValidationSummary()
<span id="comment-form-beacon" />
using (Html.BeginFormAntiForgeryPost(Url.Action("Create", "Comment", new { Area = "Orchard.Comments", ReturnUrl = Context.Request.ToUrlString() }), FormMethod.Post, new { @class = "comment-form" })) {
if (TempData.ContainsKey("Comments.InvalidCommentEditorShape")) {
@Display(TempData["Comments.InvalidCommentEditorShape"]);

View File

@@ -1,29 +1,59 @@
@if (Model.CommentCount > 0) {
@using Orchard.Comments.Models
@{
CommentsPart commentsPart = Model.ContentPart;
}
@if (Model.CommentCount > 0) {
<div id="comments">
<h2 class="comment-count">@T.Plural("1 Comment", "{0} Comments", (int)Model.CommentCount)</h2>
@Display.ListOfComments(CommentShapes: Model.CommentShapes)
</div>
}
@using(Script.Foot()) {
@* render reply button if threaded comments enabled *@
@if(commentsPart.ThreadedComments) {
Script.Require("jQuery");
using (Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
$(function () {
$('.comment-reply-button').click(function () {
$(function() {
$('.comment-reply-button').click(function() {
var self = $(this);
var id = self.data("id");
var reply = $('#Comments_RepliedOn');
reply.val(id);
// inject the form in the replied zone
$('.comment-form').appendTo(self.parents('article').first());
// don't execute the link action
var reply = $('#Comments_RepliedOn');
var currentReply = reply.val();
@* should we restore the form at its original location ? *@
if (currentReply && currentReply.length > 0) {
reply.val('');
$('#comment-form-beacon').after($('.comment-form'));
} else {
@* assign repliedOn id *@
var id = self.data('id');
reply.val(id);
@* inject the form in the replied zone *@
$('.comment-form').appendTo(self.parents('article').first());
}
@* don't execute the link action *@
return false;
});
@if (TempData.ContainsKey("Comments.RepliedOn")) {
// invalid form while replying
<text>
var reply = $('#Comments_RepliedOn');
reply.val(@TempData["Comments.RepliedOn"]);
$('.comment-form').appendTo($('#comment-@TempData["Comments.RepliedOn"]'));
</text>
}
});
//]]>
</script>
}
}