diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index 49964b91d5..f8b130e328 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -15,6 +15,7 @@ from django.core.files.storage import get_storage_class from django.utils.translation import ugettext as _ from django.conf import settings +from mitxmako.shortcuts import render_to_response, render_to_string from django_comment_client.utils import JsonResponse, JsonError, extract def thread_author_only(fn): @@ -58,7 +59,15 @@ def create_thread(request, course_id, commentable_id): if request.POST.get('autowatch', 'false').lower() == 'true': attributes['auto_subscribe'] = True response = comment_client.create_thread(commentable_id, attributes) - return JsonResponse(response) + if request.is_ajax(): + context = { + 'course_id': course_id, + 'thread': response, + } + html = render_to_string('discussion/ajax_thread_only.html', context) + return HtmlResponse(html) + else: + return JsonResponse(response) @thread_author_only @login_required @@ -68,9 +77,7 @@ def update_thread(request, course_id, thread_id): response = comment_client.update_thread(thread_id, attributes) return JsonResponse(response) -@login_required -@require_POST -def create_comment(request, course_id, thread_id): +def _create_comment(request, course_id, _response_from_attributes): attributes = extract(request.POST, ['body']) attributes['user_id'] = request.user.id attributes['course_id'] = course_id @@ -78,8 +85,24 @@ def create_comment(request, course_id, thread_id): attributes['anonymous'] = True if request.POST.get('autowatch', 'false').lower() == 'true': attributes['auto_subscribe'] = True - response = comment_client.create_comment(thread_id, attributes) - return JsonResponse(response) + response = _response_from_attributes(attributes) + if request.is_ajax(): + context = { + 'comment': response, + } + html = render_to_string('discussion/ajax_comment_only.html', context) + return JsonResponse({ + 'html': html, + }) + else: + return JsonResponse(response) + +@login_required +@require_POST +def create_comment(request, course_id, thread_id): + def _response_from_attributes(attributes): + return comment_client.create_comment(thread_id, attributes) + return _create_comment(request, course_id, _response_from_attributes) @thread_author_only @login_required @@ -107,15 +130,9 @@ def endorse_comment(request, course_id, comment_id): @login_required @require_POST def create_sub_comment(request, course_id, comment_id): - attributes = extract(request.POST, ['body']) - attributes['user_id'] = request.user.id - attributes['course_id'] = course_id - if request.POST.get('anonymous', 'false').lower() == 'true': - attributes['anonymous'] = True - if request.POST.get('autowatch', 'false').lower() == 'true': - attributes['auto_subscribe'] = True - response = comment_client.create_sub_comment(comment_id, attributes) - return JsonResponse(response) + def _response_from_attributes(attributes): + return comment_client.create_sub_comment(comment_id, attributes) + return _create_comment(request, course_id, _response_from_attributes) @comment_author_only @login_required diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index 883c25ab49..b5794fd899 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -11,9 +11,7 @@ from courseware.courses import check_course from dateutil.tz import tzlocal from datehelper import time_ago_in_words -from django_comment_client.utils import get_categorized_discussion_info, \ - extract, strip_none, \ - JsonResponse +import django_comment_client.utils as utils from urllib import urlencode import json @@ -24,13 +22,9 @@ import dateutil THREADS_PER_PAGE = 20 PAGES_NEARBY_DELTA = 2 -class HtmlResponse(HttpResponse): - def __init__(self, html=''): - super(HtmlResponse, self).__init__(html, content_type='text/plain') - def render_accordion(request, course, discussion_id): - discussion_info = get_categorized_discussion_info(request, course) + discussion_info = utils.get_categorized_discussion_info(request, course) context = { 'course': course, @@ -63,7 +57,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \ 'pages_nearby_delta': PAGES_NEARBY_DELTA, 'discussion_type': discussion_type, 'base_url': base_url, - 'query_params': strip_none(extract(query_params, ['page', 'sort_key', 'sort_order', 'tags', 'text'])), + 'query_params': utils.strip_none(utils.extract(query_params, ['page', 'sort_key', 'sort_order', 'tags', 'text'])), } context = dict(context.items() + query_params.items()) return render_to_string(template, context) @@ -86,9 +80,9 @@ def get_threads(request, course_id, discussion_id): if query_params['text'] or query_params['tags']: #TODO do tags search without sunspot query_params['commentable_id'] = discussion_id - threads, page, num_pages = comment_client.search_threads(course_id, recursive=False, query_params=strip_none(query_params)) + threads, page, num_pages = comment_client.search_threads(course_id, recursive=False, query_params=utils.strip_none(query_params)) else: - threads, page, num_pages = comment_client.get_threads(discussion_id, recursive=False, query_params=strip_none(query_params)) + threads, page, num_pages = comment_client.get_threads(discussion_id, recursive=False, query_params=utils.strip_none(query_params)) query_params['page'] = page query_params['num_pages'] = num_pages @@ -162,7 +156,7 @@ def single_thread(request, course_id, discussion_id, thread_id): context = {'thread': thread} html = render_to_string('discussion/_ajax_single_thread.html', context) - return JsonResponse({ + return utils.JsonResponse({ 'html': html, 'annotated_content_info': annotated_content_info, }) diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index 996d0d0de3..8caca7aaa8 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -116,3 +116,7 @@ class JsonError(HttpResponse): ensure_ascii=False) super(JsonError, self).__init__(content, mimetype='application/json; charset=utf8') + +class HtmlResponse(HttpResponse): + def __init__(self, html=''): + super(HtmlResponse, self).__init__(html, content_type='text/plain') diff --git a/lms/static/coffee/src/customwmd.coffee b/lms/static/coffee/src/customwmd.coffee index 4c8ee38f36..c1ea7b867e 100644 --- a/lms/static/coffee/src/customwmd.coffee +++ b/lms/static/coffee/src/customwmd.coffee @@ -108,7 +108,7 @@ $ -> (text) -> _this.replaceMath(text) if Markdown? - + Markdown.getMathCompatibleConverter = -> converter = Markdown.getSanitizingConverter() processor = new MathJaxProcessor() @@ -174,3 +174,4 @@ $ -> text: text previewSetter: previewSet editor.run() + editor diff --git a/lms/static/coffee/src/discussion/content.coffee b/lms/static/coffee/src/discussion/content.coffee index 28206562f5..c53800782d 100644 --- a/lms/static/coffee/src/discussion/content.coffee +++ b/lms/static/coffee/src/discussion/content.coffee @@ -35,7 +35,7 @@ Discussion = @Discussion showWatchCheckbox: not Discussion.isSubscribed(thread_id, "thread") } $discussionContent.append Mustache.render Discussion.replyTemplate, view - Markdown.makeWmdEditor $local(".reply-body"), "-reply-body-#{id}", Discussion.urlFor('upload') + Discussion.makeWmdEditor $content, $local, "reply-body" $local(".discussion-submit-post").click -> handleSubmitReply(this) $local(".discussion-cancel-post").click -> handleCancelReply(this) $local(".discussion-link").hide() @@ -57,7 +57,7 @@ Discussion = @Discussion else return - body = $local("#wmd-input-reply-body-#{id}").val() + body = Discussion.getWmdContent $content, $local, "reply-body" anonymous = false || $local(".discussion-post-anonymously").is(":checked") autowatch = false || $local(".discussion-auto-watch").is(":checked") @@ -70,8 +70,16 @@ Discussion = @Discussion body: body anonymous: anonymous autowatch: autowatch - success: Discussion.formErrorHandler $local(".discussion-errors"), (response, textStatus) -> - Discussion.handleAnchorAndReload(response) + success: Discussion.formErrorHandler($local(".discussion-errors"), (response, textStatus) -> + console.log response + $comment = $(response.html) + $content.children(".comments").prepend($comment) + Discussion.setWmdContent $content, $local, "reply-body", "" + Discussion.initializeContent($comment) + Discussion.bindContentEvents($comment) + $local(".discussion-reply-new").hide() + $discussionContent.attr("status", "normal") + ) dataType: 'json' handleVote = (elem, value) -> @@ -99,7 +107,7 @@ Discussion = @Discussion tags: $local(".thread-raw-tags").html() } $discussionContent.append Mustache.render Discussion.editThreadTemplate, view - Markdown.makeWmdEditor $local(".thread-body-edit"), "-thread-body-edit-#{id}", Discussion.urlFor('update_thread', id) + Discussion.makeWmdEditor $content, $local, "thread-body-edit" $local(".thread-tags-edit").tagsInput autocomplete_url: Discussion.urlFor('tags_autocomplete') autocomplete: @@ -115,7 +123,7 @@ Discussion = @Discussion handleSubmitEditThread = (elem) -> url = Discussion.urlFor('update_thread', id) title = $local(".thread-title-edit").val() - body = $local("#wmd-input-thread-body-edit-#{id}").val() + body = Discussion.getWmdContent $content, $local, "thread-body-edit" tags = $local(".thread-tags-edit").val() $.ajax url: url @@ -133,13 +141,13 @@ Discussion = @Discussion else view = { id: id, body: $local(".comment-raw-body").html() } $discussionContent.append Mustache.render Discussion.editCommentTemplate, view - Markdown.makeWmdEditor $local(".comment-body-edit"), "-comment-body-edit-#{id}", Discussion.urlFor('update_comment', id) + Discussion.makeWmdEditor $content, $local, "comment-body-edit" $local(".discussion-submit-update").unbind("click").click -> handleSubmitEditComment(this) $local(".discussion-cancel-update").unbind("click").click -> handleCancelEdit(this) handleSubmitEditComment= (elem) -> url = Discussion.urlFor('update_comment', id) - body = $local("#wmd-input-comment-body-edit-#{id}").val() + body = Discussion.getWmdContent $content, $local, "comment-body-edit" $.ajax url: url data: {body: body} @@ -169,6 +177,9 @@ Discussion = @Discussion $threadTitle = $local(".thread-title") $showComments = $local(".discussion-show-comments") + if not $showComments.length or not $threadTitle.length + return + rebindHideEvents = -> $threadTitle.unbind('click').click handleHideSingleThread $showComments.unbind('click').click handleHideSingleThread diff --git a/lms/static/coffee/src/discussion/discussion.coffee b/lms/static/coffee/src/discussion/discussion.coffee index 9654c1ce20..0f96609fe2 100644 --- a/lms/static/coffee/src/discussion/discussion.coffee +++ b/lms/static/coffee/src/discussion/discussion.coffee @@ -91,7 +91,7 @@ initializeFollowThread = (index, thread) -> handleSubmitNewPost = (elem) -> title = $local(".new-post-title").val() - body = $local("#wmd-input-new-post-body-#{id}").val() + body = Discussion.getWmdContent $discussion, $local, "new-post-body" tags = $local(".new-post-tags").val() url = Discussion.urlFor('create_thread', $local(".new-post-form").attr("_id")) $.post url, {title: title, body: body, tags: tags}, (response, textStatus) -> @@ -115,9 +115,9 @@ initializeFollowThread = (index, thread) -> else view = { discussion_id: id } $discussionNonContent.append Mustache.render Discussion.newPostTemplate, view - newPostBody = $(discussion).find(".new-post-body") + newPostBody = $discussion.find(".new-post-body") if newPostBody.length - Markdown.makeWmdEditor newPostBody, "-new-post-body-#{$(discussion).attr('_id')}", Discussion.urlFor('upload') + Discussion.makeWmdEditor $discussion, $local, "new-post-body" $local(".new-post-tags").tagsInput Discussion.tagsInputOptions() diff --git a/lms/static/coffee/src/discussion/templates.coffee b/lms/static/coffee/src/discussion/templates.coffee index 17374a95c7..1cfc30a7cd 100644 --- a/lms/static/coffee/src/discussion/templates.coffee +++ b/lms/static/coffee/src/discussion/templates.coffee @@ -4,82 +4,8 @@ if not @Discussion? Discussion = @Discussion -### -titleTemplate = """ - {{title}} -""" - -threadTemplate: """ -
- {{content}} -
-
-
-""" - -commentTemplate: """ -
- {{content}} -
-
-
-""" - -contentTemplate: """ -
-
- {{vote}} -
- {{title}} -
-
{{body}}
- - {{tags}} - {{bottom_bar}} -
-
-
-
-""" - -tagsTemplate = """ -
- -
-