Commit 51961c1c by Rocky Duan

Merge branch 'master' into discussion2

parents 4749f3a5 cc477138
...@@ -10,8 +10,8 @@ urlpatterns = patterns('django_comment_client.base.views', ...@@ -10,8 +10,8 @@ urlpatterns = patterns('django_comment_client.base.views',
url(r'threads/(?P<thread_id>[\w\-]+)/delete', 'delete_thread', name='delete_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/delete', 'delete_thread', name='delete_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/upvote$', 'vote_for_thread', {'value': 'up'}, name='upvote_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/upvote$', 'vote_for_thread', {'value': 'up'}, name='upvote_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/downvote$', 'vote_for_thread', {'value': 'down'}, name='downvote_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/downvote$', 'vote_for_thread', {'value': 'down'}, name='downvote_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/watch$', 'watch_thread', name='watch_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/follow$', 'follow_thread', name='follow_thread'),
url(r'threads/(?P<thread_id>[\w\-]+)/unwatch$', 'unwatch_thread', name='unwatch_thread'), url(r'threads/(?P<thread_id>[\w\-]+)/unfollow$', 'unfollow_thread', name='unfollow_thread'),
url(r'comments/(?P<comment_id>[\w\-]+)/update$', 'update_comment', name='update_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/update$', 'update_comment', name='update_comment'),
url(r'comments/(?P<comment_id>[\w\-]+)/endorse$', 'endorse_comment', name='endorse_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/endorse$', 'endorse_comment', name='endorse_comment'),
...@@ -21,8 +21,8 @@ urlpatterns = patterns('django_comment_client.base.views', ...@@ -21,8 +21,8 @@ urlpatterns = patterns('django_comment_client.base.views',
url(r'comments/(?P<comment_id>[\w\-]+)/downvote$', 'vote_for_comment', {'value': 'down'}, name='downvote_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/downvote$', 'vote_for_comment', {'value': 'down'}, name='downvote_comment'),
url(r'(?P<commentable_id>[\w\-]+)/threads/create$', 'create_thread', name='create_thread'), url(r'(?P<commentable_id>[\w\-]+)/threads/create$', 'create_thread', name='create_thread'),
url(r'(?P<commentable_id>[\w\-]+)/watch$', 'watch_commentable', name='watch_commentable'), url(r'(?P<commentable_id>[\w\-]+)/follow$', 'follow_commentable', name='follow_commentable'),
url(r'(?P<commentable_id>[\w\-]+)/unwatch$', 'unwatch_commentable', name='unwatch_commentable'), url(r'(?P<commentable_id>[\w\-]+)/unfollow$', 'unfollow_commentable', name='unfollow_commentable'),
url(r'search$', 'search', name='search'), url(r'search$', 'search', name='search'),
) )
...@@ -140,42 +140,42 @@ def vote_for_thread(request, course_id, thread_id, value): ...@@ -140,42 +140,42 @@ def vote_for_thread(request, course_id, thread_id, value):
@login_required @login_required
@require_POST @require_POST
def watch_thread(request, course_id, thread_id): def follow_thread(request, course_id, thread_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.subscribe_thread(user_id, thread_id) response = comment_client.subscribe_thread(user_id, thread_id)
return JsonResponse(response) return JsonResponse(response)
@login_required @login_required
@require_POST @require_POST
def watch_commentable(request, course_id, commentable_id): def follow_commentable(request, course_id, commentable_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.subscribe_commentable(user_id, commentable_id) response = comment_client.subscribe_commentable(user_id, commentable_id)
return JsonResponse(response) return JsonResponse(response)
@login_required @login_required
@require_POST @require_POST
def follow(request, course_id, followed_user_id): def follow_user(request, course_id, followed_user_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.follow(user_id, followed_user_id) response = comment_client.follow(user_id, followed_user_id)
return JsonResponse(response) return JsonResponse(response)
@login_required @login_required
@require_POST @require_POST
def unwatch_thread(request, course_id, thread_id): def unfollow_thread(request, course_id, thread_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.unsubscribe_thread(user_id, thread_id) response = comment_client.unsubscribe_thread(user_id, thread_id)
return JsonResponse(response) return JsonResponse(response)
@login_required @login_required
@require_POST @require_POST
def unwatch_commentable(request, course_id, commentable_id): def unfollow_commentable(request, course_id, commentable_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.unsubscribe_commentable(user_id, commentable_id) response = comment_client.unsubscribe_commentable(user_id, commentable_id)
return JsonResponse(response) return JsonResponse(response)
@login_required @login_required
@require_POST @require_POST
def unfollow(request, course_id, followed_user_id): def unfollow_user(request, course_id, followed_user_id):
user_id = request.user.id user_id = request.user.id
response = comment_client.unfollow(user_id, followed_user_id) response = comment_client.unfollow(user_id, followed_user_id)
return JsonResponse(response) return JsonResponse(response)
......
...@@ -28,9 +28,10 @@ Discussion = @Discussion ...@@ -28,9 +28,10 @@ Discussion = @Discussion
if $replyView.length if $replyView.length
$replyView.show() $replyView.show()
else else
thread_id = $discussionContent.parents(".thread").attr("_id")
view = { view = {
id: id id: id
showWatchCheckbox: $discussionContent.parents(".thread").attr("_id") not in $$user_info.subscribed_thread_ids showWatchCheckbox: not Discussion.isSubscribed(thread_id, "thread")
} }
$discussionContent.append Mustache.render Discussion.replyTemplate, view $discussionContent.append Mustache.render Discussion.replyTemplate, view
Markdown.makeWmdEditor $local(".reply-body"), "-reply-body-#{id}", Discussion.urlFor('upload') Markdown.makeWmdEditor $local(".reply-body"), "-reply-body-#{id}", Discussion.urlFor('upload')
...@@ -61,19 +62,15 @@ Discussion = @Discussion ...@@ -61,19 +62,15 @@ Discussion = @Discussion
autowatch = false || $local(".discussion-auto-watch").is(":checked") autowatch = false || $local(".discussion-auto-watch").is(":checked")
Discussion.safeAjax Discussion.safeAjax
$elem: $(elem)
url: url url: url
type: "POST" type: "POST"
data: data:
body: body body: body
anonymous: anonymous anonymous: anonymous
autowatch: autowatch autowatch: autowatch
success: (response, textStatus) -> success: Discussion.formErrorHandler $local(".discussion-errors"), (response, textStatus) ->
if response.errors? and response.errors.length > 0 Discussion.handleAnchorAndReload(response)
errorsField = $local(".discussion-errors").empty()
for error in response.errors
errorsField.append($("<li>").addClass("new-post-form-error").html(error))
else
Discussion.handleAnchorAndReload(response)
dataType: 'json' dataType: 'json'
handleVote = (elem, value) -> handleVote = (elem, value) ->
...@@ -119,14 +116,13 @@ Discussion = @Discussion ...@@ -119,14 +116,13 @@ Discussion = @Discussion
title = $local(".thread-title-edit").val() title = $local(".thread-title-edit").val()
body = $local("#wmd-input-thread-body-edit-#{id}").val() body = $local("#wmd-input-thread-body-edit-#{id}").val()
tags = $local(".thread-tags-edit").val() tags = $local(".thread-tags-edit").val()
$.post url, {title: title, body: body, tags: tags}, (response, textStatus) -> $.ajax
if response.errors url: url
errorsField = $local(".discussion-update-errors").empty() type: "POST"
for error in response.errors data: {title: title, body: body, tags: tags},
errorsField.append($("<li>").addClass("new-post-form-error").html(error)) success: Discussion.formErrorHandler $local(".discussion-update-errors"), (response, textStatus) ->
else
Discussion.handleAnchorAndReload(response) Discussion.handleAnchorAndReload(response)
, 'json' dataType: 'json'
handleEditComment = (elem) -> handleEditComment = (elem) ->
$local(".discussion-content-wrapper").hide() $local(".discussion-content-wrapper").hide()
...@@ -134,10 +130,7 @@ Discussion = @Discussion ...@@ -134,10 +130,7 @@ Discussion = @Discussion
if $editView.length if $editView.length
$editView.show() $editView.show()
else else
view = { view = { id: id, body: $local(".comment-raw-body").html() }
id: id
body: $local(".comment-raw-body").html()
}
$discussionContent.append Mustache.render Discussion.editCommentTemplate, view $discussionContent.append Mustache.render Discussion.editCommentTemplate, view
Markdown.makeWmdEditor $local(".comment-body-edit"), "-comment-body-edit-#{id}", Discussion.urlFor('update_comment', id) Markdown.makeWmdEditor $local(".comment-body-edit"), "-comment-body-edit-#{id}", Discussion.urlFor('update_comment', id)
$local(".discussion-submit-update").unbind("click").click -> handleSubmitEditComment(this) $local(".discussion-submit-update").unbind("click").click -> handleSubmitEditComment(this)
...@@ -146,14 +139,13 @@ Discussion = @Discussion ...@@ -146,14 +139,13 @@ Discussion = @Discussion
handleSubmitEditComment= (elem) -> handleSubmitEditComment= (elem) ->
url = Discussion.urlFor('update_comment', id) url = Discussion.urlFor('update_comment', id)
body = $local("#wmd-input-comment-body-edit-#{id}").val() body = $local("#wmd-input-comment-body-edit-#{id}").val()
$.post url, {body: body}, (response, textStatus) -> $.ajax
if response.errors url: url
errorsField = $local(".discussion-update-errors").empty() data: {body: body}
for error in response.errors type: "POST"
errorsField.append($("<li>").addClass("new-post-form-error").html(error)) success: Discussion.formErrorHandler $local(".discussion-update-errors"), (response, textStatus) ->
else
Discussion.handleAnchorAndReload(response) Discussion.handleAnchorAndReload(response)
, 'json' dataType: "json"
handleEndorse = (elem) -> handleEndorse = (elem) ->
url = Discussion.urlFor('endorse_comment', id) url = Discussion.urlFor('endorse_comment', id)
...@@ -203,35 +195,38 @@ Discussion = @Discussion ...@@ -203,35 +195,38 @@ Discussion = @Discussion
rebindHideEvents() rebindHideEvents()
dataType: 'json' dataType: 'json'
Discussion.bindLocalEvents $local,
$local(".thread-title").click handleShowSingleThread
$local(".discussion-show-comments").click handleShowSingleThread "click .thread-title": ->
handleShowSingleThread(this)
$local(".discussion-reply-thread").click -> "click .discussion-show-comments": ->
handleShowSingleThread($local(".thread-title")) handleShowSingleThread(this)
handleReply(this)
$local(".discussion-reply-comment").click -> "click .discussion-reply-thread": ->
handleReply(this) handleShowSingleThread($local(".thread-title"))
handleReply(this)
$local(".discussion-cancel-reply").click -> "click .discussion-reply-comment": ->
handleCancelReply(this) handleReply(this)
$local(".discussion-vote-up").click -> "click .discussion-cancel-reply": ->
handleVote(this, "up") handleCancelReply(this)
$local(".discussion-vote-down").click -> "click .discussion-vote-up": ->
handleVote(this, "down") handleVote(this, "up")
$local(".discussion-endorse").click -> "click .discussion-vote-down": ->
handleEndorse(this) handleVote(this, "down")
$local(".discussion-edit").click -> "click .discussion-endorse": ->
if $content.hasClass("thread") handleEndorse(this)
handleEditThread(this)
else "click .discussion-edit": ->
handleEditComment(this) if $content.hasClass("thread")
handleEditThread(this)
else
handleEditComment(this)
initializeContent: (content) -> initializeContent: (content) ->
$content = $(content) $content = $(content)
......
$ -> $ ->
Discussion = window.Discussion Discussion = window.Discussion
console.log "here"
if $('#accordion').length if $('#accordion').length
active = $('#accordion ul:has(li.active)').index('#accordion ul') active = $('#accordion ul:has(li.active)').index('#accordion ul')
$('#accordion').bind('accordionchange', @log).accordion $('#accordion').bind('accordionchange', @log).accordion
......
...@@ -9,23 +9,23 @@ Discussion = @Discussion ...@@ -9,23 +9,23 @@ Discussion = @Discussion
(selector) -> $(elem).find(selector) (selector) -> $(elem).find(selector)
generateDiscussionLink: (cls, txt, handler) -> generateDiscussionLink: (cls, txt, handler) ->
$("<a>").addClass("discussion-link"). $("<a>").addClass("discussion-link")
attr("href", "javascript:void(0)"). .attr("href", "javascript:void(0)")
addClass(cls).html(txt). .addClass(cls).html(txt)
click(-> handler(this)) .click -> handler(this)
urlFor: (name, param, param1) -> urlFor: (name, param, param1) ->
{ {
watch_commentable : "/courses/#{$$course_id}/discussion/#{param}/watch" follow_discussion : "/courses/#{$$course_id}/discussion/#{param}/follow"
unwatch_commentable : "/courses/#{$$course_id}/discussion/#{param}/unwatch" unfollow_discussion : "/courses/#{$$course_id}/discussion/#{param}/unfollow"
create_thread : "/courses/#{$$course_id}/discussion/#{param}/threads/create" create_thread : "/courses/#{$$course_id}/discussion/#{param}/threads/create"
update_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/update" update_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/update"
create_comment : "/courses/#{$$course_id}/discussion/threads/#{param}/reply" create_comment : "/courses/#{$$course_id}/discussion/threads/#{param}/reply"
delete_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/delete" delete_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/delete"
upvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/upvote" upvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/upvote"
downvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/downvote" downvote_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/downvote"
watch_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/watch" follow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/follow"
unwatch_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/unwatch" unfollow_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/unfollow"
update_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/update" update_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/update"
endorse_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/endorse" endorse_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/endorse"
create_sub_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/reply" create_sub_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/reply"
...@@ -50,3 +50,35 @@ Discussion = @Discussion ...@@ -50,3 +50,35 @@ Discussion = @Discussion
handleAnchorAndReload: (response) -> handleAnchorAndReload: (response) ->
#window.location = window.location.pathname + "#" + response['id'] #window.location = window.location.pathname + "#" + response['id']
window.location.reload() window.location.reload()
bindLocalEvents: ($local, eventsHandler) ->
for eventSelector, handler of eventsHandler
[event, selector] = eventSelector.split(' ')
$local(selector)[event] handler
tagsInputOptions: ->
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
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
formErrorHandler: (errorsField, success) ->
(response, textStatus, xhr) ->
if response.errors? and response.errors.length > 0
errorsField.empty()
for error in response.errors
errorsField.append($("<li>").addClass("new-post-form-error").html(error))
else
success(response, textStatus, xhr)
...@@ -3,16 +3,16 @@ ...@@ -3,16 +3,16 @@
<%def name="link_to_sort(key, title)"> <%def name="link_to_sort(key, title)">
% if key == sort_key: % if key == sort_key:
% if sort_order.lower() == 'desc': % if sort_order.lower() == 'desc':
${_link_to_sort(key, 'asc', title + ' [v]')} ${_link_to_sort(key, 'asc', title + ' [v]', 'sorted')}
% else: % else:
${_link_to_sort(key, 'desc', title + ' [^]')} ${_link_to_sort(key, 'desc', title + ' [^]', 'sorted')}
% endif % endif
% else: % else:
${_link_to_sort(key, 'desc', title)} ${_link_to_sort(key, 'desc', title)}
% endif % endif
</%def> </%def>
<%def name="_link_to_sort(key, order, title)"> <%def name="_link_to_sort(key, order, title, cls='')">
<% <%
def merge(dic1, dic2): def merge(dic1, dic2):
return dict(dic1.items() + dic2.items()) return dict(dic1.items() + dic2.items())
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
return base_url + '?' + urlencode(merge(query_params, {'page': 1, 'sort_key': key, 'sort_order': order})) return base_url + '?' + urlencode(merge(query_params, {'page': 1, 'sort_key': key, 'sort_order': order}))
%> %>
% if discussion_type == 'inline': % if discussion_type == 'inline':
<a class="discussion-sort-link discussion-inline-sort-link" href="javascript:void(0)" sort-url="${url_for_sort(key, order)}">${title}</a> <a class="discussion-sort-link discussion-inline-sort-link ${cls}" href="javascript:void(0)" sort-url="${url_for_sort(key, order)}">${title}</a>
% else: % else:
<a class="discussion-sort-link" href="${url_for_sort(key, order)}">${title}</a> <a class="discussion-sort-link ${cls}" href="${url_for_sort(key, order)}">${title}</a>
% endif % endif
</%def> </%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