Commit dac28f80 by Brittany Cheng

Merge branch 'master' of github.com:dementrock/mitx into discussion2

parents 839463e9 5ba879fc
...@@ -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)
......
...@@ -3,107 +3,79 @@ if not @Discussion? ...@@ -3,107 +3,79 @@ if not @Discussion?
Discussion = @Discussion Discussion = @Discussion
initializeVote = (index, content) ->
$content = $(content)
$local = Discussion.generateLocal($content.children(".discussion-content"))
id = $content.attr("_id")
if id in $$user_info.upvoted_ids
$local(".discussion-vote-up").addClass("voted")
else if id in $$user_info.downvoted_ids
$local(".discussion-vote-down").addClass("voted")
subscriptionLink = (type, id) ->
followLink = ->
Discussion.generateDiscussionLink("discussion-follow-#{type}", "Follow", handleFollow)
unfollowLink = ->
Discussion.generateDiscussionLink("discussion-unfollow-#{type}", "Unfollow", handleUnfollow)
handleFollow = (elem) ->
Discussion.safeAjax
$elem: $(elem)
url: Discussion.urlFor("follow_#{type}", id)
type: "POST"
success: (response, textStatus) ->
if textStatus == "success"
$(elem).replaceWith unfollowLink()
dataType: 'json'
handleUnfollow = (elem) ->
Discussion.safeAjax
$elem: $(elem)
url: Discussion.urlFor("unfollow_#{type}", id)
type: "POST"
success: (response, textStatus) ->
if textStatus == "success"
$(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
unfollowLink()
else
followLink()
initializeFollowDiscussion = (discussion) ->
$discussion = $(discussion)
id = $following.attr("_id")
$local = Discussion.generateLocal()
$discussion.children(".discussion-non-content")
.find(".discussion-title-wrapper")
.append(subscriptionLink('discussion', id))
initializeFollowThread = (index, thread) ->
$thread = $(thread)
id = $thread.attr("_id")
$thread.children(".discussion-content")
.find(".info")
.append(subscriptionLink('thread', id))
@Discussion = $.extend @Discussion, @Discussion = $.extend @Discussion,
initializeDiscussion: (discussion) -> initializeDiscussion: (discussion) ->
initializeVote = (index, content) ->
$content = $(content)
$local = Discussion.generateLocal($content.children(".discussion-content"))
id = $content.attr("_id")
if id in $$user_info.upvoted_ids
$local(".discussion-vote-up").addClass("voted")
else if id in $$user_info.downvoted_ids
$local(".discussion-vote-down").addClass("voted")
initializeWatchDiscussion = (discussion) ->
$discussion = $(discussion)
id = $discussion.attr("_id")
$local = Discussion.generateLocal($discussion.children(".discussion-non-content"))
handleWatchDiscussion = (elem) ->
url = Discussion.urlFor('watch_commentable', id)
$.post url, {}, (response, textStatus) ->
if textStatus == "success"
Discussion.handleAnchorAndReload(response)
, 'json'
handleUnwatchDiscussion = (elem) ->
url = Discussion.urlFor('unwatch_commentable', id)
$.post url, {}, (response, textStatus) ->
if textStatus == "success"
Discussion.handleAnchorAndReload(response)
, 'json'
if id in $$user_info.subscribed_commentable_ids
unwatchDiscussion = Discussion.generateDiscussionLink("discussion-unwatch-discussion", "Unwatch", handleUnwatchDiscussion)
$local(".discussion-title-wrapper").append(unwatchDiscussion)
else
watchDiscussion = Discussion.generateDiscussionLink("discussion-watch-discussion", "Watch", handleWatchDiscussion)
$local(".discussion-title-wrapper").append(watchDiscussion)
initializeWatchThreads = (index, thread) ->
$thread = $(thread)
id = $thread.attr("_id")
$local = Discussion.generateLocal($thread.children(".discussion-content"))
handleWatchThread = (elem) ->
$elem = $(elem)
url = Discussion.urlFor('watch_thread', id)
Discussion.safeAjax
$elem: $elem
url: url
type: "POST"
success: (response, textStatus) ->
if textStatus == "success"
$elem.removeClass("discussion-watch-thread")
.addClass("discussion-unwatch-thread")
.html("Unfollow")
.unbind('click').click ->
handleUnwatchThread(this)
dataType: 'json'
handleUnwatchThread = (elem) ->
$elem = $(elem)
url = Discussion.urlFor('unwatch_thread', id)
Discussion.safeAjax
$elem: $elem
url: url
type: "POST"
success: (response, textStatus) ->
if textStatus == "success"
$elem.removeClass("discussion-unwatch-thread")
.addClass("discussion-watch-thread")
.html("Follow")
.unbind('click').click ->
handleWatchThread(this)
dataType: 'json'
if id in $$user_info.subscribed_thread_ids
unwatchThread = Discussion.generateDiscussionLink("discussion-unwatch-thread", "Unfollow", handleUnwatchThread)
$local(".info").append(unwatchThread)
else
watchThread = Discussion.generateDiscussionLink("discussion-watch-thread", "Follow", handleWatchThread)
$local(".info").append(watchThread)
$local = Discussion.generateLocal(discussion) $local = Discussion.generateLocal(discussion)
if $$user_info? if $$user_info?
$local(".comment").each(initializeVote) $local(".comment").each(initializeVote)
$local(".thread").each(initializeVote).each(initializeWatchThreads) $local(".thread").each(initializeVote).each(initializeFollowThread)
#initializeWatchDiscussion(discussion) TODO move this somewhere else #initializeFollowDiscussion(discussion) TODO move this somewhere else
$local(".new-post-tags").tagsInput $local(".new-post-tags").tagsInput Discussion.tagsInputOptions()
autocomplete_url: Discussion.urlFor('tags_autocomplete')
autocomplete:
remoteDataType: 'json'
interactive: true
defaultText: "Tag your post"
height: "30px"
width: "90%"
removeWithBackspace: true
bindDiscussionEvents: (discussion) -> bindDiscussionEvents: (discussion) ->
$discussion = $(discussion) $discussion = $(discussion)
$discussionNonContent = $discussion.children(".discussion-non-content") $discussionNonContent = $discussion.children(".discussion-non-content")
$local = Discussion.generateLocal($discussionNonContent)#(selector) -> $discussionNonContent.find(selector) $local = Discussion.generateLocal($discussionNonContent)#(selector) -> $discussionNonContent.find(selector)
...@@ -141,32 +113,27 @@ Discussion = @Discussion ...@@ -141,32 +113,27 @@ Discussion = @Discussion
newPostForm.show() newPostForm.show()
$(elem).hide() $(elem).hide()
else else
view = { view = { discussion_id: id }
discussion_id: id
}
$discussionNonContent.append Mustache.render Discussion.newPostTemplate, view $discussionNonContent.append Mustache.render Discussion.newPostTemplate, view
newPostBody = $(discussion).find(".new-post-body") newPostBody = $(discussion).find(".new-post-body")
if newPostBody.length if newPostBody.length
Markdown.makeWmdEditor newPostBody, "-new-post-body-#{$(discussion).attr('_id')}", Discussion.urlFor('upload') Markdown.makeWmdEditor newPostBody, "-new-post-body-#{$(discussion).attr('_id')}", Discussion.urlFor('upload')
$local(".new-post-tags").tagsInput
autocomplete_url: Discussion.urlFor('tags_autocomplete') $local(".new-post-tags").tagsInput Discussion.tagsInputOptions()
autocomplete:
remoteDataType: 'json'
interactive: true
defaultText: "Tag your post: press enter after each tag"
height: "30px"
width: "100%"
removeWithBackspace: true
$local(".discussion-submit-post").click -> $local(".discussion-submit-post").click ->
handleSubmitNewPost(this) handleSubmitNewPost(this)
$local(".discussion-cancel-post").click -> $local(".discussion-cancel-post").click ->
handleCancelNewPost(this) handleCancelNewPost(this)
$(elem).hide() $(elem).hide()
handleUpdateDiscussionContent = ($elem, $discussion, url) ->
handleAjaxSearch = (elem) -> handleAjaxSearch = (elem) ->
handle
$elem = $(elem) $elem = $(elem)
$discussionModule = $elem.parents(".discussion-module") $discussion = $elem.parents(".discussion")
$discussion = $discussionModule.find(".discussion")
Discussion.safeAjax Discussion.safeAjax
$elem: $elem $elem: $elem
url: $elem.attr("action") url: $elem.attr("action")
...@@ -174,10 +141,10 @@ Discussion = @Discussion ...@@ -174,10 +141,10 @@ Discussion = @Discussion
text: $local(".search-input").val() text: $local(".search-input").val()
type: "GET" type: "GET"
success: (data, textStatus) -> success: (data, textStatus) ->
$discussion.replaceWith(data) $data = $(data)
$discussion = $discussionModule.find(".discussion") $discussion.replaceWith($data)
Discussion.initializeDiscussion($discussion) Discussion.initializeDiscussion($data)
Discussion.bindDiscussionEvents($discussion) Discussion.bindDiscussionEvents($data)
dataType: 'html' dataType: 'html'
handleAjaxSort = (elem) -> handleAjaxSort = (elem) ->
...@@ -195,25 +162,26 @@ Discussion = @Discussion ...@@ -195,25 +162,26 @@ Discussion = @Discussion
Discussion.bindDiscussionEvents($discussion) Discussion.bindDiscussionEvents($discussion)
dataType: 'html' dataType: 'html'
$local(".search-wrapper-forum > .discussion-search-form").submit (event) -> Discussion.bindLocalEvents $local,
event.preventDefault()
text = $local(".search-input").val()
isSearchWithinBoard = $local(".discussion-search-within-board").is(":checked")
handleSearch(text, isSearchWithinBoard)
$local(".discussion-new-post").click -> "submit .search-wrapper-forum>.discussion-search-form": (event) ->
handleNewPost(this) event.preventDefault()
text = $local(".search-input").val()
isSearchWithinBoard = $local(".discussion-search-within-board").is(":checked")
handleSearch(text, isSearchWithinBoard)
$local(".discussion-search-link").click -> "submit .search-wrapper-inline>.discussion-search-form": (event) ->
handleAjaxSearch(this) event.preventDefault()
handleAjaxSearch(this)
$local(".search-wrapper-inline > .discussion-search-form").submit (e)-> "click .discussion-new-post": ->
e.preventDefault() handleNewPost(this)
handleAjaxSearch(this)
$local(".discussion-inline-sort-link").click -> "click .discussion-search-link": ->
handleAjaxSort(this) handleAjaxSearch(this)
"click .discussion-inline-sort-link": ->
handleAjaxSort(this)
$discussion.find(".thread").each (index, thread) -> $discussion.find(".thread").each (index, thread) ->
Discussion.initializeContent(thread) Discussion.initializeContent(thread)
......
$ -> $ ->
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,18 @@ Discussion = @Discussion ...@@ -50,3 +50,18 @@ 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
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