Commit 72259438 by Rocky Duan

ajax editing comments / threads

parent 55053cc1
......@@ -76,16 +76,18 @@ def create_thread(request, course_id, commentable_id):
def update_thread(request, course_id, thread_id):
attributes = extract(request.POST, ['body', 'title', 'tags'])
response = comment_client.update_thread(thread_id, attributes)
if response.is_ajax():
if request.is_ajax():
context = {
'thread': response,
'course_id': course_id,
}
html = render_to_string('discussion/ajax_update_thread.html', context)
return JsonResponse({
'html': html,
'content': response,
})
return JsonResponse(response)
else:
return JsonResponse(response)
def _create_comment(request, course_id, _response_from_attributes):
attributes = extract(request.POST, ['body'])
......@@ -128,16 +130,18 @@ def delete_thread(request, course_id, thread_id):
def update_comment(request, course_id, comment_id):
attributes = extract(request.POST, ['body'])
response = comment_client.update_comment(comment_id, attributes)
if response.is_ajax():
if request.is_ajax():
context = {
'comment': response,
'course_id': course_id,
}
html = render_to_string('discussion/ajax_update_comment.html', context)
return JsonResponse({
'html': html,
'content': response,
})
return JsonResponse(response)
else:
return JsonResponse(response)
@instructor_only
@login_required
......
......@@ -48,6 +48,8 @@ def render_discussion(request, course_id, threads, discussion_id=None, \
'forum': (lambda: reverse('django_comment_client.forum.views.forum_form_discussion', args=[course_id, discussion_id])),
}[discussion_type]()
annotated_content_info = {thread['id']: get_annotated_content_info(thread, request.user.id) for thread in threads}
context = {
'threads': threads,
'discussion_id': discussion_id,
......@@ -58,6 +60,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \
'discussion_type': discussion_type,
'base_url': base_url,
'query_params': utils.strip_none(utils.extract(query_params, ['page', 'sort_key', 'sort_order', 'tags', 'text'])),
'annotated_content_info': json.dumps(annotated_content_info),
}
context = dict(context.items() + query_params.items())
return render_to_string(template, context)
......@@ -119,13 +122,17 @@ def forum_form_discussion(request, course_id, discussion_id):
}
return render_to_response('discussion/index.html', context)
def get_annotated_content_info(thread, user_id):
def get_annotated_content_info(content, user_id):
return {
'editable': str(content['user_id']) == str(user_id), # TODO may relax this to instructors
}
def get_annotated_content_infos(thread, user_id):
infos = {}
def _annotate(content):
infos[str(content['id'])] = {
'editable': str(content['user_id']) == str(user_id), # TODO may relax this to instructors
}
for child in content['children']:
infos[str(content['id'])] = get_annotated_content_info(content, user_id)
for child in content.get('children', []):
_annotate(child)
_annotate(thread)
return infos
......@@ -134,7 +141,7 @@ def render_single_thread(request, course_id, thread_id):
thread = comment_client.get_thread(thread_id, recursive=True)
annotated_content_info = get_annotated_content_info(thread=thread, \
annotated_content_info = get_annotated_content_infos(thread=thread, \
user_id=request.user.id)
context = {
......@@ -151,7 +158,7 @@ def single_thread(request, course_id, discussion_id, thread_id):
if request.is_ajax():
thread = comment_client.get_thread(thread_id, recursive=True)
annotated_content_info = get_annotated_content_info(thread=thread, \
annotated_content_info = get_annotated_content_infos(thread=thread, \
user_id=request.user.id)
context = {'thread': thread}
html = render_to_string('discussion/_ajax_single_thread.html', context)
......
......@@ -19,23 +19,22 @@ Discussion = @Discussion
$replyView.show()
else
thread_id = $discussionContent.parents(".thread").attr("_id")
view = {
view =
id: id
showWatchCheckbox: not Discussion.isSubscribed(thread_id, "thread")
}
$discussionContent.append Mustache.render Discussion.replyTemplate, view
Discussion.makeWmdEditor $content, $local, "reply-body"
$local(".discussion-submit-post").click -> handleSubmitReply(this)
$local(".discussion-cancel-post").click -> handleCancelReply(this)
$local(".discussion-reply").hide()
$local(".discussion-edit").hide()
handleCancelReply = (elem) ->
$replyView = $local(".discussion-reply-new")
if $replyView.length
$replyView.hide()
#reply = Discussion.generateDiscussionLink("discussion-reply", "Reply", handleReply)
#$(elem).replaceWith(reply)
$local(".discussion-reply").show()
$local(".discussion-edit").show()
handleSubmitReply = (elem) ->
if $content.hasClass("thread")
......@@ -68,6 +67,7 @@ Discussion = @Discussion
Discussion.bindContentEvents($comment)
$local(".discussion-reply-new").hide()
$local(".discussion-reply").show()
$local(".discussion-edit").show()
$discussionContent.attr("status", "normal")
)
......@@ -97,15 +97,7 @@ Discussion = @Discussion
}
$discussionContent.append Mustache.render Discussion.editThreadTemplate, view
Discussion.makeWmdEditor $content, $local, "thread-body-edit"
$local(".thread-tags-edit").tagsInput
autocomplete_url: Discussion.urlFor('tags_autocomplete')
autocomplete:
remoteDataType: 'json'
interactive: true
defaultText: "Tag your post: press enter after each tag"
height: "30px"
width: "100%"
removeWithBackspace: true
$local(".thread-tags-edit").tagsInput Discussion.tagsInputOptions()
$local(".discussion-submit-update").unbind("click").click -> handleSubmitEditThread(this)
$local(".discussion-cancel-update").unbind("click").click -> handleCancelEdit(this)
......@@ -114,13 +106,16 @@ Discussion = @Discussion
title = $local(".thread-title-edit").val()
body = Discussion.getWmdContent $content, $local, "thread-body-edit"
tags = $local(".thread-tags-edit").val()
$.ajax
Discussion.safeAjax
$elem: $(elem)
url: url
type: "POST"
dataType: 'json'
data: {title: title, body: body, tags: tags},
success: Discussion.formErrorHandler($local(".discussion-update-errors"), (response, textStatus) ->
Discussion.handleAnchorAndReload(response)
$discussionContent.replaceWith(response.html)
Discussion.initializeContent($content)
Discussion.bindContentEvents($content)
)
handleEditComment = (elem) ->
......@@ -138,13 +133,16 @@ Discussion = @Discussion
handleSubmitEditComment= (elem) ->
url = Discussion.urlFor('update_comment', id)
body = Discussion.getWmdContent $content, $local, "comment-body-edit"
$.ajax
Discussion.safeAjax
$elem: $(elem)
url: url
type: "POST"
dataType: "json"
data: {body: body}
success: Discussion.formErrorHandler($local(".discussion-update-errors"), (response, textStatus) ->
Discussion.handleAnchorAndReload(response)
$discussionContent.replaceWith(response.html)
Discussion.initializeContent($content)
Discussion.bindContentEvents($content)
)
handleEndorse = (elem) ->
......
......@@ -7,9 +7,9 @@ initializeVote = (index, content) ->
$content = $(content)
$local = Discussion.generateLocal($content.children(".discussion-content"))
id = $content.attr("_id")
if id in $$user_info.upvoted_ids
if Discussion.isUpvoted id
$local(".discussion-vote-up").addClass("voted")
else if id in $$user_info.downvoted_ids
else if Discussion.isDownvoted id
$local(".discussion-vote-down").addClass("voted")
subscriptionLink = (type, id) ->
......@@ -40,8 +40,7 @@ subscriptionLink = (type, id) ->
$(elem).replaceWith followLink()
dataType: 'json'
if type == 'discussion' and id in $$user_info.subscribed_commentable_ids \
or type == 'thread' and id in $$user_info.subscribed_thread_ids
if Discussion.isSubscribed(id, type)
unfollowLink()
else
followLink()
......@@ -67,10 +66,9 @@ initializeFollowThread = (index, thread) ->
$local = Discussion.generateLocal(discussion)
if $$user_info?
$local(".comment").each(initializeVote)
$local(".thread").each(initializeVote).each(initializeFollowThread)
#initializeFollowDiscussion(discussion) TODO move this somewhere else
$local(".comment").each(initializeVote)
$local(".thread").each(initializeVote).each(initializeFollowThread)
#initializeFollowDiscussion(discussion) TODO move this somewhere else
$local(".new-post-tags").tagsInput Discussion.tagsInputOptions()
......
$ ->
toggle = ->
$('.course-wrapper').toggleClass('closed')
Discussion = window.Discussion
console.log "here"
if $('#accordion').length
active = $('#accordion ul:has(li.active)').index('#accordion ul')
$('#accordion').bind('accordionchange', @log).accordion
active: if active >= 0 then active else 1
header: 'h3'
autoHeight: false
$('#open_close_accordion a').click @toggle
$('#open_close_accordion a').click toggle
$('#accordion').show()
$(".discussion-module").each (index, elem) ->
......
......@@ -69,12 +69,20 @@ wmdEditors = {}
removeWithBackspace: true
isSubscribed: (id, type) ->
if type == "thread"
id in $$user_info.subscribed_thread_ids
else if type == "commentable" or type == "discussion"
id in $$user_info.subscribed_commentable_ids
else
id in $$user_info.subscribed_user_ids
$$user_info? and (
if type == "thread"
id in $$user_info.subscribed_thread_ids
else if type == "commentable" or type == "discussion"
id in $$user_info.subscribed_commentable_ids
else
id in $$user_info.subscribed_user_ids
)
isUpvoted: (id) ->
$$user_info? and (id in $$user_info.upvoted_ids)
isDownvoted: (id) ->
$$user_info? and (id in $$user_info.downvoted_ids)
formErrorHandler: (errorsField, success) ->
(response, textStatus, xhr) ->
......
......@@ -17,7 +17,7 @@
<%include file="_sort.html" />
<div class="threads">
% for thread in threads:
${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)}
${renderer.render_thread(course_id, thread, show_comments=False)}
% endfor
</div>
<%include file="_paginator.html" />
......@@ -31,4 +31,8 @@
<script type="text/javascript">
var $$user_info = JSON.parse('${user_info | escape_quotes}');
var $$course_id = "${course_id}";
if (typeof $$annotated_content_info === undefined || $$annotated_content_info === null) {
var $$annotated_content_info = {};
}
$$annotated_content_info = $.extend($$annotated_content_info, JSON.parse("${annotated_content_info | escape_quotes}"));
</script>
......@@ -10,7 +10,7 @@
</div>
<div class="threads">
% for thread in threads:
${renderer.render_thread(course_id, thread, edit_thread=False, show_comments=False)}
${renderer.render_thread(course_id, thread, show_comments=False)}
% endfor
</div>
<%include file="_paginator.html" />
......@@ -24,4 +24,8 @@
<script type="text/javascript">
var $$user_info = JSON.parse('${user_info | escape_quotes}');
var $$course_id = "${course_id}";
if (typeof $$annotated_content_info === undefined || $$annotated_content_info === null) {
var $$annotated_content_info = {};
}
$$annotated_content_info = $.extend($$annotated_content_info, JSON.parse("${annotated_content_info | escape_quotes}"));
</script>
......@@ -3,9 +3,9 @@
<%! from dateutil.parser import parse %>
<%! import urllib %>
<%def name="render_thread(course_id, thread, edit_thread=False, show_comments=False)">
<%def name="render_thread(course_id, thread, show_comments=False)">
<div class="thread" _id="${thread['id']}">
${render_content(thread, "thread", edit_thread=edit_thread, show_comments=show_comments)}
${render_content(thread, "thread", show_comments=show_comments)}
% if show_comments:
${render_comments(thread.get('children', []))}
% endif
......@@ -77,9 +77,7 @@
<div class="info">
${render_info(content)}
${render_link("discussion-link discussion-reply discussion-reply-" + type, "Reply")}
% if type == "thread" and kwargs['edit_thread'] or type == "comment":
${render_link("discussion-link discussion-edit", "Edit")}
% endif
${render_link("discussion-link discussion-edit", "Edit")}
% if type == "comment" and request.user.is_staff:
% if content['endorsed']:
<input type="checkbox" checked="checked" class="discussion-link discussion-endorse" id="discussion-endorse-${content['id']}">
......
<%namespace name="renderer" file="_thread.html"/>
${renderer.render_thread(course_id, thread, edit_thread=True, show_comments=False)}
${renderer.render_content(thread, "thread")}
<%namespace name="renderer" file="_thread.html"/>
${renderer.render_content(comment, "comment")}
<%namespace name="renderer" file="_thread.html"/>
${renderer.render_content(thread, "thread")}
......@@ -73,10 +73,8 @@
<%def name="render_bottom_bar(content, type, **kwargs)">
<div class="info">
${render_info(content)}
% if type == "thread" and kwargs['edit_thread'] or type == "comment":
${render_link("discussion-link discussion-reply", "Reply")}
${render_link("discussion-link discussion-edit", "Edit")}
% endif
${render_link("discussion-link discussion-reply", "Reply")}
${render_link("discussion-link discussion-edit", "Edit")}
</div>
</%def>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment